{ "cells": [ { "attachments": {}, "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", "from IPython.display import HTML\n", "\n", "import openbb_terminal.config_terminal as cfg\n", "from openbb_terminal.helper_classes import TerminalStyle\n", "from openbb_terminal import OpenBBFigure, theme\n", "\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": null, "id": "03a286e7", "metadata": {}, "outputs": [], "source": [ "# Suppressing sdk logs\n", "from openbb_terminal.core.session.current_system import set_system_variable\n", "\n", "set_system_variable(\"LOGGING_SUPPRESS\", True)\n", "\n", "# Import the OpenBB SDK\n", "from openbb_terminal.sdk import openbb, widgets" ] }, { "cell_type": "code", "execution_count": null, "id": "c3fe7db5-ec6a-42cf-9e66-52dc1de22370", "metadata": {}, "outputs": [], "source": [ "from openbb_terminal.core.session.current_user import get_current_user\n", "\n", "user = get_current_user()\n", "\n", "cfg.theme = TerminalStyle(\n", " user.preferences.MPL_STYLE,\n", " user.preferences.PMF_STYLE,\n", " user.preferences.RICH_STYLE,\n", ")\n", "\n", "theme.apply_style(\"light\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4b002bfd", "metadata": {}, "outputs": [], "source": [ "stylesheet = widgets.html_report_stylesheet()" ] }, { "attachments": {}, "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" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f8a0d747", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": null, "id": "709a2a7e", "metadata": {}, "outputs": [], "source": [ "# candle\n", "\n", "candle = openbb.forex.candle(\n", " data=ticker_data,\n", " to_symbol=to_symbol,\n", " from_symbol=from_symbol,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "05f54519", "metadata": {}, "outputs": [], "source": [ "# candle graph with moving averages 7/14 and 30/60\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=True,\n", ").to_html()\n", "\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=True,\n", ").to_html()" ] }, { "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" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6c852082", "metadata": {}, "source": [ "# Technical analysis" ] }, { "cell_type": "code", "execution_count": null, "id": "4ff6345d", "metadata": {}, "outputs": [], "source": [ "# simple moving average\n", "ticker_data.index.names = [\"date\"]\n", "\n", "sma = openbb.ta.ma_chart(\n", " data=ticker_data[\"Close\"],\n", " ma_type=\"SMA\",\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "5d5089fa", "metadata": {}, "outputs": [], "source": [ "# exponential moving average\n", "ema = openbb.ta.ma_chart(\n", " data=ticker_data[\"Close\"],\n", " ma_type=\"EMA\",\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "5facfbf4", "metadata": {}, "outputs": [], "source": [ "# zero lag exponential moving average\n", "\n", "zlma = openbb.ta.ma_chart(\n", " data=ticker_data[\"Close\"],\n", " ma_type=\"ZLMA\",\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "df54f7ad", "metadata": {}, "outputs": [], "source": [ "# commodity channel index\n", "\n", "cci = openbb.ta.cci_chart(\n", " data=ticker_data,\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "f53469eb", "metadata": {}, "outputs": [], "source": [ "# moving average convergence/divergence\n", "\n", "macd = openbb.ta.macd_chart(\n", " data=ticker_data[\"Adj Close\"],\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "dd79e6b6", "metadata": {}, "outputs": [], "source": [ "# fisher transform\n", "\n", "fisher = openbb.ta.fisher_chart(\n", " data=ticker_data,\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "b699e61f", "metadata": {}, "outputs": [], "source": [ "# aroon indicator\n", "\n", "aroon = openbb.ta.aroon_chart(\n", " data=ticker_data,\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "79401027", "metadata": {}, "outputs": [], "source": [ "# bollinger bands\n", "\n", "bbands = openbb.ta.bbands_chart(\n", " data=ticker_data,\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "0b5e2949", "metadata": {}, "outputs": [], "source": [ "# fibonacci retracement\n", "\n", "fib = openbb.ta.fib_chart(\n", " data=ticker_data,\n", " symbol=ticker,\n", " external_axes=True,\n", ").to_html()" ] }, { "attachments": {}, "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", "normality = openbb.qa.normality(\n", " data=ticker_data[\"Close\"],\n", ")\n", "normality" ] }, { "cell_type": "code", "execution_count": null, "id": "66a7dccc", "metadata": {}, "outputs": [], "source": [ "# rolling mean and std deviation of prices\n", "\n", "rolling = (\n", " openbb.qa.rolling_chart(\n", " symbol=ticker,\n", " data=ticker_data,\n", " target=\"Close\",\n", " external_axes=True,\n", " )\n", " .update_layout(\n", " legend=dict(\n", " x=0.01,\n", " y=0.99,\n", " xanchor=\"left\",\n", " yanchor=\"top\",\n", " xref=\"paper\",\n", " yref=\"paper\",\n", " bgcolor=\"rgba(0, 0, 0, 0)\",\n", " ),\n", " legend2=dict(bgcolor=\"rgba(0, 0, 0, 0)\"),\n", " )\n", " .to_html()\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "d6b43dd7", "metadata": {}, "outputs": [], "source": [ "# rolling kurtosis of distribution of prices\n", "\n", "kurtosis = openbb.qa.kurtosis_chart(\n", " symbol=ticker,\n", " data=ticker_data,\n", " target=\"Close\",\n", " external_axes=True,\n", ").to_html()" ] }, { "cell_type": "code", "execution_count": null, "id": "fc31624a", "metadata": {}, "outputs": [], "source": [ "# latest news\n", "try:\n", " news = openbb.news(term=ticker).head(10)\n", " news.columns = news.columns.str.title()\n", "\n", " news = news.rename(\n", " columns={\"Date\": \"Published\", \"Description\": \"Title\", \"Url\": \"Link\"}\n", " )\n", " news[\"Title\"] = news.apply(\n", " lambda x: f'{x[\"Title\"]}', axis=1\n", " )\n", " news = news[[\"Title\", \"Published\"]]\n", " news[\"Published\"] = pd.to_datetime(news[\"Published\"]).dt.strftime(\"%Y-%m-%d %H:%M\")\n", "\n", " news = news.set_index(\"Published\")\n", " news.sort_index()\n", "except:\n", " news = pd.DataFrame()" ] }, { "attachments": {}, "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", "\n", "body += widgets.header(\n", " author,\n", " report_date,\n", " report_time,\n", " report_timezone,\n", " f\"INVESTMENT RESEARCH REPORT: {ticker}\",\n", " plotly_js=True,\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, \"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)" ] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "venv", "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.13" }, "vscode": { "interpreter": { "hash": "381a209f752e9104bff95ec5e80679ea2e476866b80e4326f1ee6b7658af6049" } } }, "nbformat": 4, "nbformat_minor": 5 }