use of suite.streamlet.Streamlet2 in project suite by stupidsing.
the class Summarize method summarize.
public <K> SummarizeByStrategy<K> summarize(Fun<Trade, K> fun) {
Streamlet2<K, Summarize_> summaryByKey = //
trades.groupBy(fun, //
trades_ -> summarize_(trades_, priceBySymbol, s -> null)).filterKey(//
key -> key != null).collect(As::streamlet2);
Map<String, Map<K, Integer>> nSharesByKeyBySymbol = //
summaryByKey.concatMap((key, summary) -> //
summary.account.portfolio().map(//
(symbol, n) -> Fixie.of(symbol, key, n))).groupBy(Fixie3::get0, fixies0 -> //
fixies0.groupBy(Fixie3::get1, fixies1 -> //
fixies1.map(Fixie3::get2).uniqueResult()).toMap()).toMap();
Map<String, Float> acquiredPrices = trades.collect(Trade_::collectBrokeredTrades).collect(Trade_::collectAcquiredPrices);
Time now = Time.now();
Summarize_ overall = summarize_(trades, priceBySymbol, symbol -> {
boolean isMarketOpen = //
false || //
HkexUtil.isMarketOpen(now) || HkexUtil.isMarketOpen(now.addHours(1));
DataSource ds = cfg.dataSource(symbol);
// acquisition price
float price0 = acquiredPrices.get(symbol);
// previous close
float price1 = ds.get(isMarketOpen ? -1 : -2).t1;
// now
float pricex = isMarketOpen ? priceBySymbol.get(symbol) : ds.get(-1).t1;
String keys = //
Read.from2(//
nSharesByKeyBySymbol.getOrDefault(symbol, Map.ofEntries())).keys().map(//
Object::toString).sort(//
String_::compare).collect(As.joinedBy("/"));
return //
percent(price1, pricex) + ", " + //
percent(price0, pricex) + (!keys.isEmpty() ? ", " + keys : "");
});
Map<K, String> outByKey = summaryByKey.mapValue(Summarize_::out0).toMap();
StringBuilder sb = new StringBuilder();
Sink<String> log = sb::append;
for (Entry<K, String> e : outByKey.entrySet()) log.sink("\nFor strategy " + e.getKey() + ":" + e.getValue());
log.sink(FormatUtil.tablize("\nOverall:\t" + Time.now().ymdHms() + overall.out1()));
// profit and loss
Map<K, Double> pnlByKey = //
sellAll(trades, priceBySymbol).groupBy(fun, //
t -> (double) Account.ofHistory(t).cash()).toMap();
return new SummarizeByStrategy<>(sb.toString(), overall.account, pnlByKey);
}
use of suite.streamlet.Streamlet2 in project suite by stupidsing.
the class FunRewrite method rewrite_.
private FunExpr rewrite_(FunExpr e0) {
return //
e0.<//
FunExpr>switch_().applyIf(ApplyFunExpr.class, e1 -> {
FunExpr object = rewrite(e1.object);
FunExpr[] parameters = Read.from(e1.parameters).map(this::rewrite).toArray(FunExpr.class);
Method method = fti.methodOf(object);
return object.invoke(method.getName(), parameters);
}).applyIf(CastFunExpr.class, e1 -> {
FunExpr e2 = e1.expr;
if (e2 instanceof DeclareParameterFunExpr) {
Class<?> interfaceClass = Type_.classOf(e1.type);
Map<String, Type> fieldTypes = new HashMap<>();
Map<String, FunExpr> fieldValues = new HashMap<>();
FunExpr e3 = rewrite(e -> {
FunExpr fieldValue;
if (e instanceof FieldStaticFunExpr) {
FieldStaticFunExpr e_ = (FieldStaticFunExpr) e;
String fieldName = e_.fieldName;
Type fieldType = fieldTypes.get(fieldName);
fieldTypes.put(fieldName, fieldType);
fieldValues.put(fieldName, e_);
return e;
} else if (e instanceof PlaceholderFunExpr && (fieldValue = placeholders.get(e)) != null) {
String fieldName = "e" + Util.temp();
Type fieldType = fti.typeOf(fieldValue);
fieldTypes.put(fieldName, fieldType);
fieldValues.put(fieldName, fieldValue);
return this_().field(fieldName, fieldType);
} else
return null;
}, e2);
FunCreator<?>.CreateClass cc = FunCreator.of(LambdaInterface.of(interfaceClass), fieldTypes).create_(e3);
Streamlet2<String, FunExpr> fieldValues0 = Read.from2(cc.fieldTypeValues).mapValue(tv -> objectField(tv.t1, tv.t0));
Streamlet2<String, FunExpr> fieldValues1 = Read.from2(fieldValues);
NewFunExpr e4 = new NewFunExpr();
e4.className = cc.className;
e4.fieldValues = Streamlet2.concat(fieldValues0, fieldValues1).toMap();
e4.implementationClass = cc.clazz;
e4.interfaceClass = interfaceClass;
return e4;
} else
return null;
}).applyIf(DeclareLocalFunExpr.class, e1 -> {
FunExpr value = rewrite(e1.value);
FunExpr lfe = local(localTypes.size());
localTypes.add(fti.typeOf(value));
AssignLocalFunExpr alfe = new AssignLocalFunExpr();
alfe.var = lfe;
alfe.value = value;
placeholders.put(e1.var, lfe);
return seq(alfe, rewrite(e1.do_));
}).applyIf(FieldFunExpr_.class, e1 -> {
FunExpr set = e1 instanceof FieldSetFunExpr ? ((FieldSetFunExpr) e1).value : null;
FunExpr object0 = rewrite(e1.object);
String fieldName = e1.fieldName;
Class<?> clazz = fti.classOf(object0);
Field field = Rethrow.ex(() -> clazz.getField(fieldName));
FunExpr object1 = object0.cast_(field.getDeclaringClass());
Type fieldType = Type.getType(field.getType());
return set == null ? object1.field(fieldName, fieldType) : object1.fieldSet(fieldName, fieldType, set);
}).applyIf(FieldInjectFunExpr.class, e1 -> {
Type type = fieldTypes.get(e1.fieldName);
if (type != null)
return rewrite(this_().field(e1.fieldName, type));
else
return Fail.t(e1.fieldName);
}).applyIf(InvokeLambdaFunExpr.class, e1 -> {
LambdaInstance<?> l_inst = e1.lambda;
LambdaImplementation<?> l_impl = l_inst.lambdaImplementation;
LambdaInterface<?> l_iface = l_impl.lambdaInterface;
FunExpr object = object_(l_impl.newFun(l_inst.fieldValues), l_iface.interfaceClass);
return rewrite(object.invoke(l_iface.interfaceClass, l_iface.methodName, e1.parameters));
}).applyIf(ObjectFunExpr.class, e1 -> {
return objectField(e1.object, e1.type);
}).applyIf(PlaceholderFunExpr.class, e1 -> {
FunExpr e2 = placeholders.get(e1);
if (e2 != null)
return e2;
else
return Fail.t("cannot resolve placeholder");
}).applyIf(ProfileFunExpr.class, e1 -> {
fieldTypeValues.put(e1.counterFieldName, Pair.of(Type.INT, 0));
return null;
}).result();
}
use of suite.streamlet.Streamlet2 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