mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 20:42:26 +00:00
Volume comparison over time (#866)
* Add volume command to comparison analysis * volume added to hugo server website * Change xlabel to have Date instead of Time
This commit is contained in:
@@ -16,6 +16,7 @@ from gamestonk_terminal.helper_funcs import (
|
||||
check_non_negative,
|
||||
get_flair,
|
||||
parse_known_args_and_warn,
|
||||
try_except,
|
||||
)
|
||||
from gamestonk_terminal.menu import session
|
||||
from gamestonk_terminal.portfolio.portfolio_optimization import po_controller
|
||||
@@ -49,6 +50,7 @@ class ComparisonAnalysisController:
|
||||
"add",
|
||||
"historical",
|
||||
"hcorr",
|
||||
"volume",
|
||||
"income",
|
||||
"balance",
|
||||
"cashflow",
|
||||
@@ -128,7 +130,8 @@ Get Similar:
|
||||
getfinviz get similar stocks from finviz API
|
||||
{Style.DIM if all_loaded else Style.NORMAL}Yahoo Finance:
|
||||
historical historical price data comparison
|
||||
hcorr historical price correlation {Style.RESET_ALL}
|
||||
hcorr historical price correlation
|
||||
volume historical volume data comparison {Style.RESET_ALL}
|
||||
Market Watch:
|
||||
income income financials comparison
|
||||
balance balance financials comparison
|
||||
@@ -439,7 +442,7 @@ Finviz:
|
||||
help="Candle data to use: o-open, h-high, l-low, c-close, a-adjusted close.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"-n",
|
||||
"--no-scale",
|
||||
action="store_false",
|
||||
dest="no_scale",
|
||||
@@ -575,6 +578,55 @@ Finviz:
|
||||
except Exception as e:
|
||||
print(e, "\n")
|
||||
|
||||
@try_except
|
||||
def call_volume(self, other_args: List[str]):
|
||||
"""Process volume command"""
|
||||
parser = argparse.ArgumentParser(
|
||||
add_help=False,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
prog="volume",
|
||||
description="""Historical volume comparison between similar companies.
|
||||
""",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--scale",
|
||||
action="store_true",
|
||||
dest="scale",
|
||||
default=False,
|
||||
help="Flag to not put all prices on same 0-1 scale",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--export",
|
||||
choices=["csv", "json", "xlsx"],
|
||||
default="",
|
||||
type=str,
|
||||
dest="export",
|
||||
help="Export dataframe data to csv,json,xlsx file",
|
||||
)
|
||||
|
||||
if self.interval != "1440min":
|
||||
print("Intraday historical data analysis comparison is not yet available.")
|
||||
# Alpha Vantage only supports 5 calls per minute, we need another API to get intraday data
|
||||
else:
|
||||
ns_parser = parse_known_args_and_warn(parser, other_args)
|
||||
if not ns_parser:
|
||||
return
|
||||
|
||||
if not self.similar or not self.ticker:
|
||||
print(
|
||||
"Please make sure there are both a loaded ticker and similar tickers selected. \n"
|
||||
)
|
||||
return
|
||||
|
||||
yahoo_finance_view.display_volume(
|
||||
ticker=self.ticker,
|
||||
similar_tickers=self.similar,
|
||||
start=self.start,
|
||||
normalize=ns_parser.scale,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
def call_balance(self, other_args: List[str]):
|
||||
"""Process balance command"""
|
||||
parser = argparse.ArgumentParser(
|
||||
|
||||
@@ -19,6 +19,7 @@ d_candle_types = {
|
||||
"l": "Low",
|
||||
"c": "Close",
|
||||
"a": "Adj Close",
|
||||
"v": "Volume",
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ d_candle_types = {
|
||||
"l": "Low",
|
||||
"c": "Close",
|
||||
"a": "Adj Close",
|
||||
"v": "Volume",
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +37,7 @@ def display_historical(
|
||||
normalize: bool = True,
|
||||
export: str = "",
|
||||
):
|
||||
"""Display historical stock prices
|
||||
"""Display historical stock prices. [Source: Yahoo Finance]
|
||||
|
||||
Parameters
|
||||
----------
|
||||
@@ -86,6 +87,62 @@ def display_historical(
|
||||
print("")
|
||||
|
||||
|
||||
def display_volume(
|
||||
ticker: str,
|
||||
similar_tickers: List[str],
|
||||
start: str = (datetime.now() - timedelta(days=366)).strftime("%Y-%m-%d"),
|
||||
normalize: bool = True,
|
||||
export: str = "",
|
||||
):
|
||||
"""Display volume stock prices. [Source: Yahoo Finance]
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ticker : str
|
||||
Base ticker
|
||||
similar_tickers : List[str]
|
||||
List of similar tickers
|
||||
start : str, optional
|
||||
Start date of comparison, by default 1 year ago
|
||||
normalize : bool, optional
|
||||
Boolean to normalize all stock prices using MinMax defaults True
|
||||
export : str, optional
|
||||
Format to export historical prices, by default ""
|
||||
"""
|
||||
ordered_tickers = [ticker, *similar_tickers]
|
||||
df_similar = yahoo_finance_model.get_historical(ticker, similar_tickers, start, "v")
|
||||
# To plot with ticker first
|
||||
df_similar = df_similar[ordered_tickers]
|
||||
|
||||
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
|
||||
# This puts everything on 0-1 scale for visualizing
|
||||
if normalize:
|
||||
mm_scale = MinMaxScaler()
|
||||
df_similar = pd.DataFrame(
|
||||
mm_scale.fit_transform(df_similar),
|
||||
columns=df_similar.columns,
|
||||
index=df_similar.index,
|
||||
)
|
||||
else:
|
||||
df_similar = df_similar.div(1_000_000)
|
||||
|
||||
df_similar.plot(ax=ax)
|
||||
ax.set_title("Volume over time")
|
||||
# ax.plot(df_similar.index, df_similar[ticker].values/1_000_000)
|
||||
ax.set_xlabel("Date")
|
||||
ax.set_ylabel(f"{['','Normalized'][normalize]} Volume {['[K]',''][normalize]}")
|
||||
ax.grid(b=True, which="major", color="#666666", linestyle="-")
|
||||
# ensures that the historical data starts from same datapoint
|
||||
ax.set_xlim([df_similar.index[0], df_similar.index[-1]])
|
||||
plt.gcf().autofmt_xdate()
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
export_data(
|
||||
export, os.path.dirname(os.path.abspath(__file__)), "volume", df_similar
|
||||
)
|
||||
print("")
|
||||
|
||||
|
||||
def display_correlation(
|
||||
ticker: str,
|
||||
similar_tickers: List[str],
|
||||
|
||||
15
website/content/stocks/comparison_analysis/volume/_index.md
vendored
Executable file
15
website/content/stocks/comparison_analysis/volume/_index.md
vendored
Executable file
@@ -0,0 +1,15 @@
|
||||
```text
|
||||
usage: volume [-s] [--export {csv,json,xlsx}] [-h]
|
||||
```
|
||||
|
||||
Historical volume comparison between similar companies.
|
||||
|
||||
```
|
||||
optional arguments:
|
||||
-s, --scale Flag to not put all prices on same 0-1 scale (default: False)
|
||||
--export {csv,json,xlsx}
|
||||
Export dataframe data to csv,json,xlsx file (default: )
|
||||
-h, --help show this help message (default: False)
|
||||
```
|
||||
|
||||
<img width="1014" alt="Captura de ecrã 2021-03-20, às 09 10 53" src="https://user-images.githubusercontent.com/25267873/111865168-5373e480-895d-11eb-960f-b919e338ab83.png">
|
||||
2
website/data/menu/main.yml
vendored
2
website/data/menu/main.yml
vendored
@@ -315,6 +315,8 @@ main:
|
||||
ref: "/stocks/comparison_analysis/historical"
|
||||
- name: hcorr
|
||||
ref: "/stocks/comparison_analysis/hcorr"
|
||||
- name: volume
|
||||
ref: "/stocks/comparison_analysis/volume"
|
||||
- name: income
|
||||
ref: "/stocks/comparison_analysis/income"
|
||||
- name: balance
|
||||
|
||||
Reference in New Issue
Block a user