From fb1392ba454ecf08bc326b72d1b813a8d379c984 Mon Sep 17 00:00:00 2001 From: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Date: Thu, 10 Nov 2022 08:59:27 +0100 Subject: [PATCH] Improve Forex Helper (#3351) * Fix errors and improve forex_helper * Small changes * Fixed how we get interval * Fixed issues --- openbb_terminal/forex/forex_helper.py | 37 +++++++++++-------- openbb_terminal/reports/templates/forex.ipynb | 21 +++++++++-- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/openbb_terminal/forex/forex_helper.py b/openbb_terminal/forex/forex_helper.py index e88ce349f79..af34377f862 100644 --- a/openbb_terminal/forex/forex_helper.py +++ b/openbb_terminal/forex/forex_helper.py @@ -11,9 +11,12 @@ import matplotlib.pyplot as plt from matplotlib.lines import Line2D from matplotlib.ticker import LogLocator, ScalarFormatter import mplfinance as mpf +import yfinance as yf import numpy as np import plotly.graph_objects as go from plotly.subplots import make_subplots + +from openbb_terminal.stocks import stocks_helper from openbb_terminal.forex import av_model, polygon_model from openbb_terminal.rich_config import console from openbb_terminal.decorators import log_start_end @@ -23,7 +26,6 @@ from openbb_terminal.helper_funcs import ( plot_autoscale, lambda_long_number_format_y_axis, ) -from openbb_terminal.stocks import stocks_helper CANDLE_SORT = [ "adjclose", @@ -52,11 +54,10 @@ SOURCES_INTERVALS: Dict = { "90min", "1hour", "1day", - # These need to be cleaned up. - # "5day", - # "1week", - # "1month", - # "3month", + "5day", + "1week", + "1month", + "3month", ], "AlphaVantage": ["1min", "5min", "15min", "30min", "60min"], } @@ -100,7 +101,7 @@ def load( interval: str = "1day", start_date: str = last_year.strftime("%Y-%m-%d"), source: str = "YahooFinance", - verbose: bool = True, + verbose: bool = False, ) -> pd.DataFrame: """Load forex for two given symbols. @@ -138,6 +139,15 @@ def load( ) return pd.DataFrame() + # Check interval in multiple ways + if interval in interval_map: + clean_interval = interval_map[interval] + elif interval in interval_map.values(): + clean_interval = interval + else: + console.print(f"[red]'{interval}' is an invalid interval[/red]\n") + return pd.DataFrame() + if source == "AlphaVantage": if "min" in interval: resolution = "i" @@ -145,19 +155,16 @@ def load( to_symbol=to_symbol, from_symbol=from_symbol, resolution=resolution, - interval=interval_map[interval], + interval=clean_interval, start_date=start_date, ) if source == "YahooFinance": - - # This works but its not pretty :( - interval = interval_map[interval] if interval != "1day" else "1440m" - return stocks_helper.load( + return yf.download( f"{from_symbol}{to_symbol}=X", - start_date=datetime.strptime(start_date, "%Y-%m-%d"), - interval=int(interval.replace("m", "")), - verbose=verbose, + start=datetime.strptime(start_date, "%Y-%m-%d"), + interval=clean_interval, + progress=verbose, ) if source == "Polygon": diff --git a/openbb_terminal/reports/templates/forex.ipynb b/openbb_terminal/reports/templates/forex.ipynb index ba11e4cef80..9e2b1fd2ac3 100644 --- a/openbb_terminal/reports/templates/forex.ipynb +++ b/openbb_terminal/reports/templates/forex.ipynb @@ -28,10 +28,7 @@ "\n", "from IPython.display import HTML\n", "\n", - "# import sys\n", - "# sys.path.append('../../')\n", - "\n", - "from openbb_terminal.reports import widget_helpers as widgets\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", @@ -750,6 +747,22 @@ "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": {