Search in sources :

Example 1 with TimeRange

use of suite.trade.TimeRange 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 TimeRange

use of suite.trade.TimeRange in project suite by stupidsing.

the class Yahoo method dataSourceYql.

public DataSource dataSourceYql(String symbol, TimeRange period) {
    String yql = // 
    "select *" + // 
    " from yahoo.finance.historicaldata" + " where symbol = \"" + symbol + // 
    "\"" + " and startDate = \"" + period.from.ymd() + // 
    "\"" + " and endDate = \"" + period.to.ymd() + "\"";
    String urlString = // 
    "http://query.yahooapis.com/v1/public/yql" + "?q=" + // 
    encode(yql) + // 
    "&format=json" + // 
    "&diagnostics=true" + // 
    "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" + "&callback=";
    return Rethrow.ex(() -> {
        try (InputStream is = Singleton.me.storeCache.http(urlString).collect(To::inputStream)) {
            JsonNode json = mapper.readTree(is);
            Streamlet<JsonNode> quotes = // 
            Read.each(json).flatMap(// 
            json_ -> json_.path("query")).flatMap(// 
            json_ -> json_.path("results")).flatMap(// 
            json_ -> json_.path("quote")).collect(As::streamlet);
            Streamlet<String[]> arrays = // 
            quotes.map(json_ -> new String[] { // 
            json_.path("Date").textValue(), // 
            json_.path("Open").textValue(), // 
            json_.path("Close").textValue(), // 
            json_.path("Low").textValue(), // 
            json_.path("High").textValue() }).collect(As::streamlet);
            long[] ts = arrays.collect(Obj_Lng.lift(array -> closeTs(array[0]))).toArray();
            float[] opens = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[1]))).toArray();
            float[] closes = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[2]))).toArray();
            float[] lows = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[3]))).toArray();
            float[] highs = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[4]))).toArray();
            float[] volumes = new float[ts.length];
            return DataSource.ofOhlcv(ts, opens, closes, lows, highs, volumes);
        }
    });
}
Also used : Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) LogUtil(suite.os.LogUtil) URL(java.net.URL) HashMap(java.util.HashMap) Obj_Lng(suite.primitive.LngPrimitives.Obj_Lng) HomeDir(suite.util.HomeDir) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) FileUtil(suite.os.FileUtil) JsonNode(com.fasterxml.jackson.databind.JsonNode) Path(java.nio.file.Path) LngFltPair(suite.primitive.adt.pair.LngFltPair) HttpUtil(suite.http.HttpUtil) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FoldOp(suite.util.FunUtil2.FoldOp) Constants(suite.Constants) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) To(suite.util.To) URLEncoder(java.net.URLEncoder) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) As(suite.streamlet.As) TimeRange(suite.trade.TimeRange) Fail(suite.util.Fail) InputStream(java.io.InputStream) As(suite.streamlet.As) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) To(suite.util.To)

Example 3 with TimeRange

use of suite.trade.TimeRange in project suite by stupidsing.

the class FactorLr method ols.

private LinearRegression ols(DataSource rds0, TimeRange period) {
    DataSource ys = rds0.range(period);
    float[][] returns_ = // 
    Read.from(// 
    indexPrices).map(// 
    prices -> DataSource.of(timestamps, prices).range(period).alignBeforePrices(ys.ts).returns()).toArray(float[].class);
    float[][] xs = mtx.transpose(returns_);
    return stat.linearRegression(xs, ys.returns(), indexSymbols.toArray(String.class));
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Statistic(suite.math.numeric.Statistic) Matrix(suite.math.linalg.Matrix) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Quant(ts.Quant) LinearRegression(suite.math.numeric.Statistic.LinearRegression) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) DataSourceView(suite.trade.data.DataSourceView) Int_Flt(suite.primitive.Int_Flt) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 4 with TimeRange

use of suite.trade.TimeRange 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)

Example 5 with TimeRange

use of suite.trade.TimeRange in project suite by stupidsing.

the class DataSourceView method get.

public V get(String symbol, int index) {
    TimeRange period = period(index);
    Map<TimeRange, V> m = viewByKey.get(symbol);
    return m != null ? m.get(period) : null;
}
Also used : TimeRange(suite.trade.TimeRange)

Aggregations

TimeRange (suite.trade.TimeRange)7 As (suite.streamlet.As)4 Read (suite.streamlet.Read)4 Streamlet (suite.streamlet.Streamlet)4 Streamlet2 (suite.streamlet.Streamlet2)4 Asset (suite.trade.Asset)4 Time (suite.trade.Time)4 DataSource (suite.trade.data.DataSource)4 List (java.util.List)3 Map (java.util.Map)3 Configuration (suite.trade.data.Configuration)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Pair (suite.adt.pair.Pair)2 LogUtil (suite.os.LogUtil)2 Ints_ (suite.primitive.Ints_)2 BackAllocator (suite.trade.backalloc.BackAllocator)2 String_ (suite.util.String_)2 To (suite.util.To)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1