Search in sources :

Example 1 with Log_

use of primal.os.Log_ in project suite by stupidsing.

the class LoadBalancer method run.

public void run() {
    var running = BooMutable.true_();
    var probe = new Thread(() -> {
        while (running.isTrue()) try {
            var alives1 = new ArrayList<String>();
            for (var server : servers) try (var socket = new Socket(server, port)) {
                alives1.add(server);
            } catch (SocketException ex) {
            }
            alives = alives1;
            Thread.sleep(500l);
        } catch (Exception ex) {
            Log_.error(ex);
        }
    });
    Io io = (is, os) -> {
        var count = counter.getAndIncrement();
        var alives0 = alives;
        var server = alives0.get(count % alives0.size());
        try (var socket = new Socket(server, port)) {
            var sis = socket.getInputStream();
            var sos = socket.getOutputStream();
            Read.each(Copy.streamByThread(is, sos), Copy.streamByThread(sis, os)).collect(Start::thenJoin);
        }
    };
    try {
        probe.start();
        new Listen().io(port, io);
    } finally {
        running.setFalse();
    }
}
Also used : Io(suite.os.Listen.Io) Socket(java.net.Socket) SocketException(java.net.SocketException) List(java.util.List) Copy(suite.util.Copy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Start(primal.Verbs.Start) Log_(primal.os.Log_) Listen(suite.os.Listen) BooMutable(primal.adt.BooMutable) Read(primal.MoreVerbs.Read) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Io(suite.os.Listen.Io) Listen(suite.os.Listen) Socket(java.net.Socket) SocketException(java.net.SocketException)

Example 2 with Log_

use of primal.os.Log_ in project suite by stupidsing.

the class DailyMain method run.

private boolean run() {
    Trade_.blackList = Union.of(Trade_.blackList, blackList);
    var sellPool = "sellpool";
    var ymd = HkexUtil.getCloseTimeBefore(Time.now()).ymd();
    var td = ymd + "#";
    // perform systematic trading
    var results = // 
    Read.each(// 
    alloc(bacs.pair_bb, 66666f), // 
    alloc("bug", bacs.bac_sell, 0f), // 
    alloc(bacs.pair_donchian, 100000f), // 
    alloc(bacs.pair_ema, 0f), // 
    mamr(50000f), // 
    alloc(bacs.pair_pmamr, 150000f), // 
    alloc(bacs.pair_pmamr2, 366666f), // 
    alloc(bacs.pair_pmmmr, 80000f), // 
    alloc(bacs.pair_revco, 0f), // 
    alloc(bacs.pair_tma, 0f), alloc(sellPool, bacs.bac_sell, 0f));
    // unused strategies
    if (Boolean.FALSE) {
        alloc(bacs.pair_donchian, 100000f);
        pairs(0f, "0341.HK", "0052.HK");
        sellForEarn(sellPool);
    }
    var sbs = Summarize.of(cfg).summarize(trade -> trade.strategy);
    var strategyTrades = // 
    results.concatMap2(// 
    result -> Read.from(result.trades).map2(trade -> result.strategy, trade -> trade)).filterValue(// 
    trade -> trade.buySell != 0).collect();
    var requestTrades = strategyTrades.filterKey(strategy -> !Equals.string(strategy, sellPool));
    var amounts = strategyTrades.values().collect(LiftDbl.of(Trade::amount));
    var buys_ = amounts.filter(amount -> 0d < amount).sum();
    var sells = amounts.filter(amount -> amount < 0d).sum();
    sb.append(// 
    sbs.log + "\n" + // 
    sbs.pnlByKey + // 
    "\nBUY REQUESTS" + // 
    requestTrades.filterValue(// 
    trade -> 0 < trade.buySell).sortByValue(// 
    Trade::compare).map((strategy, t) -> // 
    "" + Trade.of(td, -t.buySell, t.symbol, t.price, sellPool).record() + // 
    "\n" + // 
    Trade.of(td, +t.buySell, t.symbol, t.price, strategy).record()) + // 
    "\n" + // 
    "\nSELL REQUESTS" + // 
    requestTrades.filterValue(// 
    trade -> trade.buySell < 0).sortByValue(// 
    Trade::compare).map((strategy, t) -> // 
    "" + Trade.of(td, +t.buySell, t.symbol, t.price, strategy).record() + // 
    "\n" + // 
    Trade.of(td, -t.buySell, t.symbol, t.price, sellPool).record()) + // 
    "\n" + "\nTOTAL BUYS_ = " + // 
    To.string(buys_) + "\nTOTAL SELLS = " + // 
    To.string(sells) + // 
    "\n" + // 
    "\nSUGGESTIONS" + // 
    "\n- check your balance" + "\n- sell mamr and " + // 
    sellPool + "\n");
    var result = sb.toString().replace(".0\t", "\t");
    Log_.info(result);
    new SmtpSsl().send(null, getClass().getName(), result);
    return true;
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Union(primal.Verbs.Union) Singleton(suite.node.util.Singleton) SerializedStoreCache(suite.os.SerializedStoreCache) Trade_(suite.trade.Trade_) Math_(suite.math.Math_) Sink(primal.fp.Funs.Sink) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Bacs(suite.trade.backalloc.BackAllocConfigurations.Bacs) Instrument(suite.trade.Instrument) Streamlet(primal.streamlet.Streamlet) Math.round(java.lang.Math.round) BackAllocatorOld(suite.trade.backalloc.strategy.BackAllocatorOld) TradeCfg(suite.trade.data.TradeCfg) LiftDbl(primal.primitive.DblMoreVerbs.LiftDbl) SmtpSsl(suite.smtp.SmtpSsl) BackAllocTester(suite.trade.backalloc.BackAllocTester) Serialize(suite.serialize.Serialize) Pair(primal.adt.Pair) TradeCfgImpl(suite.trade.data.TradeCfgImpl) HkexUtil(suite.trade.data.HkexUtil) Summarize(suite.trade.analysis.Summarize) Set(java.util.Set) To(suite.util.To) Log_(primal.os.Log_) Read(primal.MoreVerbs.Read) Strategos(suite.trade.singlealloc.Strategos) List(java.util.List) Trade(suite.trade.Trade) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) AsDbl(primal.primitive.fp.AsDbl) Account(suite.trade.Account) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) Equals(primal.Verbs.Equals) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) SmtpSsl(suite.smtp.SmtpSsl)

