In [28]:
%pylab inline
import datetime
import pandas as pd
import matplotlib.pyplot as plt
from utils import read_url, parse
In [2]:
#CURRENCIES_URL = 'http://www.cbr.ru/scripts/XML_val.asp'
#curs = parse(CURRENCIES_URL, {'d': 0})
In [29]:
CURS = {'USD': 'R01235', 'EUR': 'R01239'}
date2str = lambda x: x.strftime('%d/%m/%Y')
str2date = lambda x: datetime.datetime.strptime(x, '%d.%m.%Y')
RATES_URL = 'http://www.cbr.ru/scripts/XML_daily.asp'
def rates_by_day(day):
rates = parse(RATES_URL, {'date_req': date2str(day)})
r = lambda code: rates.xpath('Valute[@ID="%s"]'%code)[0].xpath('Value')[0].text
return {k:float(r(v).replace(',','.')) for k, v in CURS.items()}
RATES_RANGE_URL = 'http://www.cbr.ru/scripts/XML_dynamic.asp'
def rates_range(date1, date2, currency):
rates = parse(RATES_RANGE_URL, {'date_req1': date2str(date1), 'date_req2': date2str(date2), 'VAL_NM_RQ': CURS[currency]})
return [(str2date(r.attrib['Date']), float(r.xpath('Value')[0].text.replace(',','.'))) for r in rates.getchildren()]
In [33]:
dates = pd.date_range(start='2014-01-01', end='2014-12-18')
In [34]:
usd = pd.DataFrame(rates_range(dates[0], dates[-1], 'USD'), columns=['date', 'val']).set_index(['date'])
eur = pd.DataFrame(rates_range(dates[0], dates[-1], 'EUR'), columns=['date', 'val']).set_index(['date'])
df = usd.merge(eur, left_index=True, right_index=True)
df.columns = ['USD', 'EUR']
In [26]:
#rates = {d: rate(d) for d in dates}
#df = pd.DataFrame(rates).transpose()
#df.USD
In [36]:
#pd.options.display.mpl_style = 'default'
plt.figure(figsize=(12, 6), dpi=80, facecolor='w', edgecolor='k')
df.EUR.plot(color='red', ylim=(0,100))
df.USD.plot(color='blue')
plt.legend(loc=2)
Out[36]:
In [7]: