def backtesting_engine(symbol, strategy, fromdate, todate, args=None): """ Primary function for backtesting, not entirely parameterized """ # Backtesting Engine cerebro = bt.Cerebro() # Add a Strategy if no Data Required for the model if args is None: cerebro.addstrategy(strategy) # If the Strategy requires a Model and therefore data elif args is not None: cerebro.addstrategy(strategy, args) # Retrieve Data from Alpaca data = bt.feeds.YahooFinanceData( dataname=symbol, fromdate=fromdate, # datetime.date(2015, 1, 1) todate=todate, # datetime.datetime(2016, 1, 1) reverse=False) # Add Data to Backtesting Engine cerebro.adddata(data) # Set Initial Portfolio Value cerebro.broker.setcash(100000.0) # Add Analysis Tools cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe') cerebro.addanalyzer(bt.analyzers.Returns, _name='returns') cerebro.addanalyzer(bt.analyzers.SQN, _name='sqn') cerebro.addanalyzer(bt.analyzers.DrawDown, _name='drawdown') cerebro.addanalyzer(bt.analyzers.PositionsValue, _name='posval') cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') # Starting Portfolio Value print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run the Backtesting Engine backtest = cerebro.run() # Print Analysis and Final Portfolio Value print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) print('Return: ', backtest[0].analyzers.returns.get_analysis()) print('Sharpe Ratio: ', backtest[0].analyzers.sharpe.get_analysis()) print('System Quality Number: ', backtest[0].analyzers.sqn.get_analysis()) print('Drawdown: ', backtest[0].analyzers.drawdown.get_analysis()) print('Active Position Value: ', backtest[0].analyzers.posval.get_analysis()) print('Pyfolio: ', backtest[0].analyzers.pyfolio.get_analysis()) # Print Analysis and Final Portfolio Value pyfoliozer = backtest[0].analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() # See if we can add regular FB data to compare against returns of algo pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)
def bt_anz_folio(qx): # 分析BT量化回测数据 # 专业pyFolio量化分析图表 # print('\n-----------pyFolio----------') strat = qx.bt_results[0] anzs = strat.analyzers # xpyf = anzs.getbyname('pyfolio') xret, xpos, xtran, gross_lev = xpyf.get_pf_items() # #xret.to_csv('tmp/x_ret.csv',index=True,header=None,encoding='utf8') #xpos.to_csv('tmp/x_pos.csv',index=True,encoding='utf8') #xtran.to_csv('tmp/x_tran.csv',index=True,encoding='utf8') # xret, xpos, xtran = to_utc(xret), to_utc(xpos), to_utc(xtran) # # 创建瀑布(活页)式分析图表 # 部分图表需要联网现在spy标普数据, # 可能会出现"假死"现象,需要人工中断 pf.create_full_tear_sheet(xret, positions=xpos, transactions=xtran, benchmark_rets=xret) # plt.show()
def print_pyfolio(self, strategy): pyfolio = strategy.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfolio.get_pf_items() pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, round_trips=True)
def render_pyfolio(perf): returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline( results) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev, round_trips=True)
def backtest_plot(self, account_value, baseline_start, baseline_end, baseline_ticker="^DJI"): df = deepcopy(account_value) test_returns = self.get_daily_return(df, value_col_name="account_value") baseline_df = self.get_baseline(ticker=baseline_ticker, start=baseline_start, end=baseline_end) baseline_returns = self.get_daily_return(baseline_df, value_col_name="close") with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet(returns=test_returns, benchmark_rets=baseline_returns, set_context=False)
def create_full_tear_sheet(self): """附加DataFrame方法""" returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( self) live_start_date = self.index[-int(len(self) / 4)] pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, live_start_date=live_start_date, round_trips=True)
def report_gen(): results = pd.read_pickle('test_zipline.pickle') returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( results) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, round_trips=True) fig.savefig('returns_tear_sheet.pdf')
def runstrat(args=None): args = parse_args(args) cerebro = bt.Cerebro() cerebro.broker.set_cash(args.cash) dkwargs = dict() if args.fromdate: fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') dkwargs['fromdate'] = fromdate if args.todate: todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') dkwargs['todate'] = todate data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **dkwargs) cerebro.adddata(data0, name='Data0') data1 = bt.feeds.BacktraderCSVData(dataname=args.data1, **dkwargs) cerebro.adddata(data1, name='Data1') data2 = bt.feeds.BacktraderCSVData(dataname=args.data2, **dkwargs) cerebro.adddata(data2, name='Data2') cerebro.addstrategy(St, printout=args.printout) if not args.no_pyfolio: cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') results = cerebro.run() if not args.no_pyfolio: strat = results[0] pyfoliozer = strat.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() if args.printout: print('-- RETURNS') print(returns) print('-- POSITIONS') print(positions) print('-- TRANSACTIONS') print(transactions) print('-- GROSS LEVERAGE') print(gross_lev) import pyfolio as pf pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2005-05-01', round_trips=True) if args.plot: cerebro.plot(style=args.plot_style)
def create_full_tear_sheet(self): """创建完整工作底稿(DataFrame扩展方法)""" returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( self) loc = -int(len(self) / 4) if int(len(self) / 4) else -1 live_start_date = self.index[loc] pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, live_start_date=live_start_date, round_trips=True)
def runstrat(args=None): args = parse_args(args) cerebro = bt.Cerebro() cerebro.broker.set_cash(args.cash) dkwargs = dict() if args.fromdate: fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') dkwargs['fromdate'] = fromdate if args.todate: todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') dkwargs['todate'] = todate data0 = bt.feeds.YahooFinanceCSVData(dataname=args.data0, **dkwargs) cerebro.adddata(data0, name='Data0') data1 = bt.feeds.YahooFinanceCSVData(dataname=args.data1, **dkwargs) cerebro.adddata(data1, name='Data1') data2 = bt.feeds.YahooFinanceCSVData(dataname=args.data2, **dkwargs) cerebro.adddata(data2, name='Data2') cerebro.addstrategy(St, printout=args.printout) if not args.no_pyfolio: cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') results = cerebro.run() if not args.no_pyfolio: strat = results[0] pyfoliozer = strat.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() if args.printout: print('-- RETURNS') print(returns) print('-- POSITIONS') print(positions) print('-- TRANSACTIONS') print(transactions) print('-- GROSS LEVERAGE') print(gross_lev) import pyfolio as pf pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2005-05-01', round_trips=True) if args.plot: cerebro.plot(style=args.plot_style)
def create_full_tear_sheet(self): """创建完整工作底稿(DataFrame扩展方法)""" returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( self) loc = -int(len(self) / 4) if int(len(self) / 4) else -1 live_start_date = self.index[loc] pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, live_start_date=live_start_date, round_trips=True)
def runstrat(args=None): args = parse_args(args) cerebro = bt.Cerebro() cerebro.broker.set_cash(args.cash) dkwargs = dict() if args.fromdate: fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') dkwargs['fromdate'] = fromdate if args.todate: todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') dkwargs['todate'] = todate todate = datetime.datetime.now() fromdate = todate - datetime.timedelta(days=364) for pair in args.pairs: data = get_ohlc(pair, start=fromdate, end=todate) data0 = bt.feeds.PandasData(dataname=data, datetime=None) cerebro.adddata(data0, name=pair) cerebro.addstrategy(St, printout=args.printout) if not args.no_pyfolio: cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') results = cerebro.run() if not args.no_pyfolio: strat = results[0] pyfoliozer = strat.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() if args.printout: print('-- RETURNS') print(returns) print('-- POSITIONS') print(positions) print('-- TRANSACTIONS') print(transactions) print('-- GROSS LEVERAGE') print(gross_lev) pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, # gross_lev=gross_lev, live_start_date='2005-05-01', round_trips=True) if args.plot: cerebro.plot(style=args.plot_style)
def create_pdf_report(filename: str, returns: pd.Series, positions: pd.DataFrame, transactions: pd.DataFrame, benchmark_rets: pd.DataFrame) -> None: with capture_mpl_figures_to_pdf(filename): with pf_print_table_to_mpl(): pyfolio.create_full_tear_sheet(returns, positions=positions, transactions=transactions, round_trips=True, benchmark_rets=benchmark_rets, bootstrap=None) log.info("Report created: {filename}".format(**locals()))
def BackTestPlot(account_value): df = account_value.copy() df = get_daily_return(df) dji, dow_strat = baseline_strat() df['date'] = dji['date'] df = df.dropna() DRL_strat = backtest_strat(df) with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet(returns=DRL_strat, benchmark_rets=dow_strat, set_context=False)
def result_backtest_ibov(stock, datainicial, datafinal): ibov = ['^BVSP'] dados_yahoo = web.get_data_yahoo(stock, start='{}'.format(datainicial), end='{}'.format(datafinal))['Adj Close'] dados_ibov = web.get_data_yahoo(ibov, start='{}'.format(datainicial), end='{}'.format(datafinal))['Adj Close'] bench = dados_ibov.pct_change() bench_acumulado = (1 + bench).cumprod() bench_acumulado.iloc[0] = 1 carteira_ibov = 10000 * bench_acumulado carteira_ibov['retorno'] = carteira_ibov.pct_change() retorno = dados_yahoo.pct_change() retorno_acumulado = (1 + retorno).cumprod() retorno_acumulado.iloc[0] = 1 carteira = 10000 * retorno_acumulado carteira['saldo'] = carteira.sum(axis=1) carteira['retorno'] = carteira['saldo'].pct_change() return pf.create_full_tear_sheet(carteira['retorno'], benchmark_rets=carteira_ibov['retorno'])
def backtest_plot(account_value, baseline_start=config.START_TRADE_DATE, baseline_end=config.END_DATE, baseline_ticker="^DJI", positions=None, transactions=None, value_col_name="account_value"): df = deepcopy(account_value) test_returns = get_daily_return(df, value_col_name=value_col_name) baseline_df = get_baseline(baseline_ticker, baseline_start, baseline_end) baseline_returns = get_daily_return(baseline_df, value_col_name="close") baseline_returns = baseline_returns.reindex(pd.date_range( start=test_returns.index.min(), end=test_returns.index.max()), fill_value=0) with pyfolio.plotting.plotting_context(font_scale=1.1): figs = pyfolio.create_full_tear_sheet(positions=positions, transactions=transactions, returns=test_returns, benchmark_rets=baseline_returns, set_context=False, return_figs=True) now = datetime.now().strftime(config.DATETIME_FMT) with PdfPages( f'./{config.RESULTS_DIR}/full_tear_sheet_{now}.pdf') as pages: for fig in figs: canvas = FigureCanvasPdf(fig) canvas.print_figure(pages)
def test(self, sd, ed, live_start_date, benchmark='SPY'): trading_days = date_range(sd, ed, freq=trading_day) self.timestep_progress = tqdm(total=len(trading_days) / 21) results = run_algorithm(initialize=self.initialize_testing_algo, capital_base=self.capital, start=sd, end=ed) returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( results) benchmark_returns = get_benchmark_returns(benchmark, sd, ed) benchmark_returns.ix[sd] = 0.0 pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, benchmark_rets=benchmark_returns, live_start_date=live_start_date, round_trips=True)
def view_full_tear_sheet(backtest=None, live_start_date=None): """显示完整工作底稿(DataFrame扩展方法)""" import pyfolio as pf if backtest is None: backtest = get_backtest() returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline( backtest) if live_start_date is None: loc = -int(len(backtest) / 4) if int(len(backtest) / 4) else -1 live_start_date = backtest.index[loc] else: live_start_date = backtest.index.asof(live_start_date) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, live_start_date=live_start_date, round_trips=True)
def __plot(self, stratData, testData, buyPoints, plot, report): dates = list(testData.data["DATE"][:self.__startingIndex + 1]) stockData = list(testData.data["CLOSE"][:self.__startingIndex + 1]) stratData = stratData[::-1] if (plot == True): stratData[:] = [ x / stratData[len(stockData) - 1] for x in stratData ] stockData[:] = [ x / stockData[len(stockData) - 1] for x in stockData ] plt.plot(dates, stratData, '-', label=self.__stratName) plt.plot(dates, stockData, '-', label=self.__testData.info()[0]) plt.legend() plt.show() if (report == True): if (self.__isnotebook() == True): import pyfolio as pf stratData = stratData[::-1] dates = dates[::-1] stockData = stockData[::-1] stratData[1:] = [ (stratData[x] - stratData[x - 1]) / stratData[x - 1] for x in range(1, len(stratData)) ] stratData[0] = 0 stockData[1:] = [ (stockData[x] - stockData[x - 1]) / stockData[x - 1] for x in range(1, len(stockData)) ] stockData[0] = 0 df1 = pd.DataFrame(stratData, dates) df1 = df1[0] df2 = pd.DataFrame(stockData, dates) df2 = df2[0] df2 = df2.rename(self.__testData.info()[0]) with warnings.catch_warnings(): warnings.simplefilter("ignore") pf.create_full_tear_sheet(df1, benchmark_rets=df2) else: print( "To generate a report testandtrade requires that the code be ran in a jupyter notebook." )
def BackTestPlot(account_value, baseline_start = config.START_TRADE_DATE, baseline_end = config.END_DATE, baseline_ticker = '^DJI'): df = account_value.copy() df = get_daily_return(df) dji, dow_strat = baseline_strat(ticker = baseline_ticker, start = baseline_start, end = baseline_end) df['date'] = dji['date'] df=df.dropna() DRL_strat = backtest_strat(df) with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet(returns = DRL_strat, benchmark_rets=dow_strat, set_context=False)
def backtest_plot( account_value, baseline_start=config.START_TRADE_DATE, baseline_end=config.END_DATE, baseline_ticker="^DJI", value_col_name="account_value", ): df = deepcopy(account_value) test_returns = get_daily_return(df, value_col_name=value_col_name) baseline_df = get_baseline(ticker=baseline_ticker, start=baseline_start, end=baseline_end) baseline_returns = get_daily_return(baseline_df, value_col_name="close") with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet(returns=test_returns, benchmark_rets=baseline_returns, set_context=False)
def main(): with open("app/config.yml", 'r') as stream: data_loaded = yaml.load(stream) exchangeInterface = ExchangeInterface(data_loaded['exchanges']) exchange = list(data_loaded['exchanges'].keys())[0] market_pair = data_loaded['settings']['market_pairs'][0] interval = data_loaded['settings']['update_interval'] max_periods = data_loaded['settings']['backtest_periods'] ohlcv = exchangeInterface.get_historical_data(exchange, market_pair, interval, max_periods) first = CDL_Test(ohlcv) cdl_list = list(map(lambda x: eval('talib.' + x), cdl)) params_list = {'trailing_window': [10, 15], 'indicator': cdl_list} #result = first.run_algorithm(params_list) best_sharpe, params = first.optim_algo(params_list) first.optim_grid.sort_values(by='sharpe', ascending=False) result = first.run_algorithm(params) pf.create_full_tear_sheet(result.returns)
def BackTestPlot(account_value, baseline_start="2019-01-01", baseline_end="2020-09-30", region=Region.US, baseline_ticker='^DJI'): df = account_value.copy() df = get_daily_return(df) dji, dow_strat = baseline_strat(region=region, ticker=baseline_ticker, start=baseline_start, end=baseline_end) dji.reset_index(drop=True, inplace=True) df['timestamp'] = dji['timestamp'] df = df.dropna() DRL_strat = backtest_strat(df) with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet(returns=DRL_strat, benchmark_rets=dow_strat, set_context=False)
def backtest_plot( account_value, baseline_start=config.TRADE_START_DATE, baseline_end=config.TRADE_END_DATE, baseline_ticker="^DJI", value_col_name="account_value", ): df = deepcopy(account_value) df["date"] = pd.to_datetime(df["date"]) test_returns = get_daily_return(df, value_col_name=value_col_name) baseline_df = get_baseline( ticker=baseline_ticker, start=baseline_start, end=baseline_end ) baseline_df["date"] = pd.to_datetime(baseline_df["date"], format="%Y-%m-%d") baseline_df = pd.merge(df[["date"]], baseline_df, how="left", on="date") baseline_df = baseline_df.fillna(method="ffill").fillna(method="bfill") baseline_returns = get_daily_return(baseline_df, value_col_name="close") with pyfolio.plotting.plotting_context(font_scale=1.1): pyfolio.create_full_tear_sheet( returns=test_returns, benchmark_rets=baseline_returns, set_context=False )
def full_tear_sheet(returns, positions, transactions, oos_date, sect_map): ret = returns pos = positions txn = transactions #ret.index = ret.index.tz_localize('UTC') #pos.index = pos.index.tz_localize('UTC') #txn.index = txn.index.tz_localize('UTC') oos_date = (oos_date) return pyf.create_full_tear_sheet(ret, positions=pos, transactions=txn, live_start_date=oos_date, slippage=0.0, sector_mappings=sect_map)
def make_return_tear_sheet_plot(self): for item in self.df_list: df = item[0] daily_profit = df["daily_profit"] tear_sheet_rt = pf.create_returns_tear_sheet(daily_profit, return_fig=True) figname = os.path.join( self.output, self.plot_name + "_tear_sheet_return" + ".png") tear_sheet_rt.savefig(figname, dpi=200) # Drawdown dd_plot = pf.plot_drawdown_periods(daily_profit, top=10) full_tear_sheet_rt_ax = pf.create_full_tear_sheet(daily_profit) # print(full_tear_sheet_rt_ax) ddfigname = os.path.join(self.output, self.plot_name + "_drawdown" + ".png") dd_plot.figure.savefig(ddfigname)
def runstrat(args=None): args = parse_args(args) cerebro = bt.Cerebro() cerebro.broker.set_cash(args.cash) dkwargs = dict() if args.fromdate: fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') dkwargs['fromdate'] = fromdate if args.todate: todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') dkwargs['todate'] = todate if args.timeframe: dkwargs['timeframe'] = _TFRAMES[args.timeframe] if args.compression: dkwargs['compression'] = args.compression # data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **dkwargs) data0 = bt.feeds.VCData(dataname=args.data0, historical=True, **dkwargs) cerebro.adddata(data0, name='Data0') cerebro.addstrategy(St, short=args.short, printdata=args.printdata) cerebro.addsizer(bt.sizers.FixedSize, stake=args.stake) # Own analyzerset cerebro.addanalyzer(bt.analyzers.TimeReturn, timeframe=bt.TimeFrame.Years) cerebro.addanalyzer(bt.analyzers.SharpeRatio, timeframe=bt.TimeFrame.Years) cerebro.addanalyzer(bt.analyzers.SQN,) if args.pyfolio: cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio', timeframe=_TFRAMES[args.pftimeframe]) if args.printout: print('Start run') results = cerebro.run() if args.printout: print('End Run') strat = results[0] # Results of own analyzers al = strat.analyzers.timereturn print('-- Time Return:') for k, v in al.get_analysis().items(): print('{}: {}'.format(k, v)) al = strat.analyzers.sharperatio print('-- Sharpe Ratio:') for k, v in al.get_analysis().items(): print('{}: {}'.format(k, v)) al = strat.analyzers.sqn print('-- SQN:') for k, v in al.get_analysis().items(): print('{}: {}'.format(k, v)) if args.pyfolio: pyfoliozer = strat.analyzers.getbyname('pyfolio',) returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() if args.printout: print('-- RETURNS') print(returns) print('-- POSITIONS') print(positions) print('-- TRANSACTIONS') print(transactions) print('-- GROSS LEVERAGE') print(gross_lev) if True: import pyfolio as pf pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, gross_lev=gross_lev, round_trips=True) if args.plot: pkwargs = dict(style='bar') if args.plot is not True: # evals to True but is not True pkwargs = eval('dict(' + args.plot + ')') # args were passed cerebro.plot(**pkwargs)
mpp = cumulative.ix[cumulative['cp_sum'].idxmax()]['strike'] return mpp def add_to_window(context, window_size, datapoint, ticker): tw = context.target_window[ticker] tw.append(datapoint) context.target_window[ticker] = tw[-window_size:] if len(tw) > window_size else tw # (6) Create CI for n-day window as price target range # Return tuple with upper and lower bound def CI(window, sds): mean = np.mean(window) sd = np.std(window) return (mean - sds * sd, mean + sds * sd) if __name__ == '__main__': universe = ['FB'] data = load_from_yahoo(stocks=universe, indexes={}, start=datetime(2016, 4, 3), end=datetime.today()) olmar = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000) backtest = olmar.run(data) backtest.to_csv('backtest-50-2012.csv') print backtest['algorithm_period_return'][-1] import pyfolio as pf returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(backtest) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2004-10-22')
perf_stats_strat = pf.timeseries.perf_stats(returns) perf_stats_all = perf_stats_strat if benchmark: perf_stats_bm = pf.timeseries.perf_stats(bm_ret) perf_stats_all = pd.concat([perf_stats_strat, perf_stats_bm], axis=1) perf_stats_all.columns = ['Strategy', 'Benchmark'] drawdown_table = pf.timeseries.gen_drawdown_table(returns, 5) monthly_ret_table = ep.aggregate_returns(returns, 'monthly') monthly_ret_table = monthly_ret_table.unstack().round(3) ann_ret_df = pd.DataFrame(ep.aggregate_returns(returns, 'yearly')) ann_ret_df = ann_ret_df.unstack().round(3) print('-------------- PERFORMANCE ----------------') print(perf_stats_all) print('-------------- DRAWDOWN ----------------') print(drawdown_table) print('-------------- MONTHLY RETURN ----------------') print(monthly_ret_table) print('-------------- ANNUAL RETURN ----------------') print(ann_ret_df) pf.create_full_tear_sheet( returns, benchmark_rets=bm_ret if benchmark else None, positions=positions, transactions=transactions, #live_start_date='2005-05-01', round_trips=False) plt.show()
dataname=get_yahoo_data('CSNA3.SA.weekly.csv'), fromdate=datetime(2015, 1, 1), todate=datetime(2020, 3, 20), timeframe=backtrader.TimeFrame.Weeks) data0.plotinfo.plotlog = True data0.plotinfo.plotylimited = True cerebro.adddata(data0) strats = cerebro.run() strat = strats[0] if getenv('NOSTATS') != 'true': pyfoliozer = strat.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() pyfolio.create_full_tear_sheet(returns, positions=positions, transactions=transactions) if getenv('NOPLOT') != 'true': cerebro.plot( grid=False, style='candle', # Default color for the 'line on close' plot loc='black', # Default color for a bullish bar/candle (0.75 -> intensity of gray) barup='blue', # Default color for a bearish bar/candle bardown='red', volume=False)
def create_full_tear_sheet(returns): pf.create_full_tear_sheet(returns)
def analyze(context=None, results=None): import matplotlib.pyplot as plt import logbook logbook.StderrHandler().push_application() log = logbook.Logger('Algorithm') ''' for i in range(len(results.portfolio_value)): print results.portfolio_value[i] print results.portfoliovalue[i] ''' dwRet = ((data.dowJones['Close'] - data.dowJones['Close'].shift(1)) / data.dowJones['Close'].shift(1))*100 dwRet = np.cumsum(dwRet) trans = results.ix[[t != 100000 for t in results.portfolio_value]] trans = results.ix[[t != 0 for t in results.algorithm_period_return]] fig = plt.figure() ax1 = fig.add_subplot(211) Momentum_Portfolio = trans.algorithm_period_return*100 #SP = results.benchmark_period_return[365:]*100 SP = dwRet longPositions = results.long_count shortPositions = results.short_count ax1.plot(Momentum_Portfolio, label="Momentum Portfolio") ax1.plot(SP, label="SPY") ax1.legend(loc = 2) ax1.set_ylabel('Cumulative Return %') ax2 = fig.add_subplot(212) ax2.set_ylabel('Positions') ax2.plot(longPositions, label="long positions") ax2.plot(shortPositions, label="short positions") ax2.legend(loc = 2) plt.ylim(ymax = 20, ymin = -5) print len(results.transactions) trans = results.ix[[t != [] for t in results.transactions]] buys = trans.ix[[t[0]['amount'] > 0 for t in trans.transactions]] returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(trans) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2013-10-14') for i in range(len(trans.transactions)): for n in range(len(trans.transactions[i])): print symbolst[trans.transactions[i][n]['sid']],": " , trans.transactions[i][n] plt.show() plt.show()
# -*- coding: utf-8 -*- import pandas as pd import pyfolio as pf import yfinance as yf cartera = ['YPF','GGAL','AMZN','AAPL','TLT','SHY','IEF'] data = pd.DataFrame(columns=cartera) for ticker in cartera: data[ticker] = yf .download(ticker, period='10y')['Adj Close'] data = data.pct_change().dropna().mean(axis=1) print(data.describe()) pf.create_full_tear_sheet(data)
import pyfolio as pf from extract_returns import extract_returns results = pd.read_pickle('results_1a_2016-2017.pickle') returns, positions, transactions = extract_returns(results) pos = list(positions.columns) pos.remove('cash') sectors = pd.read_csv('../bundles/meta.csv', usecols=['root_symbol', 'sector', 'sub_sector'], index_col=['root_symbol']) sectors['sector'] = sectors['sector'].str.cat(sectors['sub_sector'], sep='/') del sectors['sub_sector'] sectors.index = sectors.index.map(lambda x: x if len(x) > 1 else '_' + x) sectors = sectors.T.to_dict(orient='records')[0] sector_map = {p: sectors[p.root_symbol] for p in pos} out_of_sample = results.index[-21] transactions_mod = transactions.copy() transactions_mod.price = transactions.sid.apply( lambda x: x.multiplier) * transactions.price benchmark = (results['benchmark_period_return'] + 1).pct_change()[1:] benchmark.index = benchmark.index.normalize() pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions_mod, live_start_date=out_of_sample, round_trips=True, benchmark_rets=benchmark, sector_mappings=sector_map, hide_positions=True)
# In[4]: (1+ret['ret_co']).cumprod().plot() # In[64]: ### Load Data ### Reading 中证500 ETF(510500) data = pd.read_excel('../../data/中证500_20151027.xlsx', index_col='date', parse_dates=True) ret = pd.DataFrame() ret['cc'] = (data['close'] / data['close'].shift(1) - 1) ret['co'] = (data['close'] / data['open'] - 1) ret['oc'] = (data['open'] / data['close'].shift(1) - 1) (ret['co'] - ret['oc']).cumsum().plot() pf.create_full_tear_sheet((ret['co']), benchmark_rets=ret['cc']) # In[67]: ret_strategy = pd.DataFrame() ret_strategy['0'] = ret['cc'] #ret_strategy['1'] = ret['oc'] * (ret['co'] > 0).shift(1) ret_strategy['2'] = ret['co'] * ((ret['oc'] > 0)*1).shift(1) #ret_strategy['3'] = ret['oc'] * (ret['co'] < 0).shift(1) #ret_strategy['4'] = ret['co'] * (ret['oc'] < 0).shift(1) ret['5'] = ret['co'] * ((ret['co'] > 0)*1).shift(1) #ret_strategy['6'] = ret['co'] * (ret['co'] < 0).shift(1) #ret_strategy['7'] = ret['oc'] * (ret['oc'] > 0).shift(1) #ret_strategy['8'] = ret['oc'] * (ret['oc'] < 0).shift(1)