Skip to content

Commit

Permalink
Merge pull request #6 from westonplatter/examples-update-methods
Browse files Browse the repository at this point in the history
examples: fix some method signatures, update yahoo logic, calc yahoo data IVs
  • Loading branch information
jkirkby3 authored Oct 17, 2023
2 parents 8e4392a + ef4e627 commit 039f9c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -136,6 +137,4 @@ plt.legend()
plt.xlabel(r'strike, $K$')
plt.ylabel('implied vol')
plt.show()


```
```
3 changes: 2 additions & 1 deletion examples/implied_vol_smiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
21 changes: 17 additions & 4 deletions fypy/market/loader/YahooFinanceSurfaceLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -68,14 +68,15 @@ def load_df_from_api(self,
dfs.append(DF_puts)

df = pd.concat(dfs)

df['ticker'] = ticker
df['spot'] = spot
df['date'] = date

# Filter the data
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,
Expand Down Expand Up @@ -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)

0 comments on commit 039f9c8

Please sign in to comment.