def __init__(self, t="19-04-2020", r=0.05, S_t=90.0, sigma=0.2): print("Initializing the MarketEnvironment!") self.__t = date_string_to_datetime_obj(t) self.__r = r self.__S = S_t self.__sigma = sigma
def main(): # Calendar-Spread implementation example # default market environment market_env = MarketEnvironment() print(market_env) # options expirations T_short = "31-05-2020" T_long = "30-08-2020" # current underlying level S_t = market_env.get_S() # calendar-spread portfolio initialized (as empty portfolio) calendar_spread_ptf = Portfolio(name="Calendar Spread Strategy") print(calendar_spread_ptf) # T_long-call Vanilla_Call_long = PlainVanillaOption(market_env, T=T_long, K=S_t) print(Vanilla_Call_long) # T_short-call Vanilla_Call_short = PlainVanillaOption(market_env, T=T_short, K=S_t) print(Vanilla_Call_short) # creation of Calendar-Spread portfolio strategy calendar_spread_ptf.add_instrument(Vanilla_Call_long, 1) calendar_spread_ptf.add_instrument(Vanilla_Call_short, -1) print(calendar_spread_ptf) # portfolio plotter instance calendar_spread_ptf_plotter = PortfolioPlotter(calendar_spread_ptf) # valuation date of the portfolio valuation_date = calendar_spread_ptf.get_t() print(valuation_date) # select dependency to plot as x-axis of the plot for dependency_type in ["S", "K", "sigma", "r"]: # keyboard parameter and corresponding range to test x_axis_dict = options_x_axis_parameters_factory(calendar_spread_ptf, dependency_type) # appropriate azimut angle for best viewing azimut_angle = get_azimut_angle(dependency_type) # select metrics to plot for plot_metrics in ["price", "PnL", "delta", "theta", "gamma", "vega", "rho"]: plot_details_flag = True if plot_metrics == "price" else False # time-parameter as a date-range of 5 valuation dates between t and T_short last_date = T_short if plot_metrics in ["price", "PnL"] else date_string_to_datetime_obj(T_short) - pd.Timedelta(days=1) multiple_valuation_dates = pd.date_range(start=valuation_date, end=last_date, periods=5) print(multiple_valuation_dates) # Bull-Spread price plot calendar_spread_ptf_plotter.plot(**x_axis_dict, t=last_date, plot_metrics=plot_metrics, plot_details=plot_details_flag) # Plot at multiple dates calendar_spread_ptf_plotter.plot(**x_axis_dict, t=multiple_valuation_dates, plot_metrics=plot_metrics) # Surface plot calendar_spread_ptf_plotter.plot(**x_axis_dict, t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True) # Surface plot (rotate) - Underlying value side calendar_spread_ptf_plotter.plot(**x_axis_dict, t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True, view=(0,azimut_angle["x-axis side"])) # Price surface plot (rotate) - Date side calendar_spread_ptf_plotter.plot(**x_axis_dict, t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True, view=(0,azimut_angle["Date side"]))
def main(): # Calendar-Spread implementation example # default market environment market_env = MarketEnvironment() print(market_env) # options expirations T_short = "31-05-2020" T_long = "30-08-2020" # current underlying level S_t = market_env.get_S() # calendar-spread portfolio initialized (as empty portfolio) calendar_spread_ptf = Portfolio(name="Calendar Spread Strategy") print(calendar_spread_ptf) # T_long-call Vanilla_Call_long = PlainVanillaOption(market_env, T=T_long, K=S_t) print(Vanilla_Call_long) # T_short-call Vanilla_Call_short = PlainVanillaOption(market_env, T=T_short, K=S_t) print(Vanilla_Call_short) # creation of Calendar-Spread portfolio strategy calendar_spread_ptf.add_instrument(Vanilla_Call_long, 1) calendar_spread_ptf.add_instrument(Vanilla_Call_short, -1) print(calendar_spread_ptf) # portfolio plotter instance calendar_spread_ptf_plotter = PortfolioPlotter(calendar_spread_ptf) # valuation date of the portfolio valuation_date = calendar_spread_ptf.get_t() print(valuation_date) # select metrics to plot for plot_metrics in ["price", "PnL", "delta", "theta", "gamma", "vega", "rho"]: plot_details_flag = True if plot_metrics == "price" else False # time-parameter as a date-range of 5 valuation dates between t and T_short # being the Calendar-Spread a multi-horizon portfolio, time-to-maturity # time parameters are not allowed. last_date = T_short if plot_metrics in ["price", "PnL"] else date_string_to_datetime_obj(T_short) - pd.Timedelta(days=1) multiple_valuation_dates = pd.date_range(start=valuation_date, end=last_date, periods=5) print(multiple_valuation_dates) # Bull-Spread price plot calendar_spread_ptf_plotter.plot(t=last_date, plot_metrics=plot_metrics, plot_details=plot_details_flag) # Plot at multiple dates calendar_spread_ptf_plotter.plot(t=multiple_valuation_dates, plot_metrics=plot_metrics) # Surface plot calendar_spread_ptf_plotter.plot(t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True) # Surface plot (rotate) - Underlying value side calendar_spread_ptf_plotter.plot(t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True, view=(0,180)) # Price surface plot (rotate) - Date side calendar_spread_ptf_plotter.plot(t=multiple_valuation_dates, plot_metrics=plot_metrics, surf_plot=True, view=(0,-90))
def set_t(self, t): self.__t = date_string_to_datetime_obj(t)