Fx forward rates (#1721)

This commit is contained in:
jmaslek
2022-04-20 11:56:02 -04:00
committed by GitHub
parent 8994168e45
commit 893e68efdc
18 changed files with 4674 additions and 7 deletions

View File

@@ -13,7 +13,7 @@ repos:
]
entry: python custom_pre_commit/check_doc.py
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.2.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
@@ -68,7 +68,7 @@ repos:
["--baseline", ".secrets.baseline", "--exclude-files", "cassettes/*"]
exclude: package.lock.json
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
rev: v2.32.0
hooks:
- id: pyupgrade
args: [--py36-plus]

View File

@@ -12,9 +12,13 @@ from prompt_toolkit.completion import NestedCompleter
from openbb_terminal import feature_flags as obbff
from openbb_terminal.decorators import log_start_end
from openbb_terminal.forex import av_view, forex_helper
from openbb_terminal.forex import av_view, forex_helper, fxempire_view
from openbb_terminal.forex.forex_helper import FOREX_SOURCES, SOURCES_INTERVALS
from openbb_terminal.helper_funcs import parse_known_args_and_warn, valid_date
from openbb_terminal.helper_funcs import (
parse_known_args_and_warn,
valid_date,
EXPORT_ONLY_RAW_DATA_ALLOWED,
)
from openbb_terminal.menu import session
from openbb_terminal.parent_classes import BaseController
from openbb_terminal.rich_config import console
@@ -28,7 +32,7 @@ logger = logging.getLogger(__name__)
class ForexController(BaseController):
"""Forex Controller class."""
CHOICES_COMMANDS = ["to", "from", "load", "quote", "candle", "resources"]
CHOICES_COMMANDS = ["to", "from", "load", "quote", "candle", "resources", "fwd"]
CHOICES_MENUS = ["ta", "qa", "oanda", "pred"]
PATH = "/forex/"
FILE_PATH = os.path.join(os.path.dirname(__file__), "README.md")
@@ -63,7 +67,8 @@ class ForexController(BaseController):
[cmds]
quote get last quote [src][AlphaVantage][/src]
load get historical data
candle show candle plot for loaded data[/cmds]
candle show candle plot for loaded pair
fwd get forward rates for loaded pair[src][FXEmpire][/src][/cmds]
[menu]
> ta technical analysis for loaded coin, e.g.: ema, macd, rsi, adx, bbands, obv
> qa quantitative analysis, e.g.: decompose, cusum, residuals analysis
@@ -282,6 +287,31 @@ class ForexController(BaseController):
"[red]Make sure both a 'to' symbol and a 'from' symbol are selected.[/red]\n"
)
@log_start_end(log=logger)
def call_fwd(self, other_args: List[str]):
"""Process fwd command."""
parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="fwd",
description="Get forward rates for loaded pair.",
)
ns_parser = parse_known_args_and_warn(
parser, other_args, export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED
)
if ns_parser:
if self.to_symbol and self.from_symbol:
fxempire_view.display_forward_rates(
self.to_symbol, self.from_symbol, ns_parser.export
)
else:
logger.error(
"Make sure both a 'to' symbol and a 'from' symbol are selected."
)
console.print(
"[red]Make sure both a 'to' symbol and a 'from' symbol are selected.[/red]\n"
)
# MENUS
@log_start_end(log=logger)
@check_api_key(["OANDA_ACCOUNT_TYPE", "OANDA_ACCOUNT", "OANDA_TOKEN"])

View File

@@ -0,0 +1,36 @@
"""FXEmpire Model"""
import logging
import requests
import pandas as pd
from openbb_terminal.helper_funcs import get_user_agent
from openbb_terminal.decorators import log_start_end
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def get_forward_rates(to_cur: str, from_cur: str):
"""Gets forward rates from fxempire
Parameters
----------
to_cur:str
To currenct
from_cur:str
Returns
-------
"""
r = requests.get(
f"https://www.fxempire.com/currencies/{to_cur}-{from_cur}/forward-rates",
headers={"User-Agent": get_user_agent()},
)
if r.status_code == 200:
forwards = pd.read_html(r.text)[0].set_index("Expiration")
return forwards
logger.info("Currency not found.")
return pd.DataFrame()

View File

@@ -0,0 +1,45 @@
"""FXEmpire view"""
import logging
import os
from openbb_terminal.forex import fxempire_model
from openbb_terminal.decorators import log_start_end
from openbb_terminal.rich_config import console
from openbb_terminal.helper_funcs import print_rich_table, export_data
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def display_forward_rates(to_cur: str, from_cur: str, export: str = ""):
"""Display forward rates for currency pairs
Parameters
----------
to_cur: str
To currency
from_cur: str
From currency
export: str
Format to export data
"""
forward_rates = fxempire_model.get_forward_rates(to_cur, from_cur)
if forward_rates.empty:
console.print(f"[red]Forward rates not found for {to_cur}/{from_cur}.[/red]\n")
return
print_rich_table(
forward_rates,
index_name="Expirations",
show_index=True,
headers=forward_rates.columns,
title=f"Forward rates for {to_cur}/{from_cur}",
floatfmt=".4f",
)
console.print()
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"fwd",
forward_rates,
)

