Пример #1
0
    def setUp(self):

        self.app_context = ApplicationContext(config=empty_config)
        self.app_context.start()

        self.db = InMemoryDataStore()
        self.db.start(self.app_context)
Пример #2
0
    def init(self):
        logger.info("starting ATS")

        self.app_config = self.app_config
        self.app_context = ApplicationContext(app_config=self.app_config)
        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.get_or_new_portfolio(
            self.app_config.portfolio_id,
            self.app_config.portfolio_initial_cash)
        self.app_context.add_startable(self.portfolio)

        self.strategy = self.app_context.stg_mgr.get_or_new_stg(
            self.app_config)
        self.app_context.add_startable(self.strategy)
Пример #3
0
def main():
    backtest_config = 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=[1],
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
        ],
        from_date=date(2010, 1, 1),
        to_date=date.today(),
        broker_id=Broker.Simulator,
        feed_id=Feed.CSV,
        stg_configs={'qty': 1000},
        ref_data_mgr_type=RefDataManager.DB,
        persistence_config=backtest_mongo_persistance_config(),
        provider_configs=[
            MongoDBConfig(),
            CSVFeedConfig(path='../../data/tradedata')
        ])
    app_context = ApplicationContext(app_config=backtest_config)

    BacktestRunner(True).start(app_context)
Пример #4
0
def main():
    config = Config(load_from_yaml("../../config/live_ib.yaml"),
                    load_from_yaml("../../config/down2%.yaml"))

    app_context = ApplicationContext(config=config)

    ATSRunner().start(app_context)
Пример #5
0
    def new_app_context(self):
        name = "test"
        create_at_start = True
        delete_at_stop = False

        app_config = ApplicationConfig("app", None, Clock.Simulation, PersistenceConfig(
            ref_ds_id=DataStore.InMemoryDB, ref_persist_mode=PersistenceMode.RealTime,
            trade_ds_id=DataStore.InMemoryDB, trade_persist_mode=PersistenceMode.RealTime,
            ts_ds_id=DataStore.InMemoryDB, ts_persist_mode=PersistenceMode.RealTime,
            seq_ds_id=DataStore.InMemoryDB, seq_persist_mode=PersistenceMode.RealTime),
                                       InMemoryStoreConfig(file="%s_db.p" % name,
                                                           create_at_start=create_at_start,
                                                           delete_at_stop=delete_at_stop))
        app_context = ApplicationContext(app_config=app_config)
        app_context.start()
        return app_context
Пример #6
0
def main():
    config = Config(load_from_yaml("../../config/data_import.yaml"),
                    {"Application": {
                        "feedId": "Yahoo"
                    }})
    app_context = ApplicationContext(config=config)
    MktDataImporter().start(app_context)
Пример #7
0
def main():
    config = Config(load_from_yaml("../../config/backtest.yaml"),
                    load_from_yaml("../../config/down2%.yaml"))

    app_context = ApplicationContext(config=config)

    BacktestRunner().start(app_context)
Пример #8
0
 def create_app_context(self, override):
     return ApplicationContext(config=Config(
         load_from_yaml("../config/backtest.yaml"),
         load_from_yaml("../config/down2%.yaml"),
         test_override,
         StrategyPersistenceTest.stg_override,
         override))
Пример #9
0
    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)
Пример #10
0
def get_default_app_context():
    config = MongoDBConfig()
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.Batch, DataStore.Mongo,
        PersistenceMode.Batch, DataStore.Mongo, PersistenceMode.Batch,
        DataStore.Mongo, PersistenceMode.Batch)
    app_config = ApplicationConfig(None, None, None, persistence_config,
                                   config)
    return ApplicationContext(app_config=app_config)
 def create_app_context(self, conf):
     return ApplicationContext(config=Config(
         load_from_yaml("../config/backtest.yaml"),
         load_from_yaml("../config/down2%.yaml"), test_override, {
             "Application": {
                 "ceateAtStart": True,
                 "deleteDBAtStop": False,
                 "persistenceMode": "RealTime"
             }
         }, conf))
    def init_context(self, symbols, asset, config):

        self.app_context = ApplicationContext(config=config)

        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"]})

        self.app_context.start()

        datastore = self.app_context.get_data_store()
        load_exch_from_df(datastore, exchange_df)
        load_ccy_from_df(datastore, ccy_df)
        load_inst_from_df(datastore, inst_df)

        self.portfolio = self.app_context.portf_mgr.new_portfolio(portf_id='test2',
                                                                  initial_cash=TestCompareWithFunctionalBacktest.init_cash)
        self.portfolio.start(self.app_context)
Пример #13
0
def app_context():
    persistence_config = PersistenceConfig(None,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime,
                                           DataStore.Mongo, PersistenceMode.RealTime)
    app_config = ApplicationConfig(id=None, ref_data_mgr_type=RefDataManager.DB, clock_type=Clock.RealTime,
                                   persistence_config=persistence_config,
                                   provider_configs=[MongoDBConfig(), IBConfig(client_id=2, use_gevent=True)])
    app_context = ApplicationContext(app_config=app_config)

    return app_context
Пример #14
0
    def test_loaded_bar(self, feed_id, subscription_types):
        app_context = ApplicationContext(config=config)
        app_context.start()

        feed = app_context.provider_mgr.get(feed_id)
        feed.start(app_context)

        # logger.setLevel(logging.DEBUG)
        eventLogger = EventLogger()
        eventLogger.start(app_context)

        instruments = app_context.ref_data_mgr.get_insts_by_ids(
            ["SPY@NYSEARCA"])
        for sub_req in build_subscription_requests(feed_id, instruments,
                                                   subscription_types,
                                                   20100101, 20170101):
            feed.subscribe_mktdata(sub_req)

        self.assertTrue(eventLogger.count[Bar] > 0)
        self.assertTrue(eventLogger.count[Trade] == 0)
        self.assertTrue(eventLogger.count[Quote] == 0)
Пример #15
0
def get_default_app_context():
    config = MongoDBConfig()
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.RealTime, DataStore.Mongo,
        PersistenceMode.RealTime, DataStore.Mongo, PersistenceMode.RealTime,
        DataStore.Mongo, PersistenceMode.RealTime)

    app_config = ApplicationConfig("InstrumentImport", RefDataManager.DB,
                                   Clock.Simulation, persistence_config,
                                   config)

    return ApplicationContext(app_config=app_config)
Пример #16
0
    def init(self):
        logger.info("starting ATS")

        self.app_config = self.app_config
        self.app_context = ApplicationContext(app_config=self.app_config)
        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.get_or_new_portfolio(self.app_config.portfolio_id,
                                                                         self.app_config.portfolio_initial_cash)
        self.app_context.add_startable(self.portfolio)

        self.strategy = self.app_context.stg_mgr.get_or_new_stg(self.app_config)
        self.app_context.add_startable(self.strategy)
    def init_context(self, symbols, asset, app_config):

        self.app_context = ApplicationContext(app_config=app_config)

        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"]
        })
        ref_data_mgr = MockRefDataManager(inst_df=inst_df,
                                          ccy_df=ccy_df,
                                          exch_df=exchange_df)
        self.app_context.ref_data_mgr = ref_data_mgr

        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.new_portfolio(
            portf_id='test2', cash=TestCompareWithFunctionalBacktest.init_cash)
        self.portfolio.start(self.app_context)
Пример #18
0
class InMemoryDBTest(TestCase):
    def setUp(self):

        self.app_context = ApplicationContext(config=empty_config)
        self.app_context.start()

        self.db = InMemoryDataStore()
        self.db.start(self.app_context)

    def tearDown(self):
        self.db.remove_database()

    def test_save_and_load(self):
        inputs = []
        for x in range(0, 10):
            data = sorted([random.randint(0, 100) for i in range(0, 4)])
            bar = ModelFactory.build_bar(timestamp=x,
                                         inst_id="3",
                                         open=data[1],
                                         high=data[3],
                                         low=data[0],
                                         close=data[2],
                                         vol=random.randint(100, 1000))
            inputs.append(bar)
            self.db.save_bar(bar)

        self.db.stop()

        self.db = InMemoryDataStore()
        self.db.start(self.app_context)

        bars = self.db.load_all('bars')
        bars = sorted(bars, key=lambda x: x.timestamp, reverse=False)
        self.assertEquals(10, len(bars))

        for x in range(0, 10):
            self.assertEquals(inputs[x], bars[x])
