Add performance table on load (#1724)

This commit is contained in:
jmaslek
2022-04-21 08:28:10 -04:00
committed by GitHub
parent 893e68efdc
commit 15501940e5
12 changed files with 100056 additions and 199 deletions

View File

@@ -529,6 +529,8 @@ class StockBaseController(BaseController, metaclass=ABCMeta):
ns_parser.ticker
)
console.print(self.add_info)
if ns_parser.interval == 1440 and ns_parser.filepath is None:
stocks_helper.show_quick_performance(self.stock, ns_parser.ticker)
if "." in ns_parser.ticker:
self.ticker, self.suffix = ns_parser.ticker.upper().split(".")
else:

View File

@@ -4,7 +4,7 @@ __docformat__ = "numpy"
import argparse
import logging
import os
from datetime import datetime, timedelta
from datetime import datetime, timedelta, date
from typing import List, Union, Optional, Iterable
import financedatabase as fd
@@ -967,10 +967,9 @@ def additional_info_about_ticker(ticker: str) -> str:
"""
extra_info = ""
if ticker:
ticker_info = yf.Ticker(ticker).info
# outside US exchange
if "." in ticker:
ticker_info = yf.Ticker(ticker).info
extra_info += "\n[param]Datetime: [/param]"
if (
"exchangeTimezoneName" in ticker_info
@@ -1019,6 +1018,9 @@ def additional_info_about_ticker(ticker: str) -> str:
extra_info += "OPEN"
else:
extra_info += "CLOSED"
if "shortName" in ticker_info and ticker_info["shortName"]:
extra_info += ticker_info["shortName"]
else:
extra_info += "\n[param]Datetime: [/param]"
dtime = datetime.now(pytz.timezone("America/New_York")).strftime(
@@ -1044,12 +1046,16 @@ def additional_info_about_ticker(ticker: str) -> str:
else:
extra_info += "CLOSED"
extra_info += "\n[param]Company: [/param]"
if "shortName" in ticker_info and ticker_info["shortName"]:
extra_info += ticker_info["shortName"]
else:
extra_info += "\n[param]Datetime: [/param]"
extra_info += "\n[param]Timezone: [/param]"
extra_info += "\n[param]Exchange: [/param]"
extra_info += "\n[param]Market: [/param]"
extra_info += "\n[param]Currency: [/param]"
extra_info += "\n[param]Company: [/param]"
return extra_info + "\n"
@@ -1128,3 +1134,43 @@ def clean_function(entry: str) -> Union[str, float]:
if any(char.isdigit() for char in entry):
return float(entry.replace("$", "").replace(",", ""))
return entry
def show_quick_performance(stock_df: pd.DataFrame, ticker: str):
"""Show quick performance stats of stock prices. Daily prices expected"""
closes = stock_df["Adj Close"]
volumes = stock_df["Volume"]
perfs = {
"1 Day": 100 * closes.pct_change(2)[-1],
"1 Week": 100 * closes.pct_change(5)[-1],
"1 Month": 100 * closes.pct_change(21)[-1],
"1 Year": 100 * closes.pct_change(252)[-1],
}
if "2022-01-03" in closes.index:
closes_ytd = closes[closes.index > f"{date.today().year}-01-01"]
perfs["YTD"] = 100 * (closes_ytd[-1] - closes_ytd[0]) / closes_ytd[0]
else:
perfs["Period"] = 100 * (closes[-1] - closes[0]) / closes[0]
df = pd.DataFrame.from_dict(perfs, orient="index").dropna().T
df = df.applymap(lambda x: str(round(x, 2)) + " %")
df = df.applymap(lambda x: f"[red]{x}[/red]" if "-" in x else f"[green]{x}[/green]")
if len(closes) > 252:
df["Volatility (1Y)"] = (
str(round(100 * np.sqrt(252) * closes[:-252].pct_change().std(), 2)) + " %"
)
else:
df["Volatility (Ann)"] = (
str(round(100 * np.sqrt(252) * closes.pct_change().std(), 2)) + " %"
)
if len(volumes) > 10:
df["Volume (10D avg)"] = (
str(round(np.mean(volumes[-12:-2]) / 1_000_000, 2)) + " M"
)
df["Last Price"] = closes[-1]
print_rich_table(
df, show_index=False, headers=df.columns, title=f"{ticker.upper()} Performance"
)
console.print()

View File

@@ -9,4 +9,3 @@
7 2021-04-01 4 6 15 8 9
8 2021-03-01 4 6 14 8 9
9 2021-02-01 4 6 15 7 9

View File

@@ -4,6 +4,7 @@ import os
# IMPORTATION THIRDPARTY
import pandas as pd
import pytest
import yfinance
# IMPORTATION INTERNAL
from openbb_terminal.stocks.insider import insider_controller
@@ -602,9 +603,13 @@ def test_call_func_no_stock(func):
def test_call_load(mocker):
# MOCK LOAD
target = "openbb_terminal.parent_classes.stocks_helper.load"
mocker.patch(target=target, return_value=DF_STOCK)
yf_download = yfinance.download
def mock_yf_download(*args, **kwargs):
kwargs["threads"] = False
return yf_download(*args, **kwargs)
mocker.patch("yfinance.download", side_effect=mock_yf_download)
controller = insider_controller.InsiderController(
ticker=None,
start="2021-10-25",

View File

@@ -1,4 +1,3 @@
From 2022-01-04 to 2019-09-16
Pattern: Double Bottom (bullish)

View File

@@ -9,6 +9,7 @@ Timezone:
Exchange:
Market:
Currency:
Company:
quote view the current price for a specific stock ticker
candle view a candle chart for a specific stock ticker