Search in sources :

Example 1 with Set_

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);
}
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) Account(suite.trade.Account) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) Simulate(suite.trade.backalloc.BackAllocTester.Simulate)

Example 2 with Set_

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();
        };
    };
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Mutable(suite.adt.Mutable) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) HashMap(java.util.HashMap) Deque(java.util.Deque) IntPredicate(java.util.function.IntPredicate) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) DblFltPair(suite.primitive.adt.pair.DblFltPair) String_(suite.util.String_) Map(java.util.Map) Set_(suite.util.Set_) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Predicate(java.util.function.Predicate) ObjObj_Dbl(suite.primitive.DblPrimitives.ObjObj_Dbl) Datum(suite.trade.data.DataSource.Datum) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) Usex(suite.trade.Usex) Objects(java.util.Objects) WalkForwardAllocator(suite.trade.walkforwardalloc.WalkForwardAllocator) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Fail(suite.util.Fail) DblFltPair(suite.primitive.adt.pair.DblFltPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mutable(suite.adt.Mutable) HashMap(java.util.HashMap) Map(java.util.Map) DblFltPair(suite.primitive.adt.pair.DblFltPair) Pair(suite.adt.pair.Pair)

Example 3 with Set_

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;
}
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) As(suite.streamlet.As) DblStreamlet(suite.primitive.streamlet.DblStreamlet) SmtpSslGmail(suite.smtp.SmtpSslGmail)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Pair (suite.adt.pair.Pair)3 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)3 As (suite.streamlet.As)3 Read (suite.streamlet.Read)3 Streamlet (suite.streamlet.Streamlet)3 Streamlet2 (suite.streamlet.Streamlet2)3 Asset (suite.trade.Asset)3 Time (suite.trade.Time)3 Trade_ (suite.trade.Trade_)3 Configuration (suite.trade.data.Configuration)3 DataSource (suite.trade.data.DataSource)3 Set (java.util.Set)2 MathUtil (suite.math.MathUtil)2 LogUtil (suite.os.LogUtil)2 SerializedStoreCache (suite.os.SerializedStoreCache)2 DblStreamlet (suite.primitive.streamlet.DblStreamlet)2 SmtpSslGmail (suite.smtp.SmtpSslGmail)2