mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-03 16:02:04 +00:00
* implement user and change is guest * replace User methods * move remaining functions to user * fix bug * portfolio test * fix tests * fix tests * uncomment skip * ruff * start credentials model implementation * change deault structure * refacto quandl * change default struct and file names * bug * start using global user * add comments * move update flair * change cfg references in keys model * reddit & fmp * refactore additional keys * remaing config keys * remove unsude imports and fix hub keys * ruff * fix line too long * unblock unittests * fix keys model tests * remove os patch * fix account controller tests * fix account controller tests * fix account model * account fixed * fix user tests * User Model * User Model * User Model * Move session folder * User Model : grouping the models * User Model : fix bugs * Config Plot + bug fix * User Model : Refactoring plots * Feature flags * Remove dup * clean comments a fix some vars * fix feature flags controller * refactor obbff from settings controller * refactor a bunch of obbff variables * fix bug blocking terminal * fix integration tests * refactor controllers with obbff * refactor remaining ffs * remove comments * log not started * mispelled variable * set current user on local model * User Model : moving files + updating * User Preferences * User : Refactor is_guest function + bug fix * User : refactor is_guest function * Fix conftest.py * remove lost obbffs * typo on fileoverwrite * Plot config: move variables and start plot dpi * User : tests * refactor plot dpi references * remove plot dpi imports * refactor some config terminal leftover * /account/sync : fix bug * remove configs reload * start path refactor * installation paths vs user paths * remove cfg_plot * Tests : User Refactoring * user prefix only for preferences * user prefix only for preferences * move user paths * remove comment * update apply remote configs * fix tz not updating * save some copys * small fix * create interface for credentials and preferences * wrong docstrings fixed * ruff core * fix some pref setting * login is better * ruff * ruff * remove Literal * add small comment * Tests : User Refactoring * Credentials Model : add DataBento * Fix helper_funcs * Fixing code * Tests : fixing * User Model : freezing the models * Fixed Income + Databento * Tests : fixing keys * black * ruff * Tests : user model * Tests : user model * Fix linting * Fix linting * Linting * Linting * Fixing code and tests * Fix tests * mypy * SDK * Clean ruff cache * Fix syntax issue * Update fred_view * Black * Linting * Update code * Update tests * Linting * Linting * Tests * Black * Linting * Linting * Mock os.listdir to avoid looking for file that doesnt exist * Mock options paths * lint * Tests : fix * Code : update * Tests + Linting * Tests * Ruff * Code : update * Tests * Code : update * Fix workflow * Code : update --------- Co-authored-by: Chavithra PARANA <chavithra@gmail.com> Co-authored-by: luqazino@gmail.com <luqazino@gmail.com> Co-authored-by: James Maslek <jmaslek11@gmail.com>
183 lines
5.0 KiB
Python
183 lines
5.0 KiB
Python
"""AlphaVantage FX Model."""
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
from typing import Any, Dict, List
|
|
|
|
import pandas as pd
|
|
|
|
from openbb_terminal.core.session.current_user import get_current_user
|
|
from openbb_terminal.decorators import log_start_end
|
|
from openbb_terminal.helper_funcs import get_user_timezone, request
|
|
from openbb_terminal.rich_config import console
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def get_currency_list() -> List:
|
|
"""Load AV currency codes from a local file."""
|
|
path = os.path.join(os.path.dirname(__file__), "data/av_forex_currencies.csv")
|
|
return list(pd.read_csv(path)["currency code"])
|
|
|
|
|
|
CURRENCY_LIST = get_currency_list()
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def check_valid_forex_currency(symbol: str) -> str:
|
|
"""Check if given symbol is supported on alphavantage.
|
|
|
|
Parameters
|
|
----------
|
|
symbol : str
|
|
Symbol to check
|
|
|
|
Returns
|
|
-------
|
|
str
|
|
Currency symbol
|
|
|
|
Raises
|
|
------
|
|
argparse.ArgumentTypeError
|
|
Symbol not valid on alphavantage
|
|
"""
|
|
if symbol.upper() in CURRENCY_LIST:
|
|
return symbol.upper()
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
f"{symbol.upper()} not found in alphavantage supported currency codes. "
|
|
)
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def get_quote(to_symbol: str = "USD", from_symbol: str = "EUR") -> Dict[str, Any]:
|
|
"""Get current exchange rate quote from alpha vantage.
|
|
|
|
Parameters
|
|
----------
|
|
to_symbol : str
|
|
To forex symbol
|
|
from_symbol : str
|
|
From forex symbol
|
|
|
|
Returns
|
|
-------
|
|
Dict[str, Any]
|
|
Dictionary of exchange rate
|
|
"""
|
|
url = (
|
|
"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE"
|
|
+ f"&from_currency={from_symbol}"
|
|
+ f"&to_currency={to_symbol}"
|
|
+ f"&apikey={get_current_user().credentials.API_KEY_ALPHAVANTAGE}"
|
|
)
|
|
|
|
response = request(url)
|
|
response_json = response.json()
|
|
result = {}
|
|
|
|
# If the returned data was unsuccessful
|
|
if "Error Message" in response_json:
|
|
console.print(response_json["Error Message"])
|
|
logger.error(response_json["Error Message"])
|
|
else:
|
|
# check if json is empty
|
|
if not response_json:
|
|
console.print("No data found.\n")
|
|
else:
|
|
result = response_json
|
|
|
|
return result
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def get_historical(
|
|
to_symbol: str = "USD",
|
|
from_symbol: str = "EUR",
|
|
resolution: str = "d",
|
|
interval: int = 5,
|
|
start_date: str = "",
|
|
end_date: str = "",
|
|
) -> pd.DataFrame:
|
|
"""Get historical forex data.
|
|
|
|
Parameters
|
|
----------
|
|
to_symbol : str
|
|
To forex symbol
|
|
from_symbol : str
|
|
From forex symbol
|
|
resolution : str, optional
|
|
Resolution of data. Can be "i", "d", "w", "m" for intraday, daily, weekly or monthly
|
|
interval : int, optional
|
|
Interval for intraday data
|
|
start_date : str, optional
|
|
Start date for data.
|
|
end_date : str, optional
|
|
End date for data.
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
Historical data for forex pair
|
|
"""
|
|
d_res = {"i": "FX_INTRADAY", "d": "FX_DAILY", "w": "FX_WEEKLY", "m": "FX_MONTHLY"}
|
|
|
|
url = f"https://www.alphavantage.co/query?function={d_res[resolution]}&from_symbol={from_symbol}"
|
|
url += f"&to_symbol={to_symbol}&outputsize=full&apikey={get_current_user().credentials.API_KEY_ALPHAVANTAGE}"
|
|
if resolution == "i":
|
|
url += f"&interval={interval}min"
|
|
|
|
r = request(url)
|
|
response_json = r.json()
|
|
|
|
if r.status_code != 200:
|
|
return pd.DataFrame()
|
|
|
|
df = pd.DataFrame()
|
|
|
|
# If the returned data was unsuccessful
|
|
if "Error Message" in response_json:
|
|
console.print(response_json["Error Message"])
|
|
elif "Note" in response_json:
|
|
console.print(response_json["Note"])
|
|
else:
|
|
# check if json is empty
|
|
if not response_json:
|
|
console.print("No data found.\n")
|
|
else:
|
|
if "Meta Data" not in response_json and "Information" in response_json:
|
|
console.print(response_json["Information"])
|
|
return pd.DataFrame()
|
|
|
|
key = list(response_json.keys())[1]
|
|
|
|
df = pd.DataFrame.from_dict(response_json[key], orient="index")
|
|
df.index = pd.to_datetime(df.index)
|
|
|
|
if start_date and resolution != "i":
|
|
df = df[df.index > start_date]
|
|
|
|
if end_date and resolution != "i":
|
|
df = df[df.index < end_date]
|
|
|
|
if (df.index.hour != 0).any():
|
|
# if intraday data, convert to local timezone
|
|
df.index = df.index.tz_localize("UTC").tz_convert(get_user_timezone())
|
|
|
|
df = df.rename(
|
|
columns={
|
|
"1. open": "Open",
|
|
"2. high": "High",
|
|
"3. low": "Low",
|
|
"4. close": "Close",
|
|
}
|
|
)
|
|
df.index = pd.DatetimeIndex(df.index)
|
|
df = df[::-1]
|
|
|
|
return df.astype(float)
|