use of suite.util.String_ in project suite by stupidsing.
the class Trade_ method collectBrokeredTrades.
public static Streamlet<Trade> collectBrokeredTrades(Outlet<Trade> outlet) {
Trade[] trades0 = outlet.toArray(Trade.class);
List<Trade> trades1 = new ArrayList<>();
int length0 = trades0.length;
int i0 = 0;
IntIntSink tx = (i0_, i1_) -> {
if (Ints_.range(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
while (i0_ < i1_) {
Trade trade0 = trades0[i0_++];
if (!String_.equals(trade0.remark, "#"))
trades1.add(trade0);
}
};
for (int i = 1; i < length0; i++) {
Trade trade0 = trades0[i0];
Trade trade1 = trades0[i];
boolean isGroup = //
true && //
String_.equals(trade0.date, trade1.date) && //
String_.equals(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
if (!isGroup) {
tx.sink2(i0, i);
i0 = i;
}
}
tx.sink2(i0, length0);
return Read.from(trades1);
}
use of suite.util.String_ in project suite by stupidsing.
the class Fun23TreeTest method test.
@Test
public void test() throws IOException {
int n = 100;
String list100 = "0 until " + n + " | map {insert} | apply | {Tree (9999, Empty;)}";
Node fp0 = Suite.substitute("use 23-TREE >> " + list100);
Node result0 = Suite.evaluateFun(Suite.fcc(fp0, false));
String out0 = Formatter.dump(result0);
System.out.println("OUT:\n" + out0);
int nPars0 = Read.from(String_.chars(out0)).filter(c -> c == '(').size();
assertTrue(3 <= nPars0);
Node fp1 = Suite.substitute("use 23-TREE >> 0 until " + n / 2 + " | map {remove} | apply | {" + list100 + "}");
Node result1 = Suite.evaluateFun(Suite.fcc(fp1, false));
String out1 = Formatter.dump(result1);
System.out.println("OUT:\n" + out1);
int nPars1 = Read.from(String_.chars(out1)).filter(c -> c == '(').size();
assertTrue(3 <= nPars1);
}
use of suite.util.String_ in project suite by stupidsing.
the class BackAllocatorOld method questoQuella.
public BackAllocator questoQuella(String symbol0, String symbol1) {
int tor = 64;
double threshold = 0d;
BackAllocator ba0 = (akds, indices) -> {
Streamlet2<String, DataSource> dsBySymbol = akds.dsByKey;
Map<String, DataSource> dsBySymbol_ = dsBySymbol.toMap();
DataSource ds0 = dsBySymbol_.get(symbol0);
DataSource ds1 = dsBySymbol_.get(symbol1);
return index -> {
int ix = index - 1;
int i0 = ix - tor;
double p0 = ds0.get(i0).t1, px = ds0.get(ix).t1;
double q0 = ds1.get(i0).t1, qx = ds1.get(ix).t1;
double pdiff = Quant.return_(p0, px);
double qdiff = Quant.return_(q0, qx);
if (threshold < Math.abs(pdiff - qdiff))
return //
List.of(//
Pair.of(pdiff < qdiff ? symbol0 : symbol1, 1d), Pair.of(pdiff < qdiff ? symbol1 : symbol0, -1d));
else
return List.of();
};
};
return ba0.filterByAsset(symbol -> String_.equals(symbol, symbol0) || String_.equals(symbol, symbol1));
}
use of suite.util.String_ in project suite by stupidsing.
the class Hkex method queryBoardLot_.
private int queryBoardLot_(String symbol) {
if (String_.equals(symbol, "0700.HK"))
return 100;
else {
JsonNode json = query(//
"" + //
"https://www.hkex.com.hk/eng/csm/ws/Company.asmx/GetData" + //
"?location=companySearch" + //
"&SearchMethod=1" + //
"&LangCode=en" + "&StockCode=" + //
HkexUtil.toStockCode(symbol) + //
"&StockName=" + //
"&mkt=hk" + //
"&x=" + "&y=");
CompanyInfo companyInfo = mapper.convertValue(json, CompanyInfo.class);
String boardLotStr = //
Read.each(//
companyInfo).flatMap(//
ci -> ci.data).concatMap(//
Data::tableEntries).filter(//
td -> String_.equals(td.get(0), "Board lot")).uniqueResult().get(//
1).replace(",", "");
return Integer.parseInt(boardLotStr);
}
}
use of suite.util.String_ 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);
}
Aggregations