Search in sources :

Example 1 with Streamlet

use of suite.streamlet.Streamlet in project suite by stupidsing.

the class P3Optimize method optimize_.

private Funp optimize_(Funp n) {
    return // 
    n.<// 
    Funp>switch_().applyIf(FunpCoerce.class, f -> f.apply((coerce, expr) -> {
        return !(expr instanceof FunpDontCare) ? n : optimize(expr);
    })).applyIf(FunpData.class, f -> f.apply(pairs -> {
        return FunpData.of(Read.from2(pairs).concatMap((expr, range) -> {
            Funp expr1 = optimize(expr);
            int start = range.t0;
            Streamlet<Pair<Funp, IntIntPair>> pairsx = new // 
            Switch<Streamlet<Pair<Funp, IntIntPair>>>(// 
            expr1).applyIf(FunpData.class, g -> g.apply(pairs1 -> {
                return // 
                Read.from2(// 
                pairs1).map((exprc, range1) -> Pair.of(optimize(exprc), IntIntPair.of(start + range1.t0, start + range1.t1)));
            })).result();
            return pairsx != null ? pairsx : Read.each(Pair.of(expr1, range));
        }).toList());
    })).applyIf(FunpDeref.class, f -> f.apply(pointer -> {
        return optimize(pointer).<Funp>switch_().applyIf(FunpReference.class, g -> g.expr).result();
    })).applyIf(FunpIf.class, f -> f.apply((if_, then, else_) -> {
        return // 
        optimize(if_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? then : else_;
        })).result();
    })).applyIf(FunpMemory.class, f -> f.apply((pointer, start, end) -> {
        return // 
        optimize(pointer).<Funp>switch_().applyIf(FunpData.class, g -> g.apply(pairs -> {
            for (Pair<Funp, IntIntPair> pair : pairs) {
                IntIntPair range = pair.t1;
                if (start == range.t0 && end == range.t1)
                    return pair.t0;
            }
            return null;
        })).applyIf(FunpReference.class, g -> {
            return FunpTree.of(TermOp.PLUS__, g.expr, FunpNumber.ofNumber(start));
        }).result();
    })).applyIf(FunpReference.class, f -> f.apply(expr -> {
        return optimize(expr).<Funp>switch_().applyIf(FunpMemory.class, g -> g.pointer).result();
    })).applyIf(FunpTree.class, f -> f.apply((operator, lhs, rhs) -> {
        IntInt_Bool iib = TreeUtil.boolOperations.get(operator);
        IntInt_Int iii = TreeUtil.intOperations.get(operator);
        if (iib != null)
            return evaluate(iib, lhs, rhs);
        else if (iii != null)
            return evaluate(iii, lhs, rhs);
        else
            return null;
    })).applyIf(FunpTree2.class, f -> f.apply((operator, lhs, rhs) -> {
        return evaluate(TreeUtil.tupleOperations.get(operator), lhs, rhs);
    })).applyIf(FunpWhile.class, f -> f.apply((while_, do_, expr) -> {
        return // 
        optimize(while_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? null : expr;
        })).result();
    })).result();
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpBoolean(suite.funp.P0.FunpBoolean) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) TermOp(suite.node.io.TermOp) FunpReference(suite.funp.P0.FunpReference) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) IntInt_Int(suite.primitive.IntInt_Int) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) FunpIf(suite.funp.P0.FunpIf) FunpNumber(suite.funp.P0.FunpNumber) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpWhile(suite.funp.P2.FunpWhile) Switch(suite.util.Switch) FunpData(suite.funp.P2.FunpData) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) FunpWhile(suite.funp.P2.FunpWhile) FunpMemory(suite.funp.P2.FunpMemory) FunpBoolean(suite.funp.P0.FunpBoolean) Funp(suite.funp.Funp_.Funp) FunpDeref(suite.funp.P0.FunpDeref) Streamlet(suite.streamlet.Streamlet) FunpCoerce(suite.funp.P0.FunpCoerce) IntInt_Int(suite.primitive.IntInt_Int) FunpTree(suite.funp.P0.FunpTree) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IntIntPair(suite.primitive.adt.pair.IntIntPair) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 2 with Streamlet

use of suite.streamlet.Streamlet 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 3 with Streamlet

use of suite.streamlet.Streamlet in project suite by stupidsing.

the class Lister method leaves.

private Streamlet<IList<Node>> leaves(Node node, IList<Node> prefix) {
    NodeRead nr = NodeRead.of(node);
    Streamlet<IList<Node>> st;
    if (nr.type == ReadType.TUPLE)
        st = // 
        Read.from(// 
        nr.children).index().map(// 
        (i, p) -> leaves(p.t1, IList.cons(Int.of(i), prefix))).collect(As::concat);
    else if (nr.type != ReadType.TERM)
        st = Read.from(nr.children).concatMap(p -> leaves(p.t1, IList.cons(p.t0, prefix)));
    else
        st = Read.from(List.of(IList.cons(nr.terminal, prefix)));
    if (nr.op != null)
        st = st.cons(IList.cons(Atom.of(nr.op.toString()), prefix));
    return st;
}
Also used : List(java.util.List) Read(suite.streamlet.Read) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) As(suite.streamlet.As) IList(suite.immutable.IList) NodeRead(suite.node.io.Rewrite_.NodeRead) Int(suite.node.Int) Node(suite.node.Node) ReadType(suite.node.io.Rewrite_.ReadType) NodeRead(suite.node.io.Rewrite_.NodeRead) As(suite.streamlet.As) IList(suite.immutable.IList)

Example 4 with Streamlet

use of suite.streamlet.Streamlet 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 5 with Streamlet

use of suite.streamlet.Streamlet 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)

Aggregations

Streamlet (suite.streamlet.Streamlet)15 Read (suite.streamlet.Read)11 List (java.util.List)9 Map (java.util.Map)8 As (suite.streamlet.As)8 To (suite.util.To)8 Pair (suite.adt.pair.Pair)7 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 Asset (suite.trade.Asset)6 Time (suite.trade.Time)6 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 TimeRange (suite.trade.TimeRange)5 Trade_ (suite.trade.Trade_)5 Fail (suite.util.Fail)5 Streamlet2 (suite.streamlet.Streamlet2)4 Configuration (suite.trade.data.Configuration)4 String_ (suite.util.String_)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3