I'm trying to optimize the followiong strategy in Backtrader:
class MyStrategy(bt.Strategy):
params = (('LONG_ENTRY', 'pattern_L1'),
('SHORT_ENTRY', 'pattern_S1'),
('LONG_EXIT', 'pattern_S3'),
('SHORT_EXIT', 'pattern_L2'),)
def __init__(self):
self.dataclose = self.datas[0].close
self.order = None
self.price = None
self.comm = None
self.order_time = None
self.LONG_ENTRY = self.p.LONG_ENTRY
self.SHORT_ENTRY = self.p.SHORT_ENTRY
self.LONG_EXIT = self.p.LONG_EXIT
self.SHORT_EXIT = self.p.SHORT_EXIT
#INDICATORI PERSONALIZZATI O FEATURES
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
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(f'BUY EXECUTED --- Price: {order.executed.price:.2f}, Cost: {order.executed.value:.2f}, Commission: {order.executed.comm:.2f}')
self.price = order.executed.price
self.comm = order.executed.comm
else:
self.log(f'SELL EXECUTED --- Price: {order.executed.price:.2f}, Cost: {order.executed.value:.2f}, Commission: {order.executed.comm:.2f}')
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Failed')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log(f'OPERATION RESULT --- Gross: {trade.pnl:.2f}, Net: {trade.pnlcomm:.2f}')
def next(self):
if self.order:
return
pattern_L1 : int = self.data0_pattern_L1[0]
pattern_L2 : int = self.data0_pattern_L2[0]
pattern_L3 : int = self.data0_pattern_L3[0]
pattern_L4 : int = self.data0_pattern_L4[0]
pattern_L5 : int = self.data0_pattern_L5[0]
pattern_S1 : int = self.data0_pattern_S1[0]
pattern_S2 : int = self.data0_pattern_S2[0]
pattern_S3 : int = self.data0_pattern_S3[0]
pattern_S4 : int = self.data0_pattern_S4[0]
pattern_S5 : int = self.data0_pattern_S5[0]
self.log(f"pattern_L1: {pattern_L1}, pattern_L2: {pattern_L2}, pattern_S2: {pattern_S1}, pattern_S3: {pattern_S3}")
if not self.position:
if (eval(self.LONG_ENTRY) == 1) and (eval(self.LONG_EXIT) != -1):
self.order = self.buy()
self.log(f'BUY CREATED --- Price: {self.dataclose[0]:.2f}')
elif (eval(self.SHORT_ENTRY) == -1) and (eval(self.SHORT_EXIT) != 1):
self.log(f'SELL CREATED --- Price: {self.dataclose[0]:.2f}')
self.order = self.sell()
# CHECK DELLA POSIZIONE APERTA E CHIUSURA IN BASE ALLA CONDIZIONE
elif (self.position) and (self.position.size > 0):
if (self.LONG_EXIT == -1):
self.close()
elif (self.position) and (self.position.size < 0):
if (self.SHORT_EXIT == 1):
self.close()
the values of: 'pattern_L1', 'pattern_S1', 'pattern_S3', 'pattern_L2' are pandas series of my dataframe.
This dataframe has 40 features "pattern_L" and 40 feratures "pattern_S". I need to search and optimize my trading system, it is a computationally huge calculation for a normal GRID OPTIMIZATION. I've tried to use "BayesianOptimization" from "bayes_opt", but it requires a range of float, not pandas series and I'm not able to workaround this problem. Is there any other method as well Bayesian Optimization? Or how can I deal with this problem?