Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Forex Helper #3351

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions openbb_terminal/forex/forex_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
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
Expand All @@ -23,7 +26,6 @@
plot_autoscale,
lambda_long_number_format_y_axis,
)
from openbb_terminal.stocks import stocks_helper

CANDLE_SORT = [
"adjclose",
Expand Down Expand Up @@ -52,11 +54,10 @@
"90min",
"1hour",
"1day",
# These need to be cleaned up.
# "5day",
# "1week",
# "1month",
# "3month",
"5day",
"1week",
"1month",
"3month",
],
"AlphaVantage": ["1min", "5min", "15min", "30min", "60min"],
}
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -138,26 +139,32 @@ 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"
return av_model.get_historical(
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":
Expand Down
21 changes: 17 additions & 4 deletions openbb_terminal/reports/templates/forex.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down