Search in sources :

Example 1 with Time

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);
}
Also used : Time(suite.trade.Time)

Example 2 with Time

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);
}
Also used : OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) List(java.util.List) Read(suite.streamlet.Read) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Test(org.junit.Test) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Ints_(suite.primitive.Ints_) Assert.assertEquals(org.junit.Assert.assertEquals) Longs_(suite.primitive.Longs_) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) OnDateTime(suite.trade.backalloc.BackAllocator.OnDateTime) Pair(suite.adt.pair.Pair) Test(org.junit.Test)

Example 3 with Time

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));
}
Also used : Time(suite.trade.Time) Test(org.junit.Test)

Example 4 with Time

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;
}
Also used : Fail.fail(primal.statics.Fail.fail) LogUtil(suite.os.LogUtil) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) New(primal.Verbs.New) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Utf8(primal.Nouns.Utf8) HttpClient(suite.http.HttpClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) LiftFlt(primal.primitive.FltMoreVerbs.LiftFlt) Files(java.nio.file.Files) LiftLng(primal.primitive.LngMoreVerbs.LiftLng) Predicate(java.util.function.Predicate) Compare(primal.Verbs.Compare) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) WriteFile(primal.Verbs.WriteFile) Read(primal.MoreVerbs.Read) HomeDir(suite.cfg.HomeDir) URLEncoder(java.net.URLEncoder) List(java.util.List) Time(suite.trade.Time) Rethrow.ex(primal.statics.Rethrow.ex) As(suite.streamlet.As) Streamlet2(primal.streamlet.Streamlet2) Equals(primal.Verbs.Equals) TimeRange(suite.trade.TimeRange) LngFltPair(primal.primitive.adt.pair.LngFltPair) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with Time

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;
}
Also used : Time(suite.trade.Time)

Aggregations

Time (suite.trade.Time)13 Map (java.util.Map)5 As (suite.streamlet.As)5 Read (suite.streamlet.Read)5 Streamlet (suite.streamlet.Streamlet)5 Asset (suite.trade.Asset)5 Trade_ (suite.trade.Trade_)5 Configuration (suite.trade.data.Configuration)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Entry (java.util.Map.Entry)4 Pair (suite.adt.pair.Pair)4 DataSource (suite.trade.data.DataSource)4 Streamlet2 (suite.streamlet.Streamlet2)3 AlignKeyDataSource (suite.trade.data.DataSource.AlignKeyDataSource)3 Fun (suite.util.FunUtil.Fun)3 Object_ (suite.util.Object_)3 Files (java.nio.file.Files)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2