Files
OpenBB/openbb_terminal/stocks/options/tradier_model.py
montezdesousa 475ebb5acd [WIP] Migrate user, feature flags and configurations to dataclass (#4309)
* implement user and change is guest

* replace User methods

* move remaining functions to user

* fix bug

* portfolio test

* fix tests

* fix tests

* uncomment skip

* ruff

* start credentials model implementation

* change deault structure

* refacto quandl

* change default struct and file names

* bug

* start using global user

* add comments

* move update flair

* change cfg references in keys model

* reddit & fmp

* refactore additional keys

* remaing config keys

* remove unsude imports and fix hub keys

* ruff

* fix line too long

* unblock unittests

* fix keys model tests

* remove os patch

* fix account controller tests

* fix account controller tests

* fix account model

* account fixed

* fix user tests

* User Model

* User Model

* User Model

* Move session folder

* User Model : grouping the models

* User Model : fix bugs

* Config Plot + bug fix

* User Model : Refactoring plots

* Feature flags

* Remove dup

* clean comments a fix some vars

* fix feature flags controller

* refactor obbff from settings controller

* refactor a bunch of obbff variables

* fix bug blocking terminal

* fix integration tests

* refactor controllers with obbff

* refactor remaining ffs

* remove comments

* log not started

* mispelled variable

* set current user on local model

* User Model : moving files + updating

* User Preferences

* User : Refactor is_guest function + bug fix

* User : refactor is_guest function

* Fix conftest.py

* remove lost obbffs

* typo on fileoverwrite

* Plot config: move variables and start plot dpi

* User : tests

* refactor plot dpi references

* remove plot dpi imports

* refactor some config terminal leftover

* /account/sync : fix bug

* remove configs reload

* start path refactor

* installation paths vs user paths

* remove cfg_plot

* Tests : User Refactoring

* user prefix only for preferences

* user prefix only for preferences

* move user paths

* remove comment

* update apply remote configs

* fix tz not updating

* save some copys

* small fix

* create interface for credentials and preferences

* wrong docstrings fixed

* ruff core

* fix some pref setting

* login is better

* ruff

* ruff

* remove Literal

* add small comment

* Tests : User Refactoring

* Credentials Model : add DataBento

* Fix helper_funcs

* Fixing code

* Tests : fixing

* User Model : freezing the models

* Fixed Income + Databento

* Tests : fixing keys

* black

* ruff

* Tests : user model

* Tests : user model

* Fix linting

* Fix linting

* Linting

* Linting

* Fixing code and tests

* Fix tests

* mypy

* SDK

* Clean ruff cache

* Fix syntax issue

* Update fred_view

* Black

* Linting

* Update code

* Update tests

* Linting

* Linting

* Tests

* Black

* Linting

* Linting

* Mock os.listdir to avoid looking for file that doesnt exist

* Mock options paths

* lint

* Tests : fix

* Code : update

* Tests + Linting

* Tests

* Ruff

* Code : update

* Tests

* Code : update

* Fix workflow

* Code : update

---------

Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2023-03-05 00:37:19 +01:00

323 lines
8.1 KiB
Python

"""Tradier options model"""
__docformat__ = "numpy"
import logging
from typing import List, Optional
import pandas as pd
import requests
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.helper_funcs import request
from openbb_terminal.rich_config import console, optional_rich_track
logger = logging.getLogger(__name__)
option_columns = [
"symbol",
"bid",
"ask",
"strike",
"bidsize",
"asksize",
"volume",
"open_interest",
"option_type",
]
greek_columns = ["delta", "gamma", "theta", "vega", "ask_iv", "bid_iv", "mid_iv"]
df_columns = option_columns + greek_columns
default_columns = [
"mid_iv",
"vega",
"delta",
"gamma",
"theta",
"volume",
"open_interest",
"bid",
"ask",
]
sorted_chain_columns = [
"symbol",
"option_type",
"expiration",
"strike",
"bid",
"ask",
"open_interest",
"volume",
"mid_iv",
"delta",
"gamma",
"theta",
"vega",
]
@log_start_end(log=logger)
@check_api_key(["API_TRADIER_TOKEN"])
def get_historical_options(
symbol: str,
expiry: str,
strike: float = 0,
put: bool = False,
chain_id: Optional[str] = None,
) -> pd.DataFrame:
"""
Gets historical option pricing. This inputs either ticker, expiration, strike or the OCC chain ID and processes
the request to tradier for historical premiums.
Parameters
----------
symbol: str
Stock ticker symbol
expiry: str
Option expiration date
strike: int
Option strike price
put: bool
Is this a put option?
chain_id: Optional[str]
OCC chain ID
Returns
-------
df_hist: pd.DataFrame
Dataframe of historical option prices
"""
if not chain_id:
op_type = ["call", "put"][put]
chain = get_option_chain(symbol, expiry)
chain = chain[chain["option_type"] == op_type]
try:
symbol = chain[(chain.strike == strike)]["symbol"].values[0]
except IndexError:
error = f"Strike: {strike}, Option type: {op_type} not found"
logging.exception(error)
console.print(f"{error}\n")
return pd.DataFrame()
else:
symbol = chain_id
response = request(
"https://sandbox.tradier.com/v1/markets/history",
params={"symbol": {symbol}, "interval": "daily"},
headers={
"Authorization": f"Bearer {get_current_user().credentials.API_TRADIER_TOKEN}",
"Accept": "application/json",
},
)
if response.status_code != 200:
console.print("Error with request")
return pd.DataFrame()
data = response.json()["history"]
if not data:
console.print("No historical data available")
return pd.DataFrame()
df_hist = pd.DataFrame(data["day"])
df_hist = df_hist.set_index("date")
df_hist.index = pd.DatetimeIndex(df_hist.index)
return df_hist
# pylint: disable=no-else-return
option_cols = [
"strike",
"bid",
"ask",
"volume",
"open_interest",
"mid_iv",
]
option_col_map = {"open_interest": "openinterest", "mid_iv": "iv"}
@log_start_end(log=logger)
@check_api_key(["API_TRADIER_TOKEN"])
def get_full_option_chain(symbol: str, quiet: bool = False) -> pd.DataFrame:
"""Get available expiration dates for given ticker
Parameters
----------
symbol: str
Ticker symbol to get expirations for
quiet: bool
Suppress output of progress bar
Returns
-------
pd.DataFrame
Dataframe of all option chains
"""
expirations = option_expirations(symbol)
options_dfs: pd.DataFrame = []
for expiry in optional_rich_track(
expirations, suppress_output=quiet, desc="Getting Option Chain"
):
chain = get_option_chain(symbol, expiry)
options_dfs.append(chain)
chain = pd.concat(options_dfs)
chain = chain[sorted_chain_columns].rename(
columns={
"mid_iv": "iv",
"open_interest": "openInterest",
"option_type": "optionType",
}
)
return chain
@log_start_end(log=logger)
@check_api_key(["API_TRADIER_TOKEN"])
def option_expirations(symbol: str) -> List[str]:
"""Get available expiration dates for given ticker
Parameters
----------
symbol: str
Ticker symbol to get expirations for
Returns
-------
dates: List[str]
List of of available expirations
"""
r = request(
"https://sandbox.tradier.com/v1/markets/options/expirations",
params={"symbol": symbol, "includeAllRoots": "true", "strikes": "false"},
headers={
"Authorization": f"Bearer {get_current_user().credentials.API_TRADIER_TOKEN}",
"Accept": "application/json",
},
)
if r.status_code == 200:
try:
dates = r.json()["expirations"]["date"]
return dates
except TypeError:
logging.exception("Error in tradier JSON response. Check loaded ticker.")
console.print("Error in tradier JSON response. Check loaded ticker.\n")
return []
else:
console.print("Tradier request failed. Check token. \n")
return []
@log_start_end(log=logger)
@check_api_key(["API_TRADIER_TOKEN"])
def get_option_chain(symbol: str, expiry: str) -> pd.DataFrame:
"""Display option chains [Source: Tradier]"
Parameters
----------
symbol : str
Ticker to get options for
expiry : str
Expiration date in the form of "YYYY-MM-DD"
Returns
-------
chains: pd.DataFrame
Dataframe with options for the given Symbol and Expiration date
"""
params = {"symbol": symbol, "expiration": expiry, "greeks": "true"}
headers = {
"Authorization": f"Bearer {get_current_user().credentials.API_TRADIER_TOKEN}",
"Accept": "application/json",
}
response = request(
"https://sandbox.tradier.com/v1/markets/options/chains",
params=params,
headers=headers,
)
if response.status_code != 200:
console.print("Error in request. Check API_TRADIER_TOKEN\n")
return pd.DataFrame()
chains = process_chains(response)
chains["expiration"] = expiry
return chains
@log_start_end(log=logger)
def process_chains(response: requests.models.Response) -> pd.DataFrame:
"""Function to take in the request and return a DataFrame
Parameters
----------
response: requests.models.Response
This is the response from tradier api.
Returns
-------
opt_chain: pd.DataFrame
Dataframe with all available options
"""
json_response = response.json()
options = json_response["options"]["option"]
opt_chain = pd.DataFrame(columns=df_columns)
for idx, option in enumerate(options):
# initialize empty dictionary
d = {col: "" for col in df_columns}
# populate main dictionary values
for col in option_columns:
if col in option:
d[col] = option[col]
# populate greek dictionary values
if option["greeks"]:
for col in greek_columns:
if col in option["greeks"]:
d[col] = option["greeks"][col]
opt_chain.loc[idx, :] = d
return opt_chain
@log_start_end(log=logger)
@check_api_key(["API_TRADIER_TOKEN"])
def get_last_price(symbol: str):
"""Makes api request for last price
Parameters
----------
symbol: str
Ticker symbol
Returns
-------
float:
Last price
"""
r = request(
"https://sandbox.tradier.com/v1/markets/quotes",
params={"symbols": symbol, "includeAllRoots": "true", "strikes": "false"},
headers={
"Authorization": f"Bearer {get_current_user().credentials.API_TRADIER_TOKEN}",
"Accept": "application/json",
},
)
if r.status_code == 200:
last = r.json()["quotes"]["quote"]["last"]
if last is None:
return 0
return float(last)
else:
console.print("Error getting last price")
return None