Search in sources :

Example 1 with Streamlet2

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

the class BuildLr method build.

private Blr build(IList<Pair<String, Set<String>>> ps, Grammar eg, Transition nextx) {
    Fun<Streamlet2<String, Transition>, Blr> mergeAll = st2 -> {
        Transition next = newTransition(readLookahead.readLookahead(eg, nextx.keySet()));
        State state1 = newState(nextx);
        st2.sink((egn, next1) -> {
            next.put_(egn, Pair.of(state1, null));
            merges.add(Pair.of(next, next1));
        });
        return new Blr(1, next);
    };
    Pair<String, Set<String>> k;
    Blr blr;
    switch(eg.type) {
        case AND___:
            if (!eg.children.isEmpty()) {
                Grammar tail = new Grammar(GrammarType.AND___, List_.right(eg.children, 1));
                Blr blr1 = build(ps, tail, nextx);
                Blr blr0 = build(ps, eg.children.get(0), blr1.next);
                blr = new Blr(blr0.nTokens + blr1.nTokens, blr0.next);
            } else
                blr = new Blr(0, nextx);
            break;
        case ENTITY:
            k = Pair.of(eg.content, nextx.keySet());
            Transition next1 = transitions.computeIfAbsent(k, k_ -> new Transition());
            blr = mergeAll.apply(Read.each2(eg.content, next1));
            break;
        case NAMED_:
            Reduce reduce = new Reduce();
            Transition next = newTransition(nextx.keySet(), Pair.of(null, reduce));
            Blr blr1 = build(ps, eg.children.get(0), next);
            reduce.n = blr1.nTokens;
            reduce.name = eg.content;
            blr = new Blr(1, blr1.next);
            break;
        case OR____:
            List<Pair<String, Transition>> pairs = new ArrayList<>();
            for (Grammar eg1 : Read.from(eg.children)) {
                String egn = "OR." + System.identityHashCode(eg1);
                pairs.add(Pair.of(egn, build(ps, new Grammar(GrammarType.NAMED_, egn, eg1), nextx).next));
            }
            blr = mergeAll.apply(Read.from2(pairs));
            break;
        case STRING:
            State state1 = newState(nextx);
            blr = new Blr(1, kv(eg.content, state1));
            break;
        default:
            blr = Fail.t("LR parser cannot recognize " + eg.type);
    }
    return blr;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Set(java.util.Set) List_(suite.util.List_) HashMap(java.util.HashMap) IList(suite.immutable.IList) Grammar(suite.ebnf.Grammar) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) GrammarType(suite.ebnf.Grammar.GrammarType) Map(java.util.Map) Fail(suite.util.Fail) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Grammar(suite.ebnf.Grammar) Streamlet2(suite.streamlet.Streamlet2) Pair(suite.adt.pair.Pair)

Example 2 with Streamlet2

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

the class LibraryMain method run.

