use of suite.streamlet.Streamlet in project suite by stupidsing.
the class Pipe method shell.
public static Streamlet<String> shell(String sh) {
String[] command0 = null;
for (String s : List.of("/bin/sh", "C:\\cygwin\\bin\\sh.exe", "C:\\cygwin64\\bin\\sh.exe")) if (Files.exists(Paths.get(s)))
command0 = new String[] { s };
if (command0 != null)
LogUtil.info("START " + sh);
else
Fail.t("cannot find shell executable");
String[] command1 = command0;
return new Streamlet<>(() -> Rethrow.ex(() -> {
InputStream bis = new ByteArrayInputStream(sh.getBytes(Constants.charset));
Process process = Rethrow.ex(() -> Runtime.getRuntime().exec(command1));
InputStream pis = process.getInputStream();
InputStream pes = process.getErrorStream();
OutputStream pos = process.getOutputStream();
Thread[] threads = new Thread[] { //
Copy.streamByThread(pes, System.err), Copy.streamByThread(bis, pos) };
for (Thread thread : threads) thread.start();
return Read.lines(pis).closeAtEnd(() -> {
try {
int code = process.waitFor();
if (code == 0)
for (Thread thread : threads) thread.join();
else
Fail.t("code = " + code);
} catch (InterruptedException ex) {
Fail.t(ex);
}
process.destroy();
LogUtil.info("END__ " + sh);
});
}));
}
use of suite.streamlet.Streamlet 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);
}
Aggregations