I am trying to optimize a function using scipy.optimize, but it does not converge. I have a trading strategy with a default stop-loss based on the lowest price over 20 days. I want to optimize this stop-loss with 2 variables (i.e. I want it to be higher or lower depending on these variables). The result of the objective function is the total return. What is the best way to do this?
>> df close stop_loss variable_1 variable_2 0 111.79 114.080429 -1.124674 -0.573896 1 113.04 114.080429 -0.750894 -0.574460 2 113.07 114.080429 -0.653854 -0.572659 3 111.06 114.080429 -1.014128 -0.520336 4 109.65 112.613320 -1.258951 -0.424078 def objective(x, df): long_ = df['close'] > (df['stop_loss'] * (df['variable_1'] * x[0] + 1) * (df['variable_2'] * x[1] + 1)) returns = ((df['close'].pct_change(1).shift(-1) * long_).dropna() + 1).cumprod() return -returns[-1] res = minimize(objective, np.array([1, 1]), args=(df), method='nelder-mead', options={'tol': 1e-8, 'disp': True})