Search in sources :

Example 6 with Streamlet

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

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

the class Chr method chrIf.

private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
    Prototype prototype = Prototype.of(if_);
    Fun<State, Streamlet<State>> fun = state -> {
        ISet<Node> facts = getFacts(state, prototype);
        Predicate<Node> bindFun = bindFun(trail, if_);
        return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
    };
    return states.concatMap(fun);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Prover(suite.lp.doer.Prover) Read(suite.streamlet.Read) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IMap(suite.immutable.IMap) ISet(suite.immutable.ISet) TermOp(suite.node.io.TermOp) To(suite.util.To) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Prototype(suite.lp.kb.Prototype) Binder(suite.lp.doer.Binder) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) Streamlet(suite.streamlet.Streamlet) ISet(suite.immutable.ISet) Predicate(java.util.function.Predicate)

Example 8 with Streamlet

use of suite.streamlet.Streamlet 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 9 with Streamlet

use of suite.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) {
    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 10 with Streamlet

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

the class CollectDataTest method test.

@Test
public void test() throws IOException {
    Streamlet<String> equities = // 
    Streamlet.concat(// 
    hkex.queryCompanies().map(company -> company.symbol), forex.invertedCurrencies.keys());
    for (String code : equities) {
        String urlString = yahoo.tableUrl(code, TimeRange.ages());
        URL url = To.url(urlString);
        try (FileOutputStream fos = new FileOutputStream("/data/storey/markets/" + code + ".csv")) {
            Copy.stream(To.inputStream(HttpUtil.get(url).out), fos);
        }
        Thread_.sleepQuietly(2000);
    }
}
Also used : HttpUtil(suite.http.HttpUtil) Streamlet(suite.streamlet.Streamlet) Copy(suite.util.Copy) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) Forex(suite.trade.Forex) TimeRange(suite.trade.TimeRange) FileOutputStream(java.io.FileOutputStream) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Streamlet (suite.streamlet.Streamlet)17 Read (suite.streamlet.Read)13 List (java.util.List)11 Map (java.util.Map)10 As (suite.streamlet.As)10 To (suite.util.To)9 Set (java.util.Set)8 Pair (suite.adt.pair.Pair)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Time (suite.trade.Time)7 InputStream (java.io.InputStream)6 TimeRange (suite.trade.TimeRange)6 Fail (suite.util.Fail)6 String_ (suite.util.String_)6 Streamlet2 (suite.streamlet.Streamlet2)5 Trade_ (suite.trade.Trade_)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 URL (java.net.URL)4