Example 3 with Log_

use of primal.os.Log_ in project suite by stupidsing.

the class HttpNio method listen.

private void listen(Reg reg) {
    var rw = new Object() {

        // 0 - read, 1 - write, 2 - close after all written
        private int stage = 0;

        private Bytes br = Bytes.empty;

        private Bytes bw = Bytes.empty;

        private Puller<Bytes> write;

        private Source<Boolean> eater = () -> parseLine(line -> handleRequest1stLine(line.trim(), response -> {
            var data = // 
            "HTTP/1.1 " + response.status + "\r\n" + response.headers.streamlet().map((k, v) -> k + ": " + v + "\r\n").toJoinedString() + "\r\n";
            stage = 1;
            if (response.body != null)
                write = Puller.concat(Pull.from(data), response.body);
            else {
                bw = Bytes.of(data.getBytes(Utf8.charset));
                response.write.f(bytes -> {
                    if (bytes != null)
                        bw = bw.append(bytes);
                    else
                        stage = 2;
                    listen();
                });
            }
        }));

        private void listen() {
            if (!bw.isEmpty())
                reg.listenWrite(() -> bw, this::written);
            else if (stage == 0)
                reg.listenRead(in -> {
                    read(in);
                    listen();
                });
            else if (stage == 1 && write != null)
                reg.listenWrite(() -> bw = write.pull(), this::written);
            else if (stage == 2)
                reg.listenWrite(() -> null, null);
            else if (stage == 3)
                reg.listen(0, null, null, null);
        }

        private void read(Bytes in) {
            if (in != null) {
                br = br.append(in);
                while (eater.g()) ;
            } else
                // closes connection
                write = Puller.empty();
        }

        private void written(int n) {
            if (0 <= n)
                bw = bw.range(n);
            else
                stage = 3;
            listen();
        }

        private void handleRequest1stLine(String line, Sink<Response> cb) {
            var hrhl = handleRequestHeaderLine(lines -> handleRequestBody(line, lines, cb));
            eater = () -> parseLine(hrhl);
        }

        private Sink<String> handleRequestHeaderLine(Sink<List<String>> cb) {
            var lines = new ArrayList<String>();
            return line0 -> {
                var line1 = line0.trim();
                if (!line1.isEmpty())
                    lines.add(line1);
                else
                    cb.f(lines);
            };
        }

        private void handleRequestBody(String line0, List<String> headerLines, Sink<Response> cb) {
            eater = () -> // 
            FixieArray.of(// 
            line0.split(" ")).map((method, url, proto) -> handleRequestBody(proto, method, url, headerLines, cb));
        }

        private // 
        boolean handleRequestBody(// 
        String proto, // 
        String method, // 
        String url, // 
        List<String> lines, Sink<Response> cb) {
            var headers = // 
            Read.from(// 
            lines).fold(new Header(), (headers_, line_) -> // 
            Split.strl(line_, // 
            ":").map((k, v) -> headers_.put(k, v)));
            var queue = new ArrayBlockingQueue<Bytes>(Buffer.size);
            Sink<Bytes> offer = queue::add;
            Source<Bytes> take = queue::poll;
            Fun2<String, String, Request> requestFun = (host, pqs) -> Split.strl(pqs, "?").map((path0, query) -> {
                var path1 = path0.startsWith("/") ? path0 : "/" + path0;
                var path2 = ex(() -> URLDecoder.decode(path1, Utf8.charset));
                return // 
                Equals.string(proto, "HTTP/1.1") ? // 
                new Request(method, host, path2, query, headers, Puller.of(take)) : fail("only HTTP/1.1 is supported");
            });
            var pp = Split.string(url, "://");
            var request = pp != null ? Split.strl(pp.v, "/").map(requestFun) : requestFun.apply("", url);
            var cl = request.headers.getOpt("Content-Length").map(Long::parseLong);
            var te = Equals.ab(request.headers.getOpt("Transfer-Encoding"), Opt.of("chunked"));
            Log_.info(request.getLogString());
            if (te)
                eater = handleChunkedRequestBody(request, offer, cb);
            else if (cl.hasValue())
                eater = handleRequestBody(request, offer, cl.g(), cb);
            else
                eater = handleRequestBody(request, offer, 0, cb);
            return true;
        }

        private // 
        Source<Boolean> handleRequestBody(// 
        Request request, // 
        Sink<Bytes> body, // 
        long contentLength, Sink<Response> cb) {
            return new Source<>() {

                private int n;

                public Boolean g() {
                    body.f(br);
                    var isOpen = br != null;
                    if (isOpen) {
                        n += br.size();
                        br = Bytes.empty;
                    }
                    if (!isOpen || contentLength <= n)
                        cb.f(handler.handle(request));
                    return false;
                }
            };
        }

        private Source<Boolean> handleChunkedRequestBody(Request request, Sink<Bytes> body, Sink<Response> cb) {
            return () -> {
                for (var i0 = 0; i0 < br.size(); i0++) if (br.get(i0) == 10) {
                    var line = new String(br.range(0, i0).toArray(), Utf8.charset);
                    var size = Integer.parseInt(line.trim(), 16);
                    for (var i1 = i0 + 1 + size; i1 < br.size(); i1++) if (br.get(i1) == 10) {
                        var chunk = br.range(i0 + 1, i1);
                        br = br.range(i1);
                        body.f(chunk);
                        return true;
                    }
                    if (size == 0)
                        cb.f(handler.handle(request));
                }
                return false;
            };
        }

        private boolean parseLine(Sink<String> handleLine) {
            for (var i = 0; i < br.size(); i++) if (br.get(i) == 10) {
                var line = new String(br.range(0, i).toArray(), Utf8.charset);
                br = br.range(i + 1);
                handleLine.f(line);
                return true;
            }
            return false;
        }
    };
    rw.listen();
}
Also used : FixieArray(primal.adt.FixieArray) Fail.fail(primal.statics.Fail.fail) URLDecoder(java.net.URLDecoder) Reg(suite.os.ListenNio.Reg) Sink(primal.fp.Funs.Sink) ArrayList(java.util.ArrayList) Response(suite.http.Http.Response) Utf8(primal.Nouns.Utf8) Request(suite.http.Http.Request) Split(primal.MoreVerbs.Split) Opt(primal.adt.Opt) Pull(primal.MoreVerbs.Pull) Handler(suite.http.Http.Handler) ListenNio(suite.os.ListenNio) Buffer(primal.Nouns.Buffer) Source(primal.fp.Funs.Source) Header(suite.http.Http.Header) Log_(primal.os.Log_) Read(primal.MoreVerbs.Read) Bytes(primal.primitive.adt.Bytes) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) Rethrow.ex(primal.statics.Rethrow.ex) Fun2(primal.fp.Funs2.Fun2) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) ArrayList(java.util.ArrayList) Request(suite.http.Http.Request) Source(primal.fp.Funs.Source) Bytes(primal.primitive.adt.Bytes) Sink(primal.fp.Funs.Sink) Header(suite.http.Http.Header) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Puller(primal.puller.Puller) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Log_

