def setUp(self):
        self.app_context = ApplicationContext()
        # self.app_context.inst_data_mgr.clear()

        self.exec_handler = SimulatorTest.ExecHandler()
        self.app_context.order_mgr = self.exec_handler
        self.simulator = Simulator()
        self.simulator.start(app_context=self.app_context)
    def setUp(self):
        self.app_context = ApplicationContext()
        #self.app_context.inst_data_mgr.clear()

        self.exec_handler = SimulatorTest.ExecHandler()
        self.app_context.order_mgr = self.exec_handler
        self.simulator = Simulator()
        self.simulator.start(app_context=self.app_context)
Exemple #3
0
    def __init__(self):
        super(ProviderManager, self).__init__()

        self.add(Simulator())
        self.add(IBBroker())

        self.add(MongoDBDataStore())
        self.add(InMemoryDataStore())

        self.add(CSVDataFeed())
        self.add(PandasWebDataFeed())
        self.add(PandasMemoryDataFeed())
    def __init__(self):
        super(ProviderManager, self).__init__()

        self.add(Simulator())
        self.add(IBBroker())

        self.add(CassandraDataStore())
        self.add(InfluxDataStore())
        self.add(KDBDataStore())
        self.add(MongoDBDataStore())
        self.add(InMemoryDataStore())

        self.add(CSVDataFeed())
        self.add(PandasMemoryDataFeed())
        self.add(GoogleDataFeed())
        self.add(YahooDataFeed())
class SimulatorTest(TestCase):
    class ExecHandler(ExecutionEventHandler):
        def __init__(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def on_ord_upd(self, ord_upd):
            self.ord_upds.append(ord_upd)

        def on_exec_report(self, exec_report):
            self.exec_reports.append(exec_report)

        def reset(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def id(self):
            return "ExecHandler"

    def setUp(self):
        self.app_context = ApplicationContext()
        # self.app_context.inst_data_mgr.clear()

        self.exec_handler = SimulatorTest.ExecHandler()
        self.app_context.order_mgr = self.exec_handler
        self.simulator = Simulator()
        self.simulator.start(app_context=self.app_context)

    def test_on_limit_order_fill_with_new_data(self):
        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        nos = ModelFactory.build_new_order_request(timestamp=0,
                                                   cl_id='TestClient',
                                                   cl_ord_id="TestClientOrder",
                                                   portf_id="TestPortf",
                                                   broker_id="TestBroker",
                                                   inst_id="HSI@SEHK",
                                                   action=Buy,
                                                   type=Limit,
                                                   qty=1000,
                                                   limit_price=18.5)
        self.simulator.on_new_ord_req(nos)
        cl_ord_id = ModelFactory.build_cl_ord_id(nos.cl_id, nos.cl_ord_id)
        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[nos.inst_id]))
        self.assertIsNotNone(orders[nos.inst_id][cl_ord_id])
        self.assertEqual(1, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 0, 0,
                                Submitted)

        bar1 = ModelFactory.build_bar(timestamp=0,
                                      inst_id="HSI@SEHK",
                                      open=20,
                                      high=21,
                                      low=19,
                                      close=20.5,
                                      vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=1,
                                      inst_id="HSI@SEHK",
                                      open=16,
                                      high=18,
                                      low=15,
                                      close=17,
                                      vol=1000)

        self.exec_handler.reset()
        self.simulator.on_bar(bar1)
        self.assertEqual(0, len(self.exec_handler.exec_reports))

        self.exec_handler.reset()
        self.simulator.on_bar(bar2)
        self.assertEqual(1, len(self.exec_handler.exec_reports))
        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 1000,
                                18.5, Filled)

    def test_on_limit_order_immediate_fill(self):
        # bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=1,
                                      inst_id="HSI@SEHK",
                                      open=16,
                                      high=18,
                                      low=15,
                                      close=17,
                                      vol=1000)

        self.app_context.inst_data_mgr.on_bar(bar2)

        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        nos = ModelFactory.build_new_order_request(timestamp=0,
                                                   cl_id='TestClient',
                                                   cl_ord_id="TestClientOrder",
                                                   portf_id="TestPortf",
                                                   broker_id="TestBroker",
                                                   inst_id="HSI@SEHK",
                                                   action=Buy,
                                                   type=Limit,
                                                   qty=1000,
                                                   limit_price=18.5)

        self.simulator.on_new_ord_req(nos)

        cl_ord_id = ModelFactory.build_cl_ord_id(nos.cl_id, nos.cl_ord_id)
        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(0, len(orders[nos.inst_id]))

        self.assertEqual(2, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 0, 0,
                                Submitted)

        exec_report = self.exec_handler.exec_reports[1]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 1000,
                                18.5, Filled)

    def assert_exec_report(self, exec_report, cl_id, cl_ord_id, last_qty,
                           last_price, status):
        self.assertEqual(cl_id, exec_report.cl_id)
        self.assertEqual(cl_ord_id, exec_report.cl_ord_id)
        self.assertEqual(last_qty, exec_report.last_qty)
        self.assertEqual(last_price, exec_report.last_price)
        self.assertEqual(status, exec_report.status)
