Search in sources :

Example 1 with As

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

the class HttpUtil method httpJre.

@SuppressWarnings("unused")
private static HttpResult httpJre(String method, URL url, Outlet<Bytes> in, Map<String, String> headers) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod(method);
    headers.entrySet().forEach(e -> conn.setRequestProperty(e.getKey(), e.getValue()));
    try (OutputStream os = conn.getOutputStream()) {
        Bytes_.copy(in, os::write);
    }
    int responseCode = conn.getResponseCode();
    Outlet<Bytes> out = To.outlet(conn.getInputStream());
    if (// 
    responseCode == HttpURLConnection.HTTP_MOVED_PERM || // 
    responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
        String cookies1 = conn.getHeaderField("Set-Cookie");
        URL url1 = To.url(conn.getHeaderField("Location"));
        Map<String, String> headers1 = new HashMap<>(headers);
        if (cookies1 != null)
            headers1.put("Cookie", cookies1);
        return http(method, url1, in, headers1);
    } else if (responseCode == HttpURLConnection.HTTP_OK)
        return new HttpResult(responseCode, out);
    else
        throw new IOException(// 
        "HTTP returned " + responseCode + ": " + // 
        url + ": " + // 
        conn.getResponseMessage() + ": " + out.collect(As::string));
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) Bytes(suite.primitive.Bytes) As(suite.streamlet.As) HttpURLConnection(java.net.HttpURLConnection)

Example 2 with As

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

the class Lister method leaves.

private Streamlet<IList<Node>> leaves(Node node, IList<Node> prefix) {
    NodeRead nr = NodeRead.of(node);
    Streamlet<IList<Node>> st;
    if (nr.type == ReadType.TUPLE)
        st = // 
        Read.from(// 
        nr.children).index().map(// 
        (i, p) -> leaves(p.t1, IList.cons(Int.of(i), prefix))).collect(As::concat);
    else if (nr.type != ReadType.TERM)
        st = Read.from(nr.children).concatMap(p -> leaves(p.t1, IList.cons(p.t0, prefix)));
    else
        st = Read.from(List.of(IList.cons(nr.terminal, prefix)));
    if (nr.op != null)
        st = st.cons(IList.cons(Atom.of(nr.op.toString()), prefix));
    return st;
}
Also used : List(java.util.List) Read(suite.streamlet.Read) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) As(suite.streamlet.As) IList(suite.immutable.IList) NodeRead(suite.node.io.Rewrite_.NodeRead) Int(suite.node.Int) Node(suite.node.Node) ReadType(suite.node.io.Rewrite_.ReadType) NodeRead(suite.node.io.Rewrite_.NodeRead) As(suite.streamlet.As) IList(suite.immutable.IList)

Example 3 with As

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

the class Hkex method queryCompanies_.

private List<Asset> queryCompanies_(int pageNo) {
    JsonNode json = query(// 
    "" + // 
    "https://www.hkex.com.hk/eng/csm/ws/Result.asmx/GetData" + // 
    "?location=companySearch" + // 
    "&SearchMethod=2" + // 
    "&LangCode=en" + // 
    "&StockCode=" + // 
    "&StockName=" + // 
    "&Ranking=ByMC" + // 
    "&StockType=MB" + // 
    "&mkt=hk" + "&PageNo=" + // 
    (pageNo + 1) + // 
    "&ATypeSHEx=" + // 
    "&AType=" + // 
    "&FDD=" + // 
    "&FMM=" + // 
    "&FYYYY=" + // 
    "&TDD=" + // 
    "&TMM=" + "&TYYYY=");
    CompanySearch companySearch = mapper.convertValue(json, CompanySearch.class);
    Streamlet<List<String>> data0;
    if (Boolean.TRUE)
        data0 = // 
        Read.each(// 
        companySearch).flatMap(// 
        cs -> cs.data).concatMap(Data::tableEntries);
    else
        data0 = // 
        Read.each(// 
        json).flatMap(// 
        json_ -> json_.path("data")).flatMap(// 
        json_ -> json_.path("content")).flatMap(// 
        json_ -> json_.path("table")).flatMap(// 
        json_ -> json_.path("tr")).filter(// 
        json_ -> !json_.path("thead").asBoolean()).flatMap(// 
        json_ -> json_.path("td")).map(json_ -> Read.from(json_).map(JsonNode::asText).toList());
    Streamlet<List<String>> data1 = data0.collect(As::streamlet);
    Map<String, Integer> lotSizeBySymbol = queryLotSizeBySymbol_(data1.map(this::toSymbol));
    return data1.map(datum -> toAsset(datum, lotSizeBySymbol)).toList();
}
Also used : HttpUtil(suite.http.HttpUtil) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) SerializedStoreCache(suite.os.SerializedStoreCache) Source(suite.util.FunUtil.Source) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) To(suite.util.To) Serialize(suite.util.Serialize) HashSet(java.util.HashSet) Execute(suite.os.Execute) List(java.util.List) Streamlet(suite.streamlet.Streamlet) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) Asset(suite.trade.Asset) Fail(suite.util.Fail) InputStream(java.io.InputStream) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) As(suite.streamlet.As) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List)

