use of suite.streamlet.Read 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.Read in project suite by stupidsing.
the class BackAllocatorTest method testStop.
@Test
public void testStop() {
Time start = Time.of(2017, 1, 1);
String symbol = "S";
float[] prices = { 1f, .99f, .98f, .5f, .5f, .5f, 0f, 0f, 0f };
BackAllocator ba0 = (akds, ts) -> index -> List.of(Pair.of(symbol, 1d));
BackAllocator ba1 = ba0.stopLoss(.98d);
int length = prices.length;
long[] ts = Longs_.toArray(length, i -> start.addDays(i).epochSec());
DataSource ds = DataSource.of(ts, prices);
AlignKeyDataSource<String> akds = DataSource.alignAll(Read.from2(List.of(Pair.of(symbol, ds))));
int[] indices = Ints_.toArray(length, i -> i);
OnDateTime odt = ba1.allocate(akds, indices);
List<Double> potentials = //
Ints_.range(//
indices.length).map(//
index -> 0 < index ? Read.from(odt.onDateTime(index)) : Read.<Pair<String, Double>>empty()).map(//
pairs -> pairs.toDouble(Obj_Dbl.sum(pair -> pair.t1))).toList();
assertEquals(List.of(0d, 1d, 1d, 1d, 0d, 0d, 0d, 0d, 0d), potentials);
}
use of suite.streamlet.Read in project suite by stupidsing.
the class SingleAllocBackTestTest method backTest.
private Map<String, SingleAllocBackTest> backTest(String code, String disp) {
Strategos sr = new Strategos();
DataSource ds = cfg.dataSource(code, period);
return //
Read.<String, BuySellStrategy>empty2().cons("longHold", //
sr.longHold).cons("lowPassPrediction", //
sr.lowPassPrediction(128, 8, 8, .02f)).cons("movingAvgMeanReverting", //
sr.movingAvgMeanReverting(64, 8, .15f)).cons("macdSignalLineX", //
sr.macdSignalLineX(.8f, .9f, .85f)).cons("macdZeroLineX", //
sr.macdZeroLineX(.8f, .9f)).map2(//
(sn, strategy) -> backTest_(ds, disp + ", strategy = " + sn, strategy)).toMap();
}
Aggregations