Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def objective_function(params):
- interp_func = interp1d(STK, params, kind='cubic', fill_value='extrapolate')
- interpolated_ivs = interp_func(STK)
- return np.sum((interpolated_ivs - IV) ** 2)
- def constraint(params):
- interp_func = interp1d(STK, params, kind='cubic', fill_value='extrapolate')
- atm_IV = interp_func(CMP)
- min_IV = min(interp_func(np.arange(min(STK), max(STK) + 0.0001, 0.0001)))
- return atm_IV - min_IV
- initial_params = IV
- constraints = {'type': 'ineq', 'fun': constraint}
- result = minimize(objective_function, initial_params, constraints=constraints)
- if result.success:
- optimized_params = result.x
- adj_interp_func = interp1d(STK, optimized_params, kind='cubic', fill_value='extrapolate')
- interpolated_strikes = np.round(np.arange(min(STK), max(STK) + 0.0001, 0.0001),4)
- interpolated_ivs = np.round(adj_interp_func(interpolated_strikes),4)
- for i in range(len(interpolated_strikes)):
- delta_call = black_scholes_delta(CMP, interpolated_strikes[i], DTE_N, r, sigma, 'C')
- delta_put = black_scholes_delta(CMP, interpolated_strikes[i], DTE_N, r, sigma, 'P')
- call_deltas.append(delta_call)
- put_deltas.append(delta_put)
- del_25_c_strike = interpolated_strikes[np.argmin(np.abs(np.array(call_deltas)-0.25))]
- del_25_p_strike = interpolated_strikes[np.argmin(np.abs(np.array(put_deltas)+0.25))]
- strikes_reqd = [CMP, del_25_c_strike, del_25_p_strike]
- IV_reqd = []
- for i in strikes_reqd:
- IV_reqd.append(interpolated_ivs[np.argmin(np.abs(interpolated_strikes-i))])
- else:
- print("Optimization failed:", result.message)
- print(f"Strikes - ATM, 25_Delta_Call, 25_Delta_Put are: {strikes_reqd}")
- print(f"IVs - ATM, 25_Delta_Call, 25_Delta_Put are: {IV_reqd}")
- # Plot the original data and the interpolated curve
- plt.figure(figsize=(8, 4))
- plt.plot(interpolated_strikes, interpolated_ivs, label='Adjusted Interpolated Curve')
- plt.scatter(STK, IV_N, color='red', label='Market Data')
- plt.title('Interpolated IV_F1 vs. F1')
- plt.xlabel('F1')
- plt.ylabel('IV_F1')
- plt.legend()
- plt.grid(True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement