-
Notifications
You must be signed in to change notification settings - Fork 47
/
processor_Base.py
332 lines (297 loc) · 17.2 KB
/
processor_Base.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
This code defines a class named _Base which serves as a base class for other classes to inherit from. The
class has several methods, including __init__, download_data, clean_data, get_trading_days, and add_technical_indicator.
__init__ is the constructor for the class, it takes in 5 parameters: data_source, start_date, end_date, time_interval
and **kwargs. It sets these as instance variables, and also initializes several other instance variables such as
dataframe and dictnumpy.
download_data takes in a single parameter, ticker_list which is a list of strings. However, it doesn't have any
implementation.
clean_data method checks if 'date' or 'datetime' column exists in the dataframe and renames it to 'time'. It also
checks the data source, and renames columns accordingly. It drops any NaN values, sorts the dataframe, and makes sure
that the columns have the desired names.
get_trading_days method takes in two parameter start and end and returns a list of strings. It is not supported for
certain data sources.
add_technical_indicator method is used to calculate technical indicators. It takes in two parameters
tech_indicator_list which is a list of strings and select_stockstats_talib which is an integer that defaults to 0.
The method use stockstats/talib package to add technical inidactors to the dataframe.
"""
import numpy as np
import pandas as pd
import stockstats
import talib
from typing import List
class _Base:
def __init__(self, data_source: str, start_date: str, end_date: str, time_interval: str, **kwargs):
self.data_source: str = data_source
self.start_date: str = start_date
self.end_date: str = end_date
self.time_interval: str = time_interval # standard time_interval
# transferred_time_interval will be supported in the future.
# self.nonstandard_time_interval: str = self.calc_nonstandard_time_interval() # transferred time_interval of this processor
self.time_zone: str = ""
self.dataframe: pd.DataFrame = pd.DataFrame()
self.dictnumpy: dict = {} # e.g., self.dictnumpy["open"] = np.array([1, 2, 3]), self.dictnumpy["close"] = np.array([1, 2, 3])
def download_data(self, ticker_list: List[str]):
pass
def clean_data(self):
if "date" in self.dataframe.columns.values.tolist():
self.dataframe.rename(columns={'date': 'time'}, inplace=True)
if "datetime" in self.dataframe.columns.values.tolist():
self.dataframe.rename(columns={'datetime': 'time'}, inplace=True)
if self.data_source == "ccxt":
self.dataframe.rename(columns={'index': 'time'}, inplace=True)
if self.data_source == 'ricequant':
''' RiceQuant data is already cleaned, we only need to transform data format here.
No need for filling NaN data'''
self.dataframe.rename(columns={'order_book_id': 'tic'}, inplace=True)
# raw df uses multi-index (tic,time), reset it to single index (time)
self.dataframe.reset_index(level=[0, 1], inplace=True)
# check if there is NaN values
assert not self.dataframe.isnull().values.any()
elif self.data_source == 'baostock':
self.dataframe.rename(columns={'code': 'tic'}, inplace=True)
self.dataframe.dropna(inplace=True)
# adjusted_close: adjusted close price
if 'adjusted_close' not in self.dataframe.columns.values.tolist():
self.dataframe['adjusted_close'] = self.dataframe['close']
self.dataframe.sort_values(by=['time', 'tic'], inplace=True)
self.dataframe = self.dataframe[['tic', 'time', 'open', 'high', 'low', 'close', 'adjusted_close', 'volume']]
def get_trading_days(self, start: str, end: str) -> List[str]:
if self.data_source in ["binance", "ccxt", "quantconnect", "ricequant", "tushare"]:
print(f"Calculate get_trading_days not supported for {self.data_source} yet.")
return None
# select_stockstats_talib: 0 (stockstats, default), or 1 (use talib). Users can choose the method.
def add_technical_indicator(self, tech_indicator_list: List[str], select_stockstats_talib: int = 0):
"""
calculate technical indicators
use stockstats/talib package to add technical inidactors
:param data: (df) pandas dataframe
:return: (df) pandas dataframe
"""
if "date" in self.dataframe.columns.values.tolist():
self.dataframe.rename(columns={'date': 'time'}, inplace=True)
if self.data_source == "ccxt":
self.dataframe.rename(columns={'index': 'time'}, inplace=True)
self.dataframe.reset_index(drop=False, inplace=True)
if "level_1" in self.dataframe.columns:
self.dataframe.drop(columns=["level_1"], inplace=True)
if "level_0" in self.dataframe.columns and "tic" not in self.dataframe.columns:
self.dataframe.rename(columns={"level_0": "tic"}, inplace=True)
assert select_stockstats_talib in {0, 1}
print("tech_indicator_list: ", tech_indicator_list)
if select_stockstats_talib == 0: # use stockstats
stock = stockstats.StockDataFrame.retype(self.dataframe)
unique_ticker = stock.tic.unique()
for indicator in tech_indicator_list:
print("indicator: ", indicator)
indicator_df = pd.DataFrame()
for i in range(len(unique_ticker)):
try:
temp_indicator = stock[stock.tic == unique_ticker[i]][indicator]
temp_indicator = pd.DataFrame(temp_indicator)
temp_indicator["tic"] = unique_ticker[i]
temp_indicator["time"] = self.dataframe[self.dataframe.tic == unique_ticker[i]][
"time"
].to_list()
indicator_df = indicator_df.append(
temp_indicator, ignore_index=True
)
except Exception as e:
print(e)
if not indicator_df.empty:
self.dataframe = self.dataframe.merge(
indicator_df[["tic", "time", indicator]], on=["tic", "time"], how="left"
)
else: # use talib
final_df = pd.DataFrame()
for i in self.dataframe.tic.unique():
tic_df = self.dataframe[self.dataframe.tic == i]
tic_df['macd'], tic_df['macd_signal'], tic_df['macd_hist'] = talib.MACD(tic_df['close'], fastperiod=12,
slowperiod=26, signalperiod=9)
tic_df['rsi'] = talib.RSI(tic_df['close'], timeperiod=14)
tic_df['cci'] = talib.CCI(tic_df['high'], tic_df['low'], tic_df['close'], timeperiod=14)
tic_df['dx'] = talib.DX(tic_df['high'], tic_df['low'], tic_df['close'], timeperiod=14)
final_df = final_df.append(tic_df)
self.dataframe = final_df
self.dataframe.sort_values(by=["time", "tic"], inplace=True)
time_to_drop = self.dataframe[self.dataframe.isna().any(axis=1)].time.unique()
self.dataframe = self.dataframe[~self.dataframe.time.isin(time_to_drop)]
print("Succesfully add technical indicators")
def add_turbulence(self):
"""
add turbulence index from a precalcualted dataframe
:param data: (df) pandas dataframe
:return: (df) pandas dataframe
"""
# df = data.copy()
# turbulence_index = self.calculate_turbulence(df)
# df = df.merge(turbulence_index, on="time")
# df = df.sort_values(["time", "tic"]).reset_index(drop=True)
# return df
if self.data_source in ["binance", "ccxt", "iexcloud", "joinquant", "quantconnect"]:
print(f"Turbulence not supported for {self.data_source} yet. Return original DataFrame.")
if self.data_source in ["alpaca", "ricequant", "tushare", "wrds", "yahoofinance"]:
turbulence_index = self.calculate_turbulence()
self.dataframe = self.dataframe.merge(turbulence_index, on="time")
self.dataframe.sort_values(["time", "tic"], inplace=True).reset_index(drop=True, inplace=True)
def calculate_turbulence(self, time_period: int = 252) -> pd.DataFrame:
"""calculate turbulence index based on dow 30"""
# can add other market assets
df_price_pivot = self.dataframe.pivot(index="time", columns="tic", values="close")
# use returns to calculate turbulence
df_price_pivot = df_price_pivot.pct_change()
unique_date = self.dataframe['time'].unique()
# start after a year
start = time_period
turbulence_index = [0] * start
# turbulence_index = [0]
count = 0
for i in range(start, len(unique_date)):
current_price = df_price_pivot[df_price_pivot.index == unique_date[i]]
# use one year rolling window to calcualte covariance
hist_price = df_price_pivot[
(df_price_pivot.index < unique_date[i])
& (df_price_pivot.index >= unique_date[i - time_period])
]
# Drop tickers which has number missing values more than the "oldest" ticker
filtered_hist_price = hist_price.iloc[
hist_price.isna().sum().min():
].dropna(axis=1)
cov_temp = filtered_hist_price.cov()
current_temp = (current_price[list(filtered_hist_price)] - np.mean(
filtered_hist_price, axis=0
))
# cov_temp = hist_price.cov()
# current_temp=(current_price - np.mean(hist_price,axis=0))
temp = current_temp.values.dot(np.linalg.pinv(cov_temp)).dot(
current_temp.values.T
)
if temp > 0:
count += 1
# avoid large outlier because of the calculation just begins: else turbulence_temp = 0
turbulence_temp = temp[0][0] if count > 2 else 0
else:
turbulence_temp = 0
turbulence_index.append(turbulence_temp)
turbulence_index = pd.DataFrame(
{"time": df_price_pivot.index, "turbulence": turbulence_index}
)
return turbulence_index
def add_vix(self):
"""
add vix from processors
:param data: (df) pandas dataframe
:return: (df) pandas dataframe
"""
if self.data_source in ['binance', 'ccxt', 'iexcloud', 'joinquant', 'quantconnect', 'ricequant', 'tushare']:
print(f'VIX is not applicable for {self.data_source}. Return original DataFrame')
if self.data_source == 'yahoofinance':
ticker = "^VIX"
elif self.data_source == 'alpaca':
ticker = "VIXY"
elif self.data_source == 'wrds':
ticker = "vix"
else:
return
df = self.dataframe.copy()
self.dataframe = [ticker]
self.download_data(self.start, self.end, self.time_interval)
self.clean_data()
# vix = cleaned_vix[["time", "close"]]
# vix = vix.rename(columns={"close": "VIXY"})
cleaned_vix = self.dataframe.rename(columns={ticker: "vix"})
df = df.merge(cleaned_vix, on="time")
df = df.sort_values(["time", "tic"]).reset_index(drop=True)
self.dataframe = df
def df_to_array(self, tech_indicator_list: List[str], if_vix: bool):
unique_ticker = self.dataframe.tic.unique()
price_array = np.column_stack([self.dataframe[self.dataframe.tic == tic].close for tic in unique_ticker])
common_tech_indicator_list = [i for i in tech_indicator_list if i in self.dataframe.columns.values.tolist()]
tech_array = np.hstack([self.dataframe.loc[(self.dataframe.tic == tic), common_tech_indicator_list] for tic in unique_ticker])
if if_vix:
risk_array = np.column_stack([self.dataframe[self.dataframe.tic == tic].vix for tic in unique_ticker])
else:
risk_array = np.column_stack(
[self.dataframe[self.dataframe.tic == tic].turbulence for tic in unique_ticker]) if "turbulence" in self.dataframe.columns else None
print("Successfully transformed into array")
return price_array, tech_array, risk_array
# standard_time_interval s: second, m: minute, h: hour, d: day, w: week, M: month, q: quarter, y: year
# output time_interval of the processor
def calc_nonstandard_time_interval(self) -> str:
if self.data_source == "alpaca":
pass
elif self.data_source == "baostock":
# nonstandard_time_interval: 默认为d,日k线;d=日k线、w=周、m=月、5=5分钟、15=15分钟、30=30分钟、60=60分钟k线数据,不区分大小写;指数没有分钟线数据;周线每周最后一个交易日才可以获取,月线每月最后一个交易日才可以获取。
pass
time_intervals = ["5m", "15m", "30m", "60m", "1d", "1w", "1M"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
if "d" in self.time_interval or "w" in self.time_interval or "M" in self.time_interval:
return self.time_interval[-1:].lower()
elif "m" in self.time_interval:
return self.time_interval[:-1]
elif self.data_source == "binance":
# nonstandard_time_interval: 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
time_intervals = ["1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "6h", "8h", "12h", "1d", "3d", "1w", "1M"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
return self.time_interval
elif self.data_source == "ccxt":
pass
elif self.data_source == "iexcloud":
time_intervals = ["1d"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
return self.time_interval.upper()
elif self.data_source == "joinquant":
# '1m', '5m', '15m', '30m', '60m', '120m', '1d', '1w', '1M'
time_intervals = ["1m", "5m", "15m", "30m", "60m", "120m", "1d", "1w", "1M"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
return self.time_interval
elif self.data_source == "quantconnect":
pass
elif self.data_source == "ricequant":
# nonstandard_time_interval: 'd' - 天,'w' - 周,'m' - 月, 'q' - 季,'y' - 年
time_intervals = ["d", "w", "M", "q", "y"]
assert self.time_interval[-1] in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
if "M" in self.time_interval:
return self.time_interval.lower()
else:
return self.time_interval
elif self.data_source == "tushare":
# 分钟频度包括1分、5、15、30、60分数据. Not support currently.
# time_intervals = ["1m", "5m", "15m", "30m", "60m", "1d"]
time_intervals = ["1d"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
return self.time_interval
elif self.data_source == "wrds":
pass
elif self.data_source == "yahoofinance":
# nonstandard_time_interval: ["1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "1d", "5d","1wk", "1mo", "3mo"]
time_intervals = ["1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "1d", "5d", "1w", "1M", "3M"]
assert self.time_interval in time_intervals, "This time interval is not supported. Supported time intervals: " + ",".join(time_intervals)
if "w" in self.time_interval:
return self.time_interval + "k"
elif "M" in self.time_interval:
return self.time_interval[: -1] + "mo"
else:
return self.time_interval
else:
raise ValueError(f"Not support transfer_standard_time_interval for {self.data_source}")
# "600000.XSHG" -> "sh.600000"
# "000612.XSHE" -> "sz.000612"
def transfer_standard_ticker_to_nonstandard(self, ticker: str) -> str:
return ticker
def calc_time_zone(ticker_list: List[str], time_zone_selfdefined: str, use_time_zone_selfdefined: int) -> str:
if use_time_zone_selfdefined == 1:
time_zone = time_zone_selfdefined
elif ticker_list in [HSI_50_TICKER, SSE_50_TICKER, CSI_300_TICKER]:
time_zone = TIME_ZONE_SHANGHAI
elif ticker_list in [DOW_30_TICKER, NAS_100_TICKER, SP_500_TICKER]:
time_zone = TIME_ZONE_USEASTERN
elif ticker_list == CAC_40_TICKER:
time_zone = TIME_ZONE_PARIS
elif ticker_list in [DAX_30_TICKER, TECDAX_TICKER, MDAX_50_TICKER, SDAX_50_TICKER]:
time_zone = TIME_ZONE_BERLIN
elif ticker_list == LQ45_TICKER:
time_zone = TIME_ZONE_JAKARTA
else:
raise ValueError("Time zone is wrong.")
return time_zone