Search in sources :

Example 1 with Read

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;
}
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 2 with Read

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);
}
Also used : OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) List(java.util.List) Read(suite.streamlet.Read) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Test(org.junit.Test) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Ints_(suite.primitive.Ints_) Assert.assertEquals(org.junit.Assert.assertEquals) Longs_(suite.primitive.Longs_) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) Test(org.junit.Test)

Example 3 with Read

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();
}
Also used : ConfigurationImpl(suite.trade.data.ConfigurationImpl) List(java.util.List) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) Test(org.junit.Test) Asset(suite.trade.Asset) Forex(suite.trade.Forex) TimeRange(suite.trade.TimeRange) DataSource(suite.trade.data.DataSource)

Aggregations

List (java.util.List)3 Read (suite.streamlet.Read)3 Map (java.util.Map)2 Test (org.junit.Test)2 LogUtil (suite.os.LogUtil)2 Time (suite.trade.Time)2 TimeRange (suite.trade.TimeRange)2 DataSource (suite.trade.data.DataSource)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLEncoder (java.net.URLEncoder)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Constants (suite.Constants)1