-
Notifications
You must be signed in to change notification settings - Fork 0
/
activation_threshold.py
399 lines (294 loc) · 10.8 KB
/
activation_threshold.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import bs4 as bs
import pickle
import re
import requests
import datetime as dt
import os
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
import csv
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
#Ignoring Error warning
pd.options.mode.chained_assignment = None
# Basic initialization
time_steps = 60 # Minimum number of time data required
c = time_steps - 1 # Recent price index
counter = -1 # Counter for number of times data is obtained (Selenium)
style.use("ggplot")
# Choose the method of initialization
LIVE = 0 # 1-Obtain data live, 0-Use pre-existing data (check get_data)
# For pre-existing data or for long data intervals
get_data = 0 # 1-Obtain past data, 0-Use saved data
ticker = 'GC=F' # Stock ticker
start = dt.datetime(2020, 3, 1) # Start time for data from Yahoo
end = dt.datetime(2020, 7, 29) # End time for data from Yahoo
min_per_change = 0.03 # Minimum percentage change of stock price
# Choose the method of initialization
LIVE = 1 # 1-Obtain data live, 0-Use pre-existing data
datafile = '{}.csv'.format(ticker) # The pre-existing datafile or this will be the name of the datafile downloaded if get_data = 1
def write_csv(data):
with open(datafile, 'a') as outfile:
writer = csv.writer(outfile)
writer.writerow(data)
def read_csv():
df = pd.read_csv(datafile, index_col=False)
return df
def moving_average(df_current, counter, diff):
df_ma = df_current
df_7ma = df_ma.rolling(window = 7, min_periods = 0).mean()
df_15ma = df_ma.rolling(window = 15, min_periods = 0).mean()
df_30ma = df_ma.rolling(window = 30, min_periods = 0).mean()
weight_1 = 1
weight_2 = 0
weight_3 = 0.5
weight_4 = 0.15
if (df_7ma.iloc[c] < df_15ma.iloc[c]):
if (diff <= 0):
return -weight_1
elif (diff == 0):
return -weight_3
else:
return weight_4
if (df_7ma.iloc[c] == df_15ma.iloc[c]):
if (diff < 0):
return -weight_3
elif (diff == 0):
return weight_2
else:
return weight_3
if (df_7ma.iloc[c] > df_15ma.iloc[c]):
if (diff < 0):
return -weight_3
elif (diff == 0):
return weight_3
else:
return weight_1
def exp_moving_average(df_current, counter, diff):
df_ema = df_current
df_7ema = df_ema.ewm(span = 7, min_periods = 0).mean()
df_12ema = df_ema.ewm(span = 12, min_periods = 0).mean()
df_15ema = df_ema.ewm(span = 15, min_periods = 0).mean()
df_26ema = df_ema.ewm(span = 26, min_periods = 0).mean()
df_30ema = df_ema.ewm(span = 30, min_periods = 0).mean()
weight_1 = 1
weight_2 = 0
weight_3 = 0.5
weight_4 = 0.15
if (df_7ema.iloc[c] < df_15ema.iloc[c]):
if (diff < 0):
return -weight_1
elif (diff == 0):
return -weight_3
else:
return weight_4
elif (df_7ema.iloc[c] == df_15ema.iloc[c]):
if (diff < 0):
return -weight_3
elif (diff == 0):
return weight_2
else:
return weight_3
elif (df_7ema.iloc[c] > df_15ema.iloc[c]):
if (diff < 0):
return -weight_4
elif (diff == 0):
return weight_3
else:
return weight_1
def MACD(df_current, counter, diff):
df_ema = df_current
df_12ema = df_ema.ewm(span = 12, min_periods = 0).mean()
df_26ema = df_ema.ewm(span = 26, min_periods = 0).mean()
MACD = df_12ema - df_26ema
signal = MACD.ewm(span = 9, min_periods = 0).mean()
weight_1 = 1
weight_2 = 0
weight_3 = 0.5
weight_4 = 0.15
if (MACD.iloc[c] < signal.iloc[c]):
if (diff < 0):
return -weight_1
elif (diff == 0):
return -weight_3
else:
return weight_4
elif (MACD.iloc[c] == signal.iloc[c]):
if (diff < 0):
return -weight_3
elif (diff == 0):
return weight_2
else:
return weight_3
elif (MACD.iloc[c] > signal.iloc[c]):
if (diff < 0):
return -weight_4
elif (diff == 0):
return weight_3
else:
return weight_1
def bol_bands(df_current, counter):
df_ma = df_current
recent_price = df_current.iloc[c]
df_7ma = df_ma.rolling(window = 7, min_periods = 0).mean()
df_7std = df_ma.rolling(window = 7, min_periods = 0).std()
Upper = df_7ma.iloc[c] + (df_7std.iloc[c] * 2)
Lower = df_7ma.iloc[c] - (df_7std.iloc[c] * 2)
if (recent_price < Lower):
return -1
elif (recent_price >= Lower and recent_price <= Upper):
return 0
elif (recent_price > Upper):
return 1
def rel_strength_index(df_current, counter):
UP = 70
LOW = 30
df_high = []
df_low = []
recent_price = df_current.iloc[c]
for j in range(0, len(df_current), 1):
if(df_current.iloc[j] > recent_price):
df_high.append(df_current.iloc[j])
elif(df_current.iloc[j] < recent_price):
df_low.append(df_current.iloc[j])
else:
continue
if (len(df_high) == 0):
df_high.append(0)
if (len(df_low) == 0):
df_low.append(0)
avg_high = sum(df_high) / len(df_high)
avg_low = sum(df_low) / len(df_low)
if (avg_low == 0):
avg_low = 1
RS = avg_high / avg_low
RSI = 100 - (100 / (1 + RS)) # RSI values lie between 0 to 100
if (RSI < LOW):
return 1
elif (RSI >= LOW and RSI <= UP):
return 0
elif (RSI > UP):
return -1
def comm_chann_index(df_current, counter):
CONST = 0.015
UP = 70
LOW = 10
df_ma = df_current
recent_price = df_current.iloc[c]
df_max = max(df_current)
df_min = min(df_current)
df_25ma = df_ma.rolling(window = 25, min_periods = 0).mean()
df_25ma_std = df_ma.rolling(window = 25, min_periods = 0).std()
if (df_25ma_std.iloc[c] == 0):
df_25ma_std.iloc[c] = 1
if (df_25ma_std.iloc[c] == 0):
df_25ma_std.iloc[c] = 1
TP = (df_max + df_min + recent_price) / 3
CCI = (TP - df_25ma.iloc[c]) / (CONST * df_25ma_std.iloc[c]) # returns values between -100 to 100
if (CCI < LOW):
return 1
elif (CCI >= LOW and CCI <= UP):
return 0
elif (CCI > UP):
return -1
def stoch_oscillator(df_current, counter):
UP = 70
LOW = 30
recent_price = df_current.iloc[c]
df_max = max(df_current)
df_min = min(df_current)
denominator = df_max - df_min
if (denominator == 0):
denominator = 1
# K returns values between 0 to 100
K = ((recent_price - df_min) / denominator) * 100
if (K < LOW):
return 1
elif (K >= LOW and K <= UP):
return 0
elif (K > UP):
return -1
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.investing.com/commodities/gold")
i = 1
for i in range(100):
sleep(1)
price = driver.find_element_by_xpath('//*[@id="last_last"]').text
price = float(price.replace(",",""))
i +=1
if (LIVE == 1):
counter += 1
time = dt.datetime.now()
if(counter < time_steps):
write_csv([time, price])
if(counter > time_steps):
write_csv([time, price])
df = read_csv()
# Obtain the min. number of time period data and save it in df_current
df_temp = df[(len(df) - time_steps):]
#Changed for only prices
#df_temp.columns = ['date','price']
#df_temp = df_temp.drop(columns='date')
df_temp.drop(df.columns[0], 1, inplace=True)
#df_current = pd.Series(df_temp['price'])
df_current = df_temp.iloc[:,0]
if (len(df_current) < time_steps):
print("Time steps greater than number of periods of data")
loc_avg = sum(df_current)/ len(df_current)
diff = (df_current.iloc[c] - loc_avg) / 100
if (diff <= 0):
diff = -1
elif (diff > 0 and diff <= min_per_change):
diff = 0
else:
diff = 1
# Technical indicators
ma = moving_average(df_current, counter, diff)
ema = exp_moving_average(df_current, counter, diff)
macd = MACD(df_current, counter, diff)
bb = bol_bands(df_current, counter)
rsi = rel_strength_index(df_current, counter)
cci = comm_chann_index(df_current, counter)
so = stoch_oscillator(df_current, counter)
# Average
threshold = (ma + ema + macd + bb + rsi + cci + so) / 7
# print(threshold)
print(threshold, ma, ema, macd, bb, rsi, cci, so)
else:
if(get_data == 1):
df_web = web.get_data_yahoo(ticker, start, end)
df_web.to_csv(datafile)
df = read_csv()
df.drop(["Date","Open","High","Low","Close","Volume"], 1, inplace=True)
df_temp = df[(len(df) - time_steps):]
df_current = df_temp.iloc[:,0]
if (len(df_current) < time_steps):
if (len(df_current) == 0):
print("Input data set is empty. Check the csv file")
exit()
print("Time steps greater than number of periods of data")
loc_avg = sum(df_current) / len(df_current)
diff = (df_current.iloc[c] - loc_avg) / 100
if (diff <= 0):
diff = -1
elif (diff > 0 and diff <= min_per_change):
diff = 0
else:
diff = 1
counter = 0 # Not applicable for offline data
# Technical indicators
ma = moving_average(df_current, counter, diff)
ema = exp_moving_average(df_current, counter, diff)
macd = MACD(df_current, counter, diff)
bb = bol_bands(df_current, counter)
rsi = rel_strength_index(df_current, counter)
cci = comm_chann_index(df_current, counter)
so = stoch_oscillator(df_current, counter)
# Average
threshold = (ma + ema + macd + bb + rsi + cci + so) / 7
print(threshold)