diff --git a/README.md b/README.md index 572e3ae..1f3fc5e 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ from fypy.pricing.fourier.ProjEuropeanPricer import ProjEuropeanPricer from fypy.model.levy.BlackScholes import * from fypy.model.levy.VarianceGamma import * from fypy.termstructures.DiscountCurve import DiscountCurve_ConstRate +from fypy.termstructures.EquityForward import EquityForward from fypy.volatility.implied.ImpliedVolCalculator import ImpliedVolCalculator_Black76 import matplotlib.pyplot as plt @@ -98,7 +99,7 @@ fwd = EquityForward(S0=S0, discount=disc_curve, divDiscount=div_disc) # ============================ # Create Black-Scholes Model # ============================ -model = BlackScholes(sigma=0.2, forwardCurve=fwd) +model = BlackScholes(sigma=0.2, forwardCurve=fwd, discountCurve=fwd.discountCurve) pricer = ProjEuropeanPricer(model=model, N=2 ** 10) # Price a set of strikes @@ -136,6 +137,4 @@ plt.legend() plt.xlabel(r'strike, $K$') plt.ylabel('implied vol') plt.show() - - -``` +``` \ No newline at end of file diff --git a/examples/implied_vol_smiles.py b/examples/implied_vol_smiles.py index eb622d0..56b2d6b 100644 --- a/examples/implied_vol_smiles.py +++ b/examples/implied_vol_smiles.py @@ -4,6 +4,7 @@ from fypy.pricing.fourier.ProjEuropeanPricer import ProjEuropeanPricer from fypy.model.levy.BlackScholes import * from fypy.termstructures.DiscountCurve import DiscountCurve_ConstRate +from fypy.termstructures.EquityForward import EquityForward from fypy.volatility.implied.ImpliedVolCalculator import ImpliedVolCalculator_Black76 from fypy.model.levy import * from fypy.model.sv.Heston import Heston @@ -34,7 +35,7 @@ 'CGMY': CMGY(C=0.05, G=4, M=10, Y=1.3, forwardCurve=fwd, discountCurve=disc_curve), 'MJD': MertonJD(sigma=0.15, lam=0.3, muj=-0.2, sigj=0.3, forwardCurve=fwd, discountCurve=disc_curve), 'KDE': KouJD(sigma=0.14, lam=2., p_up=0.3, eta1=20, eta2=15, forwardCurve=fwd, discountCurve=disc_curve), - 'BSM': BlackScholes(sigma=0.2, forwardCurve=fwd), + 'BSM': BlackScholes(sigma=0.2, forwardCurve=fwd, discountCurve=disc_curve), 'Hes': Heston(v_0=0.04, theta=0.04, kappa=0.1, sigma_v=0.5, rho=-0.5, forwardCurve=fwd, discountCurve=disc_curve), 'Bates': Bates(v_0=0.04, theta=0.04, kappa=0.1, sigma_v=0.5, rho=-0.5, lam=0.15, muj=-0.1, sigj=0.3, forwardCurve=fwd, discountCurve=disc_curve), diff --git a/fypy/market/loader/YahooFinanceSurfaceLoader.py b/fypy/market/loader/YahooFinanceSurfaceLoader.py index afc3c14..dff7048 100644 --- a/fypy/market/loader/YahooFinanceSurfaceLoader.py +++ b/fypy/market/loader/YahooFinanceSurfaceLoader.py @@ -57,7 +57,7 @@ def load_df_from_api(self, dfs = [] for tenor in all_tenors: - DF_calls, DF_puts = data.option_chain(tenor) + DF_calls, DF_puts, json_summary_data = data.option_chain(tenor) DF_calls['expiry'] = tenor DF_calls['isCall'] = True @@ -68,6 +68,8 @@ def load_df_from_api(self, dfs.append(DF_puts) df = pd.concat(dfs) + + df['ticker'] = ticker df['spot'] = spot df['date'] = date @@ -75,7 +77,6 @@ def load_df_from_api(self, df = df[df['volume'] >= volume_filter] df.dropna(how='any', subset=['bid', 'ask'], inplace=True) - df['ticker'] = ticker return df def load_from_frame(self, @@ -191,9 +192,21 @@ def _average_discount(spot: float, loader = YahooFinanceLoader() - df1 = loader.load_df_from_api(ticker=tick) + # fetch option chain from yahoo and store in df1 + df1 = loader.load_df_from_api(ticker=tick, volume_filter=0) df1.to_csv(fp, index=False) - df2 = loader.load_from_file(fpath=fp) + from fypy.termstructures.EquityForward import EquityForward, DiscountCurve_ConstRate + disc_curve = DiscountCurve_ConstRate(rate=0.02) + spot = df1.iloc[0]['spot'] + fwd = EquityForward(S0=spot, discount=disc_curve) + + # gen market surface from df1 + surf = loader.load_from_file(fpath=fp, disc_curve=disc_curve) + + # calc ivs + from fypy.volatility.implied.ImpliedVolCalculator import ImpliedVolCalculator_Black76 + ivc = ImpliedVolCalculator_Black76(fwd_curve=fwd, disc_curve=disc_curve) + surf.fill_implied_vols(calculator=ivc) # surf = loader.load_from_api(ticker=tick)