use of suite.trade.Time in project suite by stupidsing.
the class Summarize method summarize.
public <K> SummarizeByStrategy<K> summarize(Fun<Trade, K> fun) {
Streamlet2<K, Summarize_> summaryByKey = //
trades.groupBy(fun, //
trades_ -> summarize_(trades_, priceBySymbol, s -> null)).filterKey(//
key -> key != null).collect(As::streamlet2);
Map<String, Map<K, Integer>> nSharesByKeyBySymbol = //
summaryByKey.concatMap((key, summary) -> //
summary.account.portfolio().map(//
(symbol, n) -> Fixie.of(symbol, key, n))).groupBy(Fixie3::get0, fixies0 -> //
fixies0.groupBy(Fixie3::get1, fixies1 -> //
fixies1.map(Fixie3::get2).uniqueResult()).toMap()).toMap();
Map<String, Float> acquiredPrices = trades.collect(Trade_::collectBrokeredTrades).collect(Trade_::collectAcquiredPrices);
Time now = Time.now();
Summarize_ overall = summarize_(trades, priceBySymbol, symbol -> {
boolean isMarketOpen = //
false || //
HkexUtil.isMarketOpen(now) || HkexUtil.isMarketOpen(now.addHours(1));
DataSource ds = cfg.dataSource(symbol);
// acquisition price
float price0 = acquiredPrices.get(symbol);
// previous close
float price1 = ds.get(isMarketOpen ? -1 : -2).t1;
// now
float pricex = isMarketOpen ? priceBySymbol.get(symbol) : ds.get(-1).t1;
String keys = //
Read.from2(//
nSharesByKeyBySymbol.getOrDefault(symbol, Map.ofEntries())).keys().map(//
Object::toString).sort(//
String_::compare).collect(As.joinedBy("/"));
return //
percent(price1, pricex) + ", " + //
percent(price0, pricex) + (!keys.isEmpty() ? ", " + keys : "");
});
Map<K, String> outByKey = summaryByKey.mapValue(Summarize_::out0).toMap();
StringBuilder sb = new StringBuilder();
Sink<String> log = sb::append;
for (Entry<K, String> e : outByKey.entrySet()) log.sink("\nFor strategy " + e.getKey() + ":" + e.getValue());
log.sink(FormatUtil.tablize("\nOverall:\t" + Time.now().ymdHms() + overall.out1()));
// profit and loss
Map<K, Double> pnlByKey = //
sellAll(trades, priceBySymbol).groupBy(fun, //
t -> (double) Account.ofHistory(t).cash()).toMap();
return new SummarizeByStrategy<>(sb.toString(), overall.account, pnlByKey);
}
use of suite.trade.Time in project suite by stupidsing.
the class BackAllocatorUtil method filterByIndexReturn.
public default BackAllocator filterByIndexReturn(Configuration cfg, String indexSymbol) {
DataSource indexDataSource = cfg.dataSource(indexSymbol);
return (akds, indices) -> {
OnDateTime onDateTime = allocate(akds, indices);
return index -> {
Time date = Time.ofEpochSec(akds.ts[index - 1]).date();
long t0 = date.addDays(-7).epochSec();
long tx = date.epochSec();
DataSource ids = indexDataSource.range(t0, tx);
double indexPrice0 = ids.get(-1).t1;
double indexPricex = ids.get(-2).t1;
double indexReturn = Quant.return_(indexPrice0, indexPricex);
return //
-.03f < indexReturn ? //
onDateTime.onDateTime(index) : List.of();
};
};
}
use of suite.trade.Time in project suite by stupidsing.
the class YahooTest method testL1Adjust.
@Test
public void testL1Adjust() {
Time time0 = Time.ofEpochSec(1490578200l);
Time timex = Time.ofEpochSec(1497490200l);
Dump.out(yahoo.dataSourceL1("0012.HK", TimeRange.of(time0, timex)));
}
Aggregations