Search in sources :

Example 16 with Streamlet

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);
        });
    }));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Streamlet(suite.streamlet.Streamlet)

Example 17 with Streamlet

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);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) SerializedStoreCache(suite.os.SerializedStoreCache) Trade_(suite.trade.Trade_) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Bacs(suite.trade.backalloc.BackAllocConfigurations.Bacs) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) BackAllocatorOld(suite.trade.backalloc.strategy.BackAllocatorOld) Map(java.util.Map) BackAllocTester(suite.trade.backalloc.BackAllocTester) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Set_(suite.util.Set_) BuySellStrategy(suite.trade.singlealloc.BuySellStrategy) Streamlet2(suite.streamlet.Streamlet2) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Summarize(suite.trade.analysis.Summarize) Set(java.util.Set) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Serialize(suite.util.Serialize) Strategos(suite.trade.singlealloc.Strategos) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Configuration(suite.trade.data.Configuration) MathUtil(suite.math.MathUtil) Account(suite.trade.Account) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) DblStreamlet(suite.primitive.streamlet.DblStreamlet) Asset(suite.trade.Asset) SmtpSslGmail(suite.smtp.SmtpSslGmail) TimeRange(suite.trade.TimeRange) SummarizeByStrategy(suite.trade.analysis.Summarize.SummarizeByStrategy) Trade(suite.trade.Trade) Account(suite.trade.Account)

Aggregations

Streamlet (suite.streamlet.Streamlet)17 Read (suite.streamlet.Read)13 List (java.util.List)11 Map (java.util.Map)10 As (suite.streamlet.As)10 To (suite.util.To)9 Set (java.util.Set)8 Pair (suite.adt.pair.Pair)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Time (suite.trade.Time)7 InputStream (java.io.InputStream)6 TimeRange (suite.trade.TimeRange)6 Fail (suite.util.Fail)6 String_ (suite.util.String_)6 Streamlet2 (suite.streamlet.Streamlet2)5 Trade_ (suite.trade.Trade_)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 URL (java.net.URL)4