use of suite.util.Set_ 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.util.Set_ 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.util.Set_ in project suite by stupidsing.
the class DailyMain method run.
@Override
protected boolean run(String[] args) {
Trade_.blackList = Set_.union(Trade_.blackList, blackList);
String sellPool = "sellpool";
String ymd = Time.now().ymd();
String td = ymd + "#";
// perform systematic trading
List<Result> results = //
List.of(//
alloc(bacs.pair_bb, 66666f), //
alloc("bug", bacs.bac_sell, 0f), //
alloc(bacs.pair_donchian, 100000f), //
alloc(bacs.pair_ema, 0f), //
mamr(50000f), //
alloc(bacs.pair_pmamr, 150000f), //
alloc(bacs.pair_pmamr2, 366666f), //
alloc(bacs.pair_pmmmr, 80000f), //
alloc(bacs.pair_revco, 0f), //
alloc(bacs.pair_tma, 0f), alloc(sellPool, bacs.bac_sell, 0f));
// unused strategies
if (Boolean.FALSE) {
alloc(bacs.pair_donchian, 100000f);
pairs(0f, "0341.HK", "0052.HK");
sellForEarn(sellPool);
}
SummarizeByStrategy<Object> sbs = Summarize.of(cfg).summarize();
Streamlet2<String, Trade> strategyTrades = //
Read.from(//
results).concatMap2(//
result -> Read.from(result.trades).map2(trade -> result.strategy, trade -> trade)).filterValue(//
trade -> trade.buySell != 0).collect(As::streamlet2);
Streamlet2<String, Trade> requestTrades = strategyTrades.filterKey(strategy -> !String_.equals(strategy, sellPool));
DblStreamlet amounts = strategyTrades.values().collect(Obj_Dbl.lift(Trade::amount));
double buys_ = amounts.filter(amount -> 0d < amount).sum();
double sells = amounts.filter(amount -> amount < 0d).sum();
sb.append(//
sbs.log + "\n" + //
sbs.pnlByKey + "\n" + //
strategyTrades.sortBy(//
(strategy, trade) -> trade.amount()).map((strategy, trade) -> //
"\n" + //
(0 <= trade.buySell ? "BUY^" : "SELL") + " SIGNAL(" + strategy + ")" + //
trade + " = " + //
To.string(trade.amount())).collect(//
As::joined) + //
"\n" + //
"\nBUY REQUESTS" + //
requestTrades.filterValue(//
trade -> 0 < trade.buySell).map((strategy, t) -> //
"" + "\n" + //
Trade.of(td, -t.buySell, t.symbol, t.price, sellPool).record() + "\n" + //
Trade.of(td, +t.buySell, t.symbol, t.price, strategy).record()).collect(//
As::joined) + //
"\n" + //
"\nSELL REQUESTS" + //
requestTrades.filterValue(//
trade -> trade.buySell < 0).map((strategy, t) -> //
"" + "\n" + //
Trade.of(td, +t.buySell, t.symbol, t.price, strategy).record() + "\n" + //
Trade.of(td, -t.buySell, t.symbol, t.price, sellPool).record()).collect(//
As::joined) + //
"\n" + "\nTOTAL BUYS_ = " + //
To.string(buys_) + "\nTOTAL SELLS = " + //
To.string(sells) + //
"\n" + //
"\nSUGGESTIONS" + //
"\n- check your balance" + //
"\n- sort the orders and get away the small ones" + //
"\n- get away the stocks after ex-date" + "\n- sell mamr and " + //
sellPool + //
"\n- for mamr, check actual execution using SingleAllocBackTestTest.testBackTestHkexDetails()" + "\n");
String result = sb.toString();
LogUtil.info(result);
SmtpSslGmail smtp = new SmtpSslGmail();
smtp.send(null, getClass().getName(), result);
return true;
}
Aggregations