use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.
the class DailyMain method alloc.
private Result alloc(String tag, float fund, BackAllocator backAllocator, Streamlet<Asset> assets) {
TimeRange period = TimeRange.daysBefore(64);
Simulate sim = BackAllocTester.of(cfg, period, assets, backAllocator, log).simulate(fund);
Account account0 = Account.ofPortfolio(cfg.queryHistory().filter(r -> String_.equals(r.strategy, tag)));
Account account1 = sim.account;
Map<String, Integer> assets0 = account0.assets();
Map<String, Integer> assets1 = account1.assets();
Set<String> symbols = Set_.union(assets0.keySet(), assets1.keySet());
Map<String, Float> priceBySymbol = cfg.quote(symbols);
List<Trade> trades = Trade_.diff(Trade.NA, assets0, assets1, priceBySymbol::get).toList();
sb.append("\nstrategy = " + tag + ", " + sim.conclusion());
return new Result(tag, trades);
}
use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.
the class BackAllocatorOld method questoQuella.
public BackAllocator questoQuella(String symbol0, String symbol1) {
int tor = 64;
double threshold = 0d;
BackAllocator ba0 = (akds, indices) -> {
Streamlet2<String, DataSource> dsBySymbol = akds.dsByKey;
Map<String, DataSource> dsBySymbol_ = dsBySymbol.toMap();
DataSource ds0 = dsBySymbol_.get(symbol0);
DataSource ds1 = dsBySymbol_.get(symbol1);
return index -> {
int ix = index - 1;
int i0 = ix - tor;
double p0 = ds0.get(i0).t1, px = ds0.get(ix).t1;
double q0 = ds1.get(i0).t1, qx = ds1.get(ix).t1;
double pdiff = Quant.return_(p0, px);
double qdiff = Quant.return_(q0, qx);
if (threshold < Math.abs(pdiff - qdiff))
return //
List.of(//
Pair.of(pdiff < qdiff ? symbol0 : symbol1, 1d), Pair.of(pdiff < qdiff ? symbol1 : symbol0, -1d));
else
return List.of();
};
};
return ba0.filterByAsset(symbol -> String_.equals(symbol, symbol0) || String_.equals(symbol, symbol1));
}
use of suite.trade.backalloc.BackAllocator 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();
};
};
}
use of suite.trade.backalloc.BackAllocator in project suite by stupidsing.
the class DailyMain method pairs.
public BackAllocConfiguration pairs(String symbol0, String symbol1) {
Streamlet<Asset> assets = Read.each(symbol0, symbol1).map(cfg::queryCompany).collect(As::streamlet);
BackAllocator backAllocator = BackAllocatorOld.me.pairs(cfg, symbol0, symbol1).unleverage();
return new BackAllocConfiguration(time -> assets, backAllocator);
}
Aggregations