Backtrader Community

    • 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/

    How to interpret TradeAnalyzer's result?

    General Code/Help
    2
    4
    1883
    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.
    • B
      backtrader14 last edited by backtrader14

      I am backtesting a single day trading strategy in backtrader in which I buy at current open and close at current close and no trade carry forward. This is my TradeAnalyzer's report.

      # data.shape >>> (197, 6)
      
      TradeAnalyzer:
        -----------------------------------------------------------------------------
        - total:
          - total: 196
          - open: 1
          - closed: 195
        -----------------------------------------------------------------------------
        - streak:
          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          - won:
            - current: 0
            - longest: 5
          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          - lost:
            - current: 2
            - longest: 5
        -----------------------------------------------------------------------------
      

      If I am guessing it right then the total section says that I took 196 positions in which 195 are completed and 1 position is still open. The confusion is in the win/lose stat that is I have won 0 and lost 2 positions, so according to this stat I only did a total of 2 trades which is contradictory to the 196 which is given in the total. I also expected the sum of my win and lose trade should be near to 196. It also shows that my longest trading length is 5 days but my strategy involves only a single day. How to make sense of TradeAnalyzer's result?

      This is my full code.

      from datetime import datetime
      import backtrader as bt
      import pandas as pd
      from pandas_datareader import data as pdr
      
      data = pdr.get_data_yahoo('AAPL', start=datetime(2018, 8, 13), end=datetime.now())
      
      data.columns=['high', 'low', 'open', 'close', 'volume', 'adj_close']
      del data.adj_close
      
      class TestStrategy(bt.Strategy):
          
          def __init__(self):
              pass
          
          def next_open(self):
              self.buy(coc=False)
              
          def next(self):
              pos = self.getposition()
              if pos:
                  self.sell()
      
      
      
      cerebro = bt.Cerebro(cheat_on_open=True)
      cerebro.addstrategy(TestStrategy)
      df=bt.feeds.PandasData(dataname=data)
      cerebro.adddata(df)
      cerebro.addanalyzer(bt.analyzers.TradeAnalyzer,_name="Basic_stats")
      cerebro.broker.set_coc(True)
      
      
      strats =  cerebro.run(stdstats=False)
      strat = strats[0]
      cerebro.addobserver(bt.observers.Value)#
      
      
      
      print('Final Balance: %.2f' % cerebro.broker.getvalue())
      for e in strat.analyzers:
          e.print()
      
      cerebro.plot()
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        This

            +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            - won:
              - current: 0
              - longest: 5
            +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            - lost:
              - current: 2
              - longest: 5
          -----------------------------------------------------------------------------
        

        is not win/loss stat but streak related stat. streak is a number of winning or losing trades in a row. So you had longest winning trades streak of 5 and longest losing trades streak of 5.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 3
        • B
          backtrader14 last edited by

          Thank you this makes complete sense. What is current: 0 and current: 2 means?

          A 1 Reply Last reply Reply Quote 0
          • A
            ab_trader @backtrader14 last edited by

            @backtrader14 means that your last two trades are losing trades.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 2
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors