Attribute Error: object has no attribute 'postion'
-
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.
-
@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 methodnext
within theclass 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 notself.postion
. Hope it works now:) -
Oh my gosh I must be blind. Thank you for taking the time to answer such a dumb question