Search in sources :

Example 11 with As

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

the class HongKongGovernment method queryWeather.

public Map<String, DataSource> queryWeather() {
    long t0 = Time.of(2000, 1, 1).epochSec();
    long tx = Time.today().epochSec();
    LongsBuilder ts = new LongsBuilder();
    FloatsBuilder fs0 = new FloatsBuilder();
    FloatsBuilder fs1 = new FloatsBuilder();
    for (long t = t0; t < tx; t += 86400l) {
        Time time = Time.ofEpochSec(t);
        String html = // 
        Singleton.me.storeCache.http(// 
        "http://www.hko.gov.hk/cgi-bin/hko/yes.pl" + "?year=" + // 
        time.year() + "&month=" + // 
        time.month() + "&day=" + // 
        time.dayOfMonth() + // 
        "&language=english&B1=Confirm#").collect(As::string);
        String data = ParseUtil.fit(html, "<pre>", "</pre>")[1];
        ts.append(t);
        fs0.append(getFloatValue(data, "Maximum Air Temperature", "C"));
        fs1.append(getFloatValue(data, "Rainfall", "mm"));
    }
    long[] ts_ = ts.toLongs().toArray();
    return // 
    Map.ofEntries(// 
    entry("hko.TEMP", DataSource.of(ts_, fs0.toFloats().toArray())), entry("hko.RAIN", DataSource.of(ts_, fs1.toFloats().toArray())));
}
Also used : As(suite.streamlet.As) LongsBuilder(suite.primitive.Longs.LongsBuilder) Time(suite.trade.Time) FloatsBuilder(suite.primitive.Floats.FloatsBuilder)

Example 12 with As

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

the class To method inputStream.

public static InputStream inputStream(Outlet<Bytes> outlet) {
    return new InputStream() {

        private InputStream is;

        private boolean isOpen = true;

        public int read() throws IOException {
            byte[] b = new byte[1];
            int nBytesRead = read(b, 0, 1);
            return 0 < nBytesRead ? b[0] : nBytesRead;
        }

        public int read(byte[] bs, int offset, int length) throws IOException {
            int nBytesRead = -1;
            while (is == null || (nBytesRead = is.read(bs, offset, length)) < 0) {
                Bytes bytes = outlet.next();
                if (isOpen = (bytes != null))
                    is = bytes.collect(As::inputStream);
                else
                    break;
            }
            return nBytesRead;
        }

        public void close() throws IOException {
            if (isOpen) {
                byte[] bs = new byte[Constants.bufferSize];
                while (0 <= read(bs, 0, bs.length)) ;
            }
        }
    };
}
Also used : Bytes(suite.primitive.Bytes) As(suite.streamlet.As) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 13 with As

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

the class Summarize method summarize.

public <K> SummarizeByStrategy<K> summarize(Fun<Trade, K> fun) {
    Streamlet2<K, Summarize_> summaryByKey = // 
    trades.groupBy(fun, // 
    trades_ -> summarize_(trades_, priceBySymbol, s -> null)).filterKey(// 
    key -> key != null).collect(As::streamlet2);
    Map<String, Map<K, Integer>> nSharesByKeyBySymbol = // 
    summaryByKey.concatMap((key, summary) -> // 
    summary.account.portfolio().map(// 
    (symbol, n) -> Fixie.of(symbol, key, n))).groupBy(Fixie3::get0, fixies0 -> // 
    fixies0.groupBy(Fixie3::get1, fixies1 -> // 
    fixies1.map(Fixie3::get2).uniqueResult()).toMap()).toMap();
    Map<String, Float> acquiredPrices = trades.collect(Trade_::collectBrokeredTrades).collect(Trade_::collectAcquiredPrices);
    Time now = Time.now();
    Summarize_ overall = summarize_(trades, priceBySymbol, symbol -> {
        boolean isMarketOpen = // 
        false || // 
        HkexUtil.isMarketOpen(now) || HkexUtil.isMarketOpen(now.addHours(1));
        DataSource ds = cfg.dataSource(symbol);
        // acquisition price
        float price0 = acquiredPrices.get(symbol);
        // previous close
        float price1 = ds.get(isMarketOpen ? -1 : -2).t1;
        // now
        float pricex = isMarketOpen ? priceBySymbol.get(symbol) : ds.get(-1).t1;
        String keys = // 
        Read.from2(// 
        nSharesByKeyBySymbol.getOrDefault(symbol, Map.ofEntries())).keys().map(// 
        Object::toString).sort(// 
        String_::compare).collect(As.joinedBy("/"));
        return // 
        percent(price1, pricex) + ", " + // 
        percent(price0, pricex) + (!keys.isEmpty() ? ", " + keys : "");
    });
    Map<K, String> outByKey = summaryByKey.mapValue(Summarize_::out0).toMap();
    StringBuilder sb = new StringBuilder();
    Sink<String> log = sb::append;
    for (Entry<K, String> e : outByKey.entrySet()) log.sink("\nFor strategy " + e.getKey() + ":" + e.getValue());
    log.sink(FormatUtil.tablize("\nOverall:\t" + Time.now().ymdHms() + overall.out1()));
    // profit and loss
    Map<K, Double> pnlByKey = // 
    sellAll(trades, priceBySymbol).groupBy(fun, // 
    t -> (double) Account.ofHistory(t).cash()).toMap();
    return new SummarizeByStrategy<>(sb.toString(), overall.account, pnlByKey);
}
Also used : Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) Fun(suite.util.FunUtil.Fun) Yahoo(suite.trade.data.Yahoo) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Fixie3(suite.adt.pair.Fixie_.Fixie3) LngFltPair(suite.primitive.adt.pair.LngFltPair) TransactionSummary(suite.trade.Account.TransactionSummary) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Object_(suite.util.Object_) Hsbc(suite.trade.data.Broker.Hsbc) To(suite.util.To) Quant(ts.Quant) Iterate(suite.util.FunUtil.Iterate) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Fixie(suite.adt.pair.Fixie) Configuration(suite.trade.data.Configuration) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Asset(suite.trade.Asset) FormatUtil(suite.util.FormatUtil) Time(suite.trade.Time) DataSource(suite.trade.data.DataSource) Trade_(suite.trade.Trade_) As(suite.streamlet.As) Map(java.util.Map)