def oldmain():
    symbols = ['SPY', 'VXX', 'XLV', 'XIV']

    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(portf_id='test', cash=100000)

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

    dates = [start_date + timedelta(days=i) for i in range(num_days)]
    sigma = 0.3
    x0 = 100
    dt = 1. / 252

    dW = np.random.normal(0, math.sqrt(dt), num_days)

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

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

    df = df.set_index(keys="dates")

    dict_df = {'SPY': df, 'VXX': df, 'XLV': df, 'XIV': df}

    # feed = PandasMemoryDataFeed(dict_df, ref_data_mgr=mgr)
    # feed = PandasMemoryDataFeed()

    broker = Simulator()

    instruments = [3348]

    backtest_config = BacktestingConfig(
        id="sma",
        stg_id='test',
        # stg_cls='algotrader.strategy.sma_strategy.SMAStrategy',
        stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
        portfolio_id='test1',
        portfolio_initial_cash=100000,
        instrument_ids=instruments,
        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_configs={'qty': 1000},
        ref_data_mgr_type=RefDataManager.DB,
        persistence_config=backtest_mongo_persistance_config(),
        provider_configs=PandasMemoryDataFeedConfig(dict_df=dict_df))

    app_context = ApplicationContext(app_config=backtest_config)

    BacktestRunner(True).start(app_context)
def main():
    symbols = ['SPY', 'VXX', 'XLV', 'XIV']

    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(portf_id='test', cash=100000)

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

    dates = [start_date + timedelta(days=i) for i in range(num_days)]
    sigma = 0.3
    x0 = 100
    dt = 1. / 252

    dW = np.random.normal(0, math.sqrt(dt), num_days)

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

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

    df = df.set_index(keys="dates")

    dict_df = {'SPY': df, 'VXX': df, 'XLV': df, 'XIV': df}

    feed = PandasMemoryDataFeed(dict_df, ref_data_mgr=mgr)

    broker = Simulator()

    instrument = 0
    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=Simulator.ID,
                               feed_id=PandasMemoryDataFeed.ID)

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

    mgr.get_insts([instrument])
    mgr.get_inst(instrument)

    # strategy = Down2PctStrategy("down2%", portfolio,
    #                             instrument=0, qty=1000,  trading_config=config, ref_data_mgr=mgr)

    strategy = SMAStrategy("sma", stg_configs={'qty': 1})

    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)

    # import matplotlib.pyplot as plt
    # plt.show()

    import talib
    sma10 = talib.SMA(df.Close.values, 10)
    sma25 = talib.SMA(df.Close.values, 25)

    #    signal = pd.Series(1*(sma10 > sma25),index=df.index.tz_localize("UTC"))
    signal = pd.Series(1 * (sma10 > sma25), index=df.index)
    target_rets = df["Close"].pct_change() * signal.shift(1)
    target_rets.index = target_rets.index.tz_localize("UTC")
    print target_rets.values[1:] - rets.values
