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();
};
};
}
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);
}
Aggregations