Search in sources :

Example 1 with LngFltPair

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

the class StockHistory method adjustPrices.

private LngFltPair[] adjustPrices(String tag) {
    LngFltPair[] pairs0 = data.get(tag);
    int length = pairs0.length;
    LngFltPair[] pairs1 = new LngFltPair[length];
    int si = splits.length - 1;
    int di = dividends.length - 1;
    float a = 0f, b = 1f;
    for (int i = length - 1; 0 <= i; i--) {
        LngFltPair pair = pairs0[i];
        long t = pair.t0;
        if (0 <= di) {
            LngFltPair dividend = dividends[di];
            if (t < dividend.t0) {
                if (Boolean.TRUE)
                    // may got negative prices
                    a -= dividend.t1 * b;
                else
                    // may got skewed profits
                    b *= (pair.t0 - dividend.t0) / pair.t0;
                di--;
            }
        }
        if (0 <= si) {
            LngFltPair split = splits[si];
            if (t < split.t0) {
                b *= split.t1;
                si--;
            }
        }
        pairs1[i] = LngFltPair.of(pair.t0, a + b * pair.t1);
    }
    return pairs1;
}
Also used : LngFltPair(suite.primitive.adt.pair.LngFltPair)

Example 2 with LngFltPair

use of suite.primitive.adt.pair.LngFltPair 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 3 with LngFltPair

use of suite.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) {
    float sum = 0f;
    for (Pair<String, List<Trade>> pair : trades.toMultimap(trade -> trade.symbol).listEntries()) {
        LngFltPair[] dividends = fun.apply(pair.t0);
        Outlet<Trade> outlet = Outlet.of(pair.t1);
        LngIntPair tn = LngIntPair.of(0l, 0);
        Source<LngIntPair> tradeSource = () -> {
            Trade trade = outlet.next();
            long 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));
        };
        LngIntPair tn1 = tradeSource.source();
        for (LngFltPair dividend : dividends) {
            while (tn1 != null && tn1.t0 < dividend.t0) {
                tn.update(tn1.t0, tn1.t1);
                tn1 = tradeSource.source();
            }
            float amount = tn.t1 * dividend.t1;
            sum += amount - feeFun.apply(amount);
        }
    }
    return sum;
}
Also used : LngIntPair(suite.primitive.adt.pair.LngIntPair) Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) IntFltPair(suite.primitive.adt.pair.IntFltPair) IntIntSink(suite.primitive.IntIntSink) HashMap(java.util.HashMap) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Ints_(suite.primitive.Ints_) Valuation(suite.trade.Account.Valuation) Set_(suite.util.Set_) LngFltPair(suite.primitive.adt.pair.LngFltPair) Friends.min(suite.util.Friends.min) Source(suite.util.FunUtil.Source) Set(java.util.Set) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) Streamlet(suite.streamlet.Streamlet) Eod(suite.trade.data.DataSource.Eod) MathUtil(suite.math.MathUtil) As(suite.streamlet.As) LngFltPair(suite.primitive.adt.pair.LngFltPair) LngIntPair(suite.primitive.adt.pair.LngIntPair) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with LngFltPair

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

the class LngFltMap method forEach.

public void forEach(LngFltSink sink) {
    LngFltPair pair = LngFltPair.of((long) 0, (float) 0);
    LngFltSource source = source_();
    while (source.source2(pair)) sink.sink2(pair.t0, pair.t1);
}
Also used : LngFltPair(suite.primitive.adt.pair.LngFltPair) LngFltSource(suite.primitive.LngFltSource)

Aggregations

LngFltPair (suite.primitive.adt.pair.LngFltPair)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Obj_Flt (suite.primitive.FltPrimitives.Obj_Flt)2 As (suite.streamlet.As)2 Read (suite.streamlet.Read)2 Streamlet (suite.streamlet.Streamlet)2 String_ (suite.util.String_)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 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1 Constants (suite.Constants)1