use of suite.trade.data.DataSource.AlignKeyDataSource in project suite by stupidsing.
the class BackAllocatorTest method testStop.
@Test
public void testStop() {
Time start = Time.of(2017, 1, 1);
String symbol = "S";
float[] prices = { 1f, .99f, .98f, .5f, .5f, .5f, 0f, 0f, 0f };
BackAllocator ba0 = (akds, ts) -> index -> List.of(Pair.of(symbol, 1d));
BackAllocator ba1 = ba0.stopLoss(.98d);
int length = prices.length;
long[] ts = Longs_.toArray(length, i -> start.addDays(i).epochSec());
DataSource ds = DataSource.of(ts, prices);
AlignKeyDataSource<String> akds = DataSource.alignAll(Read.from2(List.of(Pair.of(symbol, ds))));
int[] indices = Ints_.toArray(length, i -> i);
OnDateTime odt = ba1.allocate(akds, indices);
List<Double> potentials = //
Ints_.range(//
indices.length).map(//
index -> 0 < index ? Read.from(odt.onDateTime(index)) : Read.<Pair<String, Double>>empty()).map(//
pairs -> pairs.toDouble(Obj_Dbl.sum(pair -> pair.t1))).toList();
assertEquals(List.of(0d, 1d, 1d, 1d, 0d, 0d, 0d, 0d, 0d), potentials);
}
use of suite.trade.data.DataSource.AlignKeyDataSource in project suite by stupidsing.
the class WalkForwardAllocTester method tick.
public String tick(Time time, Map<String, Float> priceBySymbol) {
int last = windowSize - 1;
System.arraycopy(times, 0, times, 1, last);
times[last] = time.epochSec();
for (Entry<String, DataSource> e : dsBySymbol.entrySet()) {
String symbol = e.getKey();
float[] prices = e.getValue().prices;
System.arraycopy(prices, 0, prices, 1, last);
prices[last] = priceBySymbol.get(symbol);
}
AlignKeyDataSource<String> akds = new AlignKeyDataSource<>(times, Read.from2(dsBySymbol));
List<Pair<String, Double>> ratioBySymbol = wfa.allocate(akds, windowSize);
UpdatePortfolio up = Trade_.updatePortfolio(time.ymdHms(), account, ratioBySymbol, assetBySymbol, Read.from2(priceBySymbol).mapValue(Eod::of).toMap());
float valuation_;
valuations.append(valuation_ = up.valuation0);
for (Pair<String, Float> e : up.val0.streamlet()) holdBySymbol.compute(e.t0, (s, h) -> e.t1 / valuation_ + (h != null ? h : 0d));
List<Trade> trades_ = up.trades;
String actions;
if (windowSize <= valuations.size())
actions = play(trades_);
else
actions = "wait";
return //
time.ymdHms() + ", valuation = " + //
valuation_ + ", portfolio = " + //
account + ", actions = " + actions;
}
use of suite.trade.data.DataSource.AlignKeyDataSource 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);
}
Aggregations