Add Vsurf Z-index options (#1207)

* Added z index options

* Dider changes
This commit is contained in:
Colin Delahunty
2022-01-18 08:23:03 -05:00
committed by GitHub
parent c568747ab4
commit b5a95a3ed2
5 changed files with 35 additions and 13 deletions

View File

@@ -191,8 +191,6 @@ Select one of the following reports:[/info][cmds]
d_report_params["report_name"] = notebook_output
print(notebook_template)
pm.execute_notebook(
notebook_template + ".ipynb",
notebook_output + ".ipynb",

View File

@@ -1100,12 +1100,23 @@ Expiry: [/param]{self.selected_date or None}
prog="vsurf",
description="Plot 3D volatility surface.",
)
parser.add_argument(
"-z",
"--z-axis",
default="IV",
dest="z",
choices=["IV", "OI", "LP"],
type=str,
help="The data for the Z axis",
)
ns_parser = parse_known_args_and_warn(
parser, other_args, export_allowed=EXPORT_ONLY_FIGURES_ALLOWED
)
if ns_parser:
yfinance_view.display_vol_surface(self.ticker, export=ns_parser.export)
yfinance_view.display_vol_surface(
self.ticker, export=ns_parser.export, z=ns_parser.z
)
def call_parity(self, other_args: List[str]):
"""Process parity command"""

View File

@@ -193,11 +193,12 @@ def get_iv_surface(ticker: str) -> pd.DataFrame:
stock = yf.Ticker(ticker)
dates = stock.options
vol_df = pd.DataFrame()
columns = ["strike", "impliedVolatility", "openInterest", "lastPrice"]
for date in dates:
df = stock.option_chain(date).calls[["strike", "impliedVolatility"]]
df = stock.option_chain(date).calls[columns]
df["dte"] = get_dte(date)
vol_df = pd.concat([vol_df, df], axis=0)
df = stock.option_chain(date).puts[["strike", "impliedVolatility"]]
df = stock.option_chain(date).puts[columns]
df["dte"] = get_dte(date)
vol_df = pd.concat([vol_df, df], axis=0)
return vol_df

View File

@@ -934,15 +934,17 @@ def show_binom(
)
def display_vol_surface(ticker: str, export: str = ""):
def display_vol_surface(ticker: str, export: str = "", z: str = "IV"):
"""Display vol surface
Parameters
----------
ticker: str
ticker : str
Ticker to get surface for
export: str
export : str
Format to export data
z : str
The variable for the Z axis
"""
data = yfinance_model.get_iv_surface(ticker)
@@ -951,15 +953,23 @@ def display_vol_surface(ticker: str, export: str = ""):
return
X = data.dte
Y = data.strike
Z = data.impliedVolatility
if z == "IV":
Z = data.impliedVolatility
label = "Volatility"
elif z == "OI":
Z = data.openInterest
label = "Open Interest"
elif z == "LP":
Z = data.lastPrice
label = "Last Price"
fig = plt.figure()
ax = plt.axes(projection="3d")
ax.plot_trisurf(X, Y, Z, cmap="jet", linewidth=0.2)
ax.set_xlabel("DTE")
ax.set_ylabel("Strike")
ax.set_zlabel("IV")
ax.set_zlabel(z)
fig.tight_layout()
fig.suptitle(f"Volatility Surface for {ticker}")
fig.suptitle(f"{label} Surface for {ticker}")
if gtff.USE_ION:
plt.ion()
plt.show()
@@ -969,4 +979,4 @@ def display_vol_surface(ticker: str, export: str = ""):
"vsurf",
data,
)
print("")
console.print("")

View File

@@ -1,5 +1,5 @@
```
usage: vsurf [-h] [--export {png,jpg,pdf,svg}]
usage: vsurf [-h] [-z {IV,OI,LP}] [--export {png,jpg,pdf,svg}]
```
Plot 3D volatility surface.
@@ -7,6 +7,8 @@ Plot 3D volatility surface.
```
optional arguments:
-h, --help show this help message (default: False)
-z {IV,OI,LP}, --z-axis {IV,OI,LP}
variable to be used as the z-axis (default: IV)
--export {png,jpg,pdf,svg}
Export figure into png, jpg, pdf, svg (default: )
```