-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
169 lines (138 loc) · 6.32 KB
/
load_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime, math, os
from tqdm import tqdm
from models.utils import *
from finam import Exporter, Market, Timeframe
from enum import IntEnum
import investpy
class Market(IntEnum):
"""
Markets mapped to ids used by finam.ru export
List is incomplete, extend it when needed
"""
SHARES = 1
BONDS = 2
COMMODITIES = 24
CURRENCIES_WORLD = 5
CURRENCIES = 45
ETF = 28
ETF_MOEX = 515
FUTURES = 14
FUTURES_ARCHIVE = 17
FUTURES_USA = 7
INDEXES_WORLD = 6
INDEXES = 91
SPB = 517
USA = 25
CRYPTO_CURRENCIES = 520
class FinamDataLoader():
def __init__(self):
self.exporter = Exporter()
def load(self, name, market=Market.SHARES, timeframe=Timeframe.DAILY, start_date=None, end_date=None):
if start_date is None:
start_date = datetime.date(2017,1,1)
if end_date is None:
end_date = datetime.date(2020,1,1)
if market == Market.CURRENCIES:
idx = self.exporter.lookup(name=name, market=market).index[0]
else:
idx = self.exporter.lookup(code=name, market=market).index[0]
data = self.exporter.download(
idx,
market=market,
start_date=start_date,
end_date=end_date,
timeframe=timeframe)
data = data.reset_index(drop=True)
data.columns = map(lambda x: x.replace('<', '').replace('>', '').lower(), data.columns)
data = data.rename({'vol': 'volume'}, axis=1)
data['date'] = pd.to_datetime(data['date'].astype(str)+data['time'], format='%Y%m%d%H:%M:%S')
data = data.drop('time', axis=1)
data = data.drop_duplicates('date')
data['return'] = logreturn(data['close'])
data['highlow'] = (data['high'] - data['low']) / data['low']
data['volume'] = np.log(data['volume'] + 1)
data.columns = [
column
+ ('_' + name.lower()) * (column not in ['date_time', 'date', 'time'])
for column in data.columns
]
return data
def load_multiple(self, names, market=Market.SHARES, timeframe=Timeframe.DAILY, start_date=None, end_date=None):
df_list = [self.load(n, market=market, timeframe=timeframe, start_date=start_date, end_date=end_date) for n in tqdm(names)]
return multi_merge(df_list, on='date').sort_values('date')
def make_daily(data):
idx = {c.split('_')[-1] for c in data.columns}-{'date'}
op = {f'open_{d}': 'first' for d in idx}
cl = {f'close_{d}': 'last' for d in idx}
hi = {f'high_{d}': 'max' for d in idx}
lo = {f'low_{d}': 'min' for d in idx}
vo = {f'volume_{d}': 'sum' for d in idx}
groupdict = op
for i in (cl, hi, lo, vo):
groupdict |= i
data_day = data.groupby(data.date.dt.date).aggregate(groupdict)
for i in idx:
data_day[f'return_{i}'] = logreturn(data_day[f'close_{i}'])
data_day[f'volatility_{i}'] = data.groupby(data.date.dt.date).aggregate({f'return_{i}': lambda x: np.sqrt((x**2).sum())})
data_day[f'highlow_{i}'] = (data_day[f'high_{i}'] - data_day[f'low_{i}']) / data_day[f'low_{i}']
data_day[f'volume_{i}'] = np.log(data_day[f'volume_{i}'] + 1)
data_day.index = pd.DatetimeIndex(data_day.index)
return data_day.dropna().reset_index()
def add_columns_suffix(df, name):
df = df.reset_index()
df.columns = [c.lower() for c in df.columns]
df = df.set_index('date', drop=True)
df.columns = [f'{c}_{name}' for c in df.columns]
df = df.reset_index()
return df
def logreturn(s):
return np.log(s) - np.log(s.shift(1))
indexes = ['IMOEX', 'RTSI']
commodities = ['GC', 'NG', 'BZ']
shares = ['GAZP', 'SBER', 'LKOH', 'GMKN', 'NVTK', 'MGNT', 'ROSN', 'TATN', 'MTSS']
sectors = ['MOEXOG', 'MOEXEU', 'MOEXTL', 'MOEXMM', 'MOEXFN', 'MOEXCN', 'MOEXCH']
foreign = ['UKX', 'INX', 'NDX']
bonds = ['1W', '1M', '6M', '1Y', '3Y', '5Y', '10Y', '20Y']
if __name__ == '__main__':
os.makedirs('./data', exist_ok=True)
data_indexes = FinamDataLoader().load_multiple(names=indexes, market=Market.INDEXES, timeframe=Timeframe.MINUTES5)
make_daily(data_indexes).to_csv('data/indexes_day.csv', index=False)
print('indexes loaded')
data_commodities = FinamDataLoader().load_multiple(names=commodities, market=Market.COMMODITIES, timeframe=Timeframe.MINUTES5)
make_daily(data_commodities).to_csv('data/commodities_day.csv', index=False)
print('commodities loaded')
data_foreign = FinamDataLoader().load_multiple(names=foreign, market=Market.INDEXES_WORLD, timeframe=Timeframe.MINUTES5)
make_daily(data_foreign).to_csv('data/foreign_day.csv', index=False)
print('foreign loaded')
data_shares = FinamDataLoader().load_multiple(names=shares, market=Market.SHARES, timeframe=Timeframe.MINUTES5)
make_daily(data_shares).to_csv('data/shares_day.csv', index=False)
print('shares loaded')
data_sectors = FinamDataLoader().load_multiple(names=sectors, market=Market.INDEXES, timeframe=Timeframe.DAILY)
data_sectors.to_csv('data/sectors_day.csv', index=False)
print('sectors loaded')
curr_list = []
for i in ['usd', 'eur']:
data_curr = investpy.currency_crosses.get_currency_cross_historical_data(
f'{i.upper()}/RUB', from_date='01/01/2017', to_date='01/01/2020')
data_curr = data_curr.drop('Currency', axis=1)
data_curr['return'] = logreturn(data_curr['Close'])
data_curr['highlow'] = (data_curr['High'] - data_curr['Low']) / data_curr['Low']
data_curr = add_columns_suffix(data_curr, i)
curr_list.append(data_curr)
multi_merge(curr_list, on='date').sort_values('date').to_csv('data/currencies_day.csv', index=False)
print('currencies loaded')
bonds_list = []
for i in bonds:
data_bond = investpy.bonds.get_bond_historical_data(
f'Russia {i}', from_date='01/01/2017', to_date='01/01/2020', order='ascending', interval='Daily')
data_bond = data_bond[['Close']]
data_bond['yield'] = data_bond['Close'].pct_change()
data_bond = add_columns_suffix(data_bond, i.lower())
bonds_list.append(data_bond)
multi_merge(bonds_list, on='date').sort_values('date').to_csv('data/bonds_day.csv', index=False)
print('bonds loaded')