Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Analysers issue - how to remove leading zeros from returns list ?

    General Code/Help
    2
    2
    71
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      frus last edited by

      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

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @frus last edited by

        @frus If you make a custom anaylzer to track the value of the cash positions and total 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 bar total value and date to start your dictionary in your analyzer.

        Then just add each bar until finished.

        RunBacktest.com

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors