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)
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)
def main(): config = Config(load_from_yaml("../../config/data_import.yaml"), {"Application": { "feedId": "Yahoo" }}) app_context = ApplicationContext(config=config) MktDataImporter().start(app_context)
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))
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__(self, config: Config = None): super(ApplicationContext, self).__init__() self.config = config if config else Config() self.clock = self.add_startable(self.__get_clock()) self.provider_mgr = self.add_startable(ProviderManager()) self.seq_mgr = self.add_startable(SequenceManager()) self.inst_data_mgr = self.add_startable(InstrumentDataManager()) self.ref_data_mgr = self.add_startable(RefDataManager()) self.order_mgr = self.add_startable(OrderManager()) self.acct_mgr = self.add_startable(AccountManager()) self.portf_mgr = self.add_startable(PortfolioManager()) self.stg_mgr = self.add_startable(StrategyManager()) self.event_bus = EventBus() self.model_factory = ModelFactory
"plot": False }, "DataStore": {"InMemory": { "file": "../data/algotrader_backtest_db.p", "instCSV": "../data/refdata/instrument.csv", "ccyCSV": "../data/refdata/ccy.csv", "exchCSV": "../data/refdata/exch.csv" } }, "Feed": {"CSV": {"path": "/mnt/data/dev/workspaces/python-trading/data/tradedata"} } } config = Config(test_override) empty_config = Config({ "Application": { "dataStoreId": "InMemory", "createDBAtStart": True, "deleteDBAtStop": False }, "DataStore": {"InMemory": { "file": "../data/algotrader_backtest_db.p", } }, "Feed": {"CSV": {"path": "/mnt/data/dev/workspaces/python-trading/data/tradedata"} }
# from algotrader.utils import logger # 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 config = Config(load_from_yaml("../config/data_import.yaml")) app_context = ApplicationContext(config=config) def init_ib(app_context): app_context.start() broker = app_context.provider_mgr.get(Broker.IB) broker.start(app_context) return broker def import_inst_from_ib(broker, symbol, sec_type='STK',
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 range(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 = Config({ "Application": { "type": "BackTesting", "clockId": "Simulation", "dataStoreId": "InMemory", "persistenceMode": "Disable", "createDBAtStart": True, "deleteDBAtStop": False, "feedId": "PandasMemory", "brokerId": "Simulator", "portfolioId": "test2", "stg": "down2%", "stgCls": "algotrader.strategy.down_2pct_strategy.Down2PctStrategy", "instrumentIds": ["0"], "subscriptionTypes": ["Bar.Yahoo.Time.D1"], "fromDate": TestCompareWithFunctionalBacktest.dates[0], "toDate": TestCompareWithFunctionalBacktest.dates[-1], "plot": False }, "DataStore": {"InMemory": { "file": "../data/algotrader_db.p", "instCSV": "../data/refdata/instrument.csv", "ccyCSV": "../data/refdata/ccy.csv", "exchCSV": "../data/refdata/exch.csv" } }, "Strategy": { "down2%": { "qty": lot_size } } }) self.init_context(symbols=symbols, asset=asset, config=config) feed = self.app_context.get_feed() feed.set_data_frame(dict_df) close = self.app_context.inst_data_mgr.get_series("Bar.%s.Time.86400" % instrument) close.start(self.app_context) strategy = SMAStrategy("sma") 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 range(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()
def test_multiple(self): config = Config( load_from_yaml("../config/backtest.yaml"), load_from_yaml("../config/down2%.yaml")) self.assertEquals(1, config.get_strategy_config("down2%", "qty"))