Example 14 with As

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

the class Grapher method ungraph_.

private Node ungraph_(int id) {
    int size = gns.size();
    List<Node> nodes = // 
    Read.from(// 
    gns).map(gn -> {
        switch(gn.type) {
            case DICT:
                return new Dict();
            case TERM:
                return gn.terminal;
            case TREE:
                return Tree.of(gn.op, null, null);
            case TUPLE:
                return Tuple.of(new Node[gn.children.size()]);
            default:
                return Fail.t();
        }
    }).toList();
    for (int i = 0; i < size; i++) {
        GN gn = gns.get(i);
        Node node = nodes.get(i);
        List<Pair<Node, Node>> children = Read.from(gn.children).map(p -> Pair.of(nodes.get(p.t0), nodes.get(p.t1))).toList();
        switch(gn.type) {
            case DICT:
                ((Dict) node).map.putAll(Read.from2(children).mapValue(Reference::of).collect(As::map));
                break;
            case TERM:
                break;
            case TREE:
                Tree tree = (Tree) node;
                Tree.forceSetLeft(tree, children.get(0).t1);
                Tree.forceSetRight(tree, children.get(1).t1);
                break;
            case TUPLE:
                Node[] list = ((Tuple) node).nodes;
                for (int j = 0; j < children.size(); j++) list[j] = children.get(j).t1;
        }
    }
    return nodes.get(id);
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 15 with As

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

the class DailyMain method pairs.

public BackAllocConfiguration pairs(String symbol0, String symbol1) {
    Streamlet<Asset> assets = Read.each(symbol0, symbol1).map(cfg::queryCompany).collect(As::streamlet);
    BackAllocator backAllocator = BackAllocatorOld.me.pairs(cfg, symbol0, symbol1).unleverage();
    return new BackAllocConfiguration(time -> assets, backAllocator);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) As(suite.streamlet.As) Asset(suite.trade.Asset) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration)

Aggregations

As (suite.streamlet.As)16 List (java.util.List)7 Read (suite.streamlet.Read)7 To (suite.util.To)7 Streamlet (suite.streamlet.Streamlet)6 Asset (suite.trade.Asset)6 Time (suite.trade.Time)6 Map (java.util.Map)5 Trade_ (suite.trade.Trade_)5 URL (java.net.URL)4 Set (java.util.Set)4 Pair (suite.adt.pair.Pair)4 HttpUtil (suite.http.HttpUtil)4 TimeRange (suite.trade.TimeRange)4 String_ (suite.util.String_)4 IOException (java.io.IOException)3 Ints_ (suite.primitive.Ints_)3 Streamlet2 (suite.streamlet.Streamlet2)3 BackAllocConfiguration (suite.trade.backalloc.BackAllocConfiguration)3 Configuration (suite.trade.data.Configuration)3