Пример #19
0
    def test_indicator(self, name, serializer):
        self.app_context = ApplicationContext()

        bar = self.app_context.inst_data_mgr.get_series("bar")
        sma = SMA(bar.name, 'close', 1, missing_value=0)
        t1 = datetime.datetime.now()
        t2 = t1 + datetime.timedelta(0, 3)
        t3 = t2 + datetime.timedelta(0, 3)

        bar.add({"timestamp": t1, "close": 2.0, "open": 0})
        bar.add({"timestamp": t2, "close": 2.4, "open": 1.4})

        bar.add({"timestamp": t3, "close": 2.8, "open": 1.8})

        SerializerTest.ser_deser(name, serializer, sma)
Пример #20
0
    def test_indicator(self, name, datastore):
        self.app_context = ApplicationContext()

        bar = self.app_context.inst_data_mgr.get_series("bar")
        sma = SMA(bar.name, 'close', 1, missing_value=0)
        t1 = datetime.now()
        t2 = t1 + timedelta(0, 3)
        t3 = t2 + timedelta(0, 3)

        bar.add({"timestamp": t1, "close": 2.0, "open": 0})
        bar.add({"timestamp": t2, "close": 2.4, "open": 1.4})

        bar.add({"timestamp": t3, "close": 2.8, "open": 1.8})

        DataStoreTest.save_load(name, sma, datastore,
                                datastore.save_time_series, 'time_series')
Пример #21
0
def main():
    # logger.setLevel(logging.DEBUG)
    symbols = ["BAC", "F", "FCX", "TWTR", "VALE", "PFE", "NOK", "ABBV", "PBR", "MRK", "KMI", "MRO", "KEY", "AMX", "COP", "C", "CVX", "BSX", "RF",  "CVS"]


    nyse_data = {}
    with pd.HDFStore('/Users/jchan/workspace/data/temp/NYSE.h5') as store:
        for s in store.keys():
            sym = re.sub('/', '', s)
            df = store[s]
            df['DateTime'] = pd.to_datetime(df['Date'] + 'T' + df['Time'])
            df = df.set_index('DateTime')
            nyse_data[sym] = df


    # backtest_config = 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=[3348],
    #                                     subscription_types=[
    #                                         BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
    #                                     from_date=date(2010, 1, 1), to_date=date.today(),
    #                                     broker_id=Broker.Simulator,
    #                                     feed_id=Feed.CSV,
    #                                     stg_configs={'qty': 1000},
    #                                     ref_data_mgr_type=RefDataManager.DB,
    #                                     persistence_config= backtest_mongo_persistance_config(),
    #                                     provider_configs=[MongoDBConfig(), CSVFeedConfig(path='../../data/tradedata')])

    backtest_config = BacktestingConfig(id="test_alpha", stg_id="alpha2",
                                        stg_cls='algotrader.strategy.alpha_formula.AlphaFormula3',
                                        portfolio_id='test', portfolio_initial_cash=100000,
                                        instrument_ids=[1248, 450, 1225],
                                        subscription_types=[
                                            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.M5)],
                                        from_date=date(2016, 9, 1), to_date=date.today(),
                                        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=[MongoDBConfig, PandasMemoryDataFeedConfig(dict_df=nyse_data)])


    app_context = ApplicationContext(app_config=backtest_config)
    BacktestRunner(True).start(app_context)
    def init_context(self, symbols, asset, app_config):

        self.app_context = ApplicationContext(app_config=app_config)

        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"]})
        ref_data_mgr = MockRefDataManager(inst_df=inst_df, ccy_df=ccy_df, exch_df=exchange_df)
        self.app_context.ref_data_mgr = ref_data_mgr

        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.new_portfolio(portf_id='test2',
                                                                  cash=TestCompareWithFunctionalBacktest.init_cash)
        self.portfolio.start(self.app_context)
