Search in sources :

Example 1 with Streamlet

use of primal.streamlet.Streamlet in project suite by stupidsing.

the class Trade_ method collectBrokeredTrades.

public static Streamlet<Trade> collectBrokeredTrades(Puller<Trade> puller) {
    var trades0 = puller.toArray(Trade.class);
    var trades1 = new ArrayList<Trade>();
    var length0 = trades0.length;
    var i0 = 0;
    IntIntSink tx = (i0_, i1_) -> {
        if (forInt(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
            while (i0_ < i1_) {
                var trade0 = trades0[i0_++];
                if (!Equals.string(trade0.remark, "#"))
                    trades1.add(trade0);
            }
    };
    for (var i = 1; i < length0; i++) {
        var trade0 = trades0[i0];
        var trade1 = trades0[i];
        var isGroup = // 
        true && // 
        Equals.string(trade0.date, trade1.date) && // 
        Equals.string(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
        if (!isGroup) {
            tx.sink2(i0, i);
            i0 = i;
        }
    }
    tx.sink2(i0, length0);
    return Read.from(trades1);
}
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) IntIntSink(primal.primitive.IntIntSink) ArrayList(java.util.ArrayList)

Example 2 with Streamlet

use of primal.streamlet.Streamlet 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)

Example 3 with Streamlet

use of primal.streamlet.Streamlet in project suite by stupidsing.

the class Plotty method plot.

public boolean plot(Streamlet<float[]> xyts) {
    var data = // 
    xyts.map(// 
    xyt -> ReadFlt.from(xyt).index().map((y, x) -> FltFltPair.of(x, y)).collect(this::xyt) + ",").toJoinedString();
    var file = Tmp.path("plot$" + Get.temp() + ".html");
    WriteFile.to(file).writeAndClose(// 
    "" + // 
    "<head><script src='https://cdn.plot.ly/plotly-latest.min.js'></script></head>" + // 
    "<body><div id='plot'></div></body>" + // 
    "<script>" + "Plotly.newPlot('plot', [" + data + // 
    "], {" + // 
    "	yaxis: { rangemode: 'tozero', zeroline: true, }" + // 
    "});" + "</script>");
    Read.from(browsers).filter(b -> new File(b).exists()).forEach(browser -> Execute.shell("'" + browser + "' --incognito '" + file + "'"));
    return true;
}
Also used : Streamlet(primal.streamlet.Streamlet) Execute(suite.os.Execute) ReadFlt(primal.primitive.FltMoreVerbs.ReadFlt) FltFltPair(primal.primitive.adt.pair.FltFltPair) Get(primal.Verbs.Get) WriteFile(primal.Verbs.WriteFile) Puller(primal.puller.Puller) File(java.io.File) Read(primal.MoreVerbs.Read) Tmp(primal.Nouns.Tmp) WriteFile(primal.Verbs.WriteFile) File(java.io.File)

Aggregations

Read (primal.MoreVerbs.Read)3 Puller (primal.puller.Puller)3 Streamlet (primal.streamlet.Streamlet)3 Math.abs (java.lang.Math.abs)2 Math.expm1 (java.lang.Math.expm1)2 Math.floor (java.lang.Math.floor)2 Math.log1p (java.lang.Math.log1p)2 Math.max (java.lang.Math.max)2 Math.min (java.lang.Math.min)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Equals (primal.Verbs.Equals)2 Union (primal.Verbs.Union)2 Pair (primal.adt.Pair)2 Fun (primal.fp.Funs.Fun)2 Source (primal.fp.Funs.Source)2 Dbl_Dbl (primal.primitive.Dbl_Dbl)2