Search in sources :

Example 1 with LngFltPair

use of primal.primitive.adt.pair.LngFltPair in project suite by stupidsing.

the class Yahoo method getStockHistory.

private StockHistory getStockHistory(String symbol) {
    var path = HomeDir.dir("yahoo").resolve(symbol + ".txt");
    StockHistory stockHistory0;
    if (Files.exists(path))
        try {
            var lines = ex(() -> Files.readAllLines(path));
            stockHistory0 = StockHistory.of(Read.from(lines).puller());
        } catch (Exception ex) {
            stockHistory0 = StockHistory.new_();
        }
    else
        stockHistory0 = StockHistory.new_();
    var time = HkexUtil.getCloseTimeBefore(Time.now());
    StockHistory stockHistory1;
    if (stockHistory0.isActive && Time.compare(stockHistory0.time, time) < 0) {
        var json = queryL1(symbol, TimeRange.of(stockHistory0.time.addDays(-14), Time.now()));
        var jsons = // 
        Read.each(json).flatMap(json_ -> json_.path("chart").path("result"));
        var exchange = // 
        jsons.map(// 
        json_ -> json_.path("meta").path("exchangeName").textValue()).uniqueResult();
        var ts = // 
        jsons.flatMap(// 
        json_ -> json_.path("timestamp")).collect(// 
        LiftLng.of(t -> getOpenTimeBefore(exchange, t.longValue()))).toArray();
        var length = ts.length;
        var dataJsons0 = // 
        Read.<String>empty().map2(tag -> // 
        jsons.flatMap(json_ -> {
            var 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)));
        var dataJsons1 = // 
        Read.each("open", "close", "high", "low", // 
        "volume").map2(tag -> // 
        jsons.flatMap(// 
        json_ -> json_.path("indicators").path("quote")).flatMap(json_ -> json_.path(tag)));
        var data = // 
        Streamlet2.concat(dataJsons0, // 
        dataJsons1).mapValue(// 
        json_ -> json_.collect(LiftFlt.of(JsonNode::floatValue)).toArray()).filterValue(// 
        fs -> length <= fs.length).mapValue(// 
        fs -> New.array(length, LngFltPair.class, i -> LngFltPair.of(ts[i], fs[i]))).toMap();
        var 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);
        var 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();
        WriteFile.to(path).writeAndClose(stockHistory1.write());
    } else
        stockHistory1 = stockHistory0;
    Predicate<LngFltPair> splitFilter;
    LngFltPair[] splits2;
    if (Equals.string(symbol, "0700.HK"))
        splitFilter = pair -> pair.t0 != Time.of(2014, 5, 15, 9, 30).epochSec();
    else if (Equals.string(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;
    var stockHistory2 = stockHistory1.create(stockHistory1.data, stockHistory1.dividends, splits2);
    var stockHistory3 = LogUtil.prefix("for " + symbol + ": ", () -> stockHistory2.cleanse());
    return stockHistory3;
}
Also used : Fail.fail(primal.statics.Fail.fail) LogUtil(suite.os.LogUtil) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) New(primal.Verbs.New) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Utf8(primal.Nouns.Utf8) HttpClient(suite.http.HttpClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) LiftFlt(primal.primitive.FltMoreVerbs.LiftFlt) Files(java.nio.file.Files) LiftLng(primal.primitive.LngMoreVerbs.LiftLng) Predicate(java.util.function.Predicate) Compare(primal.Verbs.Compare) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) WriteFile(primal.Verbs.WriteFile) Read(primal.MoreVerbs.Read) HomeDir(suite.cfg.HomeDir) URLEncoder(java.net.URLEncoder) List(java.util.List) Time(suite.trade.Time) Rethrow.ex(primal.statics.Rethrow.ex) As(suite.streamlet.As) Streamlet2(primal.streamlet.Streamlet2) Equals(primal.Verbs.Equals) TimeRange(suite.trade.TimeRange) LngFltPair(primal.primitive.adt.pair.LngFltPair) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with LngFltPair

use of primal.primitive.adt.pair.LngFltPair in project suite by stupidsing.

the class Trade_ method dividend.

public static float dividend(Streamlet<Trade> trades, Fun<String, LngFltPair[]> fun, Dbl_Dbl feeFun) {
    var sum = 0f;
    for (var pair : Read.fromMultimap(trades.toMultimap(trade -> trade.symbol))) {
        var dividends = fun.apply(pair.k);
        var puller = Puller.of(pair.v);
        LngIntPair tn = LngIntPair.of(0l, 0);
        Source<LngIntPair> tradeSource = () -> {
            var trade = puller.pull();
            var t = trade != null ? Time.of(trade.date + " 12:00:00").epochSec(8) : Long.MAX_VALUE;
            return LngIntPair.of(t, tn.t1 + (trade != null ? trade.buySell : 0));
        };
        var tn1 = tradeSource.g();
        for (var dividend : dividends) {
            while (tn1 != null && tn1.t0 < dividend.t0) {
                tn.update(tn1.t0, tn1.t1);
                tn1 = tradeSource.g();
            }
            var amount = tn.t1 * dividend.t1;
            sum += amount - feeFun.apply(amount);
        }
    }
    return sum;
}
Also used : Union(primal.Verbs.Union) Math.log1p(java.lang.Math.log1p) Fun(primal.fp.Funs.Fun) Math_(suite.math.Math_) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) Math.abs(java.lang.Math.abs) Obj_Flt(primal.primitive.FltPrim.Obj_Flt) Dbl_Dbl(primal.primitive.Dbl_Dbl) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Math.expm1(java.lang.Math.expm1) Valuation(suite.trade.Account.Valuation) Pair(primal.adt.Pair) IntFltPair(primal.primitive.adt.pair.IntFltPair) IntIntSink(primal.primitive.IntIntSink) Source(primal.fp.Funs.Source) Set(java.util.Set) Math.min(java.lang.Math.min) Read(primal.MoreVerbs.Read) LngIntPair(primal.primitive.adt.pair.LngIntPair) List(java.util.List) Eod(suite.trade.data.DataSource.Eod) AsInt(primal.primitive.fp.AsInt) Math.max(java.lang.Math.max) Math.floor(java.lang.Math.floor) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) LngIntPair(primal.primitive.adt.pair.LngIntPair)

Aggregations

HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Read (primal.MoreVerbs.Read)2 Equals (primal.Verbs.Equals)2 LngFltPair (primal.primitive.adt.pair.LngFltPair)2 Streamlet (primal.streamlet.Streamlet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Math.abs (java.lang.Math.abs)1 Math.expm1 (java.lang.Math.expm1)1 Math.floor (java.lang.Math.floor)1 Math.log1p (java.lang.Math.log1p)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 URLEncoder (java.net.URLEncoder)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1