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();
};
};
}
Aggregations