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?



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


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



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



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


Log in to reply
 

});