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);
}
});
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations