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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations