use of suite.util.String_ in project suite by stupidsing.
the class SewingProverImpl method compileTr.
private Trampoline compileTr(BinderFactory bf, Node node) {
List<Node> list;
Trampoline tr;
Tree tree;
Node[] m;
if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size())
tr = andTr(Read.from(list).map(n -> compileTr(bf, n)));
else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
tr = orTr(Read.from(list).map(n -> compileTr(bf, n)));
else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
boolean b = complexity(m[0]) <= complexity(m[1]);
Node n0 = b ? m[0] : m[1];
Node n1 = b ? m[1] : m[0];
Bind_ p = bf.binder(n1);
Clone_ f = bf.cloner(n0);
tr = rt -> p.test(rt, f.apply(rt.env)) ? okay : fail;
} else if ((m = Suite.pattern("builtin:.0:.1 .2").match(node)) != null) {
String className = ((Atom) m[0]).name;
String fieldName = ((Atom) m[1]).name;
BuiltinPredicate predicate = Rethrow.ex(() -> {
Class<?> clazz = Class.forName(className);
return (BuiltinPredicate) clazz.getField(fieldName).get(Object_.new_(clazz));
});
tr = compileTrCallPredicate(bf, predicate, m[2]);
} else if ((m = Suite.pattern("find.all .0 .1 .2").match(node)) != null) {
Clone_ f = bf.cloner(m[0]);
Trampoline tr1 = compileTr(bf, m[1]);
Bind_ p = bf.binder(m[2]);
List<Node> vs = new ArrayList<>();
tr = rt -> {
Restore restore = save(rt);
rt.pushRem(rt_ -> {
vs.add(new Cloner().clone(f.apply(rt_.env)));
return fail;
});
rt.pushAlt(rt_ -> {
restore.restore(rt);
return p.test(rt, Tree.of(TermOp.AND___, vs)) ? okay : fail;
});
return tr1;
};
} else if ((m = Suite.pattern("if .0 .1 .2").match(node)) != null) {
Trampoline tr0 = compileTr(bf, m[0]);
Trampoline tr1 = compileTr(bf, m[1]);
Trampoline tr2 = compileTr(bf, m[2]);
tr = if_(tr0, tr1, tr2);
} else if ((m = Suite.pattern("let .0 .1").match(node)) != null) {
Bind_ p = bf.binder(m[0]);
Evaluate_ eval = new CompileExpressionImpl(bf).evaluator(m[1]);
tr = rt -> p.test(rt, Int.of(eval.evaluate(rt.env))) ? okay : fail;
} else if ((m = Suite.pattern("list.fold .0/.1/.2 .3").match(node)) != null) {
Clone_ list0_ = bf.cloner(m[0]);
Clone_ value0_ = bf.cloner(m[1]);
Bind_ valuex_ = bf.binder(m[2]);
Clone_ ht_ = bf.cloner(m[3]);
tr = rt -> {
Node[] ht = Suite.pattern(".0 .1").match(ht_.apply(rt.env));
Trampoline tr1 = saveEnvTr(compileTrRule(ht[0], ht[1]));
Mutable<Node> current = Mutable.of(value0_.apply(rt.env));
rt.pushRem(rt_ -> valuex_.test(rt_, current.get()) ? okay : fail);
for (Node elem : Tree.iter(list0_.apply(rt.env))) {
Reference result = new Reference();
rt.pushRem(rt_ -> {
current.update(result.finalNode());
return okay;
});
rt.pushRem(rt_ -> {
rt_.query = Tree.of(TermOp.ITEM__, Tree.of(TermOp.ITEM__, elem, current.get()), result);
return tr1;
});
}
return okay;
};
} else if ((m = Suite.pattern("list.fold.clone .0/.1/.2 .3/.4/.5 .6").match(node)) != null) {
Clone_ list0_ = bf.cloner(m[0]);
Clone_ value0_ = bf.cloner(m[1]);
Bind_ valuex_ = bf.binder(m[2]);
Bind_ elem_ = bf.binder(m[3]);
Bind_ v0_ = bf.binder(m[4]);
Clone_ vx_ = bf.cloner(m[5]);
Trampoline tr1 = compileTr(bf, m[6]);
tr = rt -> {
Mutable<Node> current = Mutable.of(value0_.apply(rt.env));
Env env0 = rt.env;
rt.pushRem(rt_ -> {
rt_.env = env0;
return valuex_.test(rt_, current.get()) ? okay : fail;
});
for (Node elem : Tree.iter(list0_.apply(rt.env))) {
rt.pushRem(rt_ -> {
current.update(vx_.apply(rt_.env));
return okay;
});
rt.pushRem(rt_ -> {
rt_.env = env0.clone();
return elem_.test(rt_, elem) && v0_.test(rt_, current.get()) ? tr1 : fail;
});
}
return okay;
};
} else if ((m = Suite.pattern("list.query .0 .1").match(node)) != null) {
Clone_ l_ = bf.cloner(m[0]);
Clone_ ht_ = bf.cloner(m[1]);
tr = rt -> {
Node[] ht = Suite.pattern(".0 .1").match(ht_.apply(rt.env));
Trampoline tr1 = saveEnvTr(compileTrRule(ht[0], ht[1]));
for (Node n : Tree.iter(l_.apply(rt.env))) rt.pushRem(rt_ -> {
rt_.query = n;
return tr1;
});
return okay;
};
} else if ((m = Suite.pattern("list.query.clone .0 .1 .2").match(node)) != null) {
Clone_ f = bf.cloner(m[0]);
Bind_ p = bf.binder(m[1]);
Trampoline tr1 = compileTr(bf, m[2]);
tr = rt -> {
Env env0 = rt.env;
rt.pushRem(rt_ -> {
rt_.env = env0;
return okay;
});
for (Node n : Tree.iter(f.apply(rt.env))) rt.pushRem(rt_ -> {
rt_.env = env0.clone();
return p.test(rt_, n) ? tr1 : fail;
});
return okay;
};
} else if ((m = Suite.pattern("member .0 .1").match(node)) != null && TreeUtil.isList(m[0], TermOp.AND___)) {
List<Bind_> elems_ = Read.from(Tree.iter(m[0])).map(bf::binder).toList();
Clone_ f = bf.cloner(m[1]);
tr = rt -> {
Iterator<Bind_> iter = elems_.iterator();
Trampoline[] alt = new Trampoline[1];
Restore restore = save(rt);
return alt[0] = rt_ -> {
while (iter.hasNext()) {
restore.restore(rt);
if (iter.next().test(rt_, f.apply(rt.env))) {
rt_.pushAlt(alt[0]);
return okay;
}
}
return fail;
};
};
} else if ((m = Suite.pattern("not .0").match(node)) != null)
tr = if_(compileTr(bf, m[0]), fail, okay);
else if ((m = Suite.pattern("once .0").match(node)) != null) {
Trampoline tr0 = compileTr(bf, m[0]);
tr = rt -> {
IList<Trampoline> alts0 = rt.alts;
rt.pushRem(rt_ -> {
rt_.alts = alts0;
return okay;
});
return tr0;
};
} else if ((m = Suite.pattern("suspend .0 .1 .2").match(node)) != null) {
Clone_ f0 = bf.cloner(m[0]);
Clone_ f1 = bf.cloner(m[1]);
Trampoline tr0 = compileTr(bf, m[2]);
tr = rt -> {
List<Node> results = new ArrayList<>();
Env env = rt.env;
Trampoline tr_ = andTr(Read.each(tr0, rt_ -> {
results.add(f1.apply(env));
return fail;
}));
Node n0 = f0.apply(rt.env);
Suspend suspend = new Suspend(() -> {
Runtime rt_ = new Runtime(rt, tr_);
rt_.trampoline();
return Read.from(results).uniqueResult();
});
if (n0 instanceof Reference) {
rt.trail.addBind((Reference) n0, suspend);
return okay;
} else
return fail;
};
} else if ((m = Suite.pattern("throw .0").match(node)) != null) {
Clone_ f = bf.cloner(m[0]);
tr = rt -> {
rt.handler.sink(new Cloner().clone(f.apply(rt.env)));
return okay;
};
} else if ((m = Suite.pattern("try .0 .1 .2").match(node)) != null) {
Trampoline tr0 = compileTr(bf, m[0]);
Bind_ p = bf.binder(m[1]);
Trampoline catch0 = compileTr(bf, m[2]);
tr = rt -> {
BindEnv be = rt;
Restore restore = save(rt);
IList<Trampoline> alts0 = rt.alts;
Sink<Node> handler0 = rt.handler;
rt.handler = node_ -> {
restore.restore(rt);
if (p.test(be, node_)) {
rt.alts = alts0;
rt.pushRem(catch0);
} else
handler0.sink(node_);
};
rt.pushRem(rt_ -> {
rt_.handler = handler0;
return okay;
});
return tr0;
};
} else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
tr = compileTrCallPredicate(bf, ((Atom) m[0]).name, m[1], node);
else if (node instanceof Atom) {
String name = ((Atom) node).name;
if (node == ProverConstant.cut)
tr = cutEnd();
else if (String_.equals(name, ""))
tr = okay;
else if (String_.equals(name, "fail"))
tr = fail;
else
tr = compileTrCallPredicate(bf, name, Atom.NIL, node);
} else if (node instanceof Data<?>) {
Object data = ((Data<?>) node).data;
if (data instanceof Source<?>)
tr = rt -> ((Source<?>) data).source() != Boolean.TRUE ? okay : fail;
else
tr = Fail.t("cannot understand " + node);
} else if (node instanceof Reference) {
Clone_ f = bf.cloner(node);
tr = rt -> compileTr(passThru, f.apply(rt.env));
} else if ((tree = Tree.decompose(node)) != null)
tr = compileTrCallPredicate(bf, tree.getOperator().getName(), node, node);
else if (node instanceof Tuple)
tr = compileTrCallPredicate(bf, node);
else
tr = Fail.t("cannot understand " + node);
return tr;
}
use of suite.util.String_ in project suite by stupidsing.
the class ReversePolish method fromRpn.
public Node fromRpn(Reader reader) throws IOException {
BufferedReader br = new BufferedReader(reader);
Map<String, Reference> references = new HashMap<>();
Deque<Node> deque = new ArrayDeque<>();
br.lines().filter(elem -> !elem.isEmpty()).forEach(elem -> {
char type = elem.charAt(0);
String s = elem.substring(1);
Node n;
if (type == '\\')
n = Atom.of(s);
else if (type == '^') {
String[] a = s.split(":");
int size = Integer.valueOf(a[3]);
List<Pair<Node, Node>> children = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node key = deque.pop();
Node value = deque.pop();
children.add(Pair.of(key, value));
}
n = new //
NodeWrite(//
ReadType.valueOf(a[0]), //
!String_.equals(a[1], "null") ? Suite.parse(a[1]) : null, //
TermOp.valueOf(a[2]), children).node;
// n = Suite.parse(s);
} else if (type == 'i')
n = Int.of(Integer.parseInt(s));
else if (type == 'r')
n = references.computeIfAbsent(s, key -> new Reference());
else if (type == 't') {
TermOp op = TermOp.valueOf(s);
Node left = deque.pop();
Node right = deque.pop();
n = Tree.of(op, left, right);
} else
n = Fail.t("RPN conversion error: " + elem);
deque.push(n);
});
return deque.pop();
}
use of suite.util.String_ in project suite by stupidsing.
the class DailyMain method sellForEarn.
// some orders caused by stupid bugs. need to sell those at suitable times.
private Result sellForEarn(String tag) {
Streamlet<Trade> history = cfg.queryHistory().filter(r -> String_.equals(r.strategy, tag));
Account account = Account.ofPortfolio(history);
Map<String, Float> faceValueBySymbol = //
history.groupBy(record -> record.symbol, //
rs -> (float) Read.from(rs).toDouble(Obj_Dbl.sum(Trade::amount))).toMap();
List<Trade> trades = //
account.portfolio().map((symbol, sell) -> {
double targetPrice = (1d + 3 * Trade_.riskFreeInterestRate) * faceValueBySymbol.get(symbol) / sell;
return Trade.of(-sell, symbol, (float) targetPrice);
}).toList();
return new Result(tag, trades);
}
use of suite.util.String_ 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