Analysers issue - how to remove leading zeros from returns list ?
-
Hi - i'm really new to Backtrader. I would like to make certain analyzers/statistics work from the start of the first transaction instead of from the first date in the dataset. For example, say if a 200d SMA is used for a strategy, the Sharpe ratio, CAGR calculations, etc. still use the first 200d data which skews the results as there is no move in the portfolio during those first 200 days
As a portfolio tearsheet I'm currently using quantstats, to which I am feeding the - returns series. I would like to trim that series to start at the first trade.
results=cerebro.run(maxcpus=1) strat=results[0] portfolio_stats = strat.analyzers.getbyname('PyFolio') returns, positions, transactions, gross_lev = portfolio_stats.get_pf_items() returns.index = returns.index.tz_convert(None) end_portfolio_value = cerebro.broker.getvalue() pnl = end_portfolio_value - start_portfolio_value print(f'Starting Portfolio Value: {start_portfolio_value:2f}') print(f'Final Portfolio Value: {end_portfolio_value:2f}') print(f'PnL: {pnl:.2f}') print('Sharpe Ratio:', strat.analyzers.mysharpe.get_analysis()) print('Annual Return:', strat.analyzers.myannual.get_analysis()) print('Drawdown Info:', strat.analyzers.mydrawdown.get_analysis()) quantstats.reports.html(returns, output='stats.html', title='Report')
Thanks
-
@frus If you make a custom anaylzer to track the value of the
cash
positions
andtotal value
, then you can simply evaluate at each bar, the value of the cash vs. the previous value of the cash.If the cash value has changed then trading has started.
During the first bar when
cash
is changing, grab the previous bartotal value
anddate
to start your dictionary in your analyzer.Then just add each bar until finished.