use of suite.trade.TimeRange in project suite by stupidsing.
the class DailyMain method alloc.
private Result alloc(String tag, float fund, BackAllocator backAllocator, Streamlet<Asset> assets) {
TimeRange period = TimeRange.daysBefore(64);
Simulate sim = BackAllocTester.of(cfg, period, assets, backAllocator, log).simulate(fund);
Account account0 = Account.ofPortfolio(cfg.queryHistory().filter(r -> String_.equals(r.strategy, tag)));
Account account1 = sim.account;
Map<String, Integer> assets0 = account0.assets();
Map<String, Integer> assets1 = account1.assets();
Set<String> symbols = Set_.union(assets0.keySet(), assets1.keySet());
Map<String, Float> priceBySymbol = cfg.quote(symbols);
List<Trade> trades = Trade_.diff(Trade.NA, assets0, assets1, priceBySymbol::get).toList();
sb.append("\nstrategy = " + tag + ", " + sim.conclusion());
return new Result(tag, trades);
}
use of suite.trade.TimeRange 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.trade.TimeRange in project suite by stupidsing.
the class FactorLr method ols.
private LinearRegression ols(DataSource rds0, TimeRange period) {
DataSource ys = rds0.range(period);
float[][] returns_ = //
Read.from(//
indexPrices).map(//
prices -> DataSource.of(timestamps, prices).range(period).alignBeforePrices(ys.ts).returns()).toArray(float[].class);
float[][] xs = mtx.transpose(returns_);
return stat.linearRegression(xs, ys.returns(), indexSymbols.toArray(String.class));
}
use of suite.trade.TimeRange in project suite by stupidsing.
the class BackTestMain method run.
@Override
protected boolean run(String[] args) {
// BEGIN
// END
String arg0 = 0 < args.length ? args[0] : "";
String arg1 = 1 < args.length ? args[1] : "";
String arg2 = 2 < args.length ? args[2] : "";
Streamlet<String> strategyMatches = !arg0.isEmpty() ? Read.from(arg0.split(",")) : null;
Streamlet<Integer> years = !arg1.isEmpty() ? //
Read.from(//
arg1.split(",")).concatMap(s -> {
Pair<String, String> pair = ParseUtil.search(s, "-", Assoc.RIGHT);
return //
pair != null ? //
Ints_.range(Integer.valueOf(pair.t0), Integer.valueOf(pair.t1)).map(i -> i) : Read.each(Integer.valueOf(s));
}) : //
Ints_.range(2007, Trade_.thisYear).map(i -> i);
Fun<Time, Streamlet<Asset>> fun = //
!arg2.isEmpty() ? //
time -> Read.from(arg2.split(",")).map(cfg::queryCompany).collect(As::streamlet) : cfg::queryCompaniesByMarketCap;
BackAllocConfigurations bac_ = new BackAllocConfigurations(cfg, fun);
Streamlet2<String, BackAllocConfiguration> bacByTag = bac_.bacs().bacByName;
Streamlet2<String, Simulate> simulationByKey = //
bacByTag.filterKey(//
n -> strategyMatches == null || strategyMatches.isAny(sm -> Wildcard.match(sm, n) != null)).map(//
Pair::of).join2(//
years.sort(Object_::compare).map(TimeRange::ofYear)).map2((pair, period) -> pair.t0, (pair, period) -> {
BackAllocConfiguration bac = pair.t1;
Streamlet<Asset> assets = bac.assetsFun.apply(period.from);
return runner.backTest(bac.backAllocator, period, assets);
}).collect(As::streamlet2);
String content0 = //
Read.bytes(//
Paths.get("src/main/java/" + getClass().getName().replace('.', '/') + ".java")).collect(//
As::utf8decode).map(//
Chars::toString).collect(As::joined);
String content1 = ParseUtil.fit(content0, "// BEGIN", "// END")[1];
System.out.println(content1);
System.out.println(runner.conclude(simulationByKey));
return true;
}
use of suite.trade.TimeRange in project suite by stupidsing.
the class DataSourceView method get.
public V get(String symbol, int index) {
TimeRange period = period(index);
Map<TimeRange, V> m = viewByKey.get(symbol);
return m != null ? m.get(period) : null;
}
Aggregations