Problem--when I try the guide code
-
else:
# in market we might sell
if len(self) >= (self.bar_executed + 5):
self.log('Sell create, %.2f' % self.dataclose[0])self.order = self.sell()
File "e:\trading\backtrader_ex\backtrader\cerebro.py", line 1695, in _runonce
strat._oncepost(dt0)
File "e:\trading\backtrader_ex\backtrader\strategy.py", line 309, in _oncepost
self.next()
File "e:/trading/backtrader_ex/test.py", line 46, in next
if len(self) >= (self.bar_executed + 5):
File "e:\trading\backtrader_ex\backtrader\lineseries.py", line 461, in getattr
return getattr(self.lines, name)
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'bar_executed' -
@xue said in Problem--when I try the guide code:
f len(self) >= (self.bar_executed + 5):
Look like
self.bar_executed
doesn't exist. Other than that, given the limited information you have provided, cannot really provide you with assitance. If you wish, you could copy all of your code here between triple backslashes (see instructions at the top of this page) and then maybe we can help you. -
@run-out from future import (absolute_import, division,
print_function, unicode_literals)import os.path
import sys
import datetime
import backtrader as btcreate a strategy
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]: return if order.status in [order.Completed]: if order.isbuy(): self.log('Buy executed, %.2f' % order.executed.price) elif order.issell(): self.lot('Sell executed, %.2f' % order.executed.price) elif order.status in [order.Canceld, 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 # check if we are in the market if not self.position: if self.dataclose[0] < self.dataclose[-1]: if self.dataclose[-1] < self.dataclose[-2]: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy() else: # in market we might sell 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()# add a strategy cerebro.addstrategy(TestStrategy) modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, 'datas/orcl-1995-2014.txt') # create a data feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2000, 12, 31), reverse=False) # add datafeed to crerbro cerebro.adddata(data) # set out cash cerebro.broker.setcash(100000.0) # print out the starting conditions print('starting portfolio value:%.2f' % cerebro.broker.getvalue()) # run over everthing cerebro.run() # print out the final result print('Fianl Portfolio value:%.2f' % cerebro.broker.getvalue())
-
from __future__ import (absolute_import, division, print_function, unicode_literals) import os.path import sys import datetime import backtrader as bt # create a strategy 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]: return if order.status in [order.Completed]: if order.isbuy(): self.log('Buy executed, %.2f' % order.executed.price) elif order.issell(): self.lot('Sell executed, %.2f' % order.executed.price) elif order.status in [order.Canceld, 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 # check if we are in the market if not self.position: if self.dataclose[0] < self.dataclose[-1]: if self.dataclose[-1] < self.dataclose[-2]: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy() else: # in market we might sell 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() # add a strategy cerebro.addstrategy(TestStrategy) modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, 'datas/orcl-1995-2014.txt') # create a data feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2000, 12, 31), reverse=False) # add datafeed to crerbro cerebro.adddata(data) # set out cash cerebro.broker.setcash(100000.0) # print out the starting conditions print('starting portfolio value:%.2f' % cerebro.broker.getvalue()) # run over everthing cerebro.run() # print out the final result print('Fianl Portfolio value:%.2f' % cerebro.broker.getvalue())
-
@xue
The error is telling you thatself.bar_exectued
does not exist. You need to set the value ofself.bar_exectued
when the order is initially executed. You can do this in thenotify_order
method when the buy order is complete. Simply add in:self.bar_executed = len(self)
in the
notify_order
method. Like this:def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: return if order.status in [order.Completed]: if order.isbuy(): ####### HERE ######### self.bar_executed = len(self) self.log('Buy executed, %.2f' % order.executed.price) elif order.issell(): self.lot('Sell executed, %.2f' % order.executed.price) elif order.status in [order.Canceld, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') self.order = None
-
thank for your answer but also another problem meeted.
raceback (most recent call last): File "e:/trading/backtrader_ex/test.py", line 73, in <module> cerebro.run() File "e:\trading\backtrader_ex\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "e:\trading\backtrader_ex\backtrader\cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "e:\trading\backtrader_ex\backtrader\cerebro.py", line 1695, in _runonce strat._oncepost(dt0) File "e:\trading\backtrader_ex\backtrader\strategy.py", line 305, in _oncepost self._notify() File "e:\trading\backtrader_ex\backtrader\strategy.py", line 590, in _notify self.notify_order(order) File "e:/trading/backtrader_ex/test.py", line 30, in notify_order self.lot('Sell executed, %.2f' % order.executed.price) File "e:\trading\backtrader_ex\backtrader\lineseries.py", line 461, in __getattr__ return getattr(self.lines, name) AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'lot'
-
@xue These two errors are really basic python errors you are making, not backtrader errors. You are being informed that
self.lot
doesn't exist, that's because your method isself.log
.elif order.issell(): self.lot('Sell executed, %.2f' % order.executed.price)
We are happy to help people learn backtrader, but we are not so eager to help people learn python. Take some time and practice some python tutorials, and when you improve a bit backtrader will easier for you to work with. Good luck!
-
Sorry! the last error is not about python, it is a spell error
self.log(...)
is correct.
'''
set.lot(...)is not correct. thanks for you patient answer.