Search in sources :

Example 1 with DataSource

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

the class BackAllocatorUtil method stop.

public default BackAllocator stop(double stopLoss, double stopGain) {
    return (akds, indices) -> {
        OnDateTime onDateTime = allocate(akds, indices);
        Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
        Mutable<Map<String, Double>> mutable = Mutable.of(new HashMap<>());
        Map<String, List<DblFltPair>> entriesBySymbol = new HashMap<>();
        return index -> {
            int last = index - 1;
            List<Pair<String, Double>> potentialBySymbol = onDateTime.onDateTime(index);
            Map<String, Double> potentialBySymbol0 = mutable.get();
            Map<String, Double> potentialBySymbol1 = Read.from2(potentialBySymbol).toMap();
            // find out the transactions
            Map<String, Double> diffBySymbol = // 
            Read.from(// 
            Set_.union(potentialBySymbol0.keySet(), potentialBySymbol1.keySet())).map2(symbol -> {
                double potential0 = potentialBySymbol0.getOrDefault(symbol, 0d);
                double potential1 = potentialBySymbol1.getOrDefault(symbol, 0d);
                return potential1 - potential0;
            }).toMap();
            // check on each stock symbol
            for (Entry<String, Double> e : diffBySymbol.entrySet()) {
                String symbol = e.getKey();
                double diff = e.getValue();
                int bs = Quant.sign(diff);
                float price = dsBySymbol.get(symbol).prices[last];
                List<DblFltPair> entries0 = entriesBySymbol.getOrDefault(symbol, new ArrayList<>());
                List<DblFltPair> entries1 = new ArrayList<>();
                Collections.sort(entries0, (pair0, pair1) -> -bs * Float.compare(pair0.t1, pair1.t1));
                for (DblFltPair entry0 : entries0) {
                    double potential0 = entry0.t0;
                    float entryPrice = entry0.t1;
                    double cancellation;
                    // a recent buy would cancel out the lowest price sell
                    if (bs == -1)
                        cancellation = min(0, max(diff, -potential0));
                    else if (bs == 1)
                        cancellation = max(0, min(diff, -potential0));
                    else
                        cancellation = 0d;
                    double potential1 = potential0 + cancellation;
                    diff -= cancellation;
                    double min = entryPrice * (potential1 < 0 ? stopGain : stopLoss);
                    double max = entryPrice * (potential1 < 0 ? stopLoss : stopGain);
                    // drop entries that got past their stopping prices
                    if (min < price && price < max)
                        entries1.add(DblFltPair.of(potential1, entryPrice));
                }
                if (diff != 0d)
                    entries1.add(DblFltPair.of(diff, price));
                entriesBySymbol.put(symbol, entries1);
            }
            mutable.update(potentialBySymbol1);
            // re-assemble the entries into current profile
            return // 
            Read.multimap(// 
            entriesBySymbol).groupBy(// 
            entries -> entries.toDouble(Obj_Dbl.sum(pair -> pair.t0))).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) DblFltPair(suite.primitive.adt.pair.DblFltPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mutable(suite.adt.Mutable) HashMap(java.util.HashMap) Map(java.util.Map) DblFltPair(suite.primitive.adt.pair.DblFltPair) Pair(suite.adt.pair.Pair)

Example 2 with DataSource

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

the class Factor method project.

private double project(DataSource irds0, DataSource rds0, TimeRange period) {
    DataSource rds1 = rds0.range(period);
    DataSource irds1 = irds0.range(period).alignBeforePrices(rds1.ts);
    return stat.project(irds1.returns(), rds1.returns());
}
Also used : AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 3 with DataSource

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

the class FactorLr method ols.

private LinearRegression ols(DataSource rds0, TimeRange period) {
    DataSource ys = rds0.range(period);
    float[][] returns_ = // 
    Read.from(// 
    indexPrices).map(// 
    prices -> DataSource.of(timestamps, prices).range(period).alignBeforePrices(ys.ts).returns()).toArray(float[].class);
    float[][] xs = mtx.transpose(returns_);
    return stat.linearRegression(xs, ys.returns(), indexSymbols.toArray(String.class));
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Statistic(suite.math.numeric.Statistic) Matrix(suite.math.linalg.Matrix) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Quant(ts.Quant) LinearRegression(suite.math.numeric.Statistic.LinearRegression) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) DataSourceView(suite.trade.data.DataSourceView) Int_Flt(suite.primitive.Int_Flt) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 4 with DataSource

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

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

the class StatisticalArbitrageTest method testMarketDirection.

@Test
public void testMarketDirection() {
    DataSource ds = cfg.dataSource(Asset.hsiSymbol).cleanse();
    int[] flagsArray = mt.time(ds.prices);
    String flags0 = "-----";
    for (int i = 0; i < ds.ts.length; i++) {
        String flags = // 
        String_.right("00000" + Integer.toBinaryString(flagsArray[i]), // 
        -5).replace('0', // 
        '-').replace('1', 'M');
        if (!String_.equals(flags0, flags))
            System.out.println(Time.ofEpochSec(ds.ts[i]).ymd() + " " + flags);
        flags0 = flags;
    }
}
Also used : 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