mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 20:42:26 +00:00
Create comparison analysis report (#993)
* Improve comparison analysis - yf model/view * similar analysis report
This commit is contained in:
369
gamestonk_terminal/reports/similar_analysis.ipynb
vendored
Normal file
369
gamestonk_terminal/reports/similar_analysis.ipynb
vendored
Normal file
@@ -0,0 +1,369 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "phantom-facility",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Import most generic modules\n",
|
||||
"import importlib\n",
|
||||
"import pathlib\n",
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"from datetime import datetime, timedelta\n",
|
||||
"import pandas as pd\n",
|
||||
"from IPython.display import display, Markdown\n",
|
||||
"import warnings\n",
|
||||
"warnings.filterwarnings('ignore')\n",
|
||||
"module_path = os.path.abspath(os.path.join('../..'))\n",
|
||||
"\n",
|
||||
"if module_path not in sys.path:\n",
|
||||
" sys.path.append(module_path)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "compact-count",
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"parameters"
|
||||
]
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Parameters that will be replaced when calling this notebook\n",
|
||||
"ticker=\"TSLA\"\n",
|
||||
"\n",
|
||||
"report_name=f\"{datetime.now().strftime('%Y%m%d_%H%M%S')}_comparison_{ticker.upper()}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "possible-constraint",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"display(Markdown(f\"# {ticker.upper()} - Comparison - {datetime.now().strftime('%Y/%m/%d %H:%M:%S')}\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b9eaca4a-a46d-45a3-9306-e80e74234f12",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_model, yahoo_finance_model\n",
|
||||
"\n",
|
||||
"finviz_similar = finviz_compare_model.get_similar_companies(ticker, [\"Sector\", \"Industry\", \"Country\"])[0]\n",
|
||||
"finviz_similar.remove(ticker)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ffea4266-e801-4a4e-8cac-3d60e735aa4e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Similar companies and descriptions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "21122245-457b-4347-b89f-d4aabb5bdc9a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.fundamental_analysis import yahoo_finance_model\n",
|
||||
"\n",
|
||||
"df_info = yahoo_finance_model.get_info(ticker)\n",
|
||||
"if \"Long business summary\" in df_info.index:\n",
|
||||
" summary = df_info.loc[\"Long business summary\"].values[0]\n",
|
||||
"if \"Website\" in df_info.index:\n",
|
||||
" website = df_info.loc[\"Website\"].values[0]\n",
|
||||
" \n",
|
||||
"if finviz_similar:\n",
|
||||
" print(f\"{ticker}: {website}\")\n",
|
||||
" print(summary)\n",
|
||||
" \n",
|
||||
" for symbol in finviz_similar:\n",
|
||||
" df_info = yahoo_finance_model.get_info(symbol)\n",
|
||||
" if \"Long business summary\" in df_info.index:\n",
|
||||
" summary = df_info.loc[\"Long business summary\"].values[0]\n",
|
||||
" if \"Website\" in df_info.index:\n",
|
||||
" website = df_info.loc[\"Website\"].values[0]\n",
|
||||
" \n",
|
||||
" print(\"\")\n",
|
||||
" print(f\"{symbol}: {website}\")\n",
|
||||
" print(summary)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "temporal-clerk",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Historical prices"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1fdfac4c-62cd-4bd3-b7df-41ba67263798",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import math\n",
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import yahoo_finance_view\n",
|
||||
"\n",
|
||||
"for i in range(math.ceil(len(finviz_similar)/4)):\n",
|
||||
" yahoo_finance_view.display_historical(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar_tickers=finviz_similar[4*(i):4*(i+1)],\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2a07f866-7939-4fb7-8b14-c0701087a8ae",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Historical correlation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "eb1850dc-1d62-4ad4-971f-1bbce6209726",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from matplotlib import pyplot as plt\n",
|
||||
"\n",
|
||||
"plt.figure(figsize=(25,10))\n",
|
||||
"yahoo_finance_view.display_correlation(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar_tickers=finviz_similar,\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "21005668-7f21-4884-a231-846130695602",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Historical volumes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a7bf528c-427c-4d84-8cb9-ec5656f920e1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for i in range(math.ceil(len(finviz_similar)/4)):\n",
|
||||
" yahoo_finance_view.display_volume(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar_tickers=finviz_similar[4*(i):4*(i+1)],\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6e370470-8014-4e97-95f2-13b4f66645c5",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Overview"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f82b7162-f7e5-4d5c-8ca9-50466f789f48",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"overview\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5277ae24-b7d7-48e6-b46c-fcffdf84631f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Valuation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "caed21ad-a31d-4063-a4cb-f5b6fd1c63ed",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"valuation\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "98030d12-9a77-4f08-b4ad-2c558d9ad225",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Financial"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e98bf90d-bf2b-4eea-b533-08211e8eec47",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"financial\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f7cf8774-5162-47ea-8bfe-5005ed2fb9d9",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Ownership"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d3f90d1f-7a40-403f-bf3e-17703e151f4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"ownership\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c1833238-5ea8-486b-a6e7-6043e7c0aa0e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Performance"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "62057b47-2364-49ed-80e7-7d6938f2819d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"performance\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d0494c0-e66a-414a-ba2d-89e4072e1a8f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Technical"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1178a080-83ce-4170-a355-9faa8cf9abe6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gamestonk_terminal.stocks.comparison_analysis import finviz_compare_view\n",
|
||||
"\n",
|
||||
"finviz_compare_view.screener(\n",
|
||||
" ticker=ticker,\n",
|
||||
" similar=finviz_similar,\n",
|
||||
" data_type=\"technical\",\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "sought-phase",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!jupyter nbconvert {report_name + \".ipynb\"} --to html --no-input"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Tags",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -52,7 +52,7 @@ def get_historical(
|
||||
# where all tickers are columns.
|
||||
return yf.download(all_tickers, start=start, progress=False, threads=False)[
|
||||
d_candle_types[candle_type]
|
||||
]
|
||||
][all_tickers]
|
||||
|
||||
|
||||
def get_1y_sp500() -> pd.DataFrame:
|
||||
@@ -75,7 +75,7 @@ def get_1y_sp500() -> pd.DataFrame:
|
||||
|
||||
|
||||
def get_sp500_comps_tsne(
|
||||
ticker: str, lr: int = 200, no_plot: bool = False
|
||||
ticker: str, lr: int = 200, no_plot: bool = False, num_tickers: int = 10
|
||||
) -> List[str]:
|
||||
"""
|
||||
Runs TSNE on SP500 tickers (along with ticker if not in SP500).
|
||||
@@ -91,6 +91,8 @@ def get_sp500_comps_tsne(
|
||||
Learning rate for TSNE
|
||||
no_plot : bool
|
||||
Flag to hold off on plotting
|
||||
num_tickers : int
|
||||
Number of tickers to return
|
||||
|
||||
Returns
|
||||
-------
|
||||
@@ -128,4 +130,4 @@ def get_sp500_comps_tsne(
|
||||
x0, y0 = data.loc[ticker]
|
||||
data["dist"] = (data.X - x0) ** 2 + (data.Y - y0) ** 2
|
||||
data = data.sort_values(by="dist")
|
||||
return data.iloc[1:11].index.to_list()
|
||||
return data.iloc[1 : num_tickers + 1].index.to_list()
|
||||
|
||||
@@ -75,7 +75,6 @@ def display_historical(
|
||||
)
|
||||
df_similar.plot(ax=ax)
|
||||
ax.set_title(f"Similar companies to {ticker}")
|
||||
ax.plot(df_similar.index, df_similar[ticker].values)
|
||||
ax.set_xlabel("Time")
|
||||
ax.set_ylabel(f"{['','Normalized'][normalize]} Share Price {['($)',''][normalize]}")
|
||||
ax.grid(b=True, which="major", color="#666666", linestyle="-")
|
||||
|
||||
Reference in New Issue
Block a user