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/

    Possibly wrong output of tradeanalyzer?

    Indicators/Strategies/Analyzers
    2
    3
    628
    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.
    • K
      kfeeeeee last edited by

      Hello everyone,

      I recently noticed some strange output when using the trade analyzer. Specifically, the total.won and total.lost values are always equal. I am not sure if this is really a bug in bt or just in my head , so please see below a working example:

      This will generate 3 trades (first two are losers, the last one is a winner) but the tradeanaylzer outputs:

      open: 0
      closed: 3
      won: 3
      lost: 3

      Any help is appreciated. Thanks.

      import backtrader as bt
      import datetime
      
      
      class simpleStrat(bt.Strategy):
         
          def __init__(self):
              self.p.stake = 1
              pass
      
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enougth cash
              if order.status in [order.Completed, order.Canceled, order.Margin]:
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm))
                  else:  # Sell
                      self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                               (order.executed.price,
                                order.executed.value,
                                order.executed.comm))
      
          def notify_trade(self, trade):
              if trade.isclosed:
                  self.log('TRADE PROFIT, GROSS %.2f, NET %.2f' %
                           (trade.pnl, trade.pnlcomm))
      
      
          def next(self):
              if len(self.data) == 20:
                  self.buy(size=self.p.stake)
                  self.log('BUY CREATE , %.2f' % self.data.close[0])
      
              if len(self.data) == 30:
                  self.close(size=self.p.stake)
                  self.log('CLOSE CREATE , %.2f' % self.data.close[0])
      
              if len(self.data) == 40:
                  self.buy(size=self.p.stake)
                  self.log('BUY CREATE , %.2f' % self.data.close[0])
      
              if len(self.data) == 50:
                  self.close(size=self.p.stake)
                  self.log('CLOSE CREATE , %.2f' % self.data.close[0])
      
              if len(self.data) == 60:
                  self.sell(size=self.p.stake)
                  self.log('SELL CREATE , %.2f' % self.data.close[0])
      
              if len(self.data) == 70:
                  self.close(size=self.p.stake)
                  self.log('CLOSE CREATE , %.2f' % self.data.close[0])
      
      
      
      
      cerebro = bt.Cerebro()
      
      
      cerebro.broker.set_cash(10000)
      cerebro.broker.setcommission(commission=0.001,margin=1)
      
      
      data = bt.feeds.Quandl(dataname='YHOO', apikey='XXX', fromdate=datetime.datetime(2015, 6, 20),)
      cerebro.adddata(data)
      cerebro.addstrategy(simpleStrat)
      cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="ta")
      
      
      
      strategies = cerebro.run()
      
      cerebro.plot()
      
      analysis = strategies[0].analyzers.ta.get_analysis()
      
      print('open:',analysis.total.open)
      print('closed:',analysis.total.closed)
      print('won:',analysis.total.closed)
      print('lost:',analysis.total.closed)
      
      
      1 Reply Last reply Reply Quote 0
      • K
        kfeeeeee last edited by

        Doh, just saw a post that makes this one irrelevant. Sorry for that. Please close/delete.

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          No issue keeping it. It was a regression error and it will soon be in the mainline.

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