Search in sources :

Example 1 with Outlet

use of suite.streamlet.Outlet in project suite by stupidsing.

the class TopDownParse method build.

private Parser build(Grammar eg) {
    Parser parser, g;
    List<Parser> parsers;
    switch(eg.type) {
        case AND___:
            parsers = buildChildren(eg);
            parser = (parse, st) -> {
                Outlet<State> o = Outlet.of(st);
                for (Parser g_ : parsers) o = o.concatMap(st_ -> st_.pr(parse, g_));
                return o;
            };
            break;
        case ENTITY:
            parser = buildEntity(eg.content);
            break;
        case EXCEPT:
            Parser parser0 = build(eg.children.get(0));
            Parser parser1 = build(eg.children.get(1));
            parser = (parse, st) -> st.p(parse, parser0).filter(st1 -> {
                String in1 = parse.in.substring(st.pos, st1.pos);
                return new State(null, 0, null, 0).p(new Parse(in1), parser1).count() == 0;
            });
            break;
        case NAMED_:
            parser = deepen(build(eg.children.get(0)), eg.content);
            break;
        case ONCE__:
            g = build(eg.children.get(0));
            parser = (parse, st) -> Outlet.of(st.pr(parse, g).take(1));
            break;
        case OPTION:
            g = build(eg.children.get(0));
            parser = (parse, st) -> st.pr(parse, g).cons(st);
            break;
        case OR____:
            parsers = buildChildren(eg);
            parser = (parse, st) -> Outlet.of(parsers).concatMap(g_ -> st.pr(parse, g_));
            break;
        case REPT0_:
            parser = buildRepeat(eg, true);
            break;
        case REPT0H:
            parser = buildRepeatHeadRecursion(eg);
            break;
        case REPT1_:
            parser = buildRepeat(eg, false);
            break;
        case STRING:
            ExpectFun e = expect.string(eg.content);
            parser = skipWhitespaces((parse, st) -> parse.expect(st, e, st.pos));
            break;
        default:
            parser = null;
    }
    return parser;
}
Also used : Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Ast(suite.ebnf.Ebnf.Ast) ExpectFun(suite.ebnf.topdown.Expect.ExpectFun) LogUtil(suite.os.LogUtil) IntIntPair(suite.primitive.adt.pair.IntIntPair) Source(suite.util.FunUtil.Source) Grammar(suite.ebnf.Grammar) Deque(java.util.Deque) List(java.util.List) GrammarType(suite.ebnf.Grammar.GrammarType) String_(suite.util.String_) Map(java.util.Map) ArrayDeque(java.util.ArrayDeque) Fail(suite.util.Fail) ExpectFun(suite.ebnf.topdown.Expect.ExpectFun)

Example 2 with Outlet

use of suite.streamlet.Outlet in project suite by stupidsing.

the class Trade_ method collectBrokeredTrades.

public static Streamlet<Trade> collectBrokeredTrades(Outlet<Trade> outlet) {
    Trade[] trades0 = outlet.toArray(Trade.class);
    List<Trade> trades1 = new ArrayList<>();
    int length0 = trades0.length;
    int i0 = 0;
    IntIntSink tx = (i0_, i1_) -> {
        if (Ints_.range(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
            while (i0_ < i1_) {
                Trade trade0 = trades0[i0_++];
                if (!String_.equals(trade0.remark, "#"))
                    trades1.add(trade0);
            }
    };
    for (int i = 1; i < length0; i++) {
        Trade trade0 = trades0[i0];
        Trade trade1 = trades0[i];
        boolean isGroup = // 
        true && // 
        String_.equals(trade0.date, trade1.date) && // 
        String_.equals(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 : 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) IntIntSink(suite.primitive.IntIntSink) ArrayList(java.util.ArrayList)

Example 3 with Outlet

use of suite.streamlet.Outlet 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 Outlet

use of suite.streamlet.Outlet in project suite by stupidsing.

the class Trade_ method collectAcquiredPrices.

public static Map<String, Float> collectAcquiredPrices(Outlet<Trade> outlet) {
    Map<String, List<IntFltPair>> acquireBySymbol = new HashMap<>();
    for (Trade trade : outlet) {
        String symbol = trade.symbol;
        int buySell = trade.buySell;
        float price = trade.price;
        List<IntFltPair> acquires0 = acquireBySymbol.getOrDefault(symbol, List.of());
        List<IntFltPair> acquires1 = new ArrayList<>();
        for (IntFltPair acquire : acquires0) {
            int n0 = acquire.t0;
            int diff = buySell < 0 ? max(0, min(-buySell, n0)) : min(0, max(-buySell, n0));
            int n1 = n0 - diff;
            buySell += diff;
            if (n1 != 0)
                acquires1.add(IntFltPair.of(n1, acquire.t1));
        }
        if (buySell != 0)
            acquires1.add(IntFltPair.of(buySell, price));
        acquireBySymbol.put(symbol, acquires1);
    }
    return // 
    Read.from2(// 
    acquireBySymbol).mapValue(acquires -> {
        IntFltPair sum = IntFltPair.of(0, 0f);
        for (IntFltPair acquire : acquires) sum.update(sum.t0 + acquire.t0, sum.t1 + acquire.t0 * acquire.t1);
        return sum.t1 / sum.t0;
    }).toMap();
}
Also used : IntFltPair(suite.primitive.adt.pair.IntFltPair) 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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

List (java.util.List)4 Map (java.util.Map)4 Outlet (suite.streamlet.Outlet)4 Read (suite.streamlet.Read)4 Source (suite.util.FunUtil.Source)4 String_ (suite.util.String_)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 Pair (suite.adt.pair.Pair)3 MathUtil (suite.math.MathUtil)3 Dbl_Dbl (suite.primitive.Dbl_Dbl)3 Obj_Flt (suite.primitive.FltPrimitives.Obj_Flt)3 IntIntSink (suite.primitive.IntIntSink)3 Obj_Int (suite.primitive.IntPrimitives.Obj_Int)3 Ints_ (suite.primitive.Ints_)3 IntFltPair (suite.primitive.adt.pair.IntFltPair)3 LngFltPair (suite.primitive.adt.pair.LngFltPair)3 LngIntPair (suite.primitive.adt.pair.LngIntPair)3 As (suite.streamlet.As)3