Search in sources :

Example 1 with DataSourceView

use of suite.trade.data.DataSourceView in project suite by stupidsing.

the class MovingAvgMeanReversionBackAllocator method backAllocator.

public BackAllocator backAllocator() {
    return (akds, indices) -> {
        Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
        double dailyRiskFreeInterestRate = Trade_.riskFreeInterestRate(1);
        DataSourceView<String, MeanReversionStat> dsv = // 
        DataSourceView.of(tor, 256, akds, (symbol, ds, period) -> new MeanReversionStat(ds, period));
        return index -> {
            Map<String, MeanReversionStat> mrsBySymbol = // 
            akds.dsByKey.map2(// 
            (symbol, ds) -> dsv.get(symbol, index)).filterValue(// 
            mrsReversionStat -> mrsReversionStat != null).toMap();
            // ensure 0 < half-life: determine investment period
            return // 
            Read.from2(mrsBySymbol).filterValue(mrs -> // 
            mrs.adf < 0d && // 
            mrs.hurst < .5d && // 
            mrs.movingAvgMeanReversionRatio() < 0d).map2((symbol, mrs) -> {
                DataSource ds = dsBySymbol.get(symbol);
                double price = ds.prices[index - 1];
                double lma = mrs.latestMovingAverage();
                double diff = mrs.movingAvgMeanReversion.predict(new float[] { (float) lma, 1f });
                double dailyReturn = diff / price - dailyRiskFreeInterestRate;
                ReturnsStat returnsStat = ts.returnsStatDaily(ds.prices);
                double sharpe = returnsStat.sharpeRatio();
                double kelly = dailyReturn * price * price / mrs.movingAvgMeanReversion.sse;
                return new PotentialStat(dailyReturn, sharpe, kelly);
            }).filterValue(// 
            ps -> 0d < ps.kelly).cons(Asset.cashSymbol, // 
            new PotentialStat(Trade_.riskFreeInterestRate, 1d, 0d)).mapValue(// 
            ps -> ps.kelly).sortBy(// 
            (symbol, potential) -> -potential).take(// 
            top).toList();
        };
    };
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) MovingAverage(suite.trade.analysis.MovingAverage) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) DataSource(suite.trade.data.DataSource) TimeSeries(ts.TimeSeries) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) DataSourceView(suite.trade.data.DataSourceView) DataSourceView(suite.trade.data.DataSourceView) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) DataSource(suite.trade.data.DataSource)

Aggregations

Map (java.util.Map)1 LinearRegression (suite.math.numeric.Statistic.LinearRegression)1 Read (suite.streamlet.Read)1 Asset (suite.trade.Asset)1 TimeRange (suite.trade.TimeRange)1 Trade_ (suite.trade.Trade_)1 MovingAverage (suite.trade.analysis.MovingAverage)1 BackAllocator (suite.trade.backalloc.BackAllocator)1 DataSource (suite.trade.data.DataSource)1 DataSourceView (suite.trade.data.DataSourceView)1 To (suite.util.To)1 TimeSeries (ts.TimeSeries)1 ReturnsStat (ts.TimeSeries.ReturnsStat)1