-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndicators_90.py
176 lines (135 loc) · 5.86 KB
/
Indicators_90.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
import numpy as np
from sklearn import preprocessing
##################################################################################################################################
# --------------------------------------------------------- INDICATORS --------------------------------------------------------- #
##################################################################################################################################
# ---------------------- SMA ---------------------- #
def SMA(data, smalen):
sma = np.zeros(data.shape)
for i in range(smalen, len(data)):
sma[i] = sum(data[i-smalen+1: i+1]) / smalen
return sma
# ---------------------- EMA ---------------------- #
def EMA(data, alpha):
ema = np.zeros(data.shape)
ema[0] = data[0]
for i in range(1, len(data)):
ema[i] = data[i]*alpha + ema[i-1]*(1-alpha)
return ema
# ---------------------- RSI ---------------------- #
def RSI(data, rsilen):
U = np.zeros(data.shape)
D = np.zeros(data.shape); D[0] = 1
U[1:] = np.maximum(data[1:]-data[0:-1], 0)
D[1:] = np.maximum(data[0:-1]-data[1:], 0)
emaU = EMA(U, 1/rsilen)
emaD = EMA(D, 1/rsilen)
rs = np.divide(emaU, emaD)
rsi = 100 - 100 / (1+rs)
return rsi
# ------------------- Williams_R ------------------ #
def Williams_R(data, willen):
wil = np.zeros((data.shape[0], 1))
for i in range(willen, len(data)):
maxx = max(data[i-willen+1:i+1, 0])
minn = min(data[i-willen+1:i+1, 1])
wil[i] = 100* (data[i, 2] - maxx) / (maxx - minn)
return wil
# -------------------- Momentum ------------------- #
def Momentum(data, momlen):
mom = np.zeros(data.shape)
mom[momlen:] = data[momlen:] - data[0:-momlen]
return mom
# ------------------- Stochastic ------------------ #
def Stochastic(data, periodK):
smoothK = 3
periodD = 3
sto = np.zeros((data.shape[0], 1))
for i in range(periodK, data.shape[0]):
maxx = max(data[i-periodK+1:i+1, 0])
minn = min(data[i-periodK+1:i+1, 1])
sto[i] = 100* (data[i, 2] - minn) / (maxx - minn)
k = SMA(sto, smoothK)
d = SMA(k, periodD)
return k, d
##################################################################################################################################
# ------------------------------------------------------- TEMPORAL SERIES ------------------------------------------------------ #
##################################################################################################################################
def xy_sample(df_data, close_pos, n_indic, SEQ_LEN, FUTURE_PERIOD_BASED, FUTURE_PERIOD_PREDICT):
# ------------------- y vector ------------------ #
y_aux1 = df_data[SEQ_LEN-1+FUTURE_PERIOD_BASED:-FUTURE_PERIOD_PREDICT+FUTURE_PERIOD_BASED, range(close_pos, df_data.shape[1], n_indic)]
y_aux2 = df_data[SEQ_LEN-1+FUTURE_PERIOD_PREDICT:, range(close_pos, df_data.shape[1], n_indic)]
y_sample = np.zeros((y_aux1.shape))
y_sample[y_aux2>y_aux1] = 1
# ------------------- x vector ------------------ #
cases = df_data.shape[0] - FUTURE_PERIOD_PREDICT - SEQ_LEN + 1
x_sample = np.zeros((cases, SEQ_LEN, df_data.shape[1]))
for i in range(SEQ_LEN, df_data.shape[0]-FUTURE_PERIOD_PREDICT+1):
x_sample[i-SEQ_LEN, :, :] = df_data[i-SEQ_LEN:i, :]
oi1 = np.transpose(x_sample, (0, 2, 1))
oi1 = np.squeeze(np.reshape(oi1, (1, cases, n_indic*oi1.shape[2])))
oi1 = np.concatenate((oi1, y_sample), axis=1)
return oi1
##################################################################################################################################
# ----------------------------------------------------------- SCALING ---------------------------------------------------------- #
##################################################################################################################################
# ------------------- percentage scaling ------------------ #
def pct_scale(data):
data1 = data.T
data1 = np.divide(data1[1:, :]-data1[0:-1, :], data1[0:-1, :])
data1 = preprocessing.scale(data1)
data1 = data1.T
return data1
# --------------------- normal scaling -------------------- #
def scl_scale(data):
data1 = data.T
data1 = data1[1:, :]
data1 = preprocessing.scale(data1)
data1 = data1.T
return data1
# ------------------ normal scaling with ------------------ #
def scl_scale_group(data):
data1 = data[:, 1:, :]
data1 = np.transpose(data1, (0, 2, 1))
data1 = np.reshape(data1, (data1.shape[0], data1.shape[1]*data1.shape[2]))
data1 = preprocessing.scale(data1.T)
data1 = data1.T
data1 = np.reshape(data1, (data.shape[0], data.shape[2], data.shape[1]-1))
data1 = np.transpose(data1, (0, 2, 1))
return data1
# ------------------- difference scaling ------------------ #
def dif_scale(data):
data1 = data.T
data1 = data1[1:, :]-data1[0:-1, :]
data1 = preprocessing.scale(data1)
data1 = data1.T
return data1
# -------------------- RSI-like scaling ------------------- #
def rsi_scale(data):
data1 = data.T
data1 = data1[1:, :]
data1 = (data1 - 50) / 100
data1 = data1.T
return data1
# ----------------- Stochastic-like scaling --------------- #
def sto_scale(data):
data1 = data.T
data1 = data1[1:, :]
data1 = (data1 - 50) / 100
data1 = data1.T
return data1
# -------------------- WIL-like scaling ------------------- #
def wil_scale(data):
data1 = data.T
data1 = data1[1:, :]
data1 = (data1 + 50) / 100
data1 = data1.T
return data1
# --------------------- Model scaling --------------------- #
def mdl_scale(data):
data1 = data.T
data1 = data1[1:, :]
data1 = data1 - 0.5
data1 = data1.T
return data1
dispatcher = {'pct':pct_scale, 'scl':scl_scale, 'scl_group':scl_scale_group, 'dif':dif_scale, 'rsi':rsi_scale, 'sto':sto_scale, 'wil':wil_scale, 'mdl':mdl_scale}