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