-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrategies.py
536 lines (411 loc) · 20.9 KB
/
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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import numpy as np
from broker import *
from _config import *
# ==================================================================================================
# global variables
# ==================================================================================================
''' moved to _config.py
DEBUG = True
'''
STRATEGIES = {
"Donchian's Four Weel Rule" : {
'name' : 'donchian',
'text' : "Donchian's Four Weel Rule",
'ind_1' : '4 Week High',
'ind_2' : '4 Week Low',
},
"Dreyfus's 52 Week Rule" : {
'name' : 'dreyfus',
'text' : "Dreyfus's 52 Week Rule",
'ind_1' : '52 Week High',
'ind_2' : '52 Week Low',
},
"Golden Cross 20v200 SMA" : {
'name' : 'goldencross',
'text' : "Golden Cross 20v200 SMA",
'ind_1' : 'Fast SMA',
'ind_2' : 'Slow SMA',
},
}
# ==================================================================================================
# define one function per strategy
# ==================================================================================================
""" Iterate through len-1 rows
1. process orders
2. check the trading strategy for 'buy' or 'sell' indicators
"""
''' STRATEGY 1 '''
def execute_donchians_strategy(broker, orders, positions, stocks):
weekly_rolling_high = 20 # 20 periods is ~4 weeks
weekly_rolling_low = 20 # same as above
# generate rolling highs and lows
stocks['Indicator_1'] = stocks['Close'].rolling(weekly_rolling_high).max()
stocks['Indicator_2'] = stocks['Close'].rolling(weekly_rolling_low).min()
# generate buy / sell signal columns
stocks['BuySignal'] = np.NaN
stocks['SellSignal'] = np.NaN
# iterate over the dataframe starting with the second row
for i in range(1, len(stocks)) :
# ------------------------------------------------------------------------------------------
# update the brokerage acount total value and cash value so we're prepared for any trading
yesterday = stocks.Date[i-1]
today = stocks.Date[i]
# initialize today's total value and total cash based on yesterday's values
broker_idx = len(broker)
broker.loc[broker_idx] = [today, broker.iloc[broker_idx-1,1], broker.iloc[broker_idx-1,2]]
# doing this so we KNOW our brokerage account contains an entry for today
# ------------------------------------------------------------------------------------------
# process any open orders
broker, orders, positions = process_orders(broker, orders, positions, stocks, i)
# get any open positions
open_positions = positions.loc[positions.Status=='open']
# REPLACE ME -- this comment and print() should be a log statement
# print info about the STOCK_DF that we're processing
if DEBUG:
print(f'strategy ------- stock idx: {i}, # open positions = {len(open_positions)}, close: {stocks.Close[i]}, rolling low: {stocks.Indicator_2[i]}, rolling high: {stocks.Indicator_1[i]}, ')
# ------------------------------------------------------------------------------------------
# Execute our Trading Strategy
# ------------------------------------------------------------------------------------------
"""
IF no open positions, THEN
IF no recent buy signals, AND
the Close price meets the Rolling High
Create a buy order
Set the Buy Signal to the Close price
ELSE IF no recent sell signals, AND
the Close price meets the Rolling Low
Create a sell order
Set the Sell Signal to the Close price
Else If open positions exist, THEN
Do the same as above, except
Create a close order
Create a buy/sell order (as appropriate)
Set the Buy/Sell Signal to the Close price
"""
# ------------------------------------------------------------------------------------------
# IF there's NO open positions, THEN...
if (len(open_positions) <= 0) :
# Confirm there's NO prior buy signal, AND
# Check if 'Close' has met the 'Rolling High'
if (np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_1[i]) :
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
elif (np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_2[i]) :
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
# ELSE IF there are open positions, THEN...
elif (0 < len(open_positions) < 2) :
# REPLACE ME -- this comment and print() should be a log statement
# print info about the STOCK_DF that we're processing
# print('positions type =', positions.loc[positions['Status']=='open']['Type'])
# get the index for the open position
pos_idx = open_positions.index[0]
if (open_positions.Type[pos_idx] is not 'long') and \
(np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_1[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
elif (open_positions.Type[pos_idx] is not 'short') and \
(np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_2[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
elif (len(open_positions) > 1) :
if DEBUG:
print(f'strategy ------- there\'s mutliple open positions - what do we do???')
# ------------------------------------------------------------------------------------------
# update the positions's unrealized value and the brokerage account's total value
if (len(positions) > 0) and (positions.iloc[-1].Status=='open'):
# POSITION_COLUMNS = ['Date','Position','Price','Cost','Type','Status','Unrealized','Realized']
# Unrealized = 6
# update the open position's Unrealized(gain)
# Unrealized(gain) = Cost - (Position * Closing Price)
positions.iloc[-1,6] = positions.iloc[-1].Cost - (abs(positions.iloc[-1].Position) * stocks.iloc[i].Close)
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update positions - row: {positions.index[-1]}, date: {broker.iloc[-1].Date}, cost: {positions.iloc[-1].Cost}, unrlz: {positions.iloc[-1].Unrealized}, rlizd: {positions.iloc[-1].Realized} ')
# BROKER_COLUMNS = ['Date','TotalCash','CashValue']
# update the brokerage account's total value
broker.iloc[-1].TotalValue = broker.iloc[-1].TotalCash + positions.iloc[-1].Cost + positions.iloc[-1].Unrealized
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update broker -- row: {broker.index[-1]}, date: {broker.iloc[-1].Date}, cash: {broker.iloc[-1].TotalCash}, value: {broker.iloc[-1].TotalValue}, ')
#---- end FOR loop
if DEBUG:
print('--------------------------------------------------------------------------------')
print(stocks.tail(50))
print(orders.tail(20))
print(positions.tail(20))
print(broker.tail(50))
print('--------------------------------------------------------------------------------')
return broker, orders, positions, stocks
''' STRATEGY 2 '''
def execute_dreyfus_strategy(broker, orders, positions, stocks):
weekly_rolling_high = 250 # 250 periods is ~52 weeks
weekly_rolling_low = 250 # same as above
# generate rolling highs and lows
stocks['Indicator_1'] = stocks['Close'].rolling(weekly_rolling_high).max()
stocks['Indicator_2'] = stocks['Close'].rolling(weekly_rolling_low).min()
# generate buy / sell signal columns
stocks['BuySignal'] = np.NaN
stocks['SellSignal'] = np.NaN
# iterate over the dataframe starting with the second row
for i in range(1, len(stocks)) :
# ------------------------------------------------------------------------------------------
# update the brokerage acount total value and cash value so we're prepared for any trading
yesterday = stocks.Date[i-1]
today = stocks.Date[i]
# initialize today's total value and total cash based on yesterday's values
broker_idx = len(broker)
broker.loc[broker_idx] = [today, broker.iloc[broker_idx-1,1], broker.iloc[broker_idx-1,2]]
# doing this so we KNOW our brokerage account contains an entry for today
# ------------------------------------------------------------------------------------------
# process any open orders
broker, orders, positions = process_orders(broker, orders, positions, stocks, i)
# get any open positions
open_positions = positions.loc[positions.Status=='open']
# REPLACE ME -- this comment and print() should be a log statement
# print info about the STOCK_DF that we're processing
if DEBUG:
print(f'strategy ------- stock idx: {i}, # open positions = {len(open_positions)}, close: {stocks.Close[i]}, rolling low: {stocks.Indicator_2[i]}, rolling high: {stocks.Indicator_1[i]}, ')
# ------------------------------------------------------------------------------------------
# Execute our Trading Strategy
# ------------------------------------------------------------------------------------------
"""
IF no open positions, THEN
IF no recent buy signals, AND
the Close price meets the Rolling High
Create a buy order
Set the Buy Signal to the Close price
ELSE IF no recent sell signals, AND
the Close price meets the Rolling Low
Create a sell order
Set the Sell Signal to the Close price
Else If open positions exist, THEN
Do the same as above, except
Create a close order
Create a buy/sell order (as appropriate)
Set the Buy/Sell Signal to the Close price
"""
# ------------------------------------------------------------------------------------------
# IF there's NO open positions, THEN...
if (len(open_positions) <= 0) :
# Confirm there's NO prior buy signal, AND
# Check if 'Close' has met the 'Rolling High'
if (np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_1[i]) :
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
elif (np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_2[i]) :
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
# ELSE IF there are open positions, THEN...
elif (0 < len(open_positions) < 2) :
# REPLACE ME -- this comment and print() should be a log statement
# print info about the STOCK_DF that we're processing
# print('positions type =', positions.loc[positions['Status']=='open']['Type'])
# get the index for the open position
pos_idx = open_positions.index[0]
if (open_positions.Type[pos_idx] is not 'long') and \
(np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_1[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
elif (open_positions.Type[pos_idx] is not 'short') and \
(np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Close[i]==stocks.Indicator_2[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
elif (len(open_positions) > 1) :
if DEBUG:
print(f'strategy ------- there\'s mutliple open positions - what do we do???')
# ------------------------------------------------------------------------------------------
# update the positions's unrealized value and the brokerage account's total value
if (len(positions) > 0) and (positions.iloc[-1].Status=='open'):
# POSITION_COLUMNS = ['Date','Position','Price','Cost','Type','Status','Unrealized','Realized']
# Unrealized = 6
# update the open position's Unrealized(gain)
# Unrealized(gain) = Cost - (Position * Closing Price)
positions.iloc[-1,6] = positions.iloc[-1].Cost - (abs(positions.iloc[-1].Position) * stocks.iloc[i].Close)
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update positns - row: {positions.index[-1]}, date: {broker.iloc[-1].Date}, cost: {positions.iloc[-1].Cost}, unrlz: {positions.iloc[-1].Unrealized}, rlizd: {positions.iloc[-1].Realized} ')
# BROKER_COLUMNS = ['Date','TotalCash','CashValue']
# update the brokerage account's total value
broker.iloc[-1].TotalValue = broker.iloc[-1].TotalCash + positions.iloc[-1].Cost + positions.iloc[-1].Unrealized
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update broker -- row: {broker.index[-1]}, date: {broker.iloc[-1].Date}, cash: {broker.iloc[-1].TotalCash}, value: {broker.iloc[-1].TotalValue}, ')
#---- end FOR loop
if DEBUG:
print('--------------------------------------------------------------------------------')
print(stocks.tail(50))
print(orders.tail(20))
print(positions.tail(20))
print(broker.tail(50))
print('--------------------------------------------------------------------------------')
return broker, orders, positions, stocks
''' STRATEGY 3 '''
def execute_golden_cross_strategy(broker, orders, positions, stocks):
fast_sma = 20 # 20 periods is ~4 weeks / 1 month
slow_sma = 200 # 200 periods is most of the year
# generate fast and slow SMAs
stocks['Indicator_1'] = stocks['Close'].rolling(fast_sma).mean()
stocks['Indicator_2'] = stocks['Close'].rolling(slow_sma).mean()
# generate buy / sell signal columns
stocks['BuySignal'] = np.NaN
stocks['SellSignal'] = np.NaN
# iterate over the dataframe starting with the second row
for i in range(1, len(stocks)) :
# ------------------------------------------------------------------------------------------
# update the brokerage acount total value and cash value so we're prepared for any trading
yesterday = stocks.Date[i-1]
today = stocks.Date[i]
# initialize today's total value and total cash based on yesterday's values
broker_idx = len(broker)
broker.loc[broker_idx] = [today, broker.iloc[broker_idx-1,1], broker.iloc[broker_idx-1,2]]
# doing this so we KNOW our brokerage account contains an entry for today
# ------------------------------------------------------------------------------------------
# process any open orders
broker, orders, positions = process_orders(broker, orders, positions, stocks, i)
# get any open positions
open_positions = positions.loc[positions.Status=='open']
# REPLACE ME -- this comment and print() should be a log statement
# print info about the STOCK_DF that we're processing
if DEBUG:
print(f'strategy ------- stock idx: {i}, # open positions = {len(open_positions)}, close: {stocks.Close[i]}, slow sma: {stocks.Indicator_2[i]}, fast sma: {stocks.Indicator_1[i]}, ')
# ------------------------------------------------------------------------------------------
# Execute our Trading Strategy
# ------------------------------------------------------------------------------------------
"""
IF no open positions, THEN
IF no recent buy signals, AND
the Fast SMA crosses the Slow SMA to the upside
Create a buy order
Set the Buy Signal to the Close price
ELSE IF no recent sell signals, AND
the Fast SMA crosses the Slow SMA to the downside
Create a sell order
Set the Sell Signal to the Close price
Else If open positions exist, THEN
Do the same as above, except
Create a close order
Create a buy/sell order (as appropriate)
Set the Buy/Sell Signal to the Close price
"""
# ------------------------------------------------------------------------------------------
# IF there's NO open positions, THEN...
if (len(open_positions) <= 0) :
# Confirm there's NO prior BUY signal, AND
# Check if the Fast SMA (Indicator_1) has crossed Slow SMA (Indicator_2) to the updside
if (np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Indicator_1[i]>=stocks.Indicator_2[i]) :
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
# Confirm there's NO prior SELL signal, AND
# Check if the Fast SMA (Indicator_1) has crossed Slow SMA (Indicator_2) to the downside
elif (np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Indicator_1[i]<=stocks.Indicator_2[i]) :
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
# ELSE IF there are open positions, THEN...
elif (0 < len(open_positions) < 2) :
# get the index for the open position
pos_idx = open_positions.index[0]
# Confirm the current open position is SHORT, AND
# that there's NO recent BUY signal, AND
# that Fast SMA (Indicator_1) has crossed Slow SMA (Indicator_2) to the updside
if (open_positions.Type[pos_idx] is not 'long') and \
(np.isnan(stocks.BuySignal[i-1])) and \
(stocks.Indicator_1[i]>=stocks.Indicator_2[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a buy order
orders = create_order(orders, stocks.Date[i], 'buy')
# set Close price -> Buy Signal
stocks.loc[i,'BuySignal'] = stocks.Close[i]
# Confirm the current open position is LONG, AND
# that there's NO recent SELL signal, AND
# that Fast SMA (Indicator_1) has crossed Slow SMA (Indicator_2) to the downside
elif (open_positions.Type[pos_idx] is not 'short') and \
(np.isnan(stocks.SellSignal[i-1])) and \
(stocks.Indicator_1[i]<=stocks.Indicator_2[i]) :
# create a close order
orders = create_order(orders, stocks.Date[i], 'close')
# create a sell order
orders = create_order(orders, stocks.Date[i], 'sell')
# set Close price -> Sell Signal
stocks.loc[i,'SellSignal'] = stocks.Close[i]
# ------------------------------------------------------------------------------------------
elif (len(open_positions) > 1) :
if DEBUG:
print(f'strategy ------- there\'s mutliple open positions - what do we do???')
# ------------------------------------------------------------------------------------------
# update the positions's unrealized value and the brokerage account's total value
if (len(positions) > 0) and (positions.iloc[-1].Status=='open'):
# POSITION_COLUMNS = ['Date','Position','Price','Cost','Type','Status','Unrealized','Realized']
# Unrealized = 6
# update the open position's Unrealized(gain)
# Unrealized(gain) = Cost - (Position * Closing Price)
positions.iloc[-1,6] = positions.iloc[-1].Cost - (abs(positions.iloc[-1].Position) * stocks.iloc[i].Close)
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update positns - row: {positions.index[-1]}, date: {broker.iloc[-1].Date}, cost: {positions.iloc[-1].Cost}, unrlz: {positions.iloc[-1].Unrealized}, rlizd: {positions.iloc[-1].Realized} ')
# BROKER_COLUMNS = ['Date','TotalCash','CashValue']
# TotalValue = 2
# update the brokerage account's total value
broker.iloc[-1].TotalValue = broker.iloc[-1].TotalCash + positions.iloc[-1].Cost + positions.iloc[-1].Unrealized
# REPLACE ME -- this comment and print() should be a log statement
# print info from BROKER_DF
if DEBUG:
print(f'strategy ------- update broker -- row: {broker.index[-1]}, date: {broker.iloc[-1].Date}, cash: {broker.iloc[-1].TotalCash}, value: {broker.iloc[-1].TotalValue}, ')
#---- end FOR loop
if DEBUG:
print('--------------------------------------------------------------------------------')
print(stocks.tail(50))
print(orders.tail(20))
print(positions.tail(20))
print(broker.tail(50))
print('--------------------------------------------------------------------------------')
return broker, orders, positions, stocks