-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataPrep.py
233 lines (149 loc) · 6.48 KB
/
DataPrep.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
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import math
def OneD_data(inputfile,columns):
data = pd.read_csv(inputfile, usecols=columns)
df = data.copy()
df=np.array(np.array_split(df,int(len(df)/10))).reshape([int(len(df)/10),10])#testing examples
x_raw =df.copy()
x_norm=df[::2]
#for i in range(0,len(x_norm)):
# x_norm[i]=tf.keras.utils.normalize(x_norm[i]) #make it so the actual value of the currency doesnt matter.
y_norm=[]
for i in range(0,len(df[1::2])):
if max(df[1::2][i]) - df[1::2][i][0] > df[1::2][i][0] - min(df[1::2][i]): #if the max is bigger than the // change is the same still needs implementation
y_norm.append(1)
else:
y_norm.append(-1)
return [x_norm,y_norm,x_raw]
"""def mean_movement(inputfile,columns):
data = pd.read_csv(inputfile, usecols=columns)
df = data.copy()
df = np.array(np.array_split(df, int(len(df) / 10))).reshape([int(len(df) / 10), 10]) # testing examples
x_raw = df.copy()
x_norm = df[::2]
for i in range(0,len(x_norm)):
x_norm[i]=tf.keras.utils.normalize(x_norm[i]) #make it so the actual value of the currency doesnt matter.
y_norm = []
for i in range(0, len(df[1::2])):
y_norm.append(np.mean(df[1::2][i]))
return [x_norm, y_norm, x_raw]"""
def normalize(inp):
A = [i*i for i in inp]
v = (sum(A))**(0.5)
A = [i / v for i in inp]
return A
def relationfy(x):
output = []
no_order=[]
#viz=[]
for j in range(len(x)-1):
temp=[]
#color=(np.random.random(), np.random.random(), np.random.random())
for i in range(j+1,len(x)):
temp.append(x[j]/x[i])
no_order.append(x[j]/x[i])
#viz.append([[j,i],[x[j],x[i]]])
output.append(temp)
return [output,normalize(no_order)]
def mean_mvmnt(inputfile,columns):
data = pd.read_csv(inputfile, usecols=columns)
df = data.copy()
df = np.array(np.array_split(df, int(len(df) / 10))).reshape([int(len(df) / 10), 10]) # testing examples
x_norm = []
for i in range(0, len(df[::2])):
x_norm.append(relationfy(df[::2][i])[1])
y_norm = []
for i in range(0, len(df[1::2])):
y_norm.append(np.mean(normalize(df[1::2][i])))
return [x_norm, y_norm, df]
#print(mean_mvmnt('market_data/AUD_CHF.csv',[4])[0][0:10])
def mean_movement(inputfile,columns):
data = pd.read_csv(inputfile, usecols=columns)
df = data.copy()
df = np.array(np.array_split(df, int(len(df) / 10))).reshape([int(len(df) / 10), 10]) # testing examples
x_raw = df.copy()
for i in range(0, len(df)):
df[i]=normalize(df[i])
x_norm = df[::2]
y_norm = []
for i in range(0, len(df[1::2])):
y_norm.append(np.mean(df[1::2][i]))
return [x_norm, y_norm, df,x_raw]
def data_prep_10(input,am,backward,forward,):
picks = np.random.randint(0+backward,len(input)-forward,size=am)
data = [ [input[picks[i]-backward : picks[i]], 1 if np.mean(input[picks[i]+1 : picks[i]+forward+1])> input[picks[i]] else -1 ] for i in range(len(picks)) ]
return data
print(data_prep_10( np.array(pd.read_csv('EURmajors/EURGBP_H.csv',usecols=[4])),100,10,5))
#print(mean_movement('market_data/AUD_CHF.csv',[4]))
"""
import pickle
def next_candle(data,minus):
picker = np.random.randint(minus + 1, len(data)-1)
imp_value = data[picker][3]
hist_data = np.array(normalize(data[picker - minus:picker] / imp_value)).reshape(minus*4, )
deNormFaktor=10
epi_data = np.array(normalize(data[picker+1] / imp_value))
return [hist_data.tolist(), epi_data.tolist(),deNormFaktor]
ting=next_candle(np.array(pd.read_csv('EURmajors/EURGBP_H.csv',usecols=[1,2,3,4])),10)
print(ting)
data={'x':[],'y':[]}
for x in range(0,1000):
pciked=next_candle(np.array(pd.read_csv('EURmajors/EURGBP_H.csv',usecols=[1,2,3,4])),10)
data['x'].append(pciked[0])
data['y'].append(pciked[1])
pciked = next_candle(np.array(pd.read_csv('EURmajors/EURUSD_H.csv', usecols=[1,2,3,4])), 10)
data['x'].append(pciked[0])
data['y'].append(pciked[1])
pciked = next_candle(np.array(pd.read_csv('EURmajors/EURAUD_H.csv', usecols=[1,2,3,4])), 10)
data['x'].append(pciked[0])
data['y'].append(pciked[1])
with open('nn.pkl', 'wb') as f:
pickle.dump(data, f)"""
#df.to_csv('market_data/EUR_USD_D_corrected.csv')
def long_short_ident(data,minus,plus):
picker = np.random.randint(minus+1,len(data)-plus-1)
imp_value = data[picker]
hist_data = np.array(normalize(data[picker-minus:picker]/imp_value)).reshape(minus,)
bigger=np.array(np.where(imp_value<np.array(data[picker+1:picker+plus+1]))[0])
smaller=np.array(np.where(imp_value>np.array(data[picker+1:picker+plus+1]))[0])
#print(smaller.size/plus,bigger.size/plus)
if bigger.size < smaller.size :
epi_data=[1.0,0.0]#DO NOTHING
elif bigger.size > smaller.size:
epi_data = [0.0, 1.0]
else:
epi_data=[0.0,0.0]
#epi_data=[bigger.size/plus,smaller.size/plus]
#print(imp_value,np.array(data[picker + 1:picker + plus + 1]), np.array(np.where(imp_value < np.array(data[picker + 1:picker + plus + 1]))[0]))
return [imp_value, hist_data.tolist(), epi_data]
"""
data={'x':[],'y':[]}
for x in range(0,500):
pciked=long_short_ident(np.array(pd.read_csv('EURmajors/EURGBP_H.csv',usecols=[3])),10,6)
data['x'].append(pciked[1])
data['y'].append(pciked[2])
pciked = long_short_ident(np.array(pd.read_csv('EURmajors/EURUSD_H.csv', usecols=[3])), 10, 6)
data['x'].append(pciked[1])
data['y'].append(pciked[2])
pciked = long_short_ident(np.array(pd.read_csv('EURmajors/EURAUD_H.csv', usecols=[3])), 10, 6)
data['x'].append(pciked[1])
data['y'].append(pciked[2])
with open('nn.pkl', 'wb') as f:
pickle.dump(data, f)
"""
def angular_price_movement(inputfile,columns):
data = pd.read_csv(inputfile, usecols=columns)
df = data.copy()
df=np.array(np.array_split(df,int(len(df)/10))).reshape([int(len(df)/10),10])#testing examples
x_raw =df.copy()
x_norm=df[::2]
#for i in range(0,len(x_norm)):
# x_norm[i]=tf.keras.utils.normalize(x_norm[i]) #make it so the actual value of the currency doesnt matter.
y_norm=[]
for i in range(0, len(df[1::2]) ):
dom_change=max(max(df[1::2][i])-df[1::2][i][0] , df[1::2][i][0]-min(df[1::2][i]))
y_norm.append(math.asin( dom_change / abs(df[1::2][i][0] - dom_change) ) )
return [x_norm,y_norm,x_raw]