Search in sources :

Example 1 with DblFltPair

use of suite.primitive.adt.pair.DblFltPair 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 2 with DblFltPair

use of suite.primitive.adt.pair.DblFltPair in project suite by stupidsing.

the class DblFltMap method forEach.

public void forEach(DblFltSink sink) {
    DblFltPair pair = DblFltPair.of((double) 0, (float) 0);
    DblFltSource source = source_();
    while (source.source2(pair)) sink.sink2(pair.t0, pair.t1);
}
Also used : DblFltPair(suite.primitive.adt.pair.DblFltPair) DblFltSource(suite.primitive.DblFltSource)

Aggregations

DblFltPair (suite.primitive.adt.pair.DblFltPair)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 IntPredicate (java.util.function.IntPredicate)1 Predicate (java.util.function.Predicate)1 Mutable (suite.adt.Mutable)1 Pair (suite.adt.pair.Pair)1 Statistic (suite.math.numeric.Statistic)1 DblDbl_Dbl (suite.primitive.DblDbl_Dbl)1 DblFltSource (suite.primitive.DblFltSource)1 ObjObj_Dbl (suite.primitive.DblPrimitives.ObjObj_Dbl)1 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)1