forked from sofienkaabar/the-book-of-trading-strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
All_Functions_Master_File.py
4144 lines (2682 loc) · 128 KB
/
All_Functions_Master_File.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import pytz
import pandas as pd
import MetaTrader5 as mt5
import matplotlib.pyplot as plt
import numpy as np
import statistics as stats
from scipy.stats import pearsonr
from scipy.stats import spearmanr
from scipy.ndimage.interpolation import shift
from scipy import stats
investment = 100000
lot = 100000
expected_cost = 0.00000 # 1/10 of a Pip
sigchart = False
signal_quality_period = 3
frame_MIN1 = mt5.TIMEFRAME_M1
frame_M5 = mt5.TIMEFRAME_M5
frame_M10 = mt5.TIMEFRAME_M10
frame_M15 = mt5.TIMEFRAME_M15
frame_M30 = mt5.TIMEFRAME_M30
frame_H1 = mt5.TIMEFRAME_H1
frame_H2 = mt5.TIMEFRAME_H2
frame_H3 = mt5.TIMEFRAME_H3
frame_H4 = mt5.TIMEFRAME_H4
frame_H6 = mt5.TIMEFRAME_H6
frame_D1 = mt5.TIMEFRAME_D1
frame_W1 = mt5.TIMEFRAME_W1
frame_M1 = mt5.TIMEFRAME_MN1
now = datetime.datetime.now()
def asset_list(asset_set):
if asset_set == 1:
assets = ['EURUSD', 'USDCHF', 'GBPUSD', 'AUDUSD', 'NZDUSD',
'USDCAD', 'EURCAD', 'EURGBP', 'EURCHF', 'AUDCAD',
'EURNZD', 'NZDCHF', 'NZDCAD', 'EURAUD','AUDNZD',
'GBPCAD', 'AUDCHF', 'GBPAUD', 'GBPCHF', 'GBPNZD']
elif asset_set == 'CRYPTO':
assets = ['BTCUSD', 'ETHUSD', 'XRPUSD', 'LTCUSD']
elif asset_set == 'COMMODITIES':
assets = ['XAUUSD', 'XAGUSD', 'XPTUSD', 'XPDUSD']
return assets
def mass_import(asset, horizon):
if horizon == 'MN1':
data = get_quotes(frame_MIN1, 2021, 7, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'M5':
data = get_quotes(frame_M5, 2021, 6, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'M10':
data = get_quotes(frame_M10, 2020, 8, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'M15':
data = get_quotes(frame_M15, 2019, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'M30':
data = get_quotes(frame_M30, 2016, 8, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'H1':
data = get_quotes(frame_H1, 2020, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'H2':
data = get_quotes(frame_H2, 2010, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'H3':
data = get_quotes(frame_H3, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'H4':
data = get_quotes(frame_H4, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'H6':
data = get_quotes(frame_H6, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'D1':
data = get_quotes(frame_D1, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'W1':
data = get_quotes(frame_W1, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
if horizon == 'M1':
data = get_quotes(frame_M1, 2000, 1, 1, asset = assets[asset])
data = data.iloc[:, 1:5].values
data = data.round(decimals = 5)
return data
def get_quotes(time_frame, year = 2005, month = 1, day = 1, asset = "EURUSD"):
# Establish connection to MetaTrader 5
if not mt5.initialize():
print("initialize() failed, error code =", mt5.last_error())
quit()
timezone = pytz.timezone("Europe/Paris")
utc_from = datetime.datetime(year, month, day, tzinfo = timezone)
utc_to = datetime.datetime(now.year, now.month, now.day + 1, tzinfo = timezone)
rates = mt5.copy_rates_range(asset, time_frame, utc_from, utc_to)
rates_frame = pd.DataFrame(rates)
return rates_frame
def count_annotation(Data, name, onwhat, what_bull, what_bear, td, window = 50):
Plottable = Data[-window:, ]
fig, ax = plt.subplots(figsize = (10, 5))
ax.grid()
ax.plot(Plottable[:, onwhat], color = 'black', linewidth = 1.5, label = name)
for i in range(len(Plottable)):
if Plottable[i, what_bull] < 0 and Plottable[i, what_bull] != -td:
x = i
y = Plottable[i, onwhat]
ax.annotate(int(Plottable[i, what_bull]), xy = (x, y), textcoords = "offset points", xytext = (0, - 10), ha = 'center',
color = 'blue')
elif Plottable[i, what_bull] == -td:
x = i
y = Plottable[i, onwhat]
ax.annotate(int(Plottable[i, what_bull]), xy = (x, y), textcoords = "offset points", xytext = (0, - 10), ha = 'center',
color = 'red')
elif Plottable[i, what_bear] > 0 and Plottable[i, what_bear] != td:
x = i
y = Plottable[i, onwhat]
ax.annotate(int(Plottable[i, what_bear]), xy = (x, y), textcoords = "offset points", xytext = (0, 10), ha = 'center',
color = 'blue' )
elif Plottable[i, what_bear] == td:
x = i
y = Plottable[i, onwhat]
ax.annotate(int(Plottable[i, what_bear]), xy = (x, y), textcoords = "offset points", xytext = (0, 10), ha = 'center',
color = 'red' )
ax.set_facecolor((0.95, 0.95, 0.95))
plt.legend()
def adder(Data, times):
for i in range(1, times + 1):
new = np.zeros((len(Data), 1), dtype = float)
Data = np.append(Data, new, axis = 1)
return Data
def deleter(Data, index, times):
for i in range(1, times + 1):
Data = np.delete(Data, index, axis = 1)
return Data
def jump(Data, jump):
Data = Data[jump:, ]
return Data
def rounding(Data, how_far):
Data = Data.round(decimals = how_far)
return Data
def rolling_correlation(Data, first_data, second_data, lookback, where):
# Adding an extra column
Data = adder(Data, 1)
for i in range(len(Data)):
try:
Data[i, where] = pearsonr(Data[i - lookback + 1:i + 1, first_data], Data[i - lookback + 1:i + 1, second_data])[0]
except ValueError:
pass
Data = jump(Data, lookback)
return Data
def auto_correlation(Data, first_data, second_data, shift_degree, lookback, where):
new_array = shift(Data[:, first_data], shift_degree, cval = 0)
new_array = np.reshape(new_array, (-1, 1))
Data = np.concatenate((Data, new_array), axis = 1)
Data = adder(Data, 1)
for i in range(len(Data)):
try:
Data[i, where] = pearsonr(Data[i - lookback + 1:i + 1, first_data], Data[i - lookback + 1:i + 1, second_data])[0]
except ValueError:
pass
Data = jump(Data, lookback)
Data = deleter(Data, where - 1, 1)
return Data
def volatility(Data, lookback, what, where):
# Adding an extra column
Data = adder(Data, 1)
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].std())
except IndexError:
pass
# Cleaning
Data = jump(Data, lookback)
return Data
def ma(Data, lookback, close, where):
Data = adder(Data, 1)
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, close].mean())
except IndexError:
pass
# Cleaning
Data = jump(Data, lookback)
return Data
def ema(Data, alpha, lookback, what, where):
alpha = alpha / (lookback + 1.0)
beta = 1 - alpha
# First value is a simple SMA
Data = ma(Data, lookback, what, where)
# Calculating first EMA
Data[lookback + 1, where] = (Data[lookback + 1, what] * alpha) + (Data[lookback, where] * beta)
# Calculating the rest of EMA
for i in range(lookback + 2, len(Data)):
try:
Data[i, where] = (Data[i, what] * alpha) + (Data[i - 1, where] * beta)
except IndexError:
pass
return Data
def lwma(Data, lookback, what):
weighted = []
for i in range(len(Data)):
try:
total = np.arange(1, lookback + 1, 1)
matrix = Data[i - lookback + 1: i + 1, what:what + 1]
matrix = np.ndarray.flatten(matrix)
matrix = total * matrix
wma = (matrix.sum()) / (total.sum())
weighted = np.append(weighted, wma)
except ValueError:
pass
Data = Data[lookback - 1:, ]
weighted = np.reshape(weighted, (-1, 1))
Data = np.concatenate((Data, weighted), axis = 1)
return Data
def kama(Data, what, where, lookback):
Data = adder(Data, 10)
# lookback from previous period
for i in range(len(Data)):
Data[i, where] = abs(Data[i, what] - Data[i - 1, what])
Data[0, where] = 0
# Sum of lookbacks
for i in range(len(Data)):
Data[i, where + 1] = (Data[i - lookback + 1:i + 1, where].sum())
# Volatility
for i in range(len(Data)):
Data[i, where + 2] = abs(Data[i, what] - Data[i - lookback, what])
Data = Data[lookback + 1:, ]
# Efficiency Ratio
Data[:, where + 3] = Data[:, where + 2] / Data[:, where + 1]
for i in range(len(Data)):
Data[i, where + 4] = np.square(Data[i, where + 3] * 0.6666666666666666667)
for i in range(len(Data)):
Data[i, where + 5] = Data[i - 1, where + 5] + (Data[i, where + 4] * (Data[i, what] - Data[i - 1, where + 5]))
Data[11, where + 5] = 0
Data = deleter(Data, where, 5)
Data = jump(Data, lookback * 2)
return Data
def BollingerBands(Data, boll_lookback, standard_distance, what, where):
# Adding a few columns
Data = adder(Data, 2)
# Calculating means
Data = ma(Data, boll_lookback, what, where)
Data = volatility(Data, boll_lookback, what, where + 1)
Data[:, where + 2] = Data[:, where] + (standard_distance * Data[:, where + 1])
Data[:, where + 3] = Data[:, where] - (standard_distance * Data[:, where + 1])
Data = jump(Data, boll_lookback)
Data = deleter(Data, where, 2)
return Data
def augmented_BollingerBands(Data, boll_lookback, standard_distance, high, low, where):
Data = adder(Data, 10)
# Calculating means
Data = ema(Data, 2, boll_lookback, high, where)
Data = ema(Data, 2, boll_lookback, low, where + 1)
Data = volatility(Data, boll_lookback, high, where + 2)
Data = volatility(Data, boll_lookback, low, where + 3)
Data[:, where + 4] = Data[:, where] + (standard_distance * Data[:, where + 2])
Data[:, where + 5] = Data[:, where + 1] - (standard_distance * Data[:, where + 3])
Data = jump(Data, boll_lookback)
Data = deleter(Data, where, 4)
return Data
def atr(Data, lookback, high, low, close, where, genre = 'Smoothed'):
# Adding the required columns
Data = adder(Data, 1)
# True Range Calculation
for i in range(len(Data)):
try:
Data[i, where] = max(Data[i, high] - Data[i, low],
abs(Data[i, high] - Data[i - 1, close]),
abs(Data[i, low] - Data[i - 1, close]))
except ValueError:
pass
Data[0, where] = 0
if genre == 'Smoothed':
# Average True Range Calculation
Data = ema(Data, 2, lookback, where, where + 1)
if genre == 'Simple':
# Average True Range Calculation
Data = ma(Data, lookback, where, where + 1)
# Cleaning
Data = deleter(Data, where, 1)
Data = jump(Data, lookback)
return Data
def pure_pupil(Data, lookback, high, low, where):
volatility(Data, lookback, high, where)
volatility(Data, lookback, low, where + 1)
Data[:, where + 2] = (Data[:, where] + Data[:, where + 1]) / 2
Data = jump(Data, lookback)
Data = ema(Data, 2, lookback, where + 2, where + 3)
Data = jump(Data, lookback)
Data = deleter(Data, where, 3)
return Data
def rsi(Data, lookback, close, where, width = 1, genre = 'Smoothed'):
# Adding a few columns
Data = adder(Data, 5)
# Calculating Differences
for i in range(len(Data)):
Data[i, where] = Data[i, close] - Data[i - width, close]
# Calculating the Up and Down absolute values
for i in range(len(Data)):
if Data[i, where] > 0:
Data[i, where + 1] = Data[i, where]
elif Data[i, where] < 0:
Data[i, where + 2] = abs(Data[i, where])
# Calculating the Smoothed Moving Average on Up and Down absolute values
if genre == 'Smoothed':
lookback = (lookback * 2) - 1 # From exponential to smoothed
Data = ema(Data, 2, lookback, where + 1, where + 3)
Data = ema(Data, 2, lookback, where + 2, where + 4)
if genre == 'Simple':
Data = ma(Data, lookback, where + 1, where + 3)
Data = ma(Data, lookback, where + 2, where + 4)
if genre == 'Hull':
hull_moving_average(Data, where + 1, lookback, where + 3)
hull_moving_average(Data, where + 2, lookback, where + 4)
# Calculating the Relative Strength
Data[:, where + 5] = Data[:, where + 3] / Data[:, where + 4]
# Calculate the Relative Strength Index
Data[:, where + 6] = (100 - (100 / (1 + Data[:, where + 5])))
# Cleaning
Data = deleter(Data, where, 6)
Data = jump(Data, lookback)
return Data
def fractal_indicator(Data, high, low, ema_lookback, min_max_lookback, where):
Data = adder(Data, 20)
Data = ema(Data, 2, ema_lookback, high, where)
Data = ema(Data, 2, ema_lookback, low, where + 1)
Data = volatility(Data, ema_lookback, high, where + 2)
Data = volatility(Data, ema_lookback, low, where + 3)
Data[:, where + 4] = Data[:, high] - Data[:, where]
Data[:, where + 5] = Data[:, low] - Data[:, where + 1]
for i in range(len(Data)):
try:
Data[i, where + 6] = max(Data[i - min_max_lookback + 1:i + 1, where + 4])
except ValueError:
pass
for i in range(len(Data)):
try:
Data[i, where + 7] = min(Data[i - min_max_lookback + 1:i + 1, where + 5])
except ValueError:
pass
Data[:, where + 8] = (Data[:, where + 2] + Data[:, where + 3]) / 2
Data[:, where + 9] = (Data[:, where + 6] - Data[:, where + 7]) / Data[:, where + 8]
Data = deleter(Data, 5, 9)
Data = jump(Data, min_max_lookback)
return Data
def stochastic(Data, lookback, close, where, genre = 'High-Low'):
# Adding a column
Data = adder(Data, 1)
if genre == 'High-Low':
for i in range(len(Data)):
try:
Data[i, where] = (Data[i, close] - min(Data[i - lookback + 1:i + 1, 2])) / (max(Data[i - lookback + 1:i + 1, 1]) - min(Data[i - lookback + 1:i + 1, 2]))
except ValueError:
pass
Data[:, where] = Data[:, where] * 100
Data = jump(Data, lookback)
if genre == 'Normalization':
for i in range(len(Data)):
try:
Data[i, where] = (Data[i, close] - min(Data[i - lookback + 1:i + 1, close])) / (max(Data[i - lookback + 1:i + 1, close]) - min(Data[i - lookback + 1:i + 1, close]))
except ValueError:
pass
Data[:, where] = Data[:, where] * 100
Data = jump(Data, lookback)
return Data
def divergence(Data, indicator, lower_barrier, upper_barrier, width, buy, sell):
for i in range(len(Data)):
try:
if Data[i, indicator] < lower_barrier:
for a in range(i + 1, i + width):
# First trough
if Data[a, indicator] > lower_barrier:
for r in range(a + 1, a + width):
if Data[r, indicator] < lower_barrier and \
Data[r, indicator] > Data[i, indicator] and Data[r, 3] < Data[i, 3]:
for s in range(r + 1, r + width):
# Second trough
if Data[s, indicator] > lower_barrier:
Data[s, buy] = 1
break
else:
break
else:
break
else:
break
else:
break
except IndexError:
pass
for i in range(len(Data)):
try:
if Data[i, indicator] > upper_barrier:
for a in range(i + 1, i + width):
# First trough
if Data[a, indicator] < upper_barrier:
for r in range(a + 1, a + width):
if Data[r, indicator] > upper_barrier and \
Data[r, indicator] < Data[i, indicator] and Data[r, 3] > Data[i, 3]:
for s in range(r + 1, r + width):
# Second trough
if Data[s, indicator] < upper_barrier:
Data[s, sell] = -1
break
else:
break
else:
break
else:
break
else:
break
except IndexError:
pass
return Data
def hidden_divergence(Data, lower_barrier, upper_barrier, width):
for i in range(len(Data)):
try:
if Data[i, 5] < lower_barrier and Data[i - 1, 5] > lower_barrier:
for a in range(i + 1, i + width):
# First trough
if Data[a, 5] > lower_barrier:
for r in range(a + 1, a + width):
if Data[r, 5] < lower_barrier and \
Data[r, 5] < Data[i, 5] and Data[r, 3] > Data[i, 3]:
for s in range(r + 1, r + width):
# Second trough
if Data[s, 5] > lower_barrier:
Data[s, 6] = 1
break
else:
break
else:
break
else:
break
else:
break
except IndexError:
pass
for i in range(len(Data)):
try:
if Data[i, 5] > upper_barrier and Data[i - 1, 5] < upper_barrier:
for a in range(i + 1, i + width):
# First trough
if Data[a, 5] < upper_barrier:
for r in range(a + 1, a + width):
if Data[r, 5] > upper_barrier and \
Data[r, 5] > Data[i, 5] and Data[r, 3] < Data[i, 3]:
for s in range(r + 1, r + width):
# Second trough
if Data[s, 5] < upper_barrier:
Data[s, 7] = -1
break
else:
break
else:
break
else:
break
else:
break
except IndexError:
pass
return Data
def vami(Data, lookback, moving_average_lookback, what, where):
for i in range(len(Data)):
Data[i, where] = Data[i, what] - Data[i - lookback, what]
volatility(Data, lookback, what, where + 1)
Data = jump(Data, lookback)
Data[:, where + 2] = (Data[:, where] - Data[:, where + 1]) * 1000
Data = ema(Data, 2, moving_average_lookback, where + 2, where + 3)
Data = jump(Data, moving_average_lookback)
Data = deleter(Data, 5, 3)
return Data
def sar(s, af = 0.02, amax = 0.2):
high, low = s.high, s.low
# Starting values
sig0, xpt0, af0 = True, high[0], af
sar = [low[0] - (high - low).std()]
for i in range(1, len(s)):
sig1, xpt1, af1 = sig0, xpt0, af0
lmin = min(low[i - 1], low[i])
lmax = max(high[i - 1], high[i])
if sig1:
sig0 = low[i] > sar[-1]
xpt0 = max(lmax, xpt1)
else:
sig0 = high[i] >= sar[-1]
xpt0 = min(lmin, xpt1)
if sig0 == sig1:
sari = sar[-1] + (xpt1 - sar[-1])*af1
af0 = min(amax, af1 + af)
if sig0:
af0 = af0 if xpt0 > xpt1 else af1
sari = min(sari, lmin)
else:
af0 = af0 if xpt0 < xpt1 else af1
sari = max(sari, lmax)
else:
af0 = af
sari = xpt0
sar.append(sari)
return sar
def rri(Data, lookback, where):
# Adding a column
Data = adder(Data, 1)
for i in range(len(Data)):
Data[i, where] = (Data[i, 3] - Data[i - lookback, 0]) / (Data[i - lookback, 1] - Data[i - lookback, 2])
if Data[i - lookback, 1] == Data[i - lookback, 2]:
Data[i, where] = 0
return Data
def macd(Data, what, long_ema, short_ema, signal_ema, where):
Data = adder(Data, 1)
Data = ema(Data, 2, long_ema, what, where)
Data = ema(Data, 2, short_ema, what, where + 1)
Data[:, where + 2] = Data[:, where + 1] - Data[:, where]
Data = jump(Data, long_ema)
Data = ema(Data, 2, signal_ema, where + 2, where + 3)
Data = deleter(Data, where, 2)
Data = jump(Data, signal_ema)
return Data
def maci(Data, lookback, normalization_lookback, what, where):
Data = adder(Data, 1)
Data = ema(Data, 2, lookback, what, where)
Data[:, where + 1] = Data[:, what] - Data[:, where]
Data = stochastic(Data, normalization_lookback, where + 1, where + 2, genre = 'Normalization')
Data = jump(Data, lookback)
Data = deleter(Data, where, 2)
return Data
def rainbow(Data, ma1, ma2, ma3, ma4, ma5, ma6, ma7, what, where):
# Converting Exponential lookback to Smoothed Lookback
ma1 = (ma1 * 2) - 1
ma2 = (ma2 * 2) - 1
ma3 = (ma3 * 2) - 1
ma4 = (ma4 * 2) - 1
ma5 = (ma5 * 2) - 1
ma6 = (ma6 * 2) - 1
ma7 = (ma7 * 2) - 1
# Calculating the Smoothed Moving Averages A.K.A The Rainbow Moving Average
Data = ema(Data, 2, ma1, what, where)
Data = ema(Data, 2, ma2, what, where + 1)
Data = ema(Data, 2, ma3, what, where + 2)
Data = ema(Data, 2, ma4, what, where + 3)
Data = ema(Data, 2, ma5, what, where + 4)
Data = ema(Data, 2, ma6, what, where + 5)
Data = ema(Data, 2, ma7, what, where + 6)
Data = jump(Data, ma7)
# The Rainbow Oscillator
Data[:, where + 7] = Data[:, where] - Data[:, where + 6]
return Data
def momentum_indicator(Data, lookback, what, where):
Data = adder(Data, 1)
for i in range(len(Data)):
Data[i, where] = Data[i, what] / Data[i - lookback, what] * 100
Data = jump(Data, lookback)
return Data
def fmi(Data, what, where, lookback, boll_lookback, standard_distance):
for i in range(len(Data)):
Data[i, where] = Data[i, what] / Data[i - lookback, what] * 100
Data = BollingerBands(Data, boll_lookback, standard_distance, where, where + 1)
Data[:, where + 3] = Data[:, where + 1] - Data[:, where]
Data[:, where + 4] = Data[:, where] - Data[:, where + 2]
Data = jump(Data, lookback)
return Data
def adx(Data, high, low, close, lookback, where):
# DM+
for i in range(len(Data)):
if (Data[i, high] - Data[i - 1, high]) > (Data[i - 1, low] - Data[i, low]):
Data[i, where] = Data[i, high] - Data[i - 1, high]
else:
Data[i, where] = 0
# DM-
for i in range(len(Data)):
if (Data[i, high] - Data[i - 1, high]) < (Data[i - 1, low] - Data[i, low]):
Data[i, where + 1] = Data[i - 1, low] - Data[i, low]
else:
Data[i, where + 1] = 0
# Smoothing DI+
Data = ema(Data, 2, (lookback * 2 - 1), where, where + 2)
# Smoothing DI-
Data = ema(Data, 2, (lookback * 2 - 1), where + 1, where + 3)
# Smoothing ATR
Data = atr(Data, (lookback * 2 - 1), high, low, close, where + 4)
Data = jump(Data, lookback)
# DI+
Data[:, where + 5] = Data[:, where + 2] / Data[:, where + 4]
# DI-
Data[:, where + 6] = Data[:, where + 3] / Data[:, where + 4]
# ADX
for i in range(len(Data)):
Data[i, where + 7] = abs(Data[i, where + 5] - Data[i, where + 6]) / abs(Data[i, where + 5] + Data[i, where + 6]) * 100
Data = ema(Data, 2, (lookback * 2 - 1), where + 7, where + 8)
Data = jump(Data, lookback)
Data = deleter(Data, where, 5)
return Data
def donchian(Data, low, high, lookback, where, median = 1):
for i in range(len(Data)):
try:
Data[i, where] = max(Data[i - lookback:i + 1, high])
except ValueError:
pass
for i in range(len(Data)):
try:
Data[i, where + 1] = min(Data[i - lookback:i + 1, low])
except ValueError:
pass
if median == 1:
for i in range(len(Data)):
try:
Data[i, where + 2] = (Data[i, where] + Data[i, where + 1]) / 2
except ValueError:
pass
Data = jump(Data, lookback)
return Data
def ichimoku(Data, close, high, low, kijun_lookback,
tenkan_lookback,
chikou_lookback,
senkou_span_projection,
senkou_span_b_lookback,
where):
Data = adder(Data, 3)
# Kijun-sen
for i in range(len(Data)):
try:
Data[i, where] = max(Data[i - kijun_lookback:i + 1, high]) + min(Data[i - kijun_lookback:i + 1, low])
except ValueError:
pass
Data[:, where] = Data[:, where] / 2
# Tenkan-sen
for i in range(len(Data)):
try:
Data[i, where + 1] = max(Data[i - tenkan_lookback:i + 1, high]) + min(Data[i - tenkan_lookback:i + 1, low])
except ValueError:
pass
Data[:, where + 1] = Data[:, where + 1] / 2
# Senkou-span A
senkou_span_a = (Data[:, where] + Data[:, where + 1]) / 2
senkou_span_a = np.reshape(senkou_span_a, (-1, 1))
# Senkou-span B
for i in range(len(Data)):
try:
Data[i, where + 2] = max(Data[i - senkou_span_b_lookback:i + 1, high]) + min(Data[i - senkou_span_b_lookback:i + 1, low])
except ValueError:
pass
Data[:, where + 2] = Data[:, where + 2] / 2
senkou_span_b = Data[:, where + 2]
senkou_span_b = np.reshape(senkou_span_b, (-1, 1))
kumo = np.concatenate((senkou_span_a, senkou_span_b), axis = 1)