Пример #23
0
    def setUp(self):

        persistence_config = PersistenceConfig(
            None, DataStore.InMemoryDB, PersistenceMode.Batch,
            DataStore.InMemoryDB, PersistenceMode.Batch, DataStore.InMemoryDB,
            PersistenceMode.Batch, DataStore.InMemoryDB, PersistenceMode.Batch)

        name = "test"
        create_at_start = True
        delete_at_stop = False

        app_config = ApplicationConfig(
            None, None, Clock.RealTime, persistence_config,
            InMemoryStoreConfig(file="%s_db.p" % name,
                                create_at_start=create_at_start,
                                delete_at_stop=delete_at_stop))
        self.context = ApplicationContext(app_config=app_config)

        self.db = InMemoryDataStore()
        self.db.start(self.context)
Пример #24
0
def main():
    persistence_config = PersistenceConfig(
        None, DataStore.Mongo, PersistenceMode.RealTime, DataStore.Mongo,
        PersistenceMode.RealTime, DataStore.Mongo, PersistenceMode.RealTime,
        DataStore.Mongo, PersistenceMode.RealTime)
    app_config = RealtimeMarketDataImporterConfig(
        None,
        feed_id=Broker.IB,
        instrument_ids=[3],
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
        ],
        ref_data_mgr_type=RefDataManager.InMemory,
        clock_type=Clock.RealTime,
        persistence_config=persistence_config,
        provider_configs=[MongoDBConfig(),
                          IBConfig(client_id=2)])
    app_context = ApplicationContext(app_config=app_config)

    MktDataImporter().start(app_context)
Пример #25
0
def main():
    broker_config = IBConfig(client_id=2)
    live_trading_config = LiveTradingConfig(
        id=None,
        stg_id="down2%",
        stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
        portfolio_id='test',
        instrument_ids=[4],
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.M1)
        ],
        feed_id=Broker.IB,
        broker_id=Broker.IB,
        ref_data_mgr_type=RefDataManager.DB,
        clock_type=Clock.RealTime,
        persistence_config=PersistenceConfig(),
        configs=[broker_config])

    app_context = ApplicationContext(app_config=live_trading_config)
    ATSRunner().start(app_context)
Пример #26
0
def main():
    file = '../data/tradedata/SPY.csv'
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d')
    df = pd.read_csv(file,
                     index_col='Date',
                     parse_dates=['Date'],
                     date_parser=dateparse)
    df['Symbol'] = "SPY"
    df['BarSize'] = int(BarSize.D1)
    df = df.sort_index(ascending=True)

    dict_df = {"SPY": df}

    instruments = [3348]

    backtest_config = BacktestingConfig(
        id="sma",
        stg_id='test_sma1',
        # stg_cls='algotrader.strategy.sma_strategy.SMAStrategy',
        stg_cls='algotrader.strategy.down_2pct_strategy.Down2PctStrategy',
        portfolio_id='test2',
        portfolio_initial_cash=100000,
        instrument_ids=instruments,
        subscription_types=[
            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)
        ],
        from_date=date(2010, 1, 1),
        to_date=date.today(),
        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)
