use of suite.trade.data.DataSource in project suite by stupidsing.
the class BackAllocatorUtil method stop.
public default BackAllocator stop(double stopLoss, double stopGain) {
return (akds, indices) -> {
OnDateTime onDateTime = allocate(akds, indices);
Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
Mutable<Map<String, Double>> mutable = Mutable.of(new HashMap<>());
Map<String, List<DblFltPair>> entriesBySymbol = new HashMap<>();
return index -> {
int last = index - 1;
List<Pair<String, Double>> potentialBySymbol = onDateTime.onDateTime(index);
Map<String, Double> potentialBySymbol0 = mutable.get();
Map<String, Double> potentialBySymbol1 = Read.from2(potentialBySymbol).toMap();
// find out the transactions
Map<String, Double> diffBySymbol = //
Read.from(//
Set_.union(potentialBySymbol0.keySet(), potentialBySymbol1.keySet())).map2(symbol -> {
double potential0 = potentialBySymbol0.getOrDefault(symbol, 0d);
double potential1 = potentialBySymbol1.getOrDefault(symbol, 0d);
return potential1 - potential0;
}).toMap();
// check on each stock symbol
for (Entry<String, Double> e : diffBySymbol.entrySet()) {
String symbol = e.getKey();
double diff = e.getValue();
int bs = Quant.sign(diff);
float price = dsBySymbol.get(symbol).prices[last];
List<DblFltPair> entries0 = entriesBySymbol.getOrDefault(symbol, new ArrayList<>());
List<DblFltPair> entries1 = new ArrayList<>();
Collections.sort(entries0, (pair0, pair1) -> -bs * Float.compare(pair0.t1, pair1.t1));
for (DblFltPair entry0 : entries0) {
double potential0 = entry0.t0;
float entryPrice = entry0.t1;
double cancellation;
// a recent buy would cancel out the lowest price sell
if (bs == -1)
cancellation = min(0, max(diff, -potential0));
else if (bs == 1)
cancellation = max(0, min(diff, -potential0));
else
cancellation = 0d;
double potential1 = potential0 + cancellation;
diff -= cancellation;
double min = entryPrice * (potential1 < 0 ? stopGain : stopLoss);
double max = entryPrice * (potential1 < 0 ? stopLoss : stopGain);
// drop entries that got past their stopping prices
if (min < price && price < max)
entries1.add(DblFltPair.of(potential1, entryPrice));
}
if (diff != 0d)
entries1.add(DblFltPair.of(diff, price));
entriesBySymbol.put(symbol, entries1);
}
mutable.update(potentialBySymbol1);
// re-assemble the entries into current profile
return //
Read.multimap(//
entriesBySymbol).groupBy(//
entries -> entries.toDouble(Obj_Dbl.sum(pair -> pair.t0))).toList();
};
};
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class Factor method project.
private double project(DataSource irds0, DataSource rds0, TimeRange period) {
DataSource rds1 = rds0.range(period);
DataSource irds1 = irds0.range(period).alignBeforePrices(rds1.ts);
return stat.project(irds1.returns(), rds1.returns());
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class FactorLr method ols.
private LinearRegression ols(DataSource rds0, TimeRange period) {
DataSource ys = rds0.range(period);
float[][] returns_ = //
Read.from(//
indexPrices).map(//
prices -> DataSource.of(timestamps, prices).range(period).alignBeforePrices(ys.ts).returns()).toArray(float[].class);
float[][] xs = mtx.transpose(returns_);
return stat.linearRegression(xs, ys.returns(), indexSymbols.toArray(String.class));
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class PairTest method test.
private void test(TimeRange period, String symbol0, String symbol1) {
DataSource ds0 = cfg.dataSource(symbol0, period);
DataSource ds1 = cfg.dataSource(symbol1, period);
LngStreamlet ts0 = Longs_.of(ds0.ts);
LngStreamlet ts1 = Longs_.of(ds1.ts);
long[] tradeTimes = Longs_.concat(ts0, ts1).distinct().sort().toArray();
float[] prices0 = ds0.alignBeforePrices(tradeTimes).prices;
float[] prices1 = ds1.alignBeforePrices(tradeTimes).prices;
int length = prices0.length;
LinearRegression lr = statistic.linearRegression(//
Ints_.range(//
length).map(i -> FltObjPair.of(prices1[i], new float[] { prices0[i], 1f })));
System.out.println(symbol0 + " -> " + symbol1 + lr);
assertTrue(.4d < lr.r2);
}
use of suite.trade.data.DataSource in project suite by stupidsing.
the class StatisticalArbitrageTest method testMarketDirection.
@Test
public void testMarketDirection() {
DataSource ds = cfg.dataSource(Asset.hsiSymbol).cleanse();
int[] flagsArray = mt.time(ds.prices);
String flags0 = "-----";
for (int i = 0; i < ds.ts.length; i++) {
String flags = //
String_.right("00000" + Integer.toBinaryString(flagsArray[i]), //
-5).replace('0', //
'-').replace('1', 'M');
if (!String_.equals(flags0, flags))
System.out.println(Time.ofEpochSec(ds.ts[i]).ymd() + " " + flags);
flags0 = flags;
}
}
Aggregations