Search in sources :

Example 16 with DataSource

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

Example 17 with DataSource

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

the class SingleAllocBackTestTest method backTest.

private Map<String, SingleAllocBackTest> backTest(String code, String disp) {
    Strategos sr = new Strategos();
    DataSource ds = cfg.dataSource(code, period);
    return // 
    Read.<String, BuySellStrategy>empty2().cons("longHold", // 
    sr.longHold).cons("lowPassPrediction", // 
    sr.lowPassPrediction(128, 8, 8, .02f)).cons("movingAvgMeanReverting", // 
    sr.movingAvgMeanReverting(64, 8, .15f)).cons("macdSignalLineX", // 
    sr.macdSignalLineX(.8f, .9f, .85f)).cons("macdZeroLineX", // 
    sr.macdZeroLineX(.8f, .9f)).map2(// 
    (sn, strategy) -> backTest_(ds, disp + ", strategy = " + sn, strategy)).toMap();
}
Also used : ConfigurationImpl(suite.trade.data.ConfigurationImpl) List(java.util.List) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) Test(org.junit.Test) Asset(suite.trade.Asset) Forex(suite.trade.Forex) TimeRange(suite.trade.TimeRange) DataSource(suite.trade.data.DataSource)

Example 18 with DataSource

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

the class DailyMain method mamr.

// moving average mean reversion
private Result mamr(float factor) {
    String tag = "mamr";
    int nHoldDays = 8;
    Streamlet<Asset> assets = cfg.queryCompanies();
    BuySellStrategy strategy = new Strategos().movingAvgMeanReverting(64, nHoldDays, .15f);
    // pre-fetch quotes
    cfg.quote(assets.map(asset -> asset.symbol).toSet());
    // identify stocks that are mean-reverting
    Map<String, Boolean> backTestBySymbol = // 
    SerializedStoreCache.of(// 
    serialize.mapOfString(serialize.boolean_)).get(getClass().getSimpleName() + ".backTestBySymbol", () -> // 
    assets.map2(stock -> stock.symbol, stock -> {
        try {
            TimeRange period = TimeRange.threeYears();
            DataSource ds = cfg.dataSource(stock.symbol, period).range(period).validate();
            SingleAllocBackTest backTest = SingleAllocBackTest.test(ds, strategy);
            return MathUtil.isPositive(backTest.account.cash());
        } catch (Exception ex) {
            LogUtil.warn(ex + " for " + stock);
            return false;
        }
    }).toMap());
    TimeRange period = TimeRange.daysBefore(128);
    List<Trade> trades = new ArrayList<>();
    // capture signals
    for (Asset asset : assets) {
        String symbol = asset.symbol;
        if (backTestBySymbol.get(symbol))
            try {
                DataSource ds = cfg.dataSource(symbol, period).validate();
                float[] prices = ds.prices;
                int last = prices.length - 1;
                float latestPrice = prices[last];
                int signal = strategy.analyze(prices).get(last);
                int nShares = signal * asset.lotSize * Math.round(factor / nHoldDays / (asset.lotSize * latestPrice));
                Trade trade = Trade.of(nShares, symbol, latestPrice);
                if (signal != 0)
                    trades.add(trade);
            } catch (Exception ex) {
                LogUtil.warn(ex.getMessage() + " in " + asset);
            }
    }
    return new Result(tag, trades);
}
Also used : ArrayList(java.util.ArrayList) DataSource(suite.trade.data.DataSource) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) Asset(suite.trade.Asset) Strategos(suite.trade.singlealloc.Strategos) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) BuySellStrategy(suite.trade.singlealloc.BuySellStrategy)

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