Search in sources :

Example 1 with Longs_

use of suite.primitive.Longs_ in project suite by stupidsing.

the class PairTest method test.

private void test(TimeRange period, String symbol0, String symbol1) {
    DataSource ds0 = cfg.dataSource(symbol0, period);
    DataSource ds1 = cfg.dataSource(symbol1, period);
    LngStreamlet ts0 = Longs_.of(ds0.ts);
    LngStreamlet ts1 = Longs_.of(ds1.ts);
    long[] tradeTimes = Longs_.concat(ts0, ts1).distinct().sort().toArray();
    float[] prices0 = ds0.alignBeforePrices(tradeTimes).prices;
    float[] prices1 = ds1.alignBeforePrices(tradeTimes).prices;
    int length = prices0.length;
    LinearRegression lr = statistic.linearRegression(// 
    Ints_.range(// 
    length).map(i -> FltObjPair.of(prices1[i], new float[] { prices0[i], 1f })));
    System.out.println(symbol0 + " -> " + symbol1 + lr);
    assertTrue(.4d < lr.r2);
}
Also used : ConfigurationImpl(suite.trade.data.ConfigurationImpl) Statistic(suite.math.numeric.Statistic) Configuration(suite.trade.data.Configuration) FltObjPair(suite.primitive.adt.pair.FltObjPair) LngStreamlet(suite.primitive.streamlet.LngStreamlet) DataSource(suite.trade.data.DataSource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Ints_(suite.primitive.Ints_) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Longs_(suite.primitive.Longs_) LngStreamlet(suite.primitive.streamlet.LngStreamlet) LinearRegression(suite.math.numeric.Statistic.LinearRegression) DataSource(suite.trade.data.DataSource)

Example 2 with Longs_

use of suite.primitive.Longs_ 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)

Aggregations

Test (org.junit.Test)2 Ints_ (suite.primitive.Ints_)2 Longs_ (suite.primitive.Longs_)2 DataSource (suite.trade.data.DataSource)2 List (java.util.List)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Pair (suite.adt.pair.Pair)1 Statistic (suite.math.numeric.Statistic)1 LinearRegression (suite.math.numeric.Statistic.LinearRegression)1 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)1 FltObjPair (suite.primitive.adt.pair.FltObjPair)1 LngStreamlet (suite.primitive.streamlet.LngStreamlet)1 Read (suite.streamlet.Read)1 Time (suite.trade.Time)1 OnDateTime (suite.trade.backalloc.BackAllocator.OnDateTime)1 Configuration (suite.trade.data.Configuration)1 ConfigurationImpl (suite.trade.data.ConfigurationImpl)1 AlignKeyDataSource (suite.trade.data.DataSource.AlignKeyDataSource)1