Exemple #8
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)
Exemple #9
0
class SimulatorTest(TestCase):
    class ExecHandler(ExecutionEventHandler):
        def __init__(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def on_ord_upd(self, ord_upd):
            self.ord_upds.append(ord_upd)

        def on_exec_report(self, exec_report):
            self.exec_reports.append(exec_report)

        def reset(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def id(self):
            return "ExecHandler"

    def setUp(self):
        self.app_context = ApplicationContext()
        #self.app_context.inst_data_mgr.clear()

        self.exec_handler = SimulatorTest.ExecHandler()
        self.app_context.order_mgr = self.exec_handler
        self.simulator = Simulator()
        self.simulator.start(app_context=self.app_context)

    def test_on_limit_order_fill_with_new_data(self):
        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        order1 = NewOrderRequest(cl_id='test',
                                 cl_ord_id=1,
                                 inst_id=1,
                                 action=OrdAction.BUY,
                                 type=OrdType.LIMIT,
                                 qty=1000,
                                 limit_price=18.5)
        self.simulator.on_new_ord_req(order1)

        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[order1.inst_id]))
        self.assertEqual(1, len(orders[order1.inst_id][order1.cl_id]))
        self.assertIsNotNone(
            orders[order1.inst_id][order1.cl_id][order1.cl_ord_id])
        self.assertEqual(1, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 0,
                                0, OrdStatus.SUBMITTED)

        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = Bar(inst_id=1, open=16, high=18, low=15, close=17, vol=1000)

        self.exec_handler.reset()
        self.simulator.on_bar(bar1)
        self.assertEqual(0, len(self.exec_handler.exec_reports))

        self.exec_handler.reset()
        self.simulator.on_bar(bar2)
        self.assertEqual(1, len(self.exec_handler.exec_reports))
        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id,
                                1000, 18.5, OrdStatus.FILLED)

    def test_on_limit_order_immediate_fill(self):
        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = Bar(inst_id=1, open=16, high=18, low=15, close=17, vol=1000)

        self.app_context.inst_data_mgr.on_bar(bar2)

        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        order1 = NewOrderRequest(cl_id='test',
                                 cl_ord_id=1,
                                 inst_id=1,
                                 action=OrdAction.BUY,
                                 type=OrdType.LIMIT,
                                 qty=1000,
                                 limit_price=18.5)
        self.simulator.on_new_ord_req(order1)

        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[order1.inst_id]))
        self.assertEqual(0, len(orders[order1.inst_id][order1.cl_id]))

        self.assertEqual(2, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 0,
                                0, OrdStatus.SUBMITTED)

        exec_report = self.exec_handler.exec_reports[1]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id,
                                1000, 18.5, OrdStatus.FILLED)

    def assert_exec_report(self, exec_report, cl_id, cl_ord_id, last_qty,
                           last_price, status):
        self.assertEqual(cl_id, exec_report.cl_id)
        self.assertEqual(cl_ord_id, exec_report.cl_ord_id)
        self.assertEqual(last_qty, exec_report.last_qty)
        self.assertEqual(last_price, exec_report.last_price)
        self.assertEqual(status, exec_report.status)
class SimulatorTest(TestCase):
    class ExecHandler(ExecutionEventHandler):
        def __init__(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def on_ord_upd(self, ord_upd):
            self.ord_upds.append(ord_upd)

        def on_exec_report(self, exec_report):
            self.exec_reports.append(exec_report)

        def reset(self):
            self.ord_upds = list()
            self.exec_reports = list()

        def id(self):
            return "ExecHandler"

    def setUp(self):
        self.app_context = ApplicationContext()
        #self.app_context.inst_data_mgr.clear()

        self.exec_handler = SimulatorTest.ExecHandler()
        self.app_context.order_mgr = self.exec_handler
        self.simulator = Simulator()
        self.simulator.start(app_context=self.app_context)

    def test_on_limit_order_fill_with_new_data(self):
        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        order1 = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT,
                                 qty=1000, limit_price=18.5)
        self.simulator.on_new_ord_req(order1)

        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[order1.inst_id]))
        self.assertEqual(1, len(orders[order1.inst_id][order1.cl_id]))
        self.assertIsNotNone(orders[order1.inst_id][order1.cl_id][order1.cl_ord_id])
        self.assertEqual(1, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 0, 0, OrdStatus.SUBMITTED)

        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = Bar(inst_id=1, open=16, high=18, low=15, close=17, vol=1000)

        self.exec_handler.reset()
        self.simulator.on_bar(bar1)
        self.assertEqual(0, len(self.exec_handler.exec_reports))

        self.exec_handler.reset()
        self.simulator.on_bar(bar2)
        self.assertEqual(1, len(self.exec_handler.exec_reports))
        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 1000, 18.5, OrdStatus.FILLED)

    def test_on_limit_order_immediate_fill(self):
        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = Bar(inst_id=1, open=16, high=18, low=15, close=17, vol=1000)

        self.app_context.inst_data_mgr.on_bar(bar2)

        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        order1 = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT,
                                 qty=1000, limit_price=18.5)
        self.simulator.on_new_ord_req(order1)

        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[order1.inst_id]))
        self.assertEqual(0, len(orders[order1.inst_id][order1.cl_id]))

        self.assertEqual(2, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 0, 0, OrdStatus.SUBMITTED)

        exec_report = self.exec_handler.exec_reports[1]
        self.assert_exec_report(exec_report, order1.cl_id, order1.cl_ord_id, 1000, 18.5, OrdStatus.FILLED)

    def assert_exec_report(self, exec_report, cl_id, cl_ord_id, last_qty, last_price, status):
        self.assertEqual(cl_id, exec_report.cl_id)
        self.assertEqual(cl_ord_id, exec_report.cl_ord_id)
        self.assertEqual(last_qty, exec_report.last_qty)
        self.assertEqual(last_price, exec_report.last_price)
        self.assertEqual(status, exec_report.status)