Search in sources :

Example 6 with TimeRange

use of suite.trade.TimeRange 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 7 with TimeRange

use of suite.trade.TimeRange 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

TimeRange (suite.trade.TimeRange)7 As (suite.streamlet.As)4 Read (suite.streamlet.Read)4 Streamlet (suite.streamlet.Streamlet)4 Streamlet2 (suite.streamlet.Streamlet2)4 Asset (suite.trade.Asset)4 Time (suite.trade.Time)4 DataSource (suite.trade.data.DataSource)4 List (java.util.List)3 Map (java.util.Map)3 Configuration (suite.trade.data.Configuration)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Pair (suite.adt.pair.Pair)2 LogUtil (suite.os.LogUtil)2 Ints_ (suite.primitive.Ints_)2 BackAllocator (suite.trade.backalloc.BackAllocator)2 String_ (suite.util.String_)2 To (suite.util.To)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1