Files
OpenBB/openbb_terminal/reports/templates/forex.ipynb
James Maslek 3dd9fb9567 Fix reports after SDK changes (#3444)
* Fix sdk changes

* Forex report

* Crypto Report

* etf report

* economy report
2022-11-17 12:43:40 -05:00

780 lines
21 KiB
Plaintext
Vendored

{
"cells": [
{
"cell_type": "markdown",
"id": "70ba2c8a",
"metadata": {},
"source": [
"## Notebook setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "driven-billion",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import io\n",
"import warnings\n",
"import datetime\n",
"import numpy as np\n",
"import base64\n",
"import pandas as pd\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib_inline.backend_inline\n",
"\n",
"from IPython.display import HTML\n",
"\n",
"from openbb_terminal.sdk import widgets\n",
"from openbb_terminal.sdk import openbb\n",
"from openbb_terminal.helper_classes import TerminalStyle\n",
"from openbb_terminal.core.config.paths import REPOSITORY_DIRECTORY\n",
"\n",
"%matplotlib inline\n",
"matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3fe7db5-ec6a-42cf-9e66-52dc1de22370",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" theme = TerminalStyle(\"light\", \"light\", \"light\")\n",
"except:\n",
" pass\n",
"stylesheet = widgets.html_report_stylesheet()"
]
},
{
"cell_type": "markdown",
"id": "807fda4e",
"metadata": {},
"source": [
"## Select Ticker"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "intended-sherman",
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"# Parameters that will be replaced when calling this notebook\n",
"# Do not leave parameters blank as notebook will not run otherwise\n",
"symbol = \"EURUSD\"\n",
"report_name = \"Forex Report for EURUSD\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "613cea1d",
"metadata": {},
"outputs": [],
"source": [
"ticker = symbol"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4897334",
"metadata": {},
"outputs": [],
"source": [
"author = \"\"\n",
"report_title = f\"INVESTMENT RESEARCH REPORT ON {ticker.upper()}\"\n",
"report_date = datetime.datetime.now().strftime(\"%d %B, %Y\")\n",
"report_time = datetime.datetime.now().strftime(\"%H:%M\")\n",
"report_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo\n",
"report_title, report_date, report_time, report_timezone"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07d09ce8",
"metadata": {},
"outputs": [],
"source": [
"from_symbol = ticker[:3]\n",
"to_symbol = ticker[3:]\n",
"last_year = datetime.datetime.now() - datetime.timedelta(days=365)\n",
"\n",
"ticker_data = openbb.forex.load(\n",
" from_symbol=from_symbol,\n",
" to_symbol=to_symbol,\n",
" start_date=last_year.strftime(\"%Y-%m-%d\"),\n",
" interval=\"1day\",\n",
" resolution=\"d\",\n",
")\n",
"\n",
"# 1 year historical data\n",
"ticker_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88af0fbe",
"metadata": {},
"outputs": [],
"source": [
"last_year = datetime.datetime.now() - datetime.timedelta(days=365)\n",
"\n",
"ticker_data_interval_1month = openbb.forex.load(\n",
" from_symbol=from_symbol,\n",
" to_symbol=to_symbol,\n",
" start_date=last_year.strftime(\"%Y-%m-%d\"),\n",
" interval=\"1month\",\n",
" resolution=\"d\",\n",
")\n",
"ticker_data_interval_1month = ticker_data_interval_1month.drop(columns=\"Volume\")\n",
"ticker_data_interval_1month"
]
},
{
"cell_type": "markdown",
"id": "f8a0d747",
"metadata": {},
"source": [
"## Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "709a2a7e",
"metadata": {},
"outputs": [],
"source": [
"# candle\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.forex.candle(\n",
" data=ticker_data,\n",
" to_symbol=to_symbol,\n",
" from_symbol=from_symbol,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"candle = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05f54519",
"metadata": {},
"outputs": [],
"source": [
"# candle graph with moving averages 7/14 and 30/60\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"candle_ma_7_14 = openbb.forex.candle(\n",
" data=ticker_data,\n",
" to_symbol=to_symbol,\n",
" from_symbol=from_symbol,\n",
" ma=[7, 14],\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"candle_ma_7_14 = f.getvalue().decode(\"utf-8\")\n",
"\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"candle_ma_30_60 = openbb.forex.candle(\n",
" data=ticker_data,\n",
" to_symbol=to_symbol,\n",
" from_symbol=from_symbol,\n",
" ma=[30, 60],\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"candle_ma_30_60 = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc17a601",
"metadata": {},
"outputs": [],
"source": [
"fwd = openbb.forex.fwd(\n",
" to_symbol=to_symbol,\n",
" from_symbol=from_symbol,\n",
")\n",
"\n",
"# forward rates\n",
"fwd"
]
},
{
"cell_type": "markdown",
"id": "6c852082",
"metadata": {},
"source": [
"# Technical analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39afc6a7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ff6345d",
"metadata": {},
"outputs": [],
"source": [
"# simple moving average\n",
"ticker_data.index.names = [\"date\"]\n",
"\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.ta.ma_chart(\n",
" data=ticker_data[\"Close\"],\n",
" ma_type=\"SMA\",\n",
" symbol=ticker,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"sma = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d5089fa",
"metadata": {},
"outputs": [],
"source": [
"# exponential moving average\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.ta.ma_chart(\n",
" data=ticker_data[\"Close\"],\n",
" ma_type=\"EMA\",\n",
" symbol=ticker,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"ema = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5facfbf4",
"metadata": {},
"outputs": [],
"source": [
"# zero lag exponential moving average\n",
"\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.ta.ma_chart(\n",
" data=ticker_data[\"Close\"],\n",
" ma_type=\"ZLMA\",\n",
" symbol=ticker,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"zlma = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df54f7ad",
"metadata": {},
"outputs": [],
"source": [
"# commodity channel index\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(11, 5), dpi=150)\n",
"openbb.ta.cci_chart(\n",
" data=ticker_data,\n",
" symbol=ticker,\n",
" external_axes=[ax1, ax2],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"cci = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f53469eb",
"metadata": {},
"outputs": [],
"source": [
"# moving average convergence/divergence\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(11, 5), dpi=150)\n",
"openbb.ta.macd_chart(\n",
" data=ticker_data[\"Adj Close\"],\n",
" symbol=ticker,\n",
" external_axes=[ax1, ax2],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"macd = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd79e6b6",
"metadata": {},
"outputs": [],
"source": [
"# fisher transform\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(11, 11), dpi=150)\n",
"ax3 = ax2.twinx()\n",
"openbb.ta.fisher_chart(\n",
" data=ticker_data,\n",
" symbol=ticker,\n",
" external_axes=[ax1, ax2, ax3],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"fisher = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b699e61f",
"metadata": {},
"outputs": [],
"source": [
"# aroon indicator\n",
"\n",
"fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, ncols=1, figsize=(11, 11), dpi=150)\n",
"openbb.ta.aroon_chart(\n",
" data=ticker_data,\n",
" symbol=ticker,\n",
" external_axes=[ax1, ax2, ax3],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"aroon = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79401027",
"metadata": {},
"outputs": [],
"source": [
"# bollinger bands\n",
"\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.ta.bbands_chart(\n",
" data=ticker_data,\n",
" symbol=ticker,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"bbands = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b5e2949",
"metadata": {},
"outputs": [],
"source": [
"# fibonacci retracement\n",
"\n",
"fig, ax1 = plt.subplots(figsize=(11, 5), dpi=150)\n",
"ax2 = ax1.twinx()\n",
"openbb.ta.fib_chart(\n",
" data=ticker_data,\n",
" symbol=ticker,\n",
" external_axes=[ax1, ax2],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"fib = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "markdown",
"id": "e6f6ce8a",
"metadata": {},
"source": [
"# Quantitative Analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e77b373b",
"metadata": {},
"outputs": [],
"source": [
"# summary\n",
"\n",
"summary = openbb.qa.summary(data=ticker_data)\n",
"summary"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ecfc93e",
"metadata": {},
"outputs": [],
"source": [
"# normality statistics and tests\n",
"\n",
"# fig, _ = plt.subplots(figsize=(11, 3), dpi=150)\n",
"# openbb.qa.normality_chart(\n",
"# data=ticker_data,\n",
"# target=\"Close\",\n",
"# )\n",
"# fig.tight_layout()\n",
"# f = io.BytesIO()\n",
"# fig.savefig(f, format=\"svg\")\n",
"\n",
"normality = openbb.qa.normality(\n",
" data=ticker_data[\"Close\"],\n",
")\n",
"normality"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8abe96b0",
"metadata": {},
"outputs": [],
"source": [
"# box and whisker plot\n",
"\n",
"fig, ax = plt.subplots(figsize=(11, 3), dpi=150)\n",
"openbb.qa.bw(\n",
" symbol=ticker,\n",
" data=ticker_data,\n",
" target=\"Close\",\n",
" yearly=False,\n",
" external_axes=[ax],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"bw = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66a7dccc",
"metadata": {},
"outputs": [],
"source": [
"# rolling mean and std deviation of prices\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(11, 5), dpi=150)\n",
"openbb.qa.rolling_chart(\n",
" symbol=ticker,\n",
" data=ticker_data,\n",
" target=\"Close\",\n",
" external_axes=[ax1, ax2],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"rolling = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6b43dd7",
"metadata": {},
"outputs": [],
"source": [
"# rolling kurtosis of distribution of prices\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(11, 5), dpi=150)\n",
"openbb.qa.kurtosis_chart(\n",
" symbol=ticker,\n",
" data=ticker_data,\n",
" target=\"Close\",\n",
" external_axes=[ax1, ax2],\n",
")\n",
"fig.tight_layout()\n",
"f = io.BytesIO()\n",
"fig.savefig(f, format=\"svg\")\n",
"kurtosis = f.getvalue().decode(\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc31624a",
"metadata": {},
"outputs": [],
"source": [
"# latest news\n",
"\n",
"news = openbb.news(term=ticker).head(10)\n",
"\n",
"news[\"published\"] = pd.to_datetime(news[\"published\"])\n",
"news[\"published\"] = pd.to_datetime(\n",
" news[\"published\"], format=\"%d/%m/%y %H:%M:%S\"\n",
").dt.strftime(\"%Y-%m-%d\")\n",
"news = news[[\"published\", \"title\", \"link\"]]\n",
"\n",
"news[\"link\"] = news[\"link\"].apply(lambda x: f'<a href=\"{x}\">{x}</a>')\n",
"news = news.set_index(\"published\")\n",
"news.sort_index()\n",
"news"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "077fb111",
"metadata": {},
"outputs": [],
"source": [
"news.index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd5136de",
"metadata": {},
"outputs": [],
"source": [
"# TODO: add prediction"
]
},
{
"cell_type": "markdown",
"id": "ac501a32-124d-4d21-aae1-a8d147bb1071",
"metadata": {},
"source": [
"## Render the report template to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abfe37e4-ea38-42c3-8fb5-7cceb233fb12",
"metadata": {},
"outputs": [],
"source": [
"body = \"\"\n",
"\n",
"img = (\n",
" str(REPOSITORY_DIRECTORY)\n",
" + \"/openbb_terminal/reports/templates/OpenBB_reports_logo.png\"\n",
")\n",
"floppy_disk_img = (\n",
" str(REPOSITORY_DIRECTORY) + \"/openbb_terminal/reports/templates/floppy-disc.png\"\n",
")\n",
"body += widgets.header(\n",
" img,\n",
" floppy_disk_img,\n",
" author,\n",
" report_date,\n",
" report_time,\n",
" report_timezone,\n",
" f\"<b>INVESTMENT RESEARCH REPORT:</b> {ticker}\",\n",
")\n",
"\n",
"body += widgets.tablinks(\n",
" [\n",
" \"SUMMARY\",\n",
" \"Overview\",\n",
" \"Technical Analysis\",\n",
" \"Quantitative Analysis\",\n",
" ]\n",
")\n",
"\n",
"comment = \"Every analysis displayed considered every day data YTD\"\n",
"\n",
"\n",
"# TODO: add KPIs\n",
"\n",
"htmlcode = \"\"\n",
"\n",
"# htmlcode += widgets.h(3, \"KPIs\")\n",
"# htmlcode += widgets.kpi(\n",
"# [30, 70],\n",
"# [\"RSI level is oversold\", \"RSI level is normal\", \"RSI level is overbought\"],\n",
"# 999,\n",
"# )\n",
"# htmlcode += widgets.kpi(\n",
"# [0],\n",
"# [\n",
"# \"The sum of last 10 insider trading (in millions) was negative\",\n",
"# \"The sum of last 10 insider trading (in millions) was positive\",\n",
"# ],\n",
"# 999,\n",
"# )\n",
"# htmlcode += widgets.kpi(\n",
"# [-0.1, 0.1],\n",
"# [\n",
"# \"Last FinBrain sentiment is bearish\",\n",
"# \"Last FinBrain sentiment is neutral\",\n",
"# \"Last FinBrain sentiment is bullish\",\n",
"# ],\n",
"# 999,\n",
"# )\n",
"\n",
"# Summary tab\n",
"htmlcode += widgets.row([widgets.h(3, \"Latest news\") + news.to_html(escape=False)])\n",
"htmlcode += widgets.row(\n",
" [\n",
" widgets.h(3, \"One year historical data for 1st day each month\")\n",
" + ticker_data_interval_1month.to_html()\n",
" ]\n",
")\n",
"body += widgets.add_tab(\"SUMMARY\", htmlcode)\n",
"\n",
"# Overview tab\n",
"htmlcode = \"\"\n",
"htmlcode = widgets.row([widgets.h(3, \"Candlestick chart\")])\n",
"htmlcode += widgets.row([candle])\n",
"htmlcode += widgets.row(\n",
" [widgets.h(3, \"Candlestick chart with moving averages 7 and 14\")]\n",
")\n",
"htmlcode += widgets.row([candle_ma_7_14])\n",
"htmlcode += widgets.row(\n",
" [widgets.h(3, \"Candlestick chart with moving averages 30 and 60\")]\n",
")\n",
"htmlcode += widgets.row([candle_ma_30_60])\n",
"htmlcode += widgets.row([widgets.h(3, \"Forward rates\")])\n",
"htmlcode += widgets.row([fwd.to_html()])\n",
"body += widgets.add_tab(\"Overview\", htmlcode, True, comment)\n",
"\n",
"# Technical Analysis tab\n",
"htmlcode = widgets.row([widgets.h(3, \"Simple moving average\")])\n",
"htmlcode += widgets.row([sma])\n",
"htmlcode += widgets.row([widgets.h(3, \"Exponential moving average\")])\n",
"htmlcode += widgets.row([ema])\n",
"htmlcode += widgets.row([widgets.h(3, \"Zero lag exponential moving average\")])\n",
"htmlcode += widgets.row([zlma])\n",
"htmlcode += widgets.row([widgets.h(3, \"Commodity channel index\")])\n",
"htmlcode += widgets.row([cci])\n",
"htmlcode += widgets.row([widgets.h(3, \"Moving average convergence/divergence\")])\n",
"htmlcode += widgets.row([macd])\n",
"htmlcode += widgets.row([widgets.h(3, \"Fisher transform\")])\n",
"htmlcode += widgets.row([fisher])\n",
"htmlcode += widgets.row([widgets.h(3, \"Aroon indicator\")])\n",
"htmlcode += widgets.row([aroon])\n",
"htmlcode += widgets.row([widgets.h(3, \"Bollinger bands\")])\n",
"htmlcode += widgets.row([bbands])\n",
"htmlcode += widgets.row([widgets.h(3, \"Fibonacci retracement\")])\n",
"htmlcode += widgets.row([fib])\n",
"body += widgets.add_tab(\"Technical Analysis\", htmlcode, True, comment)\n",
"\n",
"# Quantitative Analysis tab\n",
"htmlcode = widgets.row([widgets.h(3, \"Summary\") + summary.to_html()])\n",
"htmlcode += widgets.row(\n",
" [widgets.h(3, \"Normality statistics and tests\") + normality.to_html()]\n",
")\n",
"htmlcode += widgets.row([widgets.h(3, \"Box and whisker plot\")])\n",
"htmlcode += widgets.row([bw])\n",
"htmlcode += widgets.row([widgets.h(3, \"Rolling mean and std deviation of prices\")])\n",
"htmlcode += widgets.row([rolling])\n",
"htmlcode += widgets.row([widgets.h(3, \"Rolling kurtosis of distribution of prices\")])\n",
"htmlcode += widgets.row([kurtosis])\n",
"body += widgets.add_tab(\"Quantitative Analysis\", htmlcode, True, comment)\n",
"\n",
"body += widgets.tab_clickable_and_save_evt()\n",
"report = widgets.html_report(title=report_name, stylesheet=stylesheet, body=body)\n",
"\n",
"# # to save the results\n",
"with open(report_name + \".html\", \"w\", encoding=\"utf-8\") as fh:\n",
" fh.write(report)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f749ab6-2631-41d9-988b-c0a95765856d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "30e917a5-e702-44c9-8c0c-7acbb59dc2da",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Tags",
"kernelspec": {
"display_name": "Python 3.9.6 ('obb')",
"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.9.6"
},
"vscode": {
"interpreter": {
"hash": "cb66ba39c97f15743fbb79e204a841ea03600987d49579cfb0ffc8e8dd934c69"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}