Files
OpenBB/openbb_terminal/futures/yfinance_model.py
James Maslek e7185b10f3 merge the release changes into develop (#4016)
* Fix plot look when using eval and add query to choices (#3881)

* convert index to datetime and update choices

* uncomment economy integration test

* fix treasury concat bug

* fix datasets concat on duplciates

* Lock ruff version so that new lints dont break our CI (#3905)

* Lock ruff version so that new lints dont break our CI

* Bumped pre-commit ruff version

* bump

* Update release branch

* revert conda yaml

* logging hash

* Update version number

* Added scipy to intel

* Add xgboost to intel

* add scipy libs to windows only

* add scipy sparse

* update scipy and pywin

* update tests

* UPdate installation.md to reflect 2.2

* fix yf unit tests

* Try to get futures tests working

* Ignore tz in yf download

* Found another end date needed

* delelte yf cache

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
2023-01-19 11:41:39 -05:00

196 lines
4.9 KiB
Python

"""Yahoo Finance model"""
__docformat__ = "numpy"
import os
import sys
import logging
from typing import List, Optional
from datetime import datetime, timedelta
import yfinance as yf
import pandas as pd
from dateutil.relativedelta import relativedelta
from openbb_terminal.rich_config import console
from openbb_terminal.decorators import log_start_end
from openbb_terminal.core.config.paths import MISCELLANEOUS_DIRECTORY
# pylint: disable=attribute-defined-outside-init
logger = logging.getLogger(__name__)
FUTURES_DATA = pd.read_csv(MISCELLANEOUS_DIRECTORY / "futures" / "futures.csv")
MONTHS = {
1: "F",
2: "G",
3: "H",
4: "J",
5: "K",
6: "M",
7: "N",
8: "Q",
9: "U",
10: "V",
11: "X",
12: "Z",
}
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
@log_start_end(log=logger)
def get_search_futures(
category: str = "",
exchange: str = "",
description: str = "",
):
"""Get search futures [Source: Yahoo Finance]
Parameters
----------
category: str
Select the category where the future exists
exchange: str
Select the exchange where the future exists
description: str
Select the description where the future exists
"""
df = FUTURES_DATA
if category:
df = df[df["Category"] == category]
if exchange:
df = df[df["Exchange"] == exchange]
if description:
df = df[
[description.lower() in desc.lower() for desc in df["Description"].values]
]
return df
@log_start_end(log=logger)
def get_historical_futures(
symbols: List[str],
expiry: str = "",
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> pd.DataFrame:
"""Get historical futures [Source: Yahoo Finance]
Parameters
----------
symbols: List[str]
List of future timeseries symbols to display
expiry: str
Future expiry date with format YYYY-MM
start_date: Optional[str]
Start date of the historical data with format YYYY-MM-DD
end_date: Optional[str]
End date of the historical data with format YYYY-MM-DD
Returns
-------
pd.DataFrame
Dictionary with sector weightings allocation
"""
if start_date is None:
start_date = (datetime.now() - timedelta(days=3 * 365)).strftime("%Y-%m-%d")
if end_date is None:
end_date = datetime.now().strftime("%Y-%m-%d")
if start_date >= end_date:
console.print("[yellow]Start date must be before end date.[/yellow]")
return pd.DataFrame()
if expiry:
symbols_with_expiry = list()
for symbol in symbols:
expiry_date = datetime.strptime(expiry, "%Y-%m")
exchange = FUTURES_DATA[FUTURES_DATA["Ticker"] == symbol][
"Exchange"
].values[0]
symbols_with_expiry.append(
f"{symbol}{MONTHS[expiry_date.month]}{str(expiry_date.year)[-2:]}.{exchange}"
)
return yf.download(
symbols_with_expiry,
start=start_date,
end=end_date,
progress=False,
period="max",
ignore_tz=True,
)
df = yf.download(
[t + "=F" for t in symbols],
start=start_date,
end=end_date,
progress=False,
period="max",
ignore_tz=True,
)
if len(symbols) > 1:
df.columns = pd.MultiIndex.from_tuples(
[(tup[0], tup[1].replace("=F", "")) for tup in df.columns]
)
return df
@log_start_end(log=logger)
def get_curve_futures(
symbol: str = "",
) -> pd.DataFrame:
"""Get curve futures [Source: Yahoo Finance]
Parameters
----------
symbol: str
symbol to get forward curve
Returns
-------
pd.DataFrame
Dictionary with sector weightings allocation
"""
if symbol not in FUTURES_DATA["Ticker"].unique().tolist():
return pd.DataFrame()
exchange = FUTURES_DATA[FUTURES_DATA["Ticker"] == symbol]["Exchange"].values[0]
today = datetime.today()
futures_index = list()
futures_curve = list()
for i in range(36):
future = today + relativedelta(months=i)
future_symbol = (
f"{symbol}{MONTHS[future.month]}{str(future.year)[-2:]}.{exchange}"
)
with HiddenPrints():
data = yf.download(future_symbol, progress=False, ignore_tz=True)
if not data.empty:
futures_index.append(future.strftime("%Y-%b"))
futures_curve.append(data["Adj Close"].values[-1])
if not futures_index:
return pd.DataFrame()
futures_index = pd.to_datetime(futures_index)
return pd.DataFrame(index=futures_index, data=futures_curve, columns=["Futures"])