use of suite.trade.Asset 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);
}
use of suite.trade.Asset in project suite by stupidsing.
the class Hkex method queryCompanies_.
private List<Asset> queryCompanies_(int pageNo) {
JsonNode json = query(//
"" + //
"https://www.hkex.com.hk/eng/csm/ws/Result.asmx/GetData" + //
"?location=companySearch" + //
"&SearchMethod=2" + //
"&LangCode=en" + //
"&StockCode=" + //
"&StockName=" + //
"&Ranking=ByMC" + //
"&StockType=MB" + //
"&mkt=hk" + "&PageNo=" + //
(pageNo + 1) + //
"&ATypeSHEx=" + //
"&AType=" + //
"&FDD=" + //
"&FMM=" + //
"&FYYYY=" + //
"&TDD=" + //
"&TMM=" + "&TYYYY=");
CompanySearch companySearch = mapper.convertValue(json, CompanySearch.class);
Streamlet<List<String>> data0;
if (Boolean.TRUE)
data0 = //
Read.each(//
companySearch).flatMap(//
cs -> cs.data).concatMap(Data::tableEntries);
else
data0 = //
Read.each(//
json).flatMap(//
json_ -> json_.path("data")).flatMap(//
json_ -> json_.path("content")).flatMap(//
json_ -> json_.path("table")).flatMap(//
json_ -> json_.path("tr")).filter(//
json_ -> !json_.path("thead").asBoolean()).flatMap(//
json_ -> json_.path("td")).map(json_ -> Read.from(json_).map(JsonNode::asText).toList());
Streamlet<List<String>> data1 = data0.collect(As::streamlet);
Map<String, Integer> lotSizeBySymbol = queryLotSizeBySymbol_(data1.map(this::toSymbol));
return data1.map(datum -> toAsset(datum, lotSizeBySymbol)).toList();
}
use of suite.trade.Asset in project suite by stupidsing.
the class Hkex method queryCompany.
public Asset queryCompany(String symbol) {
Asset asset = companyBySymbol.get(symbol);
if (asset == null && !delisted.contains(symbol)) {
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);
asset = //
Asset.of(//
HkexUtil.toSymbol(companyInfo.stockCode), //
companyInfo.stockName.split("\\[")[0].trim(), queryBoardLot(symbol));
}
return asset;
}
use of suite.trade.Asset in project suite by stupidsing.
the class Summarize method summarize_.
private //
Summarize_ summarize_(//
Streamlet<Trade> trades_, //
Map<String, Float> priceBySymbol, Iterate<String> infoFun) {
Streamlet<Trade> trades0 = trades_;
Streamlet<Trade> trades1 = sellAll(trades0, priceBySymbol);
Streamlet<String> details = //
Read.from2(//
Trade_.portfolio(trades0)).map((symbol, nShares) -> {
Asset asset = cfg.queryCompany(symbol);
float price = priceBySymbol.get(symbol);
String info = infoFun.apply(symbol);
return //
asset + ": " + price + " * " + //
nShares + " = " + //
((long) (nShares * price)) + (info != null ? " \t(" + info + ")" : "");
}).sort(//
Object_::compare).collect(As::streamlet);
return new Summarize_(details, trades0, trades1);
}
use of suite.trade.Asset in project suite by stupidsing.
the class BackTestMain method run.
@Override
protected boolean run(String[] args) {
// BEGIN
// END
String arg0 = 0 < args.length ? args[0] : "";
String arg1 = 1 < args.length ? args[1] : "";
String arg2 = 2 < args.length ? args[2] : "";
Streamlet<String> strategyMatches = !arg0.isEmpty() ? Read.from(arg0.split(",")) : null;
Streamlet<Integer> years = !arg1.isEmpty() ? //
Read.from(//
arg1.split(",")).concatMap(s -> {
Pair<String, String> pair = ParseUtil.search(s, "-", Assoc.RIGHT);
return //
pair != null ? //
Ints_.range(Integer.valueOf(pair.t0), Integer.valueOf(pair.t1)).map(i -> i) : Read.each(Integer.valueOf(s));
}) : //
Ints_.range(2007, Trade_.thisYear).map(i -> i);
Fun<Time, Streamlet<Asset>> fun = //
!arg2.isEmpty() ? //
time -> Read.from(arg2.split(",")).map(cfg::queryCompany).collect(As::streamlet) : cfg::queryCompaniesByMarketCap;
BackAllocConfigurations bac_ = new BackAllocConfigurations(cfg, fun);
Streamlet2<String, BackAllocConfiguration> bacByTag = bac_.bacs().bacByName;
Streamlet2<String, Simulate> simulationByKey = //
bacByTag.filterKey(//
n -> strategyMatches == null || strategyMatches.isAny(sm -> Wildcard.match(sm, n) != null)).map(//
Pair::of).join2(//
years.sort(Object_::compare).map(TimeRange::ofYear)).map2((pair, period) -> pair.t0, (pair, period) -> {
BackAllocConfiguration bac = pair.t1;
Streamlet<Asset> assets = bac.assetsFun.apply(period.from);
return runner.backTest(bac.backAllocator, period, assets);
}).collect(As::streamlet2);
String content0 = //
Read.bytes(//
Paths.get("src/main/java/" + getClass().getName().replace('.', '/') + ".java")).collect(//
As::utf8decode).map(//
Chars::toString).collect(As::joined);
String content1 = ParseUtil.fit(content0, "// BEGIN", "// END")[1];
System.out.println(content1);
System.out.println(runner.conclude(simulationByKey));
return true;
}
Aggregations