Files
OpenBB/openbb_terminal/stocks/options/chartexchange_model.py
James Maslek 06d737f4be Adding intrinio and cleaning up options a bit more (#4127)
* Add intrinio load functions into stocks and options.  First try at options sdk with intrinio

* Add a new eodchain function + add intrinio model test

* Unit tests for intrinio options

* dont add eodchain yet -- wait for other PR

* Make null handling better + add intrinio sdk to reqs

* mock dates

* update test

* fix ruff

* black and gitignore

* Clean up and make options load class

* Clean up clean up

* Fix intrinio stock load

* Get requirements even

* sort intrinio chain and fix

* Dont rename the candle input argument

* Fix bug #4060

* Historical option prices

* Add grhist from intrinio and isort

* Fix things for presentation

* Fix tradier chain printing

* YahooFInance chain cleanup

* standardize nasdaq

* Format intrinio

* Cleanup SDK

* Fix tests

* mypy

* Clean up trail map and edit docs

* Fix mapping of info on sdk

* Add intrinio to deps

* Add a rich progress bar to expirations :)

* controller test

* Silly error + tests

* Required arg

* bad assert statememts

* Mutable default

* Keys and switch another tqdm

* BizIns Tests

* tests

* Last Test
2023-02-10 16:54:05 +00:00

84 lines
2.7 KiB
Python

"""Chartexchange model"""
import logging
from typing import Union
import pandas as pd
from bs4 import BeautifulSoup
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import get_user_agent, request
from openbb_terminal.rich_config import console
from openbb_terminal.stocks.options.op_helpers import convert
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def get_option_history(
symbol: str = "GME",
date: str = "2021-02-05",
call: bool = True,
price: Union[str, Union[int, float]] = "90",
chain_id: str = None,
) -> pd.DataFrame:
"""Historic prices for a specific option [chartexchange]
Parameters
----------
symbol : str
Ticker symbol to get historical data from
date : str
Date as a string YYYYMMDD
call : bool
Whether to show a call or a put
price : Union[str, Union[int, float]]
Strike price for a specific option
chain_id: str
OCC option symbol. Overwrites other inputs
Returns
-------
historical : pd.Dataframe
Historic information for an option
"""
if chain_id is None:
url = f"https://chartexchange.com/symbol/opra-{symbol.lower()}{date.replace('-', '')}"
url += f"{'c' if call else 'p'}{float(price):g}/historical/"
else:
url = f"https://chartexchange.com/symbol/opra-{chain_id}/historical/"
data = request(url, headers={"User-Agent": get_user_agent()}).content
soup = BeautifulSoup(data, "html.parser")
table = soup.find("div", attrs={"style": "display: table; font-size: 0.9em; "})
if table:
rows = table.find_all("div", attrs={"style": "display: table-row;"})
else:
return pd.DataFrame()
clean_rows = []
if rows:
for row in rows[1:]:
item = row.find_all("div")
clean_rows.append([x.text for x in item])
else:
if chain_id is not None:
console.print(
f"No data for {chain_id}. Check that you are using OPRA notation.\n"
)
return pd.DataFrame()
console.print("No data for this option\n")
return pd.DataFrame()
df = pd.DataFrame()
df["Date"] = [x[0] for x in clean_rows]
df["Open"] = [convert(x[2], ",") for x in clean_rows]
df["High"] = [convert(x[3], ",") for x in clean_rows]
df["Low"] = [convert(x[4], ",") for x in clean_rows]
df["Close"] = [convert(x[5], ",") for x in clean_rows]
df["Change"] = [convert(x[6], "%") for x in clean_rows]
df["Volume"] = [convert(x[7], ",") for x in clean_rows]
df["Open Interest"] = [convert(x[8], ",") for x in clean_rows]
df["Change Since"] = [convert(x[9], "%") for x in clean_rows]
return df