Пример #27
0
def test_vix():
    logger.setLevel(logging.DEBUG)
    symbols = ["VXF2015", "VXG2015", "VXH2015", "VXJ2015"]
    vix_data = {}

    with pd.HDFStore('/Users/jchan/workspace/data/temp/VX.h5') as store:
        for s in store.keys():
            sym = re.sub('/', '', s)
            df = store[s]
            df = df.reset_index()
            df['DateTime'] = df['Trade Date']
            df['Volume'] = df['Total Volume']
            df = df.set_index('DateTime')
            vix_data[sym] = df


    backtest_config = BacktestingConfig(id="test_vix", stg_id="vix6",
                                        stg_cls= 'algotrader.strategy.vix_future.VIXFuture',
                                        portfolio_id='test', portfolio_initial_cash=100000,
                                        instrument_ids=[125, 126, 127, 128, 129],
                                        subscription_types=[
                                            BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
                                        from_date=date(2014, 11, 1), to_date=date(2015,4,1),
                                        broker_id=Broker.Simulator,
                                        feed_id=Feed.PandasMemory,
                                        #stg_configs={'qty': 10, 'contracts': [125, 126, 127, 128]},
                                        stg_configs={'qty': 10,
                                                     'exp_date_lb' : 10,
                                                     'exp_date_ub' : 180,
                                                     'short_entry_threshold' : 0.02,
                                                     'short_exit_threshold' : -0.01 },
                                        ref_data_mgr_type=RefDataManager.DB,
                                        persistence_config= backtest_mongo_persistance_config(),
                                        provider_configs=[MongoDBConfig, PandasMemoryDataFeedConfig(dict_df=vix_data)])


    app_context = ApplicationContext(app_config=backtest_config)
    BacktestRunner(True).start(app_context)
Пример #28
0
class ATSRunner(Application):
    def init(self):
        logger.info("starting ATS")

        self.app_config = self.app_config
        self.app_context = ApplicationContext(app_config=self.app_config)
        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.get_or_new_portfolio(self.app_config.portfolio_id,
                                                                         self.app_config.portfolio_initial_cash)
        self.app_context.add_startable(self.portfolio)

        self.strategy = self.app_context.stg_mgr.get_or_new_stg(self.app_config)
        self.app_context.add_startable(self.strategy)

    def run(self):
        self.strategy.start(self.app_context)

        logger.info("ATS started, presss Ctrl-C to stop")
Пример #29
0
class ATSRunner(Application):
    def init(self):
        logger.info("starting ATS")

        self.app_config = self.app_config
        self.app_context = ApplicationContext(app_config=self.app_config)
        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.get_or_new_portfolio(
            self.app_config.portfolio_id,
            self.app_config.portfolio_initial_cash)
        self.app_context.add_startable(self.portfolio)

        self.strategy = self.app_context.stg_mgr.get_or_new_stg(
            self.app_config)
        self.app_context.add_startable(self.strategy)

    def run(self):
        self.strategy.start(self.app_context)

        logger.info("ATS started, presss Ctrl-C to stop")
Пример #30
0
 def setUp(self):
     self.app_context = ApplicationContext()
     self.app_context.start()
     self.portfolio = self.app_context.portf_mgr.new_portfolio(portf_id="test", cash=100000)
     self.portfolio.start(self.app_context)
Пример #31
0
 def setUp(self):
     self.app_context = ApplicationContext()
     self.utils = DataSeriesUtils(self.app_context)
Пример #32
0
    broker.on_ord_cancel_req(order)
    time.sleep(5)


if __name__ == "__main__":

    logger.setLevel(logging.DEBUG)

    persistence_config = PersistenceConfig(
        None, DataStore.InMemoryDB, PersistenceMode.RealTime,
        DataStore.InMemoryDB, PersistenceMode.RealTime, DataStore.InMemoryDB,
        PersistenceMode.RealTime, DataStore.InMemoryDB,
        PersistenceMode.RealTime)
    app_config = RealtimeMarketDataImporterConfig(
        None, RefDataManager.InMemory, Clock.RealTime, Broker.IB, [3],
        [BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
        persistence_config, MongoDBConfig(),
        IBConfig(client_id=2, use_gevent=True))
    app_context = ApplicationContext(app_config=app_config)

    app_context.start()
    broker = app_context.provider_mgr.get(Broker.IB)
    broker.start(app_context)

    # test_sub_hist_bar(broker)
    test_sub_realtime_bar(broker)

    time.sleep(1000)

    # test_sub_realtime_trade(broker)
    # test_sub_realtime_quote(broker)
Пример #33
0
 def setUp(self):
     self.app_context = ApplicationContext()
Пример #34
0
    None,
    Clock.RealTime,
    persistence_config,
    provider_configs=[
        MongoDBConfig(dbname=name,
                      create_at_start=create_at_start,
                      delete_at_stop=mongo_delete_at_stop),
        CassandraConfig(contact_points=['127.0.0.1'],
                        keyspace=name,
                        create_at_start=create_at_start,
                        delete_at_stop=cass_delete_at_stop),
        InMemoryStoreConfig(file="%s_db.p" % name,
                            create_at_start=create_at_start,
                            delete_at_stop=im_memory_delete_at_stop)
    ])
context = ApplicationContext(app_config=app_config)
clock = context.clock
mongo = context.provider_mgr.get(DataStore.Mongo)
cassandra = context.provider_mgr.get(DataStore.Cassandra)
inmemory = context.provider_mgr.get(DataStore.InMemory)

params = [
    param('Mongo', mongo),
    #param('Cassandra', cassandra),
    param('InMemory', inmemory)
]


class DataStoreTest(TestCase):
    @classmethod
    def setUpClass(cls):
class TestCompareWithFunctionalBacktest(TestCase):
    start_date = datetime(2000, 1, 1)
    num_days = 3000
    dates = [start_date + timedelta(days=i) for i in range(num_days)]
    init_cash = 1000000

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

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

        return df

    def init_context(self, symbols, asset, app_config):

        self.app_context = ApplicationContext(app_config=app_config)

        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"]})
        ref_data_mgr = MockRefDataManager(inst_df=inst_df, ccy_df=ccy_df, exch_df=exchange_df)
        self.app_context.ref_data_mgr = ref_data_mgr

        self.app_context.start()

        self.portfolio = self.app_context.portf_mgr.new_portfolio(portf_id='test2',
                                                                  cash=TestCompareWithFunctionalBacktest.init_cash)
        self.portfolio.start(self.app_context)

    def tearDown(self):
        self.app_context.stop()

    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()
Пример #36
0
    broker.on_ord_cancel_req(order)
    time.sleep(5)


if __name__ == "__main__":

    logger.setLevel(logging.DEBUG)

    persistence_config = PersistenceConfig(None,
                                           DataStore.InMemoryDB, PersistenceMode.RealTime,
                                           DataStore.InMemoryDB, PersistenceMode.RealTime,
                                           DataStore.InMemoryDB, PersistenceMode.RealTime,
                                           DataStore.InMemoryDB, PersistenceMode.RealTime)
    app_config = RealtimeMarketDataImporterConfig(None, RefDataManager.InMemory, Clock.RealTime,
                                              Broker.IB, [3],
                                              [BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1)],
                                              persistence_config,
                                              MongoDBConfig(), IBConfig(client_id=2, use_gevent=True))
    app_context = ApplicationContext(app_config=app_config)

    app_context.start()
    broker = app_context.provider_mgr.get(Broker.IB)
    broker.start(app_context)

    # test_sub_hist_bar(broker)
    test_sub_realtime_bar(broker)

    time.sleep(1000)

    # test_sub_realtime_trade(broker)
    # test_sub_realtime_quote(broker)
    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()
