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/

    Attribute Error: object has no attribute 'postion'

    General Code/Help
    2
    3
    244
    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.
    • C
      Carson Lansdowne last edited by

      Hello all,

      I am working on a project and frustratingly something about my self.position code does not seem to be working. It works in other strategies I have written so I am not sure what the issue is

      The error thrown is AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'postion'

      import backtrader as bt
      from datetime import datetime, timedelta, date
      import math
      
      
      class exampleSizer(bt.Sizer):
          params = (('size',1),)
          def _getsizing(self, comminfo, cash, data, isbuy):
              return self.p.size
      
      class buynhold_1(bt.Strategy):
          #params = (('year',1))
          
          def __init__(self):
              self.dataclose = self.datas[0].close()
              #self.currdate = self.datas[0].datetime.date(0)
              self.order = None
          
          def start(self):
              self.val_start = self.broker.get_cash()  # keep the starting cash
      
          def nextstart(self):
              # Buy all the available cash
              self.buy()
              self.bar_executed = 0
              
          def next(self):
              self.currdate = self.datas[0].datetime.date(len(self))
              self.duration = len(self) - self.bar_executed + 1
              
              if not self.postion:
                  if self.currdate == datetime.date(2016, 1, 3):
                      self.buy()
                      self.bar_executed = len(self)
              
              elif self.duration == 100:
                  self.close()
                  
              #else:
               #   self.duration = len(self) - self.bar_executed + 1
      
          def stop(self):
              # calculate the actual returns
              self.roi = (self.broker.get_value() / self.val_start) - 1.0
              print('ROI:        {:.2f}%'.format(100.0 * self.roi))
      

      Any advice is sincerely appreciated, I literally started python today.

      1 Reply Last reply Reply Quote 0
      • Soham Jain
        Soham Jain last edited by

        @Carson-Lansdowne said in Attribute Error: object has no attribute 'postion':

        postion

        Hey Carson.
        A little modification in the 31st line of your code is required.
        Under the class method next within the class buynhold_1, make the following changes:

        Old code:

        if not self.postion:
                    if self.currdate == datetime.date(2016, 1, 3):
                        self.buy()
                        self.bar_executed = len(self)
        

        New Code:

        if not self.position:
                    if self.currdate == datetime.date(2016, 1, 3):
                        self.buy()
                        self.bar_executed = len(self)
        

        It's a mere spelling error self.position and not self.postion. Hope it works now:)

        1 Reply Last reply Reply Quote 3
        • C
          Carson Lansdowne last edited by

          Oh my gosh I must be blind. Thank you for taking the time to answer such a dumb question

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