Ejemplo n.º 1
0
    def test_subscribe_bars(self, name, datastore):
        start_date = date(2011, 1, 1)
        end_date = date(2011, 1, 5)
        sub_key = HistDataSubscriptionKey(
            inst_id=10,
            provider_id=Broker.IB,
            subscription_type=BarSubscriptionType(bar_type=BarType.Time,
                                                  bar_size=BarSize.D1),
            from_date=start_date,
            to_date=end_date)

        date_val = start_date

        expect_val = []
        for i in range(1, 5):
            persistable = Bar(
                timestamp=DateUtils.date_to_unixtimemillis(date_val),
                type=BarType.Time,
                size=BarSize.D1,
                inst_id=10,
                open=18 + i,
                high=19 + i,
                low=17 + i,
                close=17.5 + i,
                vol=100)
            datastore.save_bar(persistable)
            expect_val.append(persistable)
            date_val = date_val + timedelta(days=1)

        actual_val = datastore.load_mktdata(sub_key)
        self.assertEqual(expect_val, actual_val)
Ejemplo n.º 2
0
    def test_strategy(self, name, datastore):
        instrument = 0

        dates = [datetime(2000, 1, 1), datetime(2015, 1, 1)]

        conf = BacktestingConfig(stg_id="sma",
                                 portfolio_id='test',
                                 instrument_ids=[instrument],
                                 subscription_types=[
                                     BarSubscriptionType(bar_type=BarType.Time,
                                                         bar_size=BarSize.D1)
                                 ],
                                 from_date=dates[0],
                                 to_date=dates[-1],
                                 broker_id=Broker.Simulator,
                                 feed_id=Feed.PandasMemory)

        stg = Strategy(stg_id='st1')
        # nos = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000,
        #                       limit_price=18.5)
        #
        # order = Order(nos=nos)
        # stg.ord_reqs[nos.cl_ord_id] = nos
        # stg.orders[order.cl_ord_id] = order
        # stg.add_position(nos.inst_id, nos.cl_id, nos.cl_ord_id, nos.qty)
        # stg.update_position_price(time=0, inst_id=nos.inst_id, price=100)

        DataStoreTest.save_load(name, stg, datastore, datastore.save_strategy,
                                'strategies')
Ejemplo n.º 3
0
    def __init__(self, id=None, stg_id=None, stg_cls=None, portfolio_id=None,
                 instrument_ids=None,
                 subscription_types=None,
                 feed_id=None, broker_id=None, stg_configs=None,
                 ref_data_mgr_type=RefDataManager.InMemory,
                 clock_type = Clock.Simulation,
                 persistence_config=None,
                 provider_configs=None):
        super(TradingConfig, self).__init__(id = id if id else stg_id, ref_data_mgr_type=ref_data_mgr_type, clock_type=clock_type, persistence_config= persistence_config,  provider_configs=provider_configs)
        self.stg_id = stg_id
        self.stg_cls = stg_cls
        self.portfolio_id = portfolio_id

        self.instrument_ids = instrument_ids
        if not isinstance(self.instrument_ids, (list, tuple)):
            self.instrument_ids = [self.instrument_ids]

        self.subscription_types = subscription_types if subscription_types else [
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)]
        if not isinstance(self.subscription_types, (list, tuple)):
            self.subscription_types = [self.subscription_types]

        self.feed_id = feed_id
        self.broker_id = broker_id
        self.clock_type = clock_type
        self.stg_configs = stg_configs
Ejemplo n.º 4
0
    def test_trading_config(self, name, serializer):
        instrument = 0

        dates = [datetime.datetime(2000, 1, 1), datetime.datetime(2015, 1, 1)]

        config = BacktestingConfig(stg_id="sma", portfolio_id='test',
                                   instrument_ids=[instrument],
                                   subscription_types=[BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
                                   from_date=dates[0], to_date=dates[-1],
                                   broker_id=Broker.Simulator,
                                   feed_id=Feed.PandasMemory)
        SerializerTest.ser_deser(name, serializer, config)
Ejemplo n.º 5
0
    def test_trading_config(self, name, datastore):
        instrument = 0

        dates = [datetime(2000, 1, 1), datetime(2015, 1, 1)]

        persistable = BacktestingConfig(stg_id="sma",
                                        portfolio_id='test',
                                        instrument_ids=[instrument],
                                        subscription_types=[
                                            BarSubscriptionType(
                                                bar_type=BarType.Time,
                                                bar_size=BarSize.D1)
                                        ],
                                        from_date=dates[0],
                                        to_date=dates[-1],
                                        broker_id=Broker.Simulator,
                                        feed_id=Feed.PandasMemory)

        DataStoreTest.save_load(name, persistable, datastore,
                                datastore.save_config, 'configs')
Ejemplo n.º 6
0
def main():
    symbols = ['XXX', 'YYY']

    inst_df = build_inst_dataframe_from_list(symbols)
    ccy_df = pd.DataFrame({
        "ccy_id": ["USD", "HKD"],
        "name": ["US Dollar", "HK Dollar"]
    })

    exchange_df = pd.DataFrame({
        "exch_id": ["NYSE"],
        "name": ["New York Stock Exchange"]
    })

    mgr = MockRefDataManager(inst_df=inst_df,
                             ccy_df=ccy_df,
                             exch_df=exchange_df)
    portfolio = Portfolio(cash=100000)

    start_date = datetime(2000, 1, 1)
    num_days = 300

    dates = [start_date + timedelta(days=i) for i in range(num_days)]

    drift = lambda x, t: 0.02 * x
    diffusion = lambda x, t: 0.3 * x

    ou_k = 2.0
    ou_theta = 0.25
    ou_eta = 0.08

    ou_drift = lambda x, t: ou_k * (ou_theta - x)
    ou_diffusion = lambda x, t: ou_eta * x

    sim_asset_paths = euler(drift, diffusion, 100.0, 1.0, num_days, 10)
    sim_spread_paths = euler(ou_drift, ou_diffusion, 0.1, 1.0, num_days, 10)
    asset_x = sim_asset_paths[1, :]
    spread = sim_spread_paths[1, :]
    asset_y = np.exp(np.log(asset_x) + spread)

    asset_x_df = pd.DataFrame({
        "dates": dates,
        "Open": asset_x,
        "High": asset_x,
        "Low": asset_x,
        "Close": asset_x,
        "Volume": 10000 * np.ones(num_days)
    })

    asset_y_df = pd.DataFrame({
        "dates": dates,
        "Open": asset_y,
        "High": asset_y,
        "Low": asset_y,
        "Close": asset_y,
        "Volume": 10000 * np.ones(num_days)
    })

    asset_x_df = asset_x_df.set_index(keys="dates")
    asset_y_df = asset_y_df.set_index(keys="dates")

    dict_df = {'XXX': asset_x_df, 'YYY': asset_y_df}

    feed = PandasMemoryDataFeed(dict_df, ref_data_mgr=mgr)
    broker = Simulator()

    config = BacktestingConfig(stg_id="pairou",
                               portfolio_id='test',
                               instrument_ids=[0, 1],
                               subscription_types=[
                                   BarSubscriptionType(bar_type=BarType.Time,
                                                       bar_size=BarSize.D1)
                               ],
                               from_date=dates[0],
                               to_date=dates[-1],
                               broker_id=Simulator.ID,
                               feed_id=PandasMemoryDataFeed.ID)

    ou_params = {"k": ou_k, "theta": ou_theta, "eta": ou_eta}

    strategy = PairTradingWithOUSpread("pairou",
                                       ou_params=ou_params,
                                       gamma=1.0,
                                       trading_config=config,
                                       ref_data_mgr=mgr)

    runner = BacktestRunner(strategy)
    runner.start()
    print portfolio.get_result()

    # pyfolio
    rets = strategy.get_portfolio().get_return()
    # import pyfolio as pf
    # pf.create_returns_tear_sheet(rets)
    # pf.create_full_tear_sheet(rets)

    # build in plot
    plotter = StrategyPlotter(strategy)
    plotter.plot(instrument=0)
Ejemplo n.º 7
0
    def test(self):
        backtest_config0 = BacktestingConfig(
            id="down2%-test-config",
            stg_id="down2%",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(1993, 1, 1),
            to_date=date(2017, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context0 = ApplicationContext(app_config=backtest_config0)
        runner = BacktestRunner(isplot=False)

        runner.start(app_context0)

        total_begin_result = runner.initial_result
        total_end_result = runner.portfolio.get_result()

        backtest_config1 = BacktestingConfig(
            id="down2%-test-config_1",
            stg_id="down2%_1",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test_1',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(1993, 1, 1),
            to_date=date(2008, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(
                seq_ds_id=DataStore.InMemoryDB,
                seq_persist_mode=PersistenceMode.Batch,
                ts_ds_id=DataStore.InMemoryDB,
                ts_persist_mode=PersistenceMode.Batch,
                trade_ds_id=DataStore.InMemoryDB,
                trade_persist_mode=PersistenceMode.Batch),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context1 = ApplicationContext(app_config=backtest_config1)
        runner1 = BacktestRunner(isplot=False)
        runner1.start(app_context1)

        part1_begin_result = runner1.initial_result
        part1_end_result = runner1.portfolio.get_result()

        backtest_config2 = BacktestingConfig(
            id="down2%-test-config_1",
            stg_id="down2%_1",
            stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
            portfolio_id='test_1',
            portfolio_initial_cash=100000,
            instrument_ids=[4],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=date(2008, 1, 1),
            to_date=date(2017, 1, 1),
            broker_id=Broker.Simulator,
            feed_id=Feed.CSV,
            stg_configs={'qty': 1000},
            ref_data_mgr_type=RefDataManager.InMemory,
            persistence_config=PersistenceConfig(
                seq_ds_id=DataStore.InMemoryDB,
                seq_persist_mode=PersistenceMode.Batch,
                ts_ds_id=DataStore.InMemoryDB,
                ts_persist_mode=PersistenceMode.Batch,
                trade_ds_id=DataStore.InMemoryDB,
                trade_persist_mode=PersistenceMode.Batch),
            provider_configs=CSVFeedConfig(path='data/tradedata'))
        app_context2 = ApplicationContext(app_config=backtest_config2)
        app_context2.start()
        db = app_context2.get_seq_data_store()

        runner2 = BacktestRunner(isplot=False)
        runner2.start(app_context2)

        part2_begin_result = runner2.initial_result
        part2_end_result = runner2.portfolio.get_result()

        self.assertEqual(total_begin_result, part1_begin_result)
        self.assertEqual(part1_end_result, part2_begin_result)
        self.assertEqual(total_end_result, part2_end_result)

        print "total begin = %s" % total_begin_result
        print "total end = %s" % total_end_result
        print "part1 begin = %s" % part1_begin_result
        print "part1 end = %s" % part1_end_result
        print "part2 begin = %s" % part2_begin_result
        print "part2 end = %s" % part2_end_result

        db.remove_database()
    def test_with_sma(self):

        sigma = 0.3
        x0 = 100
        dt = 1. / 252
        dW = np.random.normal(0, math.sqrt(dt),
                              TestCompareWithFunctionalBacktest.num_days)

        asset = []
        asset.append(x0)
        for i in xrange(1, TestCompareWithFunctionalBacktest.num_days):
            xprev = asset[-1]
            x = xprev + xprev * 0.02 * dt + sigma * xprev * dW[i]
            asset.append(x)

        instrument = 0

        lot_size = 10000

        symbols = ['SPY', 'VXX', 'XLV', 'XIV']
        dict_df = {}

        self.df = self.get_df(asset=asset)
        for symbol in symbols:
            dict_df[symbol] = self.df

        config = BacktestingConfig(
            id=None,
            stg_id='sma',
            portfolio_id='test2',
            instrument_ids=[instrument],
            subscription_types=[
                BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
            ],
            from_date=TestCompareWithFunctionalBacktest.dates[0],
            to_date=TestCompareWithFunctionalBacktest.dates[-1],
            broker_id=Broker.Simulator,
            feed_id=Feed.PandasMemory,
            stg_configs={'qty': lot_size},
            ref_data_mgr_type=None,
            persistence_config=None,
            provider_configs=PandasMemoryDataFeedConfig(dict_df=dict_df))

        self.init_context(symbols=symbols, asset=asset, app_config=config)

        close = self.app_context.inst_data_mgr.get_series("Bar.%s.Time.86400" %
                                                          instrument)
        close.start(self.app_context)

        strategy = SMAStrategy("sma", config.stg_configs)
        strategy.start(self.app_context)

        rets = strategy.get_portfolio().get_return()
        bt_result = strategy.get_portfolio().get_result()

        sma10 = talib.SMA(self.df.Close.values, 10)
        sma25 = talib.SMA(self.df.Close.values, 25)
        signal = pd.Series(1 * (sma10 > sma25), index=self.df.index)

        cash = []
        stock_value = []
        cash.append(TestCompareWithFunctionalBacktest.init_cash)
        stock_value.append(0)
        for i in xrange(1, signal.shape[0]):
            cash.append(cash[-1] - lot_size *
                        (signal[i] - signal[i - 1]) * self.df['Close'][i])
            stock_value.append(lot_size * signal[i] * self.df['Close'][i])

        target_port = pd.DataFrame({"cash": cash, "stock_value": stock_value})

        target_port[
            "total_equity"] = target_port["cash"] + target_port["stock_value"]
        target_port["return"] = target_port["total_equity"].pct_change()

        try:
            np.testing.assert_almost_equal(target_port["return"].values[1:],
                                           rets.values, 5)
        except AssertionError as e:
            self.fail(e.message)
        finally:
            strategy.stop()
Ejemplo n.º 9
0
    def test_multi_subscriptions(self, name, datastore):
        start_date = date(2011, 1, 1)
        end_date = date(2011, 1, 5)

        sub_key1 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=BarSubscriptionType(bar_type=BarType.Time,
                                                  bar_size=BarSize.D1),
            from_date=start_date,
            to_date=end_date)

        sub_key2 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=QuoteSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        sub_key3 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=TradeSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        expect_val = []

        #out of range
        persistable = Bar(timestamp=DateUtils.date_to_unixtimemillis(
            date(2010, 12, 31)),
                          type=BarType.Time,
                          size=BarSize.D1,
                          inst_id=99,
                          open=18,
                          high=19,
                          low=17,
                          close=17.5,
                          vol=100)
        datastore.save_bar(persistable)

        persistable = Bar(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 1)),
                          type=BarType.Time,
                          size=BarSize.D1,
                          inst_id=99,
                          open=28,
                          high=29,
                          low=27,
                          close=27.5,
                          vol=100)
        datastore.save_bar(persistable)
        expect_val.append(persistable)

        persistable = Trade(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 2)),
                            price=20,
                            size=200,
                            inst_id=99)
        datastore.save_trade(persistable)
        expect_val.append(persistable)

        persistable = Trade(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 3)),
                            price=30,
                            size=200,
                            inst_id=99)
        datastore.save_trade(persistable)
        expect_val.append(persistable)

        # not same instrument
        persistable = Quote(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 3)),
                            bid=18,
                            ask=19,
                            bid_size=200,
                            ask_size=500,
                            inst_id=11)
        datastore.save_quote(persistable)

        persistable = Quote(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 4)),
                            bid=18,
                            ask=19,
                            bid_size=200,
                            ask_size=500,
                            inst_id=99)
        datastore.save_quote(persistable)
        expect_val.append(persistable)

        # out of range
        persistable = Quote(timestamp=DateUtils.date_to_unixtimemillis(
            date(2011, 1, 5)),
                            bid=28,
                            ask=29,
                            bid_size=200,
                            ask_size=500,
                            inst_id=99)
        datastore.save_quote(persistable)

        actual_val = datastore.load_mktdata(sub_key1, sub_key2, sub_key3)
        self.assertEqual(expect_val, actual_val)