use of suite.trade.data.DataSource in project suite by stupidsing.
the class StatisticalArbitrageTest method testCointegration.
// Auto-regressive test
@Test
public void testCointegration() {
// 0004.HK, 0020.HK
// 0011.HK, 0005.HK
int tor = 8;
String symbol0 = "0004.HK";
String symbol1 = "0945.HK";
AlignKeyDataSource<String> akds = cfg.dataSources(period, Read.each(symbol0, symbol1));
Map<String, float[]> pricesBySymbol = akds.dsByKey.mapValue(DataSource::returns).toMap();
int length = akds.ts.length;
float[] prices0 = pricesBySymbol.get(symbol0);
float[] prices1 = pricesBySymbol.get(symbol1);
LinearRegression lr = stat.linearRegression(//
Ints_.range(tor, //
length).map(i -> FltObjPair.of(prices1[i], Floats_.toArray(tor, j -> prices0[i + j - tor]))));
System.out.println(lr);
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class SingleAllocBackTestTest method backTest.
private Map<String, SingleAllocBackTest> backTest(String code, String disp) {
Strategos sr = new Strategos();
DataSource ds = cfg.dataSource(code, period);
return //
Read.<String, BuySellStrategy>empty2().cons("longHold", //
sr.longHold).cons("lowPassPrediction", //
sr.lowPassPrediction(128, 8, 8, .02f)).cons("movingAvgMeanReverting", //
sr.movingAvgMeanReverting(64, 8, .15f)).cons("macdSignalLineX", //
sr.macdSignalLineX(.8f, .9f, .85f)).cons("macdZeroLineX", //
sr.macdZeroLineX(.8f, .9f)).map2(//
(sn, strategy) -> backTest_(ds, disp + ", strategy = " + sn, strategy)).toMap();
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class DailyMain method mamr.
// moving average mean reversion
private Result mamr(float factor) {
String tag = "mamr";
int nHoldDays = 8;
Streamlet<Asset> assets = cfg.queryCompanies();
BuySellStrategy strategy = new Strategos().movingAvgMeanReverting(64, nHoldDays, .15f);
// pre-fetch quotes
cfg.quote(assets.map(asset -> asset.symbol).toSet());
// identify stocks that are mean-reverting
Map<String, Boolean> backTestBySymbol = //
SerializedStoreCache.of(//
serialize.mapOfString(serialize.boolean_)).get(getClass().getSimpleName() + ".backTestBySymbol", () -> //
assets.map2(stock -> stock.symbol, stock -> {
try {
TimeRange period = TimeRange.threeYears();
DataSource ds = cfg.dataSource(stock.symbol, period).range(period).validate();
SingleAllocBackTest backTest = SingleAllocBackTest.test(ds, strategy);
return MathUtil.isPositive(backTest.account.cash());
} catch (Exception ex) {
LogUtil.warn(ex + " for " + stock);
return false;
}
}).toMap());
TimeRange period = TimeRange.daysBefore(128);
List<Trade> trades = new ArrayList<>();
// capture signals
for (Asset asset : assets) {
String symbol = asset.symbol;
if (backTestBySymbol.get(symbol))
try {
DataSource ds = cfg.dataSource(symbol, period).validate();
float[] prices = ds.prices;
int last = prices.length - 1;
float latestPrice = prices[last];
int signal = strategy.analyze(prices).get(last);
int nShares = signal * asset.lotSize * Math.round(factor / nHoldDays / (asset.lotSize * latestPrice));
Trade trade = Trade.of(nShares, symbol, latestPrice);
if (signal != 0)
trades.add(trade);
} catch (Exception ex) {
LogUtil.warn(ex.getMessage() + " in " + asset);
}
}
return new Result(tag, trades);
}
Aggregations