use of suite.trade.Time 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.Time 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.trade.Time in project suite by stupidsing.
the class Yahoo method tableUrl.
public String tableUrl(String symbol, TimeRange period) {
Time frDate = period.from;
Time toDate = period.to;
return //
"https://chart.finance.yahoo.com/table.csv" + "?s=" + //
encode(symbol) + "&a=" + frDate.month() + "&b=" + frDate.dow() + "&c=" + //
frDate.year() + "&d=" + toDate.month() + "&e=" + toDate.dow() + "&f=" + //
toDate.year() + //
"&g=d" + "&ignore=.csv";
}
use of suite.trade.Time in project suite by stupidsing.
the class WalkForwardAllocTester method tick.
public String tick(Time time, Map<String, Float> priceBySymbol) {
int last = windowSize - 1;
System.arraycopy(times, 0, times, 1, last);
times[last] = time.epochSec();
for (Entry<String, DataSource> e : dsBySymbol.entrySet()) {
String symbol = e.getKey();
float[] prices = e.getValue().prices;
System.arraycopy(prices, 0, prices, 1, last);
prices[last] = priceBySymbol.get(symbol);
}
AlignKeyDataSource<String> akds = new AlignKeyDataSource<>(times, Read.from2(dsBySymbol));
List<Pair<String, Double>> ratioBySymbol = wfa.allocate(akds, windowSize);
UpdatePortfolio up = Trade_.updatePortfolio(time.ymdHms(), account, ratioBySymbol, assetBySymbol, Read.from2(priceBySymbol).mapValue(Eod::of).toMap());
float valuation_;
valuations.append(valuation_ = up.valuation0);
for (Pair<String, Float> e : up.val0.streamlet()) holdBySymbol.compute(e.t0, (s, h) -> e.t1 / valuation_ + (h != null ? h : 0d));
List<Trade> trades_ = up.trades;
String actions;
if (windowSize <= valuations.size())
actions = play(trades_);
else
actions = "wait";
return //
time.ymdHms() + ", valuation = " + //
valuation_ + ", portfolio = " + //
account + ", actions = " + actions;
}
use of suite.trade.Time in project suite by stupidsing.
the class WalkForwardRecorderMain method run.
@Override
protected boolean run(String[] args) {
Streamlet<Asset> assets = cfg.queryCompaniesByMarketCap(Time.now());
float fund0 = 1000000f;
Trade_.isCacheQuotes = false;
Trade_.isShortSell = true;
Trade_.leverageAmount = fund0;
if (Boolean.FALSE) {
// record
String ts = Time.now().ymdHms().replace("-", "").replace(" ", "-").replace(":", "");
String filename = "wfa." + ts + ".csv";
Schedule schedule = //
Schedule.ofRepeat(5, () -> {
String ymdHms = Time.now().ymdHms();
Map<String, Float> priceBySymbol = cfg.quote(assets.map(asset -> asset.symbol).toSet());
try (OutputStream os = //
Files.newOutputStream(//
HomeDir.resolve(filename), //
StandardOpenOption.APPEND, //
StandardOpenOption.CREATE, //
StandardOpenOption.WRITE);
PrintWriter bw = new PrintWriter(os)) {
for (Entry<String, Float> e : priceBySymbol.entrySet()) bw.println(ymdHms + ", " + e.getKey() + ", " + e.getValue());
} catch (IOException ex) {
Fail.t(ex);
}
});
Scheduler.of(schedule.filterTime(dt -> HkexUtil.isMarketOpen(Time.of(dt)))).run();
} else {
// replay
String ts = "20170612-092616";
String filename = "wfa." + ts + ".csv";
Map<Time, Map<String, Float>> data = new TreeMap<>();
try (//
InputStream is = Files.newInputStream(HomeDir.resolve(filename));
//
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
while (br.ready()) {
String[] array = br.readLine().split(",");
Time time = Time.of(array[0].trim());
String symbol = array[1].trim();
float price = Float.parseFloat(array[2].trim());
data.computeIfAbsent(time, s -> new HashMap<>()).put(symbol, price);
}
} catch (IOException ex) {
Fail.t(ex);
}
WalkForwardAllocConfiguration wfac = new //
WalkForwardAllocConfiguration(//
cfg.queryCompaniesByMarketCap(Time.now()), bag.rsi.unleverage().walkForwardAllocator());
WalkForwardAllocTester tester = new WalkForwardAllocTester(cfg, wfac.assets, fund0, wfac.walkForwardAllocator);
for (Entry<Time, Map<String, Float>> e : data.entrySet()) System.out.println(tester.tick(e.getKey(), e.getValue()));
System.out.println(tester.conclusion());
}
return true;
}
Aggregations