Example 4 with As

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

the class Yahoo method dataSourceYql.

public DataSource dataSourceYql(String symbol, TimeRange period) {
    String yql = // 
    "select *" + // 
    " from yahoo.finance.historicaldata" + " where symbol = \"" + symbol + // 
    "\"" + " and startDate = \"" + period.from.ymd() + // 
    "\"" + " and endDate = \"" + period.to.ymd() + "\"";
    String urlString = // 
    "http://query.yahooapis.com/v1/public/yql" + "?q=" + // 
    encode(yql) + // 
    "&format=json" + // 
    "&diagnostics=true" + // 
    "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" + "&callback=";
    return Rethrow.ex(() -> {
        try (InputStream is = Singleton.me.storeCache.http(urlString).collect(To::inputStream)) {
            JsonNode json = mapper.readTree(is);
            Streamlet<JsonNode> quotes = // 
            Read.each(json).flatMap(// 
            json_ -> json_.path("query")).flatMap(// 
            json_ -> json_.path("results")).flatMap(// 
            json_ -> json_.path("quote")).collect(As::streamlet);
            Streamlet<String[]> arrays = // 
            quotes.map(json_ -> new String[] { // 
            json_.path("Date").textValue(), // 
            json_.path("Open").textValue(), // 
            json_.path("Close").textValue(), // 
            json_.path("Low").textValue(), // 
            json_.path("High").textValue() }).collect(As::streamlet);
            long[] ts = arrays.collect(Obj_Lng.lift(array -> closeTs(array[0]))).toArray();
            float[] opens = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[1]))).toArray();
            float[] closes = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[2]))).toArray();
            float[] lows = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[3]))).toArray();
            float[] highs = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[4]))).toArray();
            float[] volumes = new float[ts.length];
            return DataSource.ofOhlcv(ts, opens, closes, lows, highs, volumes);
        }
    });
}
Also used : 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) As(suite.streamlet.As) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) To(suite.util.To)

Example 5 with As

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

the class Summarize method summarize_.

private // 
Summarize_ summarize_(// 
Streamlet<Trade> trades_, // 
Map<String, Float> priceBySymbol, Iterate<String> infoFun) {
    Streamlet<Trade> trades0 = trades_;
    Streamlet<Trade> trades1 = sellAll(trades0, priceBySymbol);
    Streamlet<String> details = // 
    Read.from2(// 
    Trade_.portfolio(trades0)).map((symbol, nShares) -> {
        Asset asset = cfg.queryCompany(symbol);
        float price = priceBySymbol.get(symbol);
        String info = infoFun.apply(symbol);
        return // 
        asset + ": " + price + " * " + // 
        nShares + " = " + // 
        ((long) (nShares * price)) + (info != null ? " \t(" + info + ")" : "");
    }).sort(// 
    Object_::compare).collect(As::streamlet);
    return new Summarize_(details, trades0, trades1);
}
Also used : Trade(suite.trade.Trade) As(suite.streamlet.As) Asset(suite.trade.Asset)

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