I just reviewed the code again and finally fixed the issues by myself.
Posts made by KopiC
-
RE: AttributeError: object has no attribute 'bar_executed'
-
RE: AttributeError: object has no attribute 'bar_executed'
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 btclass 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())
-
RE: AttributeError: object has no attribute 'bar_executed'
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
-
AttributeError: object has no attribute 'bar_executed'
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'"