protected boolean run(String[] args) {
    Pair<Streamlet2<Path, Long>, Streamlet2<Path, Long>> partition = // 
    FileUtil.findPaths(Paths.get(inputDir)).filter(// 
    path -> fileExtensions.contains(FileUtil.getFileExtension(path))).map2(// 
    path -> Rethrow.ex(() -> Files.size(path))).partition((path, size) -> 0 < size);
    // remove empty files
    partition.t1.sink((path, size) -> {
        try {
            Files.delete(path);
        } catch (IOException ex) {
            Fail.t(ex);
        }
    });
    Streamlet2<Path, FileInfo> path_fileInfos = // 
    partition.t0.map2((path, size) -> {
        BasicFileAttributes attrs = Rethrow.ex(() -> Files.readAttributes(path, BasicFileAttributes.class));
        // get all file information
        List<String> tags = // 
        Ints_.range(// 
        path.getNameCount()).map(// 
        i -> path.getName(i).toString()).cons(// 
        To.string(attrs.lastModifiedTime().toInstant())).toList();
        FileInfo fileInfo = new FileInfo();
        fileInfo.md5 = Rethrow.ex(() -> Md5Crypt.md5Crypt(Files.readAllBytes(path)));
        fileInfo.tags = tags;
        return fileInfo;
    });
    // construct file listing
    try (OutputStream os = FileUtil.out(inputDir + ".listing");
        PrintWriter pw = new PrintWriter(os)) {
        for (Pair<Path, FileInfo> path_fileInfo : path_fileInfos) pw.println(path_fileInfo.t0 + path_fileInfo.t1.md5);
    } catch (IOException ex) {
        Fail.t(ex);
    }
    // 
    path_fileInfos.map2((path, fileInfo) -> {
        // move file to library, by md5
        Path path1 = Paths.get(libraryDir, fileInfo.md5.substring(0, 2), fileInfo.md5);
        FileUtil.mkdir(path1.getParent());
        Rethrow.ex(() -> Files.move(path, path1, StandardCopyOption.REPLACE_EXISTING));
        return fileInfo;
    }).concatMap((path, fileInfo) -> Read.from(fileInfo.tags).map(tag -> {
        // add to tag indices
        Path path1 = Paths.get(tagsDir, tag, fileInfo.md5);
        return Rethrow.ex(() -> {
            Files.newOutputStream(path1).close();
            return Pair.of(tag, fileInfo);
        });
    }));
    return true;
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Md5Crypt(org.apache.commons.codec.digest.Md5Crypt) Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Files(java.nio.file.Files) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) IOException(java.io.IOException) To(suite.util.To) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) RunUtil(suite.util.RunUtil) StandardCopyOption(java.nio.file.StandardCopyOption) Pair(suite.adt.pair.Pair) List(java.util.List) Paths(java.nio.file.Paths) Rethrow(suite.util.Rethrow) FileUtil(suite.os.FileUtil) Ints_(suite.primitive.Ints_) Path(java.nio.file.Path) Fail(suite.util.Fail) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Streamlet2(suite.streamlet.Streamlet2) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 3 with Streamlet2

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

the class Yahoo method getStockHistory.

private StockHistory getStockHistory(String symbol) {
    Path path = HomeDir.dir("yahoo").resolve(symbol + ".txt");
    StockHistory stockHistory0;
    if (Files.exists(path))
        try {
            List<String> lines = Rethrow.ex(() -> Files.readAllLines(path));
            stockHistory0 = StockHistory.of(Read.from(lines).outlet());
        } catch (Exception ex) {
            stockHistory0 = StockHistory.new_();
        }
    else
        stockHistory0 = StockHistory.new_();
    Time time = HkexUtil.getCloseTimeBefore(Time.now());
    StockHistory stockHistory1;
    if (stockHistory0.isActive && Time.compare(stockHistory0.time, time) < 0) {
        JsonNode json = queryL1(symbol, TimeRange.of(stockHistory0.time.addDays(-14), Time.now()));
        Streamlet<JsonNode> jsons = // 
        Read.each(json).flatMap(json_ -> json_.path("chart").path("result"));
        String exchange = // 
        jsons.map(// 
        json_ -> json_.path("meta").path("exchangeName").textValue()).uniqueResult();
        long[] ts = // 
        jsons.flatMap(// 
        json_ -> json_.path("timestamp")).collect(// 
        Obj_Lng.lift(t -> getOpenTimeBefore(exchange, t.longValue()))).toArray();
        int length = ts.length;
        Streamlet2<String, Streamlet<JsonNode>> dataJsons0 = // 
        Read.<String>empty().map2(tag -> // 
        jsons.flatMap(json_ -> {
            JsonNode json0 = json_.path("indicators");
            JsonNode json1;
            if (// 
            false || // 
            !(json1 = json0.path("unadjclose")).isMissingNode() || !(json1 = json0.path("unadjquote")).isMissingNode())
                return json1;
            else
                return List.of();
        }).flatMap(json_ -> json_.path("unadj" + tag)));
        Streamlet2<String, Streamlet<JsonNode>> dataJsons1 = // 
        Read.each("open", "close", "high", "low", // 
        "volume").map2(tag -> // 
        jsons.flatMap(// 
        json_ -> json_.path("indicators").path("quote")).flatMap(json_ -> json_.path(tag)));
        Map<String, LngFltPair[]> data = // 
        Streamlet2.concat(dataJsons0, // 
        dataJsons1).mapValue(// 
        json_ -> json_.collect(Obj_Flt.lift(JsonNode::floatValue)).toArray()).filterValue(// 
        fs -> length <= fs.length).mapValue(// 
        fs -> To.array(length, LngFltPair.class, i -> LngFltPair.of(ts[i], fs[i]))).toMap();
        LngFltPair[] dividends = // 
        jsons.flatMap(// 
        json_ -> json_.path("events").path("dividends")).map(// 
        json_ -> LngFltPair.of(json_.path("date").longValue(), json_.path("amount").floatValue())).sort(// 
        LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
        LngFltPair[] splits = // 
        jsons.flatMap(// 
        json_ -> json_.path("events").path("splits")).map(json_ -> LngFltPair.of(json_.path("date").longValue(), // 
        json_.path("numerator").floatValue() / json_.path("denominator").floatValue())).sort(// 
        LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
        if (data.containsKey("close"))
            stockHistory1 = // 
            StockHistory.of(exchange, time, true, data, dividends, // 
            splits).merge(// 
            stockHistory0).alignToDate();
        else
            stockHistory1 = Fail.t();
        FileUtil.write(path, stockHistory1.write());
    } else
        stockHistory1 = stockHistory0;
    Predicate<LngFltPair> splitFilter;
    LngFltPair[] splits2;
    if (String_.equals(symbol, "0700.HK"))
        splitFilter = pair -> pair.t0 != Time.of(2014, 5, 15, 9, 30).epochSec();
    else if (String_.equals(symbol, "2318.HK"))
        splitFilter = pair -> pair.t0 != Time.of(2015, 7, 27, 9, 30).epochSec();
    else
        splitFilter = null;
    splits2 = // 
    splitFilter != null ? // 
    Read.from(stockHistory1.splits).filter(splitFilter).toArray(LngFltPair.class) : stockHistory1.splits;
    StockHistory stockHistory2 = stockHistory1.create(stockHistory1.data, stockHistory1.dividends, splits2);
    StockHistory stockHistory3 = LogUtil.prefix("for " + symbol + ": ", () -> stockHistory2.cleanse());
    return stockHistory3;
}
Also used : Path(java.nio.file.Path) 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) Time(suite.trade.Time) JsonNode(com.fasterxml.jackson.databind.JsonNode) LngFltPair(suite.primitive.adt.pair.LngFltPair) Streamlet(suite.streamlet.Streamlet) List(java.util.List)

Example 4 with Streamlet2

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

use of suite.streamlet.Streamlet2 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));
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Arrays(java.util.Arrays) Streamlet2(suite.streamlet.Streamlet2) Statistic(suite.math.numeric.Statistic) MovingAverage(suite.trade.analysis.MovingAverage) Quant(ts.Quant) BollingerBands(ts.BollingerBands) MovingRange(suite.trade.analysis.MovingAverage.MovingRange) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) MeanVariance(suite.math.numeric.Statistic.MeanVariance) String_(suite.util.String_) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) TimeSeries(ts.TimeSeries) BackAllocator(suite.trade.backalloc.BackAllocator) Streamlet2(suite.streamlet.Streamlet2) Map(java.util.Map) DataSource(suite.trade.data.DataSource)

Aggregations

Streamlet2 (suite.streamlet.Streamlet2)8 Read (suite.streamlet.Read)7 List (java.util.List)6 Map (java.util.Map)6 Pair (suite.adt.pair.Pair)6 Configuration (suite.trade.data.Configuration)4 To (suite.util.To)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 As (suite.streamlet.As)3 Streamlet (suite.streamlet.Streamlet)3 Asset (suite.trade.Asset)3 Time (suite.trade.Time)3 Trade_ (suite.trade.Trade_)3 DataSource (suite.trade.data.DataSource)3 Fail (suite.util.Fail)3 String_ (suite.util.String_)3 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2