-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathall_strategies.py
366 lines (305 loc) · 13.1 KB
/
all_strategies.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
import numpy as np
import pandas as pd
import talib
from talib import MA_Type
# 這是我們的策略的部分
# 主要只是要算出進出的訊號 signals 跟何時持有部位 positions
# 底下是一個突破系統的範例
def Breakout_strategy(df):
# Donchian Channel
df['20d_high'] = np.round(pd.Series.rolling(df['Close'], window=20).max(), 2)
df['10d_low'] = np.round(pd.Series.rolling(df['Close'], window=10).min(), 2)
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['Close'][t] > df['20d_high'][t-1]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['Close'][t] < df['10d_low'][t-1]:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def RSI_7030_strategy(df):
df['RSI'] = talib.RSI(df['Close'].values)
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['RSI'][t-1] < 30:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['RSI'][t-1] > 70:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def RSI_8020_strategy(df):
"""
RSI < 20: buy
RSI > 80: sell
"""
df['RSI'] = talib.RSI(df['Close'].values)
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['RSI'][t-1] < 20:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['RSI'][t-1] > 80:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def BBands_strategy(df):
df['UBB'], df['MBB'], df['LBB'] = talib.BBANDS(df['Close'].values, matype=MA_Type.T3)
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['Close'][t] < df['LBB'][t-1]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['Close'][t] > df['UBB'][t-1]:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def 第一組_strategy(df):
close=pd.DataFrame(df["Close"])
short_win = 12 # 短期EMA平滑天数
long_win = 26 # 長期EMA平滑天数
macd_win = 20 # DEA線平滑天数
macd_tmp = talib.MACD( df['Close'].values,fastperiod = short_win ,slowperiod = long_win ,signalperiod = macd_win )
df['DIF'] =macd_tmp [ 0 ]
df['DEA'] =macd_tmp [ 1 ]
df['MACD']=macd_tmp [ 2 ]
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['DIF'][t] > 0 and df['DEA'][t] >0 and df['DIF'][t] > df['DEA'][t] and df['DIF'][t-1]<df['DEA'][t-1]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['DIF'][t] < 0 and df['DEA'][t] < 0 and df['DIF'][t] < df['DEA'][t] and df['DIF'][t-1]>df['DEA'][t-1]:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def Team3_strategy(df):
"""
主要使用BBand + 5MA策略,
中軌為20ma,上下軌為正負1.5sd
# 若5MA開始向上突破下軌,低檔買進
# 若收盤價向下跌破中軌,獲利了結趕快落跑
"""
df['5ma'] = pd.Series.rolling(df['Close'], window=5).mean()
# bbands策略,N=20
df['20ma'] = pd.Series.rolling(df['Close'], window=20).mean()
df['SD'] = pd.Series.rolling(df['Close'], window=20).std()
# 上軌=20ma+1.5sd ,中軌=20ma, 下軌=20ma-1.5sd
df['upbbands'] = df['20ma']+1.5*df['SD']
df['midbbands']=df['20ma']
df['lowbbands'] = df['20ma']-1.5*df['SD']
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if (df['5ma'][t] > df['lowbbands'][t-1]):
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif (df['Close'][t] < df['midbbands'][t-1]):
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def 中山南拳寶寶_strategy(df):
"""
WMSR < 80時進場
WMSR > 20時出場
"""
df['low9'] = df['Low'].rolling(window=9).min()
df['high9'] = df['High'].rolling(window=9).max()
df['WMSR'] = 100*((df['high9'] - df['Close']) / (df['high9'] - df['low9']) )
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['WMSR'][t] < 80:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['WMSR'][t] > 20:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def JuianJuian4715_strategy(df):
"""
##strategy:以20MA為中心,上下各2個標準差為範圍的一個軌道操作方式。
##買進訊號:
#1.價格由下向上 穿越下軌線時,是買進訊號
#2.價格由下向上 穿越中間線時,股價可能加速向上,是加碼買進訊號
#3.價格在中間線與上軌線之間波動時,為多頭市場,可作多
"""
has_position = False
df['signals'] = 0
ave = pd.Series.rolling(df['Close'], window=20).mean()
std = pd.Series.rolling(df['Close'], window=20).std()
df['ave']= pd.Series.rolling(df['Close'], window=20).mean()
df['upper'] = ave + 2*std
df['lower'] = ave -2*std
for t in range(2, df['signals'].size):
if df['upper'][t] > df['ave'][t-1]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['lower'][t] < df['ave'][t-1]:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def 大盜韓不住_strategy(df):
"""
乖離率,乖離率代表的就是投資者的平均報酬率,當股價漲離平均成本很多的時候,
就可能會有大的獲利賣壓出現,讓股價往均線跌回,當股價跌出平均成本太多的時候,攤平或逢低的買盤可能會進入
乖離率<-3% 進場 , >3.5% 出場
"""
has_position = False
df['6d'] = pd.Series.rolling(df['Close'], window=6).mean()
df['BIAS'] = (df['Close'] - df['6d'] )/df['6d']
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['BIAS'][t] < -0.02:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['BIAS'][t] > 0.025:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def 財運滾滾來_strategy(df):
"""
進場訊號:黃金交叉(20MA>60MA)、今日收盤價跌破前日BBANDS下限、乖離率(BIAS)小於-0.05[三項條件同時符合即進場]
出場訊號:死亡交叉(20MA<60MA)、今日收盤價漲破前日BBANDS上限、乖離率(BIAS)大於 0.1[三項條件同時符合即出場]
"""
df['20MA'] = pd.Series.rolling(df['Close'], window=20).mean()
df['60MA'] = pd.Series.rolling(df['Close'], window=60).mean()
df['UBB'], df['MBB'], df['LBB'] = talib.BBANDS(df['Close'].values, matype=MA_Type.T3)
df['BIAS']= (df['Close']-df['20MA'])/df['20MA']
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['20MA'][t] > df['60MA'][t] and df['Close'][t] < df['LBB'][t-1] and df['BIAS'][t] < -0.05:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['20MA'][t] < df['60MA'][t] and df['Close'][t] > df['UBB'][t-1] and df['BIAS'][t] > 0.1:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def team2_strategy(df):
has_position = False
df['signals'] = 0
"""
原先布林逆勢策略是股價上到下突破UBB買進、下到上突破賣出
想透過收盤價下到上突破ubb、上到下突破UBB放慢進出場,搭配乖離率做調整
"""
#
# 進場訊號:1.六日乖離率 < -0.06
# 2.收盤價下到上突破LBB
# 賣出訊號:1.六日乖離率 > 0.06
# 2.價格上到下突破UBB
df['UBB'], df['MBB'], df['LBB'] = talib.BBANDS(df['Close'].values, matype=MA_Type.T3)
df['MA6'] = pd.Series.rolling(df['Close'], window=6).mean()
df['BIAS6']= (df['Close']-df['MA6'])/df['MA6']
for t in range(2, df['signals'].size):
if df['BIAS6'][t] < -0.06 or ( df['Close'][t] > df['LBB'][t] and df['Close'][t-1] < df['LBB'][t-1]) :
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['BIAS6'][t] > 0.06 or ( df['Close'][t-1] > df['UBB'][t-1] and df['Close'][t] > df['UBB'][t] ):
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def LG_minus3_CL(df):
"""
KDJ 策略
進場訊號: 當K>D 值 且J值小於10時進場
出場訊號: 當K<D 值 且J值大於90時出場
KDJ策略為 J<0 進場 J>100出場,但此設定回測結果大部分股票皆無交易訊號,故設定區間為(10,90)
"""
df['Talib_K_index']=talib.STOCH(df["High"].values,df['Low'].values,df['Close'].values )[0]
df['Talib_D_index']=talib.STOCH(df["High"].values,df['Low'].values,df['Close'].values )[1]
df['Talib_J_index']=3*df['Talib_K_index']-2*df['Talib_D_index']
has_position = False
df['signals'] = 0
for t in range(2,df['signals'].size):
if df['Talib_K_index'][t] > df["Talib_D_index"][t] and df['Talib_J_index'][t]<10 :
if not has_position:
df.loc[df.index[t] ,"signals"]=1
has_position =True
elif df['Talib_K_index'][t] <df['Talib_D_index'][t] and df['Talib_J_index'][t]>90:
if has_position:
df.loc[df.index[t],'signals']= -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def Best_strategy(df):
"""
MACD:對長期與短期的移動平均線 收斂或發散的徵兆,加以雙重平滑處理,用來判斷買賣股票的時機與訊號(確定波段漲幅 找到買賣點)
MACD策略:快線 (DIF) 向上突破 慢線 (MACD)。 → 買進訊號
快線 (DIF) 向下跌破 慢線 (MACD)。→ 賣出訊號
"""
df['EMAfast'] = pd.Series.ewm(df['Close'], span = 12).mean()
df['EMAslow'] = pd.Series.ewm(df['Close'], span = 26).mean()
df['DIF'] = (df['EMAfast'] - df['EMAslow'])
df['MACD'] = pd.Series.ewm(df['DIF'], span = 9).mean()
has_position = False
df['signals'] = 0
for t in range(2, df['signals'].size):
if df['DIF'][t-1]<df['MACD'][t] and df['DIF'][t]>df['MACD'][t]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1
has_position = True
elif df['DIF'][t-1]>df['MACD'][t] and df['DIF'][t]<df['MACD'][t]:
if has_position:
df.loc[df.index[t], 'signals'] = -1
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df
def 第六組_strategy(df):
df['MA20'] = np.round(pd.Series.rolling(df['Close'], window=20).max(), 2)
df['MA60'] = np.round(pd.Series.rolling(df['Close'], window=60).min(), 2)
has_position = False
df['signals'] = 0
"""
# 當短周期均線(MA20)由下方突破長周期均線(MA60),即為[黃金交叉],買進
# 反之;當短周期均線(MA20)由下方跌破長周期均線(MA60),即為[死亡交叉],賣出
"""
for t in range(3, df['signals'].size):
if df['MA20'][t-2] < df['MA60'][t-2] and df['MA20'][t-1] > df['MA60'][t-1]:
if not has_position:
df.loc[df.index[t], 'signals'] = 1 #買進的訊號
has_position = True
elif df['MA20'][t-2] > df['MA60'][t-2] and df['MA20'][t-1] < df['MA60'][t-1]:
if has_position:
df.loc[df.index[t], 'signals'] = -1 #賣出的訊號
has_position = False
df['positions'] = df['signals'].cumsum().shift()
return df