forked from conor19w/Binance-Futures-Trading-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTradingStrats.py
818 lines (739 loc) · 40.1 KB
/
TradingStrats.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import pandas as pd
import numpy as np
from ta.momentum import stochrsi_d,stochrsi_k,stoch,stoch_signal,rsi,awesome_oscillator
from ta.trend import ema_indicator,macd_signal,macd,sma_indicator,adx,sma_indicator,cci
from ta.volatility import average_true_range,bollinger_pband,bollinger_hband,bollinger_lband,bollinger_mavg,bollinger_wband
from ta.volume import ease_of_movement,on_balance_volume,force_index,money_flow_index
from ta.momentum import tsi
import math
import statsmodels.api as sm
from sklearn import tree
from statsmodels.tsa.stattools import adfuller,coint
from math import log10, floor
from copy import copy
import time
'''def heikin_ashi(Close_H,Open_H,Low_H,High_H,Close):
Close_S = pd.Series(Close)
BB = bollinger_wband(Close_S)
EMA18 = ema_indicator(Close_S)
if Open_H[-1]==Low_H[-1] and BB[-1]<0 and Close_H[-1]'''
def single_candle_swing_pump(prediction,Close,High,Low,CurrentPos,ClosePos,count,stoploss):
##This function requires Hold_Pos to be switched on in Bot.py
prediction = -99
number_bars = 1 ##how many bars to wait until we sell
if High[-5]<High[-4]<High[-3] and High[-3]>High[-2]>High[-1] and CurrentPos == -99: ##if we have a peak
prediction = 0
#stoploss = 1.5*np.array(average_true_range(pd.Series(High), pd.Series(Low), pd.Series(Close)))[-1]
elif Low[-5]<Low[-4]<Low[-3] and Low[-3]<Low[-2]<Low[-1] and CurrentPos == -99: ##if we have a trough
prediction = 1
#stoploss = 1.5*np.array(average_true_range(pd.Series(High), pd.Series(Low), pd.Series(Close)))[-1]
if CurrentPos!=-99 and count==number_bars: ##check if we should close position
ClosePos = 1
count=0
elif CurrentPos!=-99 and count!=number_bars: ##iterate count
count+=1
else:
ClosePos = -99 ##not in a position so closePos is intitialized
return prediction,ClosePos,count,stoploss
def RSI_trade(prediction,Close,CurrentPos,ClosePos):
RSI = np.array(rsi(pd.Series(Close)))
if RSI[-1]<30 and CurrentPos == -99: ## Oversold
prediction = 1
elif RSI[-1]>70 and CurrentPos == -99: ##Overbought
prediction = 0
elif CurrentPos == 0 and (RSI[-1]<30 or (RSI[-4]<40 and RSI[-1]>50)): ##RSI is oversold or trending up
ClosePos = 1
elif CurrentPos == 1 and (RSI[-1]>70 or (RSI[-4]>60 and RSI[-1]<50)): ##RSI is overbought or trending down
ClosePos = 1
else:
ClosePos = 0
return prediction,ClosePos
def candle_wick(prediction,Close,Open,High,Low):
if Close[-5]<Close[-4]<Close[-3] and Close[-2]<Open[-2] and (High[-2]-Open[-2] + Close[-2]-Low[-2])> 15*(Open[-2]-Close[-2]) and Close[-1]<Close[-2]:
##3 green candles followed by a red candle with a huge wick
prediction = 0
elif Close[-5]>Close[-4]>Close[-3] and Close[-2]>Open[-2] and (High[-2]-Close[-2] + Open[-2]-Low[-2])> 15*(Close[-2]-Open[-2]) and Close[-1]>Close[-2]:
##3 red candles followed by a green candle with a huge wick
prediction = 1
return prediction,9
def fibMACD(prediction,Close,Open,High,Low):
stoplossval = 0
takeprofitval = 0
period = 100 ##Record peaks and troughs in last period timesteps
MACD_signal = np.array(macd_signal(pd.Series(Close)))
MACD = np.array(macd(pd.Series(Close)))
Close_peaks = [] ##Store peak values
location_peaks = [] ##store index of peak value , used for debugging
Close_troughs = [] ##store trough values
location_troughs = [] ##store index of peak trough , used for debugging
#####################Find peaks & troughs in Close ##############################
for i in range(len(High) - period, len(High) - 2):
if High[i] > High[i - 1] and High[i] > High[i + 1] and High[i] > High[i - 2] and High[i] > High[i + 2]:
##Weve found a peak:
Close_peaks.append(High[i])
location_peaks.append(i)
elif Low[i] < Low[i - 1] and Low[i] < Low[i + 1] and Low[i] < Low[i - 2] and Low[i] < Low[i + 2]:
##Weve found a trough:
Close_troughs.append(Low[i])
location_troughs.append(i)
EMA200 = np.array(sma_indicator(pd.Series(Close), window=200))
trend = -99 ##indicate the direction of trend
if Close[-1] < EMA200[-1]:
trend = 0
elif Close[-1] > EMA200[-1]:
trend = 1
max_pos=-99
min_pos=-99
if trend ==1:
##Find the start and end of the pullback
max_Close = -9999999
min_Close = 9999999
max_flag=0
min_flag=0
for i in range(len(Close_peaks)-1,-1,-1):
if Close_peaks[i]>max_Close and max_flag<2:
max_Close = Close_peaks[i]
max_pos = location_peaks[i]
max_flag=0
elif max_flag==2:
break
else:
max_flag+=1
##Find the start and end of the pullback
startpoint=-99
for i in range(len(location_troughs)):
if location_troughs[i]<max_pos:
startpoint=i
else:
break
for i in range(startpoint,-1,-1):
if Close_troughs[i]<min_Close and min_flag<2:
min_Close = Close_troughs[i]
min_pos = location_troughs[i]
min_flag=0
elif min_flag==2:
break
else:
min_flag+=1
##fibonacci levels
fib_level_0 = max_Close
fib_level_1 = max_Close - .236*(max_Close - min_Close)
fib_level_2 = max_Close - .382 * (max_Close - min_Close)
fib_level_3 = max_Close - .5 * (max_Close - min_Close)
fib_level_4 = max_Close - .618 * (max_Close - min_Close)
fib_level_5 = max_Close - .786 * (max_Close - min_Close)
fib_level_6 = min_Close
##Take profit targets, Don't think this is configured properly so maybe have a look at fibonacci extensions and fix here, Right hand side is ment to be the corresponding extension level
fib_retracement_level_1 = fib_level_0+1.236*(max_Close - min_Close) - Close[-1]##target max_Close+1.236*(max_Close - min_Close)
fib_retracement_level_2 = fib_level_0+1.382*(max_Close - min_Close) - Close[-1]
fib_retracement_level_3 = fib_level_0+1.5*(max_Close - min_Close) - Close[-1]
fib_retracement_level_4 = fib_level_0+1.618*(max_Close - min_Close) - Close[-1]
fib_retracement_level_5 = fib_level_0+1.786*(max_Close - min_Close) - Close[-1]
fib_retracement_level_6 = fib_level_0+2*(max_Close - min_Close) - Close[-1]
## fib_level_0>Low[-3]>fib_level_1: recent low was between two of our levels
## Close[-4]>fib_level_1 and Close[-5]>fib_level_1 and Close[-6]>fib_level_1: Ensure the bottom level was respected ie. no recent close below it
if fib_level_0>Low[-3]>fib_level_1 and Close[-4]>fib_level_1 and Close[-5]>fib_level_1 and Close[-6]>fib_level_1:
if Close[-3] < Open[-3] < Close[-2] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 1")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_1 ##target the corresponding extensiuon level
stoplossval = Close[-1] - fib_level_1*1.0001 ##stoploss below bottom level with a bit extra
elif fib_level_1>Low[-3]>fib_level_2 and Close[-4]>fib_level_2 and Close[-5]>fib_level_2 and Close[-6]>fib_level_2:
if Close[-3] < Open[-3] < Close[-2] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 1")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_2
stoplossval = Close[-1] - fib_level_2*1.0001
elif fib_level_2>Low[-2]>fib_level_3 and Close[-3]>fib_level_3 and Close[-4]>fib_level_3 and Close[-5]>fib_level_3:
if Close[-2] < Open[-2] < Close[-1] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 2")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_3
stoplossval = Close[-1] - fib_level_3*1.0001
elif fib_level_3>Low[-2]>fib_level_4 and Close[-3]>fib_level_4 and Close[-4]>fib_level_4 and Close[-5]>fib_level_4:
if Close[-2] < Open[-2] < Close[-1] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 3")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_4
stoplossval = Close[-1] - fib_level_4*1.0001
elif fib_level_4>Low[-2]>fib_level_5 and Close[-3]>fib_level_5 and Close[-4]>fib_level_5 and Close[-5]>fib_level_5:
if Close[-2] < Open[-2] < Close[-1] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 4")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_5
stoplossval = Close[-1] - fib_level_5*1.0001
elif fib_level_5>Low[-2]>fib_level_6 and Close[-3]>fib_level_6 and Close[-4]>fib_level_6 and Close[-5]>fib_level_6:
if Close[-2] < Open[-2] < Close[-1] < Close[-1] and ((MACD_signal[-2]<MACD[-2] or MACD_signal[-3]<MACD[-3]) and MACD_signal[-1]>MACD[-1]): ##Bullish Engulfing Candle and cross up on MACD
#print("level 5")
prediction=1 ##signal a buy
takeprofitval = fib_retracement_level_6
stoplossval = Close[-1] - fib_level_6*1.0001
elif trend ==0:
##Find the start and end of the pullback
max_Close = -9999999
min_Close = 9999999
max_flag = 0
min_flag = 0
for i in range(len(Close_troughs)-1, -1, -1):
if Close_troughs[i] < min_Close and min_flag < 2:
min_Close = Close_troughs[i]
min_pos = location_troughs[i]
min_flag = 0
elif min_flag == 2:
break
else:
min_flag += 1
##Find the start and end of the pullback
startpoint = -99
for i in range(len(location_peaks)):
if location_peaks[i] < min_pos:
startpoint = i
else:
break
for i in range(startpoint, -1, -1):
if Close_peaks[i] > max_Close and max_flag < 2:
max_Close = Close_peaks[i]
max_pos = location_peaks[i]
max_flag = 0
elif max_flag == 2:
break
else:
max_flag += 1
##fibonacci levels
fib_level_0 = min_Close
fib_level_1 = min_Close + .236 * (max_Close - min_Close)
fib_level_2 = min_Close + .382 * (max_Close - min_Close)
fib_level_3 = min_Close + .5 * (max_Close - min_Close)
fib_level_4 = min_Close + .618 * (max_Close - min_Close)
fib_level_5 = min_Close + .786 * (max_Close - min_Close)
fib_level_6 = max_Close
##Take profit targets, Don't think this is configured properly so maybe have a look at fibonacci extensions and fix here, Right hand side is ment to be the corresponding extension level
fib_retracement_level_1 = Close[-1] - (fib_level_0 + 1.236 * (max_Close - min_Close))
fib_retracement_level_2 = Close[-1] - (fib_level_0 + 1.382 * (max_Close - min_Close))
fib_retracement_level_3 = Close[-1] - (fib_level_0 + 1.5 * (max_Close - min_Close))
fib_retracement_level_4 = Close[-1] - (fib_level_0 + 1.618 * (max_Close - min_Close))
fib_retracement_level_5 = Close[-1] - (fib_level_0 + 1.786 * (max_Close - min_Close))
fib_retracement_level_6 = Close[-1] - (fib_level_0 + 2 * (max_Close - min_Close))
## fib_level_0 < High[-3] < fib_level_1: recent low was between two of our levels
## Close[-4] < fib_level_1 and Close[-5] < fib_level_1 and Close[-6] < fib_level_1: Ensure the Top level was respected, ie no recent close above it
if fib_level_0 < High[-3] < fib_level_1 and Close[-4] < fib_level_1 and Close[-5] < fib_level_1 and Close[-6] < fib_level_1:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_1 ##target corresponding extension level
stoplossval = fib_level_1*1.0001 - Close[-1] ##stoploss above Top level with a bit extra
elif fib_level_1 < High[-3] < fib_level_2 and Close[-4] < fib_level_2 and Close[-5] < fib_level_2 and Close[-6] < fib_level_2:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_2
stoplossval = fib_level_2*1.0001 - Close[-1]
elif fib_level_2 < High[-3] < fib_level_3 and Close[-4] < fib_level_3 and Close[-5] < fib_level_3 and Close[-6] < fib_level_3:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_3
stoplossval = fib_level_3*1.0001 - Close[-1]
elif fib_level_3 < High[-3] < fib_level_4 and Close[-4] < fib_level_4 and Close[-5] < fib_level_4 and Close[-6] < fib_level_4:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_4
stoplossval = fib_level_4*1.0001 - Close[-1]
elif fib_level_4 < High[-3] < fib_level_5 and Close[-4] < fib_level_5 and Close[-5] < fib_level_5 and Close[-6] < fib_level_5:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_5
stoplossval = fib_level_5*1.0001 - Close[-1]
elif fib_level_5 < High[-3] < fib_level_6 and Close[-4] < fib_level_6 and Close[-5] < fib_level_6 and Close[-6] < fib_level_6:
if Close[-3] > Open[-3] > Close[-2] > Close[-1] and ((MACD_signal[-2] > MACD[-2] or MACD_signal[-3] > MACD[-3]) and MACD_signal[-1] < MACD[-1]): ##Bearish Engulfing Candle and cross down on MACD
#print("level 1")
prediction = 0 ##signal a sell
takeprofitval = fib_retracement_level_6
stoplossval = fib_level_6*1.0001 - Close[-1]
return prediction,stoplossval,takeprofitval
def goldenCross(prediction,CloseStream):
EMA100 = np.array(ema_indicator(pd.Series(CloseStream), window=100))
EMA50 = np.array(ema_indicator(pd.Series(CloseStream), window=50))
EMA20 = np.array(ema_indicator(pd.Series(CloseStream), window=20))
RSI = np.array(rsi(pd.Series(CloseStream)))
if CloseStream[-1]>EMA100[-1] and RSI[-1]>50:
##looking for long entries
if (EMA20[-2]<EMA50[-2] and EMA20[-1]>EMA50[-1]) or (EMA20[-3]<EMA50[-3] and EMA20[-1]>EMA50[-1]) or (EMA20[-4]<EMA50[-4] and EMA20[-1]>EMA50[-1]):
##Cross up occured
prediction=1 ##buy
elif CloseStream[-1]<EMA100[-1] and RSI[-1]<50:
##looking for short entries
if (EMA20[-2]>EMA50[-2] and EMA20[-1]<EMA50[-1]) or (EMA20[-3]>EMA50[-3] and EMA20[-1]<EMA50[-1]) or (EMA20[-4]>EMA50[-4] and EMA20[-1]<EMA50[-1]):
##Cross up occured
prediction=0 ##Sell
return prediction,6
def StochRSI_RSIMACD(prediction,CloseStream,signal1,signal2):
#prediction=-99
EMA200 = np.array(ema_indicator(pd.Series(CloseStream),window=200))
fastd = np.array(stochrsi_d(pd.Series(CloseStream)))
fastk = np.array(stochrsi_k(pd.Series(CloseStream)))
RSI = np.array(rsi(pd.Series(CloseStream)))
MACD = np.array(macd(pd.Series(CloseStream)))
macdsignal= np.array(macd_signal(pd.Series(CloseStream)))
##buy signal
if fastk[-1] <=20 and fastd[-1] <=20:
signal1 = 1
elif fastk[-1] >=80 and fastd[-1] >=80:
signal1 = 0
if signal1 ==1:
if RSI[-1]>50:
if macdsignal[-1]<MACD[-1]:
signal2=1
elif signal1 == 0:
if RSI[-1]<50:
if macdsignal[-1] > MACD[-1]:
signal2=0
if signal1 == signal2 == 0:
if fastk[-1] >= 20 and fastd[-1] >= 20:
prediction=0
else:
signal1=-99
signal2 = -99
elif signal1 == signal2 == 1 and CloseStream[-1]>EMA200[-1] and CloseStream[-2]>EMA200[-2] and CloseStream[-3]>EMA200[-3] and CloseStream[-4]>EMA200[-4] and CloseStream[-5]>EMA200[-5]:
if fastk[-1] <=80 and fastd[-1] <=80:
prediction=1
else:
signal1=-99
signal2 = -99
return prediction,signal1,signal2,3
def StochRSIMACD(prediction,CloseStream,HighStream,LowStream):
Close = pd.Series(CloseStream)
High = pd.Series(HighStream)
Low = pd.Series(LowStream)
fastd = np.array(stoch(close=Close,high=High,low=Low))
fastk = np.array(stoch_signal(close=Close,high=High,low=Low))
RSI = np.array(rsi(Close))
MACD = np.array(macd(Close))
macdsignal= np.array(macd_signal(Close))
if ((fastd[-1]<20 and fastk[-1]<20 and RSI[-1]>50 and MACD[-1]>macdsignal[-1] and MACD[-2]<macdsignal[-2]) or
(fastd[-2] < 20 and fastk[-2] < 20 and RSI[-1] > 50 and MACD[-1] > macdsignal[-1] and MACD[-3] < macdsignal[-3] and fastd[-1] < 80 and fastk[-1] < 80) or
(fastd[-3] < 20 and fastk[-3] < 20 and RSI[-1] > 50 and MACD[-1] > macdsignal[-1] and MACD[-2] < macdsignal[-2] and fastd[-1] < 80 and fastk[-1] < 80) or
(fastd[-4] < 20 and fastk[-4] < 20 and RSI[-1] > 50 and MACD[-1] > macdsignal[-1] and MACD[-3] < macdsignal[-3] and fastd[-1] < 80 and fastk[-1] < 80)):
prediction = 1
elif ((fastd[-1]>80 and fastk[-1]>80 and RSI[-1]<50 and MACD[-1]<macdsignal[-1] and MACD[-2]>macdsignal[-2]) or
(fastd[-2]>80 and fastk[-2]>80 and RSI[-1]<50 and MACD[-1]<macdsignal[-1] and MACD[-3]>macdsignal[-3] and fastd[-1]>20 and fastk[-1]>20) or
(fastd[-3]>80 and fastk[-3]>80 and RSI[-1]<50 and MACD[-1]<macdsignal[-1] and MACD[-2]>macdsignal[-2] and fastd[-1]>20 and fastk[-1]>20) or
(fastd[-4]>80 and fastk[-4]>80 and RSI[-1]<50 and MACD[-1]<macdsignal[-1] and MACD[-3]>macdsignal[-3] and fastd[-1]>20 and fastk[-1]>20)):
prediction = 0
return prediction,2
##############################################################################################################################
##############################################################################################################################
##############################################################################################################################
def tripleEMA(CloseStream,OpenStream,prediction):
EMA3 = np.array(ema_indicator(pd.Series(CloseStream), window=5))
EMA6 = np.array(ema_indicator(pd.Series(CloseStream), window=20))
EMA9 = np.array(ema_indicator(pd.Series(CloseStream), window=50))
if EMA3[-5]>EMA6[-5] and EMA3[-5]>EMA9[-5] \
and EMA3[-4]>EMA6[-4] and EMA3[-4]>EMA9[-4] \
and EMA3[-3]>EMA6[-3] and EMA3[-3]>EMA9[-3] \
and EMA3[-2]>EMA6[-2] and EMA3[-2]>EMA9[-2] \
and EMA3[-1]<EMA6[-1] and EMA3[-1]<EMA9[-1] :
prediction=0
if EMA3[-5]<EMA6[-5] and EMA3[-5]<EMA9[-5] \
and EMA3[-4]<EMA6[-4] and EMA3[-4]<EMA9[-4] \
and EMA3[-3]<EMA6[-3] and EMA3[-3]<EMA9[-3] \
and EMA3[-2]<EMA6[-2] and EMA3[-2]<EMA9[-2] \
and EMA3[-1]>EMA6[-1] and EMA3[-1]>EMA9[-1] :
prediction=1
return prediction,3
def tripleEMAStochasticRSIATR(CloseStream,signal1,signal2,prediction):
#newOrder = 0
Close = np.array(CloseStream)
EMA50 = np.array(ema_indicator(pd.Series(CloseStream),window=50))
EMA14 = np.array(ema_indicator(pd.Series(CloseStream),window=14))
EMA8 = np.array(ema_indicator(pd.Series(CloseStream),window=8))
fastd = np.array(stochrsi_d(pd.Series(CloseStream)))
fastk = np.array(stochrsi_k(pd.Series(CloseStream)))
##buy signal
if (Close[-1]>EMA8[-1]>EMA14[-1]>EMA50[-1]) and ((fastk[-1]>fastd[-1]) and (fastk[-2]<fastd[-2])): #and (fastk[-1]<80 and fastd[-1]<80):
signal1=1
elif (Close[-1]<EMA8[-1]<EMA14[-1]<EMA50[-1]) and ((fastk[-1]<fastd[-1]) and (fastk[-2]>fastd[-2])) : #and (fastk[-1]>20 and fastd[-1]>20):
signal1=0
else:
signal1=-99
if signal1 == 0:
prediction=0
elif signal1 == 1:
prediction=1
return prediction, signal1, signal2, 1
##############################################################################################################################
##############################################################################################################################
##############################################################################################################################
def RSIStochEMA(prediction,CloseStream,HighStream,LowStream,signal1,currentPos):
period = 60
CloseS = pd.Series(CloseStream)
Close = np.array(CloseStream)
# High = np.array(HighStream)
# Low = np.array(LowStream)
fastk = np.array(stoch_signal(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
fastd = np.array(stoch(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
RSI = np.array(rsi(CloseS))
EMA200 = np.array(ema_indicator(CloseS, window=200))
peaks_RSI = []
corresponding_Close_peaks = []
location_peaks = []
troughs_RSI = []
corresponding_Close_troughs = []
location_troughs =[]
#####################Find peaks & troughs in RSI ##############################
for i in range(len(RSI)-period,len(RSI)-2):
if RSI[i]>RSI[i-1] and RSI[i]>RSI[i+1] and RSI[i]>RSI[i-2] and RSI[i]>RSI[i+2]:
##Weve found a peak:
peaks_RSI.append(RSI[i])
corresponding_Close_peaks.append(Close[i])
location_peaks.append(i)
elif RSI[i]<RSI[i-1] and RSI[i]<RSI[i+1] and RSI[i]<RSI[i-2] and RSI[i]<RSI[i+2]:
##Weve found a trough:
troughs_RSI.append(RSI[i])
corresponding_Close_troughs.append(Close[i])
location_troughs.append(i)
##Lower High Price & Higher High RSI => Bearish Divergence
##Higher Low Price & Lower low RSI => Bullish Divergence
length = 0
if len(peaks_RSI)>len(troughs_RSI):
length=len(peaks_RSI)
else:
length=len(troughs_RSI)
loc1 = -99
loc2 = -99
if length!=0:
for i in range(length-1):
if i<len(peaks_RSI):
##Check for hidden Bearish Divergence
if peaks_RSI[i]<peaks_RSI[-1] and corresponding_Close_peaks[i]>corresponding_Close_peaks[-1] and peaks_RSI[-1]-peaks_RSI[i]>1:
for j in range(i+1,len(peaks_RSI)-1):
if peaks_RSI[j]>peaks_RSI[i]:
break
elif j==len(peaks_RSI)-2:
loc1=location_peaks[i]
if i<len(troughs_RSI):
##Check for hidden Bullish Divergence
if troughs_RSI[i] > troughs_RSI[-1] and corresponding_Close_troughs[i] < corresponding_Close_troughs[-1] and troughs_RSI[i] - troughs_RSI[-1]>1:
for j in range(i+1,len(troughs_RSI)-1):
if troughs_RSI[j]<troughs_RSI[i]:
break
elif j == len(troughs_RSI)-2:
loc2 = location_troughs[i]
if loc1==loc2:
signal1=-99
elif loc1>loc2:# and 300-loc1<15:
signal1=0
#print(300-loc1)
else:# 300-loc2<15:
#print(300-loc2)
signal1=1
'''else:
signal1=-99'''
##Bullish Divergence
if signal1==1 and (fastk[-1]>fastd[-1] and (fastk[-2]<fastd[-2] or fastk[-3]<fastd[-3])) and Close[-1]>EMA200[-1]:
prediction=1
signal1=-99
##Bearish Divergence
elif signal1==0 and (fastk[-1]<fastd[-1] and (fastk[-2]>fastd[-2] or fastk[-3]>fastd[-3])) and Close[-1]<EMA200[-1]:
prediction=0
signal1=-99
if currentPos!=-99:
signal1=-99
prediction=-99
return prediction, signal1, 4
##############################################################################################################
def stochBB(prediction,CloseStream):
fastd = np.array(stochrsi_d(pd.Series(CloseStream)))
fastk = np.array(stochrsi_k(pd.Series(CloseStream)))
#print(fastd[-1],fastk[-1])
percent_B = np.array(bollinger_pband(pd.Series(CloseStream)))
percent_B1= percent_B[-1]
percent_B2 = percent_B[-2]
percent_B3 = percent_B[-3]
#print(percent_B)
if fastk[-1]<.2 and fastd[-1]<.2 and (fastk[-1]>fastd[-1] and fastk[-2]<fastd[-2]) and (percent_B1<0 or percent_B2<0 or percent_B3<0):# or percent_B3<0):# or percent_B2<.05):
prediction=1
elif fastk[-1]>.8 and fastd[-1]>.8 and (fastk[-1]<fastd[-1] and fastk[-2]>fastd[-2]) and (percent_B1>1 or percent_B2>1 or percent_B3>1):# or percent_B3>1):# or percent_B2>1):
prediction=0
return prediction,6
def breakout(prediction,CloseStream,VolumeStream,symbol):
invert=1 ## switch shorts and longs, basically fakeout instead of breakout
#if symbol=='BTCUSDT' or symbol=='ETHUSDT':
# invert=0
Close = pd.Series(CloseStream).pct_change()
Volume = pd.Series(VolumeStream[:-1])
max_Close = Close.iloc[:-1].rolling(10).max()
min_Close = Close.iloc[:-1].rolling(10).min()
max_Vol = Volume.rolling(10).max()
if invert:
if Close.iloc[-1]>max_Close.iloc[-1] and VolumeStream[-1]>max_Vol.iloc[-1]:
prediction = 0
elif Close.iloc[-1]<min_Close.iloc[-1] and VolumeStream[-1]>max_Vol.iloc[-1]:
prediction = 1
else:
if Close.iloc[-1]>max_Close.iloc[-1] and VolumeStream[-1]>max_Vol.iloc[-1]:
prediction = 1
elif Close.iloc[-1]<min_Close.iloc[-1] and VolumeStream[-1]>max_Vol.iloc[-1]:
prediction = 0
return_type=9
'''if symbol=='DOGEUSDT':
return_type=9
elif symbol=='SOLUSDT':
return_type = 8'''
return prediction,return_type
def fakeout(prediction,CloseStream,VolumeStream,symbol):
invert = 1
#if symbol == 'BTCUSDT' or symbol == 'ETHUSDT':
# invert = 0
Close = pd.Series(CloseStream) #.pct_change() ##get size of bars in a percentage
Volume = pd.Series(VolumeStream[:-1])
max_Close = Close.iloc[:-1].rolling(15).max()
min_Close = Close.iloc[:-1].rolling(15).min()
max_Vol = Volume.rolling(15).max()
if invert:
if Close.iloc[-1] > max_Close.iloc[-1] and VolumeStream[-1] < max_Vol.iloc[-1]:
prediction = 0
elif Close.iloc[-1] < min_Close.iloc[-1] and VolumeStream[-1] < max_Vol.iloc[-1]:
prediction = 1
else:
if Close.iloc[-1] > max_Close.iloc[-1] and VolumeStream[-1] < max_Vol.iloc[-1]:
prediction = 1
elif Close.iloc[-1] < min_Close.iloc[-1] and VolumeStream[-1] < max_Vol.iloc[-1]:
prediction = 0
return prediction, 9
'''def sma_crossover(prediction,CloseStream,HighStream,LowStream):
fastk = np.array(stoch_signal(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
fastd = np.array(stoch(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
SMA100 = sma_indicator(pd.Series(CloseStream),window=100)
SMA200 = sma_indicator(pd.Series(CloseStream),window=200)
if SMA100[-1]<SMA200[-1] and SMA100[-2]>SMA200[-2]:'''
def trend_Ride(prediction,Close,High,Low,percent,current_Pos,Highest_lowest):
close_pos = 0
if (Close[-1]-Close[-2])/Close[-2] > percent and current_Pos==-99:
prediction = 1 ##uptrend so open long
Highest_lowest = Close[-1]
elif (Close[-1]-Close[-2])/Close[-2] < -percent and current_Pos==-99:
prediction = 0 ##downtrend so open short position
Highest_lowest = Close[-1]
if current_Pos == 1 and (Close[-1] - Highest_lowest)/Highest_lowest < - percent/3:
close_pos = 1
elif current_Pos == 0 and (Highest_lowest - Close[-1])/Highest_lowest< - percent/3:
close_pos = 1
elif current_Pos == 1 and Highest_lowest < High:
Highest_lowest = High
elif current_Pos == 0 and Highest_lowest > Low:
Highest_lowest = Low
return prediction,Highest_lowest,close_pos
def pairTrading(prediction,Close1,Close2,log=0,TPSL=0,percent_TP=0,percent_SL=0):
new_Close = []
'''Close1_TP = 0
Close2_TP = 0
Close1_SL = 0
Close2_SL = 0'''
#multiplier = Close1[0]/Close2[0]
if not log:
multiplier = (sm.OLS(Close1, Close2).fit()).params[0]
for i in range(len(Close1)-30,len(Close1)):
new_Close.append(Close1[i]-multiplier*Close2[i])
else:
log_close1 = []
log_close2 = []
for i in range(len(Close1)-30,len(Close1)):
log_close1.append(math.log(Close1[i]))
log_close2.append(math.log(Close2[i]))
multiplier = (sm.OLS(log_close1, log_close2).fit()).params[0]
for i in range(len(log_close1)):
new_Close.append(log_close1[i] - multiplier * log_close2[i])
BB =np.array(bollinger_pband(pd.Series(new_Close),window_dev=3))
#BB1 = np.array(bollinger_hband(pd.Series(new_Close),window_dev=3))
#BB2 = np.array(bollinger_lband(pd.Series(new_Close), window_dev=3))
#SMA20 = np.array(bollinger_mavg(pd.Series(new_Close)))
#print(BB[-1])
if BB[-1]>1:
prediction = [0,1] # [1,0]
'''if TPSL:
##work out distance to the mean and allocate the change to get there to each series:
#print(new_Close[-1],SMA20[-1])
#change_in_decimal = math.fabs((new_Close[-1] - SMA20[-1])/new_Close[-1])
#Close1_TP = Close1[-1] * change_in_decimal/3
#Close2_TP = Close2[-1] * change_in_decimal/3
Close1_TP = Close1[-1]*percent_TP
Close2_TP = Close2[-1]*percent_TP
Close1_SL = Close1[-1] * percent_SL
Close2_SL = Close2[-1] * percent_SL'''
elif BB[-1]<0:
prediction = [1,0] # [0,1]
'''if TPSL:
##work out distance to the mean and allocate the change to get there to each series:
#print(new_Close[-1],SMA20[-1])
#change_in_decimal = math.fabs((new_Close[-1] - SMA20[-1])/new_Close[-1])
#Close1_TP = Close1[-1] * change_in_decimal/3
#Close2_TP = Close2[-1] * change_in_decimal/3
#print(change_in_decimal)
Close1_TP = Close1[-1] * percent_TP
Close2_TP = Close2[-1] * percent_TP
Close1_SL = Close1[-1] * percent_SL
Close2_SL = Close2[-1] * percent_SL'''
return prediction,[9,9] #,Close1_TP,Close2_TP,Close1_SL,Close2_SL
def pairTrading_Crossover(prediction, Close1, Close2, CurrentPos, percent_SL=0):
new_Close = []
Close_pos=0
Close1_SL = 0
Close2_SL = 0
multiplier = (sm.OLS(Close1, Close2).fit()).params[0]
for i in range(len(Close1)-30,len(Close1)):
new_Close.append(Close1[i]-multiplier*Close2[i])
BB =np.array(bollinger_pband(pd.Series(new_Close),window_dev=3))
SMA20 = np.array(bollinger_mavg(pd.Series(new_Close)))
if BB[-1]>1:
prediction = [0,1]
Close1_SL = Close1[-1] * percent_SL
Close2_SL = Close2[-1] * percent_SL
elif BB[-1]<0:
prediction = [1,0]
Close1_SL = Close1[-1] * percent_SL
Close2_SL = Close2[-1] * percent_SL
if CurrentPos!=-99:
if (new_Close[-1]>SMA20[-1] and (new_Close[-2]<SMA20[-2] or new_Close[-3]<SMA20[-3])) or (new_Close[-1]<SMA20[-1] and (new_Close[-2]>SMA20[-2] or new_Close[-3]>SMA20[-3])):
##Price has crossed up or down over the Moving average so close the position
Close_pos=1
return prediction,Close1_SL,Close2_SL,Close_pos
##Function used to decide stoploss values and takeprofit values based off a type variable returned by specific strategies above
def SetSLTP(stoplossval, takeprofitval,CloseStream,HighStream,LowStream,prediction,CurrentPos,Type,SL=1,TP=1):
##Average True Range with multipliers
if Type==1:
ATR = np.array(average_true_range(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
if prediction == 0 and CurrentPos == -99:
stoplossval = 1.5 * ATR[-1]
takeprofitval = 8 * ATR[-1]
elif prediction == 1 and CurrentPos == -99:
stoplossval = 1.5 * ATR[-1]
takeprofitval = 8 * ATR[-1]
## Highest/Lowest Close in last 30 periods
elif Type==2:
highswing = CloseStream[-2]
Lowswing = CloseStream[-2]
highflag = 0
lowflag = 0
for j in range(-3, -20, -1):
if CloseStream[j] > highswing and highflag == 0:
highswing = CloseStream[j]
if CloseStream[j] < Lowswing and lowflag == 0:
Lowswing = CloseStream[j]
if prediction == 0 and CurrentPos == -99:
stoplossval = (highswing - CloseStream[-1])
#if stoplossval>.15*CloseStream[-1]:
# stoplossval=.15*CloseStream[-1]
if stoplossval < 0:
stoplossval *= -1
takeprofitval = stoplossval * 2
elif prediction == 1 and CurrentPos == -99:
stoplossval = (CloseStream[-1] - Lowswing)
#if stoplossval>.15*CloseStream[-1]:
# stoplossval=.15*CloseStream[-1]
if stoplossval < 0:
stoplossval *= -1
takeprofitval = stoplossval * 2
## Closest Swing High/Low
elif Type==3:
highswing = CloseStream[-2]
Lowswing = CloseStream[-2]
highflag = 0
lowflag = 0
for j in range(-3, -50, -1):
if CloseStream[j] > highswing and CloseStream[j] > CloseStream[j - 1] and CloseStream[j] > CloseStream[j - 2] and highflag == 0:
highswing = CloseStream[j]
highflag = 1
if CloseStream[j] < Lowswing and CloseStream[j] < CloseStream[j - 1] and CloseStream[j] < CloseStream[j - 2] and lowflag == 0:
Lowswing = CloseStream[j]
lowflag = 1
if prediction == 0 and CurrentPos == -99:
stoplossval = (highswing - CloseStream[-1])*1.003
if stoplossval < 0:
stoplossval *= -1
takeprofitval = (highswing - CloseStream[-1]) * 1.25
elif prediction == 1 and CurrentPos == -99:
stoplossval = (CloseStream[-1] - Lowswing)*1.003
if stoplossval < 0:
stoplossval *= -1
takeprofitval = (CloseStream[-1] - Lowswing) * 1.25
## Closest Swing Close in Last 60 periods
elif Type == 4:
highswing = CloseStream[-1]
Lowswing = CloseStream[-1]
highflag = 0
lowflag = 0
for j in range(-3, -60, -1):
if CloseStream[j] > highswing and CloseStream[j] > CloseStream[j - 1] and CloseStream[j] > CloseStream[j - 2] and \
CloseStream[j] > CloseStream[j + 2] and CloseStream[j] > CloseStream[j + 1] and highflag == 0:
highswing = CloseStream[j]
highflag = 1
if CloseStream[j] < Lowswing and CloseStream[j] < CloseStream[j - 1] and CloseStream[j] < CloseStream[j - 2] and \
CloseStream[j] < CloseStream[j + 2] and CloseStream[j] < CloseStream[j + 1] and lowflag == 0:
Lowswing = CloseStream[j]
lowflag = 1
if prediction == 0 and CurrentPos == -99:
stoplossval = (highswing - CloseStream[-1])*.5
if stoplossval < 0:
stoplossval *= -1
takeprofitval = stoplossval * 2
elif prediction == 1 and CurrentPos == -99:
stoplossval = (CloseStream[-1] - Lowswing)*.5
if stoplossval < 0:
stoplossval *= -1
takeprofitval = stoplossval * 2
elif Type==5:
ATR = np.array(average_true_range(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
highswing = HighStream[-1]
Lowswing = LowStream[-1]
highflag = 0
lowflag = 0
for j in range(-3, -60, -1):
if HighStream[j] > highswing and HighStream[j] > HighStream[j - 1] and HighStream[j] > HighStream[j - 2] and HighStream[j] > HighStream[j + 2] and HighStream[j] > HighStream[j + 1] and highflag == 0:
highswing = HighStream[j]
highflag = 1
if LowStream[j] < Lowswing and LowStream[j] < LowStream[j - 1] and LowStream[j] < LowStream[j - 2] and LowStream[j] < LowStream[j + 2] and LowStream[j] < LowStream[j + 1] and lowflag == 0:
Lowswing = LowStream[j]
lowflag = 1
if prediction == 0 and CurrentPos == -99:
temp = (highswing - CloseStream[-1])
stoplossval = 1.25 * ATR[-1]
if temp < 0:
temp *= -1
takeprofitval = temp * 2
elif prediction == 1 and CurrentPos == -99:
temp = (CloseStream[-1] - Lowswing)
stoplossval = 1.25 * ATR[-1]
if temp < 0:
temp *= -1
takeprofitval = temp * 2
elif Type==6:
ATR = np.array(average_true_range(pd.Series(HighStream), pd.Series(LowStream), pd.Series(CloseStream)))
if prediction == 0 and CurrentPos == -99:
stoplossval = 0.5 * ATR[-1]
takeprofitval = 2 * ATR[-1]
elif prediction == 1 and CurrentPos == -99:
stoplossval = 0.5 * ATR[-1]
takeprofitval = 2 * ATR[-1]
elif Type==7:
stoplossval = .007*CloseStream[-1]
takeprofitval = .01*CloseStream[-1]
elif Type==8:
ATR = np.array(average_true_range(pd.Series(HighStream[:-1]), pd.Series(LowStream[:-1]), pd.Series(CloseStream[:-1]),window=25))
if prediction == 0 and CurrentPos == -99:
stoplossval = 2 * ATR[-1]
takeprofitval = 2.5 * ATR[-1]
elif prediction == 1 and CurrentPos == -99:
stoplossval = 2 * ATR[-1]
takeprofitval = 2.5 * ATR[-1]
elif Type==9:
ATR = np.array(average_true_range(pd.Series(HighStream[:-1]), pd.Series(LowStream[:-1]), pd.Series(CloseStream[:-1]),window=20))
if prediction == 0 and CurrentPos == -99:
stoplossval = SL * ATR[-1]
takeprofitval = TP * ATR[-1]
elif prediction == 1 and CurrentPos == -99:
stoplossval = SL * ATR[-1]
takeprofitval = TP * ATR[-1]
return stoplossval,takeprofitval