Search in sources :

Example 1 with AlignKeyDataSource

use of suite.trade.data.DataSource.AlignKeyDataSource 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 2 with AlignKeyDataSource

use of suite.trade.data.DataSource.AlignKeyDataSource 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;
}
Also used : Read(suite.streamlet.Read) Statistic(suite.math.numeric.Statistic) FloatsBuilder(suite.primitive.Floats.FloatsBuilder) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) To(suite.util.To) UpdatePortfolio(suite.trade.Trade_.UpdatePortfolio) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Eod(suite.trade.data.DataSource.Eod) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) TimeSeries(ts.TimeSeries) Asset(suite.trade.Asset) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Trade(suite.trade.Trade) UpdatePortfolio(suite.trade.Trade_.UpdatePortfolio) Eod(suite.trade.data.DataSource.Eod) Pair(suite.adt.pair.Pair)

Example 3 with AlignKeyDataSource

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

the class StatisticalArbitrageTest method testCointegration.

// Auto-regressive test
@Test
public void testCointegration() {
    // 0004.HK, 0020.HK
    // 0011.HK, 0005.HK
    int tor = 8;
    String symbol0 = "0004.HK";
    String symbol1 = "0945.HK";
    AlignKeyDataSource<String> akds = cfg.dataSources(period, Read.each(symbol0, symbol1));
    Map<String, float[]> pricesBySymbol = akds.dsByKey.mapValue(DataSource::returns).toMap();
    int length = akds.ts.length;
    float[] prices0 = pricesBySymbol.get(symbol0);
    float[] prices1 = pricesBySymbol.get(symbol1);
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(tor, // 
    length).map(i -> FltObjPair.of(prices1[i], Floats_.toArray(tor, j -> prices0[i + j - tor]))));
    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) Test(org.junit.Test)

Aggregations

Pair (suite.adt.pair.Pair)3 Read (suite.streamlet.Read)3 Time (suite.trade.Time)3 DataSource (suite.trade.data.DataSource)3 AlignKeyDataSource (suite.trade.data.DataSource.AlignKeyDataSource)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 Statistic (suite.math.numeric.Statistic)2 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)2 Ints_ (suite.primitive.Ints_)2 Streamlet (suite.streamlet.Streamlet)2 Asset (suite.trade.Asset)2 Configuration (suite.trade.data.Configuration)2 To (suite.util.To)2 TimeSeries (ts.TimeSeries)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Entry (java.util.Map.Entry)1