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/

    AttributeError: object has no attribute 'bar_executed'

    General Code/Help
    2
    6
    213
    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
      KopiC last edited by

      Hello all,

      I just started to learn backtrader with Quickstart Guide.

      When trying the sample codes of the "Do not only buy … but SELL" section on the guide, I met a problem I cannot overcome. Can anyone give your hand to me?

      Here below is the debug output.

      "AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'bar_executed'"

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

        You missed self.bar_executed = len(self) in the notify_order() function.

        1 Reply Last reply Reply Quote 0
        • K
          KopiC last edited by

          It is not missed in the function.

          Here below is the codes of the function I copied from the guide and tried it on my laptop

          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
                  self.bar_executed = len(self)
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
              self.order = None
          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by

            The code shown in the Do not only buy … but SELL works as is with the data feed shown there, I've tested it right before my previous answer. Your error pointed that self.bar_executed variable is not defined. My conclusion is that you run different code where self.bar_executed is not defined.

            1 Reply Last reply Reply Quote 0
            • K
              KopiC last edited by

              Thanks for your quick response. Your conclusion is not correct.

              My code is almost the same as the original one, but I used AAPL data derived from yahoo ( max time span, downloaded as a csv file). To make it clear I post it at the bottom.

              I tested the code several times, and finally find that the error popped up when I selected 2015,3,1 as fromdate.

              from future import (absolute_import, division, print_function, unicode_literals)
              import datetime
              import backtrader as bt

              class TestStrategy(bt.Strategy):
              def log(self, txt, dt = None):
              dt = dt or self.datas[0].datetime.date(0) #?
              print ('%s, %s' % (dt.isoformat(),txt))

              def __init__(self):
                  self.dataclose = self.datas[0].close
                  self.order = None
              
              def notify_order(self, order):
                  if order.status in [order.Submitted, order.Accepted]:
                      if order.isbuy():
                          self.log('BUY EXECUTED, %.2f' % order.executed.price)
                      elif order.issell():
                          self.log('SELL EXECUTED, %.2f' % order.executed.price)
                      self.bar_executed = len(self) # this is confusing at bit.
                  elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                      self.log('Order Canceled/Margin/Rejected')
                  self.order = None
              
              def next(self):
                  self.log('Close, %.2f' % self.dataclose[0])
              
                  if self.order:
                      return
              
                  if not self.position: # check if we are in the market
                      if self.dataclose[0] < self.dataclose[-1]:
                          if self.dataclose[-1] < self.dataclose[-2]:
                              self.log('BUY CREATE, %.2f' % self.dataclose[0])
                              self.buy()
                      else:
                          if len(self) >= (self.bar_executed + 5):
                              self.log('SELL CREATE, %.2f' % self.dataclose[0])
                              self.order = self.sell()
              

              if name == 'main':
              cerebro = bt.Cerebro()

              cerebro.addstrategy(TestStrategy)
              
              datafile = 'AAPL.csv'
              data = bt.feeds.YahooFinanceCSVData(
                  dataname = datafile,
                  fromdate = datetime.datetime(2015,3,1),
                  todate = datetime.datetime(2020,7,30),
                  reverse = False)
              
              cerebro.adddata(data)
              
              cerebro.broker.setcash(100000.0)
              
              print ('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
              
              cerebro.run()
              
              print ('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
              1 Reply Last reply Reply Quote 0
              • K
                KopiC last edited by

                I just reviewed the code again and finally fixed the issues by myself.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }