Commit a5ba1047 authored by Johannes Blaschke's avatar Johannes Blaschke

fixed an error in ForexDay

parent 2ec1e625
......@@ -26,7 +26,9 @@
"metadata": {},
"outputs": [],
"source": [
"from forex_python.converter import CurrencyCodes, CurrencyRates"
"import sys\n",
"sys.path.append(\"..\")\n",
"from forex_db import ForexDB"
]
},
{
......@@ -44,52 +46,147 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import Tuple, List\n",
"\n",
"def month_range(n_year: int, n_month: int, n_day: int = 1) -> Tuple[date, date]:\n",
" \"\"\"\n",
" Returns the (month_start, month_end) dates of a given month (n_month) in a\n",
" given year (n_year). If the optional variable n_day is set, then\n",
" month_start starts on this day.\n",
" \"\"\"\n",
" month_start = date(n_year, n_month, n_day)\n",
"\n",
" next_month_year = n_year\n",
" next_month = n_month + 1\n",
"\n",
" if next_month > 12:\n",
" next_month = 1\n",
" next_month_year = n_year + 1\n",
"\n",
" month_end = date(next_month_year, next_month, 1) - timedelta(1)\n",
"\n",
" return month_start, month_end\n",
"\n",
"def get_range_days(r_start: date, r_end: date) -> List[date]:\n",
" \"\"\"\n",
" Returns a list of days in the range from r_start to r_end.\n",
" \"\"\"\n",
" return [r_start + timedelta(x) for x in range((r_end - r_start).days + 1)]"
"fdb = ForexDB(resolution=timedelta(hours=10))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'EUR'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cr = CurrencyRates()"
"fdb.c_pair.currency_in"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'USD'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fdb.c_pair.currency_out"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API loading Forex Rate for: 2017-03-31\r"
]
}
],
"source": [
"start = date(2017, 3, 1)\n",
"end = date(2017, 3, 31)\n",
"fdb.grow_db(start, end)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(datetime.datetime(2017, 3, 1, 0, 0), 1.0533), (datetime.datetime(2017, 3, 1, 10, 0), 1.0533)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fdb.data[date(2017, 3, 1)]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"dt_now: datetime = datetime.now()\n",
"START, END = month_range(dt_now.year, dt_now.month)\n",
"DAYS: List[date] = get_range_days(START, END)"
"from forex_python.converter import CurrencyRates"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"cr = CurrencyRates()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12376.0"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cr.convert(\"EUR\", \"USD\", 10000, datetime.today())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'fdb' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-349dd971a072>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2017\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2017\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m31\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mfdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgrow_db\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'fdb' is not defined"
]
}
],
"source": [
"start = date(2017, 1, 1)\n",
"end = date(2017, 12, 31)\n",
"fdb.grow_db(start, end)"
]
},
{
......@@ -138,9 +235,7 @@
"output_type": "execute_result"
}
],
"source": [
"DAYS"
]
"source": []
},
{
"cell_type": "code",
......@@ -407,43 +502,62 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API loading Forex Rate for: 2017-04-30\r"
]
}
],
"source": [
"start = date(2017, 1, 1)\n",
"end = date(2017, 4, 30)\n",
"fdb.grow_db(start, end)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"l_date, l_mrate = fdb.get_range(date(2017, 3, 2), date(2017, 3, 20))"
"l_date, l_mrate = fdb.get_range(date(2017, 4, 2), date(2017, 4, 20))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(datetime.date(2017, 3, 2),\n",
" datetime.date(2017, 3, 3),\n",
" datetime.date(2017, 3, 4),\n",
" datetime.date(2017, 3, 5),\n",
" datetime.date(2017, 3, 6),\n",
" datetime.date(2017, 3, 7),\n",
" datetime.date(2017, 3, 8),\n",
" datetime.date(2017, 3, 9),\n",
" datetime.date(2017, 3, 10),\n",
" datetime.date(2017, 3, 11),\n",
" datetime.date(2017, 3, 12),\n",
" datetime.date(2017, 3, 13),\n",
" datetime.date(2017, 3, 14),\n",
" datetime.date(2017, 3, 15),\n",
" datetime.date(2017, 3, 16),\n",
" datetime.date(2017, 3, 17),\n",
" datetime.date(2017, 3, 18),\n",
" datetime.date(2017, 3, 19),\n",
" datetime.date(2017, 3, 20))"
"(datetime.date(2017, 4, 2),\n",
" datetime.date(2017, 4, 3),\n",
" datetime.date(2017, 4, 4),\n",
" datetime.date(2017, 4, 5),\n",
" datetime.date(2017, 4, 6),\n",
" datetime.date(2017, 4, 7),\n",
" datetime.date(2017, 4, 8),\n",
" datetime.date(2017, 4, 9),\n",
" datetime.date(2017, 4, 10),\n",
" datetime.date(2017, 4, 11),\n",
" datetime.date(2017, 4, 12),\n",
" datetime.date(2017, 4, 13),\n",
" datetime.date(2017, 4, 14),\n",
" datetime.date(2017, 4, 15),\n",
" datetime.date(2017, 4, 16),\n",
" datetime.date(2017, 4, 17),\n",
" datetime.date(2017, 4, 18),\n",
" datetime.date(2017, 4, 19),\n",
" datetime.date(2017, 4, 20))"
]
},
"execution_count": 10,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
......@@ -454,34 +568,34 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.0514,\n",
" 1.0565,\n",
" 1.0565,\n",
" 1.0565,\n",
" 1.0592,\n",
" 1.0576,\n",
" 1.0556,\n",
" 1.0551,\n",
" 1.0606,\n",
" 1.0606,\n",
" 1.0606,\n",
" 1.0663,\n",
" 1.0631,\n",
" 1.0622,\n",
" 1.0726,\n",
" 1.0737,\n",
" 1.0737,\n",
" 1.0737,\n",
" 1.0752)"
"(1.0691,\n",
" 1.0661,\n",
" 1.0651,\n",
" 1.0678,\n",
" 1.0666,\n",
" 1.063,\n",
" 1.063,\n",
" 1.063,\n",
" 1.0578,\n",
" 1.0616,\n",
" 1.0605,\n",
" 1.063,\n",
" 1.063,\n",
" 1.063,\n",
" 1.063,\n",
" 1.063,\n",
" 1.0682,\n",
" 1.0725,\n",
" 1.0745)"
]
},
"execution_count": 11,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
......@@ -585,16 +699,16 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.2411"
"1.2376"
]
},
"execution_count": 21,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
......
......@@ -30,18 +30,19 @@ class CurrencyPair(CurrencyRates):
Pair of currencies, allowing their exchange rate to be calculated. The pair is directional:
exchange rates are calculated from the "in" currency in units of the "out" currency.
"""
def __init__(self, c_in: str = "EUR", c_out: str = "USD") -> None:
def __init__(self, c_in: str = "EUR", c_out: str = "USD", quant: float = 10000) -> None:
super().__init__()
self.currency_in: str = c_in
self.currency_out: str = c_out
self.quant: float = quant
def get_pair_rate(self, c_time: datetime) -> float:
"""
Returns the exchange rate from the "in" currency to the "out" currency at the time c_time.
"""
return self.get_rate(self.currency_in, self.currency_out, c_time)
return self.convert(self.currency_in, self.currency_out, self.quant, c_time) / self.quant
......@@ -54,7 +55,7 @@ class ForexDay(object):
self.date: date = c_date
n_list: int = int(timedelta(days=1) / resolution)
t_list: List[datetime] = [as_datetime(c_date) + self.resolution for n in range(n_list)]
t_list: List[datetime] = [as_datetime(c_date) + self.resolution * n for n in range(n_list)]
self.data: List[Tuple[datetime, float]] = [(t, cp.get_pair_rate(t)) for t in t_list]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment