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
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
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()
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_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()