Skip to content

Commit 1c885cb

Browse files
committed
black
1 parent 55dc866 commit 1c885cb

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

  • openbb_platform/providers/yfinance/openbb_yfinance/utils

openbb_platform/providers/yfinance/openbb_yfinance/utils/helpers.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ async def get_defined_screener(
157157
from pytz import timezone
158158

159159
if name and name not in PREDEFINED_SCREENERS:
160-
raise ValueError(f"Invalid predefined screener name: {name}\n Valid names: {PREDEFINED_SCREENERS}")
160+
raise ValueError(
161+
f"Invalid predefined screener name: {name}\n Valid names: {PREDEFINED_SCREENERS}"
162+
)
161163

162164
results: list = []
163165

@@ -271,12 +273,16 @@ async def get_futures_quotes(symbols: list) -> "DataFrame":
271273

272274
@contextmanager
273275
def suppress_all_output():
274-
with open(os.devnull, "w") as devnull, redirect_stdout(devnull), redirect_stderr(devnull):
276+
with open(os.devnull, "w") as devnull, redirect_stdout(
277+
devnull
278+
), redirect_stderr(devnull):
275279
yield
276280

277281
with suppress_all_output(), suppress(ClientError):
278282
fetcher = YFinanceEquityQuoteFetcher()
279-
data = await fetcher.fetch_data(params={"symbol": ",".join(symbols)}, credentials={})
283+
data = await fetcher.fetch_data(
284+
params={"symbol": ",".join(symbols)}, credentials={}
285+
)
280286

281287
df = DataFrame([d.model_dump() for d in data]) # type: ignore
282288
prices = df[["symbol", "bid", "ask", "prev_close"]].copy()
@@ -287,7 +293,9 @@ def suppress_all_output():
287293
return prices[["expiration", "price"]] # type: ignore
288294

289295

290-
async def get_historical_futures_prices(symbols: list, start_date: "date", end_date: "date"):
296+
async def get_historical_futures_prices(
297+
symbols: list, start_date: "date", end_date: "date"
298+
):
291299
"""Get historical futures prices for the list of symbols."""
292300
# pylint: disable=import-outside-toplevel
293301
from openbb_yfinance.models.equity_historical import ( # noqa
@@ -344,7 +352,9 @@ async def get_futures_curve( # pylint: disable=too-many-return-statements
344352
return futures_quotes
345353

346354
if dates and futures_symbols:
347-
historical_futures_prices = await get_historical_futures_prices(futures_symbols, dates[0], dates[-1])
355+
historical_futures_prices = await get_historical_futures_prices(
356+
futures_symbols, dates[0], dates[-1]
357+
)
348358
df = DataFrame([d.model_dump() for d in historical_futures_prices]) # type: ignore
349359
df = df.set_index("date").sort_index()
350360
df.index = df.index.astype(str)
@@ -365,14 +375,18 @@ async def get_futures_curve( # pylint: disable=too-many-return-statements
365375
df = df.fillna("N/A").replace("N/A", None)
366376

367377
# Flatten the DataFrame
368-
flattened_data = df.reset_index().melt(id_vars="date", var_name="expiration", value_name="price")
378+
flattened_data = df.reset_index().melt(
379+
id_vars="date", var_name="expiration", value_name="price"
380+
)
369381
flattened_data = flattened_data.sort_values("date")
370382
flattened_data["expiration"] = Categorical(
371383
flattened_data["expiration"],
372384
categories=sorted(list(expiration_dict.values())),
373385
ordered=True,
374386
)
375-
flattened_data = flattened_data.sort_values(by=["date", "expiration"]).reset_index(drop=True)
387+
flattened_data = flattened_data.sort_values(
388+
by=["date", "expiration"]
389+
).reset_index(drop=True)
376390
flattened_data["date"] = flattened_data["date"].dt.strftime("%Y-%m-%d")
377391

378392
return flattened_data
@@ -401,13 +415,17 @@ async def get_futures_curve( # pylint: disable=too-many-return-statements
401415

402416
@contextmanager
403417
def suppress_all_output():
404-
with open(os.devnull, "w") as devnull, redirect_stdout(devnull), redirect_stderr(devnull):
418+
with open(os.devnull, "w") as devnull, redirect_stdout(
419+
devnull
420+
), redirect_stderr(devnull):
405421
yield
406422

407423
with suppress_all_output():
408424
while empty_count < 12:
409425
future = today + relativedelta(months=i)
410-
future_symbol = f"{symbol}{MONTHS[future.month]}{str(future.year)[-2:]}.{exchange}"
426+
future_symbol = (
427+
f"{symbol}{MONTHS[future.month]}{str(future.year)[-2:]}.{exchange}"
428+
)
411429
data = yf_download(future_symbol)
412430
if data.empty:
413431
empty_count += 1
@@ -428,7 +446,9 @@ def suppress_all_output():
428446
historical_curve.append(None)
429447
else:
430448
futures_index.append(future.strftime("%Y-%m"))
431-
futures_curve.append(data.query("close.notnull()")["close"].values[-1])
449+
futures_curve.append(
450+
data.query("close.notnull()")["close"].values[-1]
451+
)
432452

433453
i += 1
434454

@@ -546,7 +566,9 @@ def yf_download( # pylint: disable=too-many-positional-arguments
546566
temp = data[ticker].copy().dropna(how="all") # type: ignore
547567
if len(temp) > 0:
548568
temp["symbol"] = ticker
549-
temp = temp.reset_index().rename(columns={"Date": "date", "Datetime": "date", "index": "date"})
569+
temp = temp.reset_index().rename(
570+
columns={"Date": "date", "Datetime": "date", "index": "date"}
571+
)
550572
_data = concat([_data, temp])
551573
if not _data.empty:
552574
index_keys = ["date", "symbol"] if "symbol" in _data.columns else "date"

0 commit comments

Comments
 (0)