use of primal.os.Log_ in project suite by stupidsing.

the class Render method render.

public Image render(int width, int height, BiFun<Float, R3> f) {
    var scale = 1f / max(width, height);
    int centerX = width / 2, centerY = height / 2;
    var xs = To.vector(width + 1, x -> (x - centerX) * scale);
    var ys = To.vector(height + 1, y -> (y - centerY) * scale);
    return renderPixels(width, height, (IntInt_Obj<R3>) (x, y) -> {
        R3 color;
        try {
            color = f.apply(xs[x], ys[y]);
        } catch (Exception ex) {
            Log_.error(new RuntimeException("at (" + x + ", " + y + ")", ex));
            color = new R3(1d, 1d, 1d);
        }
        return color;
    });
}
Also used : Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) R3(suite.math.R3) To(suite.util.To) Math.min(java.lang.Math.min) IntInt_Obj(primal.primitive.IntInt_Obj) Log_(primal.os.Log_) Streamlet_.forInt(suite.util.Streamlet_.forInt) Defaults(suite.cfg.Defaults) JLabel(javax.swing.JLabel) Math.max(java.lang.Math.max) ImageIcon(javax.swing.ImageIcon) As(suite.streamlet.As) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) NewInt(primal.primitive.IntVerbs.NewInt) BiFun(primal.fp.Funs2.BiFun) R3(suite.math.R3)

Aggregations

Log_ (primal.os.Log_)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Read (primal.MoreVerbs.Read)3 Equals (primal.Verbs.Equals)2 Sink (primal.fp.Funs.Sink)2 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 Math.round (java.lang.Math.round)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 URLDecoder (java.net.URLDecoder)1 Set (java.util.Set)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ImageIcon (javax.swing.ImageIcon)1 JFrame (javax.swing.JFrame)1