Files
OpenBB/openbb_extensions/qa/openbb_qa/qa_helpers.py
Theodore Aptekarev 990e0e7209 Add TA QA and Charting extension code to V4 branch (#5197)
* Move TA extension code

* Move QA extension code

* Move charting extension code

* Exclude js and html assets from pre-commit secrets search

* Move static html and js code for the plotting infrastructure

* Remove security exclude rule that's not available on the CI
2023-07-04 14:35:48 +02:00

47 lines
1.5 KiB
Python

"""Helper functions for QA"""
from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
import pandas as pd
# ruff: ignore=S310
def get_fama_raw(start_date: str, end_date: str) -> pd.DataFrame:
"""Gets base Fama French data to calculate risk
Returns
-------
pd.DataFrame
A data with fama french model information
"""
with urlopen( # nosec
"https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip"
) as url:
# Download Zipfile and create pandas DataFrame
with ZipFile(BytesIO(url.read())) as zipfile:
with zipfile.open("F-F_Research_Data_Factors.CSV") as zip_open:
df = pd.read_csv(
zip_open,
header=0,
names=["Date", "MKT-RF", "SMB", "HML", "RF"],
skiprows=3,
)
df = df[df["Date"].apply(lambda x: len(str(x).strip()) == 6)]
df["Date"] = df["Date"].astype(str) + "01"
df["Date"] = pd.to_datetime(df["Date"], format="%Y%m%d")
df["MKT-RF"] = pd.to_numeric(df["MKT-RF"], downcast="float")
df["SMB"] = pd.to_numeric(df["SMB"], downcast="float")
df["HML"] = pd.to_numeric(df["HML"], downcast="float")
df["RF"] = pd.to_numeric(df["RF"], downcast="float")
df["MKT-RF"] = df["MKT-RF"] / 100
df["SMB"] = df["SMB"] / 100
df["HML"] = df["HML"] / 100
df["RF"] = df["RF"] / 100
df = df.set_index("Date")
df = df.loc[start_date:end_date] # type: ignore
return df