Search in sources :

Example 11 with DataSource

use of suite.trade.data.DataSource 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);
}
Also used : Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) Fun(suite.util.FunUtil.Fun) Yahoo(suite.trade.data.Yahoo) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Fixie3(suite.adt.pair.Fixie_.Fixie3) LngFltPair(suite.primitive.adt.pair.LngFltPair) TransactionSummary(suite.trade.Account.TransactionSummary) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Object_(suite.util.Object_) Hsbc(suite.trade.data.Broker.Hsbc) To(suite.util.To) Quant(ts.Quant) Iterate(suite.util.FunUtil.Iterate) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Fixie(suite.adt.pair.Fixie) Configuration(suite.trade.data.Configuration) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Asset(suite.trade.Asset) FormatUtil(suite.util.FormatUtil) Time(suite.trade.Time) DataSource(suite.trade.data.DataSource) Trade_(suite.trade.Trade_) As(suite.streamlet.As) Map(java.util.Map)

Example 12 with DataSource

use of suite.trade.data.DataSource in project suite by stupidsing.

the class BackAllocatorUtil method byRiskOfReturn.

public default BackAllocator byRiskOfReturn() {
    Statistic stat = new Statistic();
    int nDays = 32;
    return (akds, indices) -> {
        Map<String, float[]> returnsByKey = akds.dsByKey.mapValue(DataSource::returns).toMap();
        OnDateTime ba0 = allocate(akds, indices);
        return index -> // 
        Read.from2(// 
        ba0.onDateTime(index)).map2((symbol, potential) -> {
            float[] returns = Arrays.copyOfRange(returnsByKey.get(symbol), index - nDays, index);
            return potential / stat.variance(returns);
        }).toList();
    };
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Mutable(suite.adt.Mutable) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) HashMap(java.util.HashMap) Deque(java.util.Deque) IntPredicate(java.util.function.IntPredicate) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) DblFltPair(suite.primitive.adt.pair.DblFltPair) String_(suite.util.String_) Map(java.util.Map) Set_(suite.util.Set_) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Predicate(java.util.function.Predicate) ObjObj_Dbl(suite.primitive.DblPrimitives.ObjObj_Dbl) Datum(suite.trade.data.DataSource.Datum) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) Usex(suite.trade.Usex) Objects(java.util.Objects) WalkForwardAllocator(suite.trade.walkforwardalloc.WalkForwardAllocator) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Fail(suite.util.Fail) Statistic(suite.math.numeric.Statistic) HashMap(java.util.HashMap) Map(java.util.Map) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 13 with DataSource

use of suite.trade.data.DataSource in project suite by stupidsing.

the class BackAllocatorUtil method filterByIndexReturn.

public default BackAllocator filterByIndexReturn(Configuration cfg, String indexSymbol) {
    DataSource indexDataSource = cfg.dataSource(indexSymbol);
    return (akds, indices) -> {
        OnDateTime onDateTime = allocate(akds, indices);
        return index -> {
            Time date = Time.ofEpochSec(akds.ts[index - 1]).date();
            long t0 = date.addDays(-7).epochSec();
            long tx = date.epochSec();
            DataSource ids = indexDataSource.range(t0, tx);
            double indexPrice0 = ids.get(-1).t1;
            double indexPricex = ids.get(-2).t1;
            double indexReturn = Quant.return_(indexPrice0, indexPricex);
            return // 
            -.03f < indexReturn ? // 
            onDateTime.onDateTime(index) : List.of();
        };
    };
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Mutable(suite.adt.Mutable) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) HashMap(java.util.HashMap) Deque(java.util.Deque) IntPredicate(java.util.function.IntPredicate) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) DblFltPair(suite.primitive.adt.pair.DblFltPair) String_(suite.util.String_) Map(java.util.Map) Set_(suite.util.Set_) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Predicate(java.util.function.Predicate) ObjObj_Dbl(suite.primitive.DblPrimitives.ObjObj_Dbl) Datum(suite.trade.data.DataSource.Datum) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) Usex(suite.trade.Usex) Objects(java.util.Objects) WalkForwardAllocator(suite.trade.walkforwardalloc.WalkForwardAllocator) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Fail(suite.util.Fail) Time(suite.trade.Time) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 14 with DataSource

use of suite.trade.data.DataSource in project suite by stupidsing.

the class TimeSeriesTest method testSharpeRatio.

@Test
public void testSharpeRatio() {
    TimeRange period = TimeRange.of(Time.of(2016, 1, 1), Time.of(2017, 5, 1));
    DataSource ds = cfg.dataSource("0002.HK").range(period);
    double sharpe = ts.returnsStatDailyAnnualized(ds.prices).sharpeRatio();
    System.out.println("sharpe = " + sharpe);
    assertTrue(.04d < sharpe);
}
Also used : TimeRange(suite.trade.TimeRange) DataSource(suite.trade.data.DataSource) Test(org.junit.Test)

Example 15 with DataSource

use of suite.trade.data.DataSource in project suite by stupidsing.

the class StatisticalArbitrageTest method testAutoRegressivePowersOfTwo.

@Test
public void testAutoRegressivePowersOfTwo() {
    int power = 6;
    DataSource ds = cfg.dataSource(Asset.hsiSymbol).cleanse();
    float[] prices = ds.prices;
    float[][] mas = To.array(power, float[].class, p -> ma.movingAvg(prices, 1 << p));
    float[] returns = ts.returns(prices);
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(1 << power, // 
    prices.length).map(i -> FltObjPair.of(returns[i], Floats_.toArray(power, p -> mas[p][i - (1 << p)]))));
    System.out.println(lr);
}
Also used : KmeansCluster(suite.algo.KmeansCluster) Arrays(java.util.Arrays) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) IntFltPair(suite.primitive.adt.pair.IntFltPair) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) Random(java.util.Random) Sina(suite.trade.data.Sina) Fun(suite.util.FunUtil.Fun) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) Map(java.util.Map) FltObjPair(suite.primitive.adt.pair.FltObjPair) TimeSeries(ts.TimeSeries) Ints_(suite.primitive.Ints_) DiscreteCosineTransform(suite.math.transform.DiscreteCosineTransform) Streamlet2(suite.streamlet.Streamlet2) Statistic(suite.math.numeric.Statistic) Test(org.junit.Test) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) LinearRegression(suite.math.numeric.Statistic.LinearRegression) IntObjMap(suite.primitive.adt.map.IntObjMap) BollingerBands(ts.BollingerBands) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Floats_(suite.primitive.Floats_) Configuration(suite.trade.data.Configuration) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) Int_Flt(suite.primitive.Int_Flt) LinearRegression(suite.math.numeric.Statistic.LinearRegression) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Test(org.junit.Test)

Aggregations

DataSource (suite.trade.data.DataSource)18 Map (java.util.Map)11 Read (suite.streamlet.Read)11 Asset (suite.trade.Asset)11 Configuration (suite.trade.data.Configuration)11 AlignKeyDataSource (suite.trade.data.DataSource.AlignKeyDataSource)11 Statistic (suite.math.numeric.Statistic)9 Time (suite.trade.Time)9 List (java.util.List)8 Test (org.junit.Test)8 Pair (suite.adt.pair.Pair)8 Streamlet (suite.streamlet.Streamlet)8 Streamlet2 (suite.streamlet.Streamlet2)8 Quant (ts.Quant)8 As (suite.streamlet.As)7 TimeRange (suite.trade.TimeRange)7 Arrays (java.util.Arrays)6 HashMap (java.util.HashMap)6 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)6 String_ (suite.util.String_)6