-
Notifications
You must be signed in to change notification settings - Fork 19
/
Signal_generator.py
401 lines (373 loc) · 13.7 KB
/
Signal_generator.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 26 15:13:19 2019
@author: kennedy
"""
__author__ = "kennedy Czar"
__email__ = "kennedyczar@gmail.com"
__version__ = '1.0'
seed = 1333
from numpy.random import seed
seed(19)
from tensorflow import set_random_seed
set_random_seed(19)
import os
from STOCK import stock, loc
import pandas as pd
pd.options.mode.chained_assignment = None
import numpy as np
import lightgbm as lgb
from datetime import datetime
import matplotlib.pyplot as plt
from Preprocess import process_time
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.ensemble import (AdaBoostRegressor, #Adaboost regressor
RandomForestRegressor, #Random forest regressor
GradientBoostingRegressor, #Gradient boosting
BaggingRegressor, #Bagging regressor
ExtraTreesRegressor) #Extratrees regressor
#get ojects in the dataset folder and
#strip extension
def ls_STOK():
'''
:Return:
List of stock in dataset
'''
DIR_OBJ = os.listdir()
STOK_list_ = []
for x in range(len(DIR_OBJ)):
STOK_list_.append(DIR_OBJ[x].strip('.csv'))
return STOK_list_
#%% SIGNAL GENERATOR --> MACD, BOLLINGER BAND, RSI, SUPERTREND
##RSI signal
def RSI_signal(STK_data, period, lw_bound, up_bound):
'''
:Arguments:
df: stock data
:Return type:
signal
'''
stock_data = stock(STK_data)
OHLC = stock_data.OHLC()
df = stock_data.CutlerRSI(OHLC, period)
assert isinstance(df, pd.Series) or isinstance(df, pd.DataFrame)
#convert to dataframe
if isinstance(df, pd.Series):
df = df.to_frame()
else:
pass
#get signal
#1--> indicates buy position
#0 --> indicates sell posotion
df['signal'] = np.zeros(df.shape[0])
pos = 0
for ij in df.loc[:, ['RSI_Cutler_'+str(period)]].values:
print(df.loc[:, ['RSI_Cutler_'+str(period)]].values[pos])
if df.loc[:, ['RSI_Cutler_'+str(period)]].values[pos] >= up_bound:
df['signal'][pos:] = 1 #uptrend
elif df.loc[:, ['RSI_Cutler_'+str(period)]].values[pos] <= lw_bound:
df['signal'][pos:] = 0 #downtrend
pos +=1
print('*'*40)
print('RSI Signal Generation completed')
print('*'*40)
return df
def macd_crossOver(STK_data, fast, slow, signal):
'''
:Argument:
MACD dataframe
:Return type:
MACD with Crossover signal
'''
stock_data = stock(STK_data)
df = stock_data.MACD(fast, slow, signal)
try:
assert isinstance(df, pd.DataFrame) or isinstance(df, pd.Series)
#dataframe
if isinstance(df, pd.Series):
df = df.to_frame()
else:
pass
#1--> indicates buy position
#0 --> indicates sell posotion
df['result'] = np.nan
df['signal'] = np.where(df.MACD > df.MACD_SIGNAL, 1, 0)
df['result'] = np.where((df['signal'] == 1) & (df['MACD_HIST'] >= 0), 1, 0)
except IOError as e:
raise('Dataframe required {}' .format(e))
finally:
print('*'*40)
print('MACD signal generated')
print('*'*40)
return df
def SuperTrend_signal(STK_data, multiplier, period):
'''
:Argument:
SuperTrend dataframe
:Return type:
Super trend signal
'''
stock_data = stock(STK_data)
df = stock_data.SuperTrend(STK_data, multiplier, period)
try:
assert isinstance(df, pd.DataFrame) or isinstance(df, pd.Series)
#dataframe
if isinstance(df, pd.Series):
df = df.to_frame()
else:
pass
#1--> indicates buy position
#0 --> indicates sell posotion
df = df.fillna(0)
df['signal'] = np.nan
df['signal'] = np.where(stock_data.Close >= df.SuperTrend, 1, 0)
except IOError as e:
raise('Dataframe required {}' .format(e))
finally:
print('*'*40)
print('SuperTrend Signal generated')
print('*'*40)
return df
def bollinger_band_signal(STK_data, period, deviation, strategy = ''):
'''
:Argument:
df: stock data
:Return type:
:bollinger band signal
'''
stock_data = stock(STK_data)
Close = stock_data.Close
df = stock_data.Bolinger_Band(period, deviation)
df = df.fillna(value = 0)
assert isinstance(df, pd.DataFrame) or isinstance(df, pd.Series)
#dataframe
if isinstance(df, pd.Series):
df = df.to_frame()
#get signal
#1--> indicates buy position
#0 --> indicates sell posotion
df['signal'] = np.zeros(df.shape[0])
pos = 0
if strategy == '' or strategy == '0' or strategy == '2':
for ii in Close:
print(Close[pos])
if Close[pos] >= df.Upper_band.values[pos]:
df['signal'][pos:] = 0
elif Close[pos] <= df.Lower_band.values[pos]:
df['signal'][pos:] = 1
pos += 1
elif strategy == '1' or strategy == '3':
for ii in Close:
print(Close[pos])
if Close[pos] >= df.Upper_band.values[pos]:
df['signal'][pos:] = 1
elif Close[pos] <= df.Lower_band.values[pos]:
df['signal'][pos:] = 0
pos += 1
else:
raise('You have entered an incorrect strategy value')
print('*'*40)
print('Bollinger Signal Generation completed')
print('*'*40)
return df
def trading_signal(RSI, MACD, Bollinger_Band, SuperTrend = None, strategy = ''):
'''
:Arguments:
:MACD:
dataframe containing MACD signal
:Bollinger_Band:
dataframe containing Bollinger band signal
:RSI:
dataframe containing RSI signal
:Return Type:
Buy Sell or Hold signal
'''
MACD_signal = MACD.signal.values
RSI_signal = RSI.signal.values
BB_signal = Bollinger_Band.signal.values
if strategy == '' or strategy == '0' or strategy == '1':
df_prediction = pd.DataFrame({'MACD_signal': MACD_signal,
'RSI_signal': RSI_signal,
'BB_signal': BB_signal})
else:
SuperTrend_Signal = SuperTrend.signal.values
df_prediction = pd.DataFrame({'MACD_signal': MACD_signal,
'RSI_signal': RSI_signal,
'BB_signal': BB_signal,
'SuperTrend_signal': SuperTrend_Signal})
df_prediction['POSITION'] = ''
if strategy == '' or strategy == '0':
print('Calling default strategy')
for ij in range(data.shape[0]):
print(ij)
if MACD_signal[ij] == 1 and\
RSI_signal[ij] == 1 and\
BB_signal[ij] == 1:
df_prediction.POSITION[ij] = 'BUY'
elif MACD_signal[ij] == 0 and\
RSI_signal[ij] == 0 and\
BB_signal[ij] == 0:
df_prediction.POSITION[ij] = 'SELL'
else:
df_prediction.POSITION[ij] = 'HOLD'
elif strategy == '1':
print('Calling strategy %s'%strategy)
for ij in range(data.shape[0]):
print(ij)
if MACD_signal[ij] == 1 and\
RSI_signal[ij] == 1 and\
BB_signal[ij] == 1:
df_prediction.POSITION[ij] = 'BUY'
elif MACD_signal[ij] == 0 and\
RSI_signal[ij] == 0 and\
BB_signal[ij] == 0:
df_prediction.POSITION[ij] = 'SELL'
else:
df_prediction.POSITION[ij] = 'HOLD'
elif strategy == '2':
print('Calling strategy %s'%strategy)
for ij in range(data.shape[0]):
print(ij)
if MACD_signal[ij] == 1 and\
RSI_signal[ij] == 1 and\
BB_signal[ij] == 1 and\
SuperTrend_Signal[ij] == 1:
df_prediction.POSITION[ij] = 'BUY'
elif MACD_signal[ij] == 0 and\
RSI_signal[ij] == 0 and\
BB_signal[ij] == 0 and\
SuperTrend_Signal[ij] == 0:
df_prediction.POSITION[ij] = 'SELL'
else:
df_prediction.POSITION[ij] = 'HOLD'
elif strategy == '3':
print('Calling strategy %s'%strategy)
for ij in range(data.shape[0]):
print(ij)
if MACD_signal[ij] == 1 and\
RSI_signal[ij] == 1 and\
BB_signal[ij] == 1 and\
SuperTrend_Signal[ij] == 1:
df_prediction.POSITION[ij] = 'BUY'
elif MACD_signal[ij] == 0 and\
RSI_signal[ij] == 0 and\
BB_signal[ij] == 0 and\
SuperTrend_Signal[ij] == 0:
df_prediction.POSITION[ij] = 'SELL'
else:
df_prediction.POSITION[ij] = 'HOLD'
#-----------------------------------------------------------
#reset column and save to throw to csv
if strategy == '' or strategy == '0' or strategy == '1':
enlist = ['BB_signal', 'MACD_signal' , 'RSI_signal','POSITION']
df_prediction = df_prediction.reindex(columns=enlist)
else:
enlist = ['BB_signal', 'MACD_signal' , 'RSI_signal', 'SuperTrend_signal','POSITION']
df_prediction = df_prediction.reindex(columns=enlist)
print('*'*40)
print('Signal generation completed...')
print('*'*40)
return df_prediction
if __name__ == '__main__':
'''
----------------------------------
# Trading strategy
------------------------------------
[X][STRATEGY 0 or ''] --> USES DEFAULT BOLLINGER BAND:: BUY WHEN CLOSE IS BELOW LOWER BOLLINGER
SELL WHEN CLOSE IS ABOVE UPPER BOLLINGER BAND
[X][STRATEGY 1] --> SETS BOLLINGER TO:: BUY WHEN CLOSE IS ABOVE UPPER BOLLINGER BAND
AND SELL WHEN CLOSE IS BELOW LOWER BOLLINGER BAND.
[X][STRATEGY 2] --> USES STRATEGY 0 WITH SUPER TREND INDICATOR
[X][STRATEGY 3] --> USES STRATEGY 1 WITH SUPER TREND INDICATOR
'''
#---------GLOBAL SETTINGS-------------------
path = 'D:\\BITBUCKET_PROJECTS\\Forecasting 1.0\\'
STRATEGY = '3'
DEVIATION = MULTIPLIER = 2
PERIOD = 20
#--------RSI_SETTINGS------------------------
LOWER_BOUND = 30
UPPER_BOUND = 70
MIDLINE = 0
FILLCOLOR = 'skyblue'
#--------MACD SETTINGS-----------------------
FAST = 12
SLOW = 26
SIGNAL = 9
loc.set_path(path+'DATASET')
#-------get the data we need------------------
STOCK_NAME = 'MSFT.MX'
STOCK_list_ = ls_STOK()
data = loc.read_csv( STOCK_NAME+ str('.csv'))
data.index = pd.to_datetime(data.index)
#-----convert to the stock class--------------
stock_data = stock(data)
Fibo_SUP_RES_ = stock_data.fibonacci_pivot_point()
df_ketner = stock_data.Keltner_channel(data, PERIOD, PERIOD, MULTIPLIER)
df_RSI = RSI_signal(data, PERIOD, lw_bound = LOWER_BOUND, up_bound = UPPER_BOUND)
df_MACD = macd_crossOver(data, FAST, SLOW, SIGNAL)
df_BB = bollinger_band_signal(data, PERIOD, deviation = DEVIATION, strategy = STRATEGY)
#-----select strategy for saving-------------------
if STRATEGY == '2' or STRATEGY == '3':
df_STrend = SuperTrend_signal(data, MULTIPLIER, PERIOD)
prediction = trading_signal(df_RSI, df_MACD, df_BB, df_STrend, STRATEGY)
prediction.set_index(data.index, inplace = True)
prediction = pd.concat([Fibo_SUP_RES_, prediction], axis = 1)
loc.set_path(path+ 'PREDICTED')
prediction.to_csv('{}'.format(STOCK_NAME)+ '.csv', mode='w')
else:
prediction = trading_signal(df_RSI, df_MACD, df_BB, STRATEGY)
prediction.set_index(data.index, inplace = True)
prediction = pd.concat([Fibo_SUP_RES_, prediction], axis = 1)
loc.set_path(path+ 'PREDICTED')
prediction.to_csv('{}'.format(STOCK_NAME)+ '.csv', mode='w')
#---------------------------------------
if STRATEGY == '2' or STRATEGY == '3':
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex = True)
ax1.plot(data.index, df_MACD.MACD, lw = .5)
ax1.plot(data.index, df_MACD.MACD_HIST, lw = .5)
ax1.axhline(y = MIDLINE, linewidth = .5, color='g')
ax1.plot(data.index, df_MACD.MACD_SIGNAL, lw = .5)
ax1.fill_between(data.index, df_MACD.MACD_HIST, 0, where=(df_RSI.iloc[:, 0] >= 0), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax1.fill_between(data.index, df_MACD.MACD_HIST, 0, where=(df_RSI.iloc[:, 0] <= 0), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax1.legend(loc="upper left")
ax2.plot(data.index, df_RSI.iloc[:, 0], lw = .5)
ax2.axhline(y = UPPER_BOUND, linewidth=1, color='r')
ax2.axhline(y = LOWER_BOUND, linewidth=1, color='g')
ax2.fill_between(data.index, df_RSI.iloc[:, 0], 70, where=(df_RSI.iloc[:, 0] >= 70), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax2.fill_between(data.index, df_RSI.iloc[:, 0], 30, where=(df_RSI.iloc[:, 0] <= 30), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax2.legend(loc="upper left")
ax3.plot(data.index, data.Close, lw = .5)
ax3.plot(data.index, df_STrend.SuperTrend, lw = .5)
ax3.legend(loc="upper left")
ax4.plot(data.index, prediction.MACD_signal, lw = .5)
ax4.plot(data.index, prediction.RSI_signal, lw = .5)
ax4.plot(data.index, prediction.SuperTrend_signal, lw = .5)
ax4.legend(loc="upper left")
ax1.set_title('{} SIGNAL'.format(STOCK_NAME.strip('.MX')))
plt.show()
else:
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex = True)
ax1.plot(data.index, df_MACD.MACD, lw = .5)
ax1.plot(data.index, df_MACD.MACD_HIST, lw = .5)
ax1.axhline(y = MIDLINE, linewidth = .5, color='g')
ax1.plot(data.index, df_MACD.MACD_SIGNAL, lw = .5)
ax1.fill_between(data.index, df_MACD.MACD_HIST, 0, where=(df_RSI.iloc[:, 0] >= 0), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax1.fill_between(data.index, df_MACD.MACD_HIST, 0, where=(df_RSI.iloc[:, 0] <= 0), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax1.legend(loc="upper left")
ax2.plot(data.index, df_RSI.iloc[:, 0], lw = .5)
ax2.axhline(y = UPPER_BOUND, linewidth=1, color='r')
ax2.axhline(y = LOWER_BOUND, linewidth=1, color='g')
ax2.fill_between(data.index, df_RSI.iloc[:, 0], 70, where=(df_RSI.iloc[:, 0] >= 70), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax2.fill_between(data.index, df_RSI.iloc[:, 0], 30, where=(df_RSI.iloc[:, 0] <= 30), facecolor=FILLCOLOR, edgecolor=FILLCOLOR)
ax2.legend(loc="upper left")
ax3.plot(data.index, data.Close, lw = .5)
ax3.legend(loc="upper left")
ax4.plot(data.index, prediction.MACD_signal, lw = .5)
ax4.plot(data.index, prediction.RSI_signal, lw = .5)
ax4.legend(loc="upper left")
ax1.set_title('{} SIGNAL'.format(STOCK_NAME.strip('.MX')))
plt.show()