Пример #38
0
class PortfolioTest(TestCase):
    def setUp(self):
        self.app_context = ApplicationContext()
        self.app_context.start()
        self.portfolio = self.app_context.portf_mgr.new_portfolio(portf_id="test", cash=100000)
        self.portfolio.start(self.app_context)

    def tearDown(self):
        self.app_context.stop()

    def test_portfolio(self):
        self.assertEqual(Portfolio(cash=100000).cash, 100000)

    def test_position(self):

        ord_req1 = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1,
                                   action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)
        ord_req2 = NewOrderRequest(cl_id='test', cl_ord_id=2, portf_id="test", broker_id="Dummy", inst_id=1,
                                   action=OrdAction.BUY, type=OrdType.LIMIT, qty=1800, limit_price=18.2)

        self.assertEqual(0, len(self.portfolio.positions))
        self.assertEqual(100000, self.portfolio.cash)
        self.assertEqual(0, (self.portfolio.performance_series.now("total_equity")))

        order1 = self.portfolio.send_order(ord_req1)
        self.check_order(self.portfolio, [order1], {1: (1000, 0)})
        self.assertEqual(100000, self.portfolio.cash)
        self.assertEqual(0, (self.portfolio.performance_series.now("total_equity")))

        order2 = self.portfolio.send_order(ord_req2)
        self.check_order(self.portfolio, [order1, order2], {1: (2800, 0)})
        self.assertEqual(100000, self.portfolio.cash)
        self.assertEqual(0, (self.portfolio.performance_series.now("total_equity")))

    def test_on_exec_report(self):

        ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1,
                                  action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)
        order1 = self.portfolio.send_order(ord_req)

        er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED)

        self.app_context.order_mgr.on_exec_report(er1)

        self.assertEqual(500, order1.last_qty)
        self.assertEqual(18.4, order1.last_price)
        self.assertEqual(500, order1.filled_qty)
        self.assertEqual(18.4, order1.avg_price)
        self.assertEqual(OrdStatus.PARTIALLY_FILLED, order1.status)

        self.check_order(self.portfolio, [order1], {1: (1000, 500)})

        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 18.4
        expected_total_equity = expected_cash + expected_stock_value

        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.now('stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.now('total_equity'))

        er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=500, last_price=18.2,
                              status=OrdStatus.FILLED)
        self.app_context.order_mgr.on_exec_report(er2)
        self.assertEqual(500, order1.last_qty)
        self.assertEqual(18.2, order1.last_price)
        self.assertEqual(1000, order1.filled_qty)
        self.assertEqual(18.3, order1.avg_price)
        self.assertEqual(OrdStatus.FILLED, order1.status)

        self.check_order(self.portfolio, [order1], {1: (1000, 1000)})

        expected_cash = 100000 - 500 * 18.4 - 500 * 18.2
        expected_stock_value = 1000 * 18.2
        expected_total_equity = expected_cash + expected_stock_value

        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.now('stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.now('total_equity'))

    def test_on_market_date_update(self):

        ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1,
                                  action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5,
                                  timestamp=0)
        order1 = self.portfolio.on_new_ord_req(ord_req)

        er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED, timestamp=1)
        self.app_context.order_mgr.on_exec_report(er1)

        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 18.4
        expected_total_equity = expected_cash + expected_stock_value

        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(0, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(0, 'total_equity'))

        self.portfolio.on_trade(Trade(inst_id=1, price=20, size=1000, timestamp=2))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 20
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(1, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(1, 'total_equity'))

        self.portfolio.on_bar(Bar(inst_id=1, close=16, adj_close=16, vol=1000, timestamp=3))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 16
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(2, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(2, 'total_equity'))

        self.portfolio.on_quote(Quote(inst_id=1, bid=16, ask=18, timestamp=4))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 17
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(3, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(3, 'total_equity'))

    def check_order(self, portfolio, orders, qtys):
        expected_positon = defaultdict(list)
        for order in orders:
            expected_positon[order.inst_id].append(order)

        self.assertEqual(len(expected_positon), len(portfolio.positions))

        all_orders = portfolio.all_orders()
        self.assertEqual(len(orders), len(all_orders))

        for order in orders:
            self.assertTrue(order in all_orders)

        for inst, pos_orders in expected_positon.iteritems():
            position = portfolio.positions[str(inst)]

            all_position_orders = position.all_orders()
            self.assertEqual(len(pos_orders), len(all_position_orders))

            (ord_qty, fill_qty) = qtys[inst]

            for pos_order in pos_orders:
                self.assertEquals(pos_order, position.orders[pos_order.cl_id][pos_order.cl_ord_id])

            self.assertEqual(ord_qty, position.ordered_qty())
            self.assertEqual(fill_qty, position.filled_qty())

    def test_no_position(self):
        self.assertEquals(False, self.portfolio.has_position(cl_id=1, inst_id=1))
        self.assertEquals(False, self.portfolio.has_position(cl_id=2, inst_id=1))
        self.assertEquals(False, self.portfolio.has_position(cl_id=2, inst_id=2))

    def test_position(self):
        ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1,
                                  action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)
        order1 = self.portfolio.send_order(ord_req)

        er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED)

        self.app_context.order_mgr.on_exec_report(er1)

        self.assertEqual(500, order1.last_qty)
        self.assertEqual(18.4, order1.last_price)
        self.assertEqual(500, order1.filled_qty)
        self.assertEqual(18.4, order1.avg_price)
        self.assertEqual(OrdStatus.PARTIALLY_FILLED, order1.status)

        self.assertEquals(True, self.portfolio.has_position(cl_id='test', inst_id=1))
        self.assertEquals(False, self.portfolio.has_position(cl_id='test', inst_id=2))
        self.assertEquals(False, self.portfolio.has_position(cl_id='dummy', inst_id=2))