View File

@@ -8,6 +8,8 @@ forex
forex
quit
forex
to EUR
fwd
r
reset
exit

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,20 @@
Expiration,Ask,Bid,Mid,Points
Overnight,0.9223,0.92223,0.92227,-0.25
Tomorrow Next,0.9223,0.92224,0.92227,-0.245
Spot Next,0.92225,0.92218,0.92221,-0.76
One Week,0.92215,0.92208,0.92212,-1.75
Two Weeks,0.92195,0.92188,0.92192,-3.75
Three Weeks,0.92168,0.92161,0.92164,-6.48
One Month,0.92127,0.92121,0.92124,-10.5
Two Months,0.92,0.91993,0.91997,-23.225
Three Months,0.9184,0.91832,0.91836,-39.295
Four Months,0.91663,0.91654,0.91659,-57.045
Five Months,0.91484,0.91475,0.9148,-74.95
Six Months,0.91275,0.91265,0.9127,-95.87
Seven Months,0.91085,0.91073,0.91079,-115.02
Eight Months,0.9088,0.90868,0.90874,-135.51
Nine Months,0.9061,0.90598,0.90604,-162.49
Ten Months,0.90407,0.90393,0.904,-182.87
Eleven Months,0.9022,0.90208,0.90214,-201.535
One Year,0.89978,0.89964,0.89971,-225.795
Two Years,0.87878,0.87841,0.8786,-436.95
1 Expiration Ask Bid Mid Points
2 Overnight 0.9223 0.92223 0.92227 -0.25
3 Tomorrow Next 0.9223 0.92224 0.92227 -0.245
4 Spot Next 0.92225 0.92218 0.92221 -0.76
5 One Week 0.92215 0.92208 0.92212 -1.75
6 Two Weeks 0.92195 0.92188 0.92192 -3.75
7 Three Weeks 0.92168 0.92161 0.92164 -6.48
8 One Month 0.92127 0.92121 0.92124 -10.5
9 Two Months 0.92 0.91993 0.91997 -23.225
10 Three Months 0.9184 0.91832 0.91836 -39.295
11 Four Months 0.91663 0.91654 0.91659 -57.045
12 Five Months 0.91484 0.91475 0.9148 -74.95
13 Six Months 0.91275 0.91265 0.9127 -95.87
14 Seven Months 0.91085 0.91073 0.91079 -115.02
15 Eight Months 0.9088 0.90868 0.90874 -135.51
16 Nine Months 0.9061 0.90598 0.90604 -162.49
17 Ten Months 0.90407 0.90393 0.904 -182.87
18 Eleven Months 0.9022 0.90208 0.90214 -201.535
19 One Year 0.89978 0.89964 0.89971 -225.795
20 Two Years 0.87878 0.87841 0.8786 -436.95

View File

@@ -0,0 +1 @@
""
1

View File

@@ -0,0 +1,21 @@
import pytest
from openbb_terminal.forex.fxempire_model import get_forward_rates
@pytest.fixture(scope="module")
def vcr_config():
return {"filter_headers": [("User-Agent", None)]}
@pytest.mark.vcr
def test_forwards(recorder):
df = get_forward_rates("USD", "EUR")
assert df.empty is False
recorder.capture(df)
@pytest.mark.vcr
def test_forwards_not_known(recorder):
df = get_forward_rates("GWYG", "GUCHWUHW")
assert df.empty is True
recorder.capture(df)

View File

@@ -0,0 +1,19 @@
import pytest
from openbb_terminal.forex.fxempire_view import display_forward_rates
@pytest.fixture(scope="module")
def vcr_config():
return {"filter_headers": [("User-Agent", None)]}
@pytest.mark.vcr
@pytest.mark.record_stdout
def test_forwards():
display_forward_rates("USD", "EUR")
@pytest.mark.vcr
@pytest.mark.record_stdout
def test_forwards_not_known():
display_forward_rates("HUGYDIBWU", "GWUBWYBCY")

View File

@@ -8,7 +8,8 @@ Source: YahooFinance[dim]
quote get last quote [AlphaVantage]
load get historical data
candle show candle plot for loaded data
candle show candle plot for loaded pair
fwd get forward rates for loaded pair[FXEmpire]
> ta technical analysis for loaded coin, e.g.: ema, macd, rsi, adx, bbands, obv
> qa quantitative analysis, e.g.: decompose, cusum, residuals analysis

View File

@@ -0,0 +1,22 @@
Ask Bid Mid Points
Expiration
Overnight 0.92208 0.92177 0.92193 -0.250
Tomorrow Next 0.92208 0.92178 0.92193 -0.245
Spot Next 0.92203 0.92172 0.92187 -0.760
One Week 0.92193 0.92162 0.92178 -1.750
Two Weeks 0.92173 0.92142 0.92158 -3.745
Three Weeks 0.92146 0.92115 0.92130 -6.480
One Month 0.92105 0.92075 0.92090 -10.500
Two Months 0.91978 0.91947 0.91963 -23.225
Three Months 0.91818 0.91786 0.91802 -39.305
Four Months 0.91640 0.91608 0.91624 -57.085
Five Months 0.91463 0.91429 0.91446 -74.895
Six Months 0.91254 0.91220 0.91237 -95.800
Seven Months 0.91064 0.91030 0.91047 -114.835
Eight Months 0.90859 0.90825 0.90842 -135.310
Nine Months 0.90591 0.90555 0.90573 -162.165
Ten Months 0.90389 0.90353 0.90371 -182.430
Eleven Months 0.90203 0.90167 0.90185 -200.975
One Year 0.89956 0.89918 0.89937 -225.795
Two Years 0.87856 0.87795 0.87826 -436.950

View File

@@ -0,0 +1,2 @@
[red]Forward rates not found for HUGYDIBWU/GWUBWYBCY.[/red]

View File

@@ -0,0 +1,68 @@
```
usage: fwd [-h] [--export EXPORT]
```
Get forward rates for loaded pair.
```
options:
-h, --help show this help message (default: False)
--export EXPORT Export raw data into csv, json, xlsx (default: )
```
Sample usage:
```
Forward rates for EUR/USD
Forward rates for EUR/USD
┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┓
┃ Expirations ┃ Ask ┃ Bid ┃ Mid ┃ Points ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━┩
│ Overnight │ 1.0791 │ 1.0790 │ 1.0791 │ 0.3100 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Tomorrow Next │ 1.0791 │ 1.0790 │ 1.0791 │ 0.3100 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Spot Next │ 1.0792 │ 1.0791 │ 1.0791 │ 0.9050 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ One Week │ 1.0793 │ 1.0792 │ 1.0792 │ 2.0800 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Two Weeks │ 1.0795 │ 1.0795 │ 1.0795 │ 4.5000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Three Weeks │ 1.0798 │ 1.0798 │ 1.0798 │ 7.6400 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ One Month │ 1.0803 │ 1.0802 │ 1.0803 │ 12.4195 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Two Months │ 1.0818 │ 1.0817 │ 1.0818 │ 27.3000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Three Months │ 1.0838 │ 1.0835 │ 1.0836 │ 45.9245 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Four Months │ 1.0858 │ 1.0857 │ 1.0857 │ 67.0500 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Five Months │ 1.0879 │ 1.0878 │ 1.0878 │ 88.0500 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Six Months │ 1.0903 │ 1.0902 │ 1.0903 │ 112.5000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Seven Months │ 1.0925 │ 1.0924 │ 1.0925 │ 134.4000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Eight Months │ 1.0949 │ 1.0947 │ 1.0948 │ 157.7000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Nine Months │ 1.0983 │ 1.0981 │ 1.0982 │ 191.8000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Ten Months │ 1.1007 │ 1.1004 │ 1.1006 │ 215.3000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Eleven Months │ 1.1030 │ 1.1029 │ 1.1030 │ 239.3000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ One Year │ 1.1059 │ 1.1057 │ 1.1058 │ 267.7000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Two Years │ 1.1311 │ 1.1306 │ 1.1309 │ 518.3000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Three Years │ 1.1500 │ 1.1493 │ 1.1497 │ 706.2000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Four Years │ 1.1712 │ 1.1691 │ 1.1702 │ 911.2000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Five Years │ 1.1890 │ 1.1859 │ 1.1874 │ 1084.0000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Six Years │ 1.2068 │ 1.2042 │ 1.2055 │ 1264.5000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Seven Years │ 1.2236 │ 1.2200 │ 1.2218 │ 1427.5000 │
├───────────────┼────────┼────────┼────────┼───────────┤
│ Ten Years │ 1.2661 │ 1.2595 │ 1.2628 │ 1837.5000 │
└───────────────┴────────┴────────┴────────┴───────────┘
```

View File

@@ -1187,6 +1187,8 @@ main:
ref: "/terminal/forex/select"
- name: load
ref: "/terminal/forex/load"
- name: fwd
ref: "/terminal/forex/fwd"
- name: candle
ref: "/terminal/forex/candle"
- name: from