Multiple trades Long/Short Support?
Nael Shichida last edited by
Newly enlisted backtrader here, I have been looking at youtube tutorials and the documentation. A lot of the examples cover 1 position at a time such as SMA crossover.
However I am trying to have multiple buy orders using 1R/R of my portfolio for each trade, regardless of whether I am in a trade or not I want each one to be individually dealt with. The logic for entering long trades is working, however I am using a buy bracket since I need a log to see if the order is executed and if it has been executed, to set a close long either at target or stop price.
If anyone can help guide me, would be extremely grateful as to how I should formulate the logic in Python?
def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas.datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data dataseries self.order = None self.dataclose = self.datas.close self.dataopen = self.datas.open self.zone = self.datas.close - self.datas.open def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose) self.log('Open, %.2f' % self.dataopen) self.log('Zone, %.2f' % self.zone) self.log('shift, %.2f' % self.zone[-1]) print(len(self)) print(self.order) print(self.position) # """Enters Long position if below conditions are met""" if self.zone < 0: print("Bar is red") # current close less than previous close if self.zone > 0: print("Next candle is green") # Next close less than the previous close if 1.5*abs(self.zone) < abs(self.zone): print("Buy Zone found") # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose) # self.buy() self.order = self.buy_bracket(limitprice=self.dataopen, price=self.dataopen, stopprice=self.dataclose) # print(self.order) print(type(self.order.ref))