Search in sources :

Example 1 with Asset

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);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) SerializedStoreCache(suite.os.SerializedStoreCache) Trade_(suite.trade.Trade_) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Bacs(suite.trade.backalloc.BackAllocConfigurations.Bacs) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) BackAllocatorOld(suite.trade.backalloc.strategy.BackAllocatorOld) Map(java.util.Map) BackAllocTester(suite.trade.backalloc.BackAllocTester) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Set_(suite.util.Set_) BuySellStrategy(suite.trade.singlealloc.BuySellStrategy) Streamlet2(suite.streamlet.Streamlet2) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Summarize(suite.trade.analysis.Summarize) Set(java.util.Set) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Serialize(suite.util.Serialize) Strategos(suite.trade.singlealloc.Strategos) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Configuration(suite.trade.data.Configuration) MathUtil(suite.math.MathUtil) Account(suite.trade.Account) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) DblStreamlet(suite.primitive.streamlet.DblStreamlet) Asset(suite.trade.Asset) SmtpSslGmail(suite.smtp.SmtpSslGmail) TimeRange(suite.trade.TimeRange) SummarizeByStrategy(suite.trade.analysis.Summarize.SummarizeByStrategy) Account(suite.trade.Account) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) Simulate(suite.trade.backalloc.BackAllocTester.Simulate)

Example 2 with Asset

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();
}
Also used : HttpUtil(suite.http.HttpUtil) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) SerializedStoreCache(suite.os.SerializedStoreCache) Source(suite.util.FunUtil.Source) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) To(suite.util.To) Serialize(suite.util.Serialize) HashSet(java.util.HashSet) Execute(suite.os.Execute) List(java.util.List) Streamlet(suite.streamlet.Streamlet) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) Asset(suite.trade.Asset) Fail(suite.util.Fail) InputStream(java.io.InputStream) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List)

Example 3 with Asset

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;
}
Also used : Asset(suite.trade.Asset) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with 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);
}
Also used : Trade(suite.trade.Trade) As(suite.streamlet.As) Asset(suite.trade.Asset)

Example 5 with Asset

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;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Trade_(suite.trade.Trade_) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) RunUtil(suite.util.RunUtil) Wildcard(suite.parser.Wildcard) ParseUtil(suite.util.ParseUtil) Pair(suite.adt.pair.Pair) ConfigurationImpl(suite.trade.data.ConfigurationImpl) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Paths(java.nio.file.Paths) Configuration(suite.trade.data.Configuration) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) Assoc(suite.node.io.Operator.Assoc) TimeRange(suite.trade.TimeRange) Time(suite.trade.Time) Object_(suite.util.Object_) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Streamlet(suite.streamlet.Streamlet) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Pair(suite.adt.pair.Pair)

Aggregations

Asset (suite.trade.Asset)10 As (suite.streamlet.As)5 Streamlet (suite.streamlet.Streamlet)4 Map (java.util.Map)3 Read (suite.streamlet.Read)3 Time (suite.trade.Time)3 TimeRange (suite.trade.TimeRange)3 Trade (suite.trade.Trade)3 Trade_ (suite.trade.Trade_)3 BackAllocConfiguration (suite.trade.backalloc.BackAllocConfiguration)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 Test (org.junit.Test)2 Pair (suite.adt.pair.Pair)2 SerializedStoreCache (suite.os.SerializedStoreCache)2 Streamlet2 (suite.streamlet.Streamlet2)2