use of suite.trade.data.DataSource in project suite by stupidsing.
the class StatisticalArbitrageTest method testMonteCarloBestBet.
@Test
public void testMonteCarloBestBet() {
int nTrials = 10000;
int nBets = 40;
DataSource ds = cfg.dataSource(Asset.hsiSymbol).range(period).cleanse();
float[] returns = ds.returns();
for (float bet = 0f - 2f; bet < 1f + 2f; bet += .02f) {
float notBet = 1f - bet;
double sum = 0d;
for (int i = 0; i < nTrials; i++) {
double account = 1d;
for (int j = 0; j < nBets; j++) {
double return_ = returns[random.nextInt(returns.length)];
account = notBet * account + bet * account * (1d + return_);
}
sum += account;
}
System.out.println("bet = " + To.string(bet) + ", avg outcome = " + To.string(sum / nTrials));
}
}
use of suite.trade.data.DataSource 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 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.data.DataSource 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.data.DataSource 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;
}
Aggregations