Backtest Optimiser Constraint Function - syntax & structure query #335
-
Hi, I'm trying to backtest some OCHLV data with a minimisation constraint on the total number of trades, and for the basic example of SMA Cross - also n1 and n2 attributes of _strategy. I think I need to make max_trades a class variable of the SMACross class but cannot see how I factor that into the calculations that occur there. I suspect that I need to call something like this in the SMACross init method. def num_trades(values):
return np.ndarray(max([x for x in range(len(values))])
self.I(num_trades, self.data.Close) Is this remotely in the right ballpark? Thanks, Kit |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
With What you're looking for is a customized def maximize_func(stats):
return stats['Equity Final [$]'] if 3 <= stats['# Trades'] <= 10 else 0
bt = Backtest(...)
bt.optimize(..., maximize=maximize_func) |
Beta Was this translation helpful? Give feedback.
-
sorry, if I seem to hijack this thread,... I have a similar query but not exactly the same. I would like to (a) maximize the Equity Final, (b) minimize the Max. Drawdown [%], and (c) maximize the Expectancy [%] |
Beta Was this translation helpful? Give feedback.
With
constraint=
parameter, you merely constrain the input variables' value search space. You don't know anything about the number of trades in advance.What you're looking for is a customized
maximize=
function. To optimize the strategy in such a way that only between three and ten trades are made, something like this: