use of suite.trade.Time in project suite by stupidsing.
the class WalkForwardAllocTester method tick.
public String tick() {
Time time = Time.now();
Map<String, Float> priceBySymbol = cfg.quote(dsBySymbol.keySet());
for (Entry<String, Float> e : priceBySymbol.entrySet()) log.sink(time.ymdHms() + "," + e.getKey() + "," + e.getValue());
return tick(time, priceBySymbol);
}
use of suite.trade.Time in project suite by stupidsing.
the class BackAllocatorTest method testStop.
@Test
public void testStop() {
Time start = Time.of(2017, 1, 1);
String symbol = "S";
float[] prices = { 1f, .99f, .98f, .5f, .5f, .5f, 0f, 0f, 0f };
BackAllocator ba0 = (akds, ts) -> index -> List.of(Pair.of(symbol, 1d));
BackAllocator ba1 = ba0.stopLoss(.98d);
int length = prices.length;
long[] ts = Longs_.toArray(length, i -> start.addDays(i).epochSec());
DataSource ds = DataSource.of(ts, prices);
AlignKeyDataSource<String> akds = DataSource.alignAll(Read.from2(List.of(Pair.of(symbol, ds))));
int[] indices = Ints_.toArray(length, i -> i);
OnDateTime odt = ba1.allocate(akds, indices);
List<Double> potentials = //
Ints_.range(//
indices.length).map(//
index -> 0 < index ? Read.from(odt.onDateTime(index)) : Read.<Pair<String, Double>>empty()).map(//
pairs -> pairs.toDouble(Obj_Dbl.sum(pair -> pair.t1))).toList();
assertEquals(List.of(0d, 1d, 1d, 1d, 0d, 0d, 0d, 0d, 0d), potentials);
}
use of suite.trade.Time in project suite by stupidsing.
the class HkexUtilTest method test.
@Test
public void test() {
Time on_ = Time.of("2017-06-29 12:48:00");
Time off = Time.of("2017-06-29 22:48:00");
assertEquals(Time.of("2017-06-29 12:48:00"), HkexUtil.getTradeTimeBefore(on_));
assertEquals(Time.of("2017-06-29 12:48:00"), HkexUtil.getTradeTimeAfter(on_));
assertEquals(Time.of("2017-06-29 09:00:00"), HkexUtil.getOpenTimeBefore(on_));
assertEquals(Time.of("2017-06-28 16:30:00"), HkexUtil.getCloseTimeBefore(on_));
assertEquals(Time.of("2017-06-29 16:29:59"), HkexUtil.getTradeTimeBefore(off));
assertEquals(Time.of("2017-06-30 09:00:00"), HkexUtil.getTradeTimeAfter(off));
assertEquals(Time.of("2017-06-29 09:00:00"), HkexUtil.getOpenTimeBefore(off));
assertEquals(Time.of("2017-06-29 16:30:00"), HkexUtil.getCloseTimeBefore(off));
}
use of suite.trade.Time in project suite by stupidsing.
the class Yahoo method getStockHistory.
private StockHistory getStockHistory(String symbol) {
var path = HomeDir.dir("yahoo").resolve(symbol + ".txt");
StockHistory stockHistory0;
if (Files.exists(path))
try {
var lines = ex(() -> Files.readAllLines(path));
stockHistory0 = StockHistory.of(Read.from(lines).puller());
} catch (Exception ex) {
stockHistory0 = StockHistory.new_();
}
else
stockHistory0 = StockHistory.new_();
var time = HkexUtil.getCloseTimeBefore(Time.now());
StockHistory stockHistory1;
if (stockHistory0.isActive && Time.compare(stockHistory0.time, time) < 0) {
var json = queryL1(symbol, TimeRange.of(stockHistory0.time.addDays(-14), Time.now()));
var jsons = //
Read.each(json).flatMap(json_ -> json_.path("chart").path("result"));
var exchange = //
jsons.map(//
json_ -> json_.path("meta").path("exchangeName").textValue()).uniqueResult();
var ts = //
jsons.flatMap(//
json_ -> json_.path("timestamp")).collect(//
LiftLng.of(t -> getOpenTimeBefore(exchange, t.longValue()))).toArray();
var length = ts.length;
var dataJsons0 = //
Read.<String>empty().map2(tag -> //
jsons.flatMap(json_ -> {
var json0 = json_.path("indicators");
JsonNode json1;
if (//
false || //
!(json1 = json0.path("unadjclose")).isMissingNode() || !(json1 = json0.path("unadjquote")).isMissingNode())
return json1;
else
return List.of();
}).flatMap(json_ -> json_.path("unadj" + tag)));
var dataJsons1 = //
Read.each("open", "close", "high", "low", //
"volume").map2(tag -> //
jsons.flatMap(//
json_ -> json_.path("indicators").path("quote")).flatMap(json_ -> json_.path(tag)));
var data = //
Streamlet2.concat(dataJsons0, //
dataJsons1).mapValue(//
json_ -> json_.collect(LiftFlt.of(JsonNode::floatValue)).toArray()).filterValue(//
fs -> length <= fs.length).mapValue(//
fs -> New.array(length, LngFltPair.class, i -> LngFltPair.of(ts[i], fs[i]))).toMap();
var dividends = //
jsons.flatMap(//
json_ -> json_.path("events").path("dividends")).map(//
json_ -> LngFltPair.of(json_.path("date").longValue(), json_.path("amount").floatValue())).sort(//
LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
var splits = //
jsons.flatMap(//
json_ -> json_.path("events").path("splits")).map(json_ -> LngFltPair.of(json_.path("date").longValue(), //
json_.path("numerator").floatValue() / json_.path("denominator").floatValue())).sort(//
LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
if (data.containsKey("close"))
stockHistory1 = //
StockHistory.of(exchange, time, true, data, dividends, //
splits).merge(//
stockHistory0).alignToDate();
else
stockHistory1 = fail();
WriteFile.to(path).writeAndClose(stockHistory1.write());
} else
stockHistory1 = stockHistory0;
Predicate<LngFltPair> splitFilter;
LngFltPair[] splits2;
if (Equals.string(symbol, "0700.HK"))
splitFilter = pair -> pair.t0 != Time.of(2014, 5, 15, 9, 30).epochSec();
else if (Equals.string(symbol, "2318.HK"))
splitFilter = pair -> pair.t0 != Time.of(2015, 7, 27, 9, 30).epochSec();
else
splitFilter = null;
splits2 = //
splitFilter != null ? //
Read.from(stockHistory1.splits).filter(splitFilter).toArray(LngFltPair.class) : stockHistory1.splits;
var stockHistory2 = stockHistory1.create(stockHistory1.data, stockHistory1.dividends, splits2);
var stockHistory3 = LogUtil.prefix("for " + symbol + ": ", () -> stockHistory2.cleanse());
return stockHistory3;
}
use of suite.trade.Time in project suite by stupidsing.
the class HkexUtil method until.
private static Time until(Time start, int dir, Predicate<Time> pred) {
var time = start;
if (!pred.test(time)) {
time = time.thisSecond().addSeconds(dir < 0 ? 0 : 1);
Time time1 = null;
for (var d : new int[] { 14400, 3600, 300, 30, 5, 1 }) while (!pred.test(time1 = time.addSeconds(dir * d))) time = time1;
time = time1;
}
return time;
}
Aggregations