-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path[fikira] MACD & RSI+Stoch + Divergences
1796 lines (1616 loc) · 89.6 KB
/
[fikira] MACD & RSI+Stoch + Divergences
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
//@version=4
// This source code is subject to the terms of the Creative Commons Zero v1.0 Universal license
// https://github.com/fikira12/Divergences
// Copyright (c) 2019 @fikira
study(title="[fikira] MACD & RSI+Stoch + Divergences 2", shorttitle="MACD & RSI+Stoch + DIV 2", max_bars_back=500)
Pick = input(false, title="-<><><><><><>[Pick Your Poison]<><><><><><>-")
choice = input("MACD", options=["MACD", "RSI", "Stoch RSI"], title="Choice Indicator")
input = input("macd", options=["macd", "signal", "hist"], title="Source Divergences [if MACD]")
ST = input(false, title="-<><><><><><>[Calculation Period]<><><><><><>-")
// Because the script is heavy in calculation it is limited in time
// (= inspired from a script of "Che_Trader")
_Year = input(2019, "Start calculation (Year)")
_Month = input(1, "Start calculation (Month)")
_Start = timestamp(_Year, _Month, 1, 1, 1)
//_Start = timeframe.isintraday ? timestamp(year, month, dayofweek, 0, 0) : timestamp(_Year, _Month, _Day, 0, 0)
Year_ = input(2099, "Stop calculation (Year)")
Stop_ = timestamp(Year_, 12, 31, 23, 59)
Period = time >= _Start and time <= Stop_ ? true : false
col_green = choice == "Stoch RSI" or choice == "RSI" ? color.green : na
col_red = choice == "Stoch RSI" or choice == "RSI" ? color.red : na
col_white = choice == "Stoch RSI" or choice == "RSI" ? color.white : na
col_yellow = choice == "Stoch RSI" or choice == "RSI" ? color.yellow : na
col_black = choice == "Stoch RSI" or choice == "RSI" ? color.new(color.black, 100) : na
// RSI
RSI = input(false, title="-<><><><><><>[RSI]<><><><><><>-")
col_yellow_ = choice == "RSI" ? color.yellow : na
src_ = close, len_ = input(14, minval=1, title="RSI Length")
up = rma(max(change(src_), 0), len_)
down = rma(-min(change(src_), 0), len_)
rsi_ = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(choice == "RSI" ? rsi_ : na, color=col_yellow_, linewidth=1, transp=0, title="RSI Line")
// MACD
MACD = input(false, title="-<><><><><><>[MACD]<><><><><><>-")
fast_length = input(title="MACD Fast Length", type=input.integer, defval=12)
slow_length = input(title="MACD Slow Length", type=input.integer, defval=26)
src = input(title="MACD Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="SMA(Oscillator) instead of EMA ?", type=input.bool, defval=false)
sma_signal = input(title="SMA(Signal Line) instead of EMA ?", type=input.bool, defval=false)
// Plot colors
col_grow_above = choice == "MACD" ? #26A69A : na
col_grow_below = choice == "MACD" ? #FFCDD2 : na
col_fall_above =choice == "MACD" ? #B2DFDB : na
col_fall_below = choice == "MACD" ? #EF5350 : na
col_macd = choice == "MACD" ? #0094ff : na
col_signal = choice == "MACD" ? #ff6a00 : na
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
plot(choice == "MACD" ? hist : na, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=30 )
plot(choice == "MACD" ? macd : na, title="MACD", color=col_macd, linewidth=2, transp=0)
plot(choice == "MACD" ? signal : na, title="Signal", color=col_signal, linewidth=2, transp=0)
plot(crossunder(macd, signal) and choice == "MACD" ? signal : na, color=col_signal, style = plot.style_cross, linewidth = 2, transp = 0, title="X MACD under signal")
plot(crossover(macd, signal) and choice == "MACD" ? signal : na, color=col_macd, style = plot.style_cross, linewidth = 2, transp = 0, title="X MACD over signal")
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Stoch RSI
Stoch = input(false, title="-<><><><><><>[Stoch RSI]<><><><><><>-")
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1, title="Stoch (RSI) length")
src2 = input(close, title="RSI Source")
rsi1 = rsi(src2, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
pk = plot(choice == "Stoch RSI" ? k : na, transp=50, linewidth=2, color=color.aqua, title="Stoch 'k'")
pd = plot(choice == "Stoch RSI" ? d : na, transp=50, linewidth=2, color=color.red, title="Stoch 'd'")
fill(pk, pd, choice == "Stoch RSI" and k > d ? col_green : choice == "Stoch RSI" and k < d ? col_red : na, transp=75, title="BG Over/Undersold RSI")
//plot(rsi1, color=color.yellow, linewidth=1, transp=0, title="RSI Line")
h8 = hline(choice == "Stoch RSI" or choice == "RSI" ? 20 : na, color=col_black, linestyle=hline.style_dotted, title="Extreme Oversold Stoch Rsi")
h7 = hline(choice == "Stoch RSI" or choice == "RSI" ? 30 : na, color=col_black, linestyle=hline.style_dotted, title="Oversold Stoch Rsi")
hline(choice == "Stoch RSI" or choice == "RSI" ? 50 : na, color=col_yellow, linestyle=hline.style_dotted, title="Middle line")
h3 = hline(choice == "Stoch RSI" or choice == "RSI" ? 70 : na, color=col_black, linestyle=hline.style_dotted, title="Overbought Stoch Rsi")
h2 = hline(choice == "Stoch RSI" or choice == "RSI" ? 80 : na, color=col_black, linestyle=hline.style_dotted, title="Extreme Overbought Stoch Rsi")
fill(h7, h8, choice == "Stoch RSI" or choice == "RSI" ? col_green : na,transp=80, title="BG Color overbought to extreme overbought zone")
fill(h2, h3, choice == "Stoch RSI" or choice == "RSI" ? col_red : na, transp=80, title="BG Color oversold to extreme oversold zone")
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DIV = input(false, title="-<><><><><><>[Divergences]<><><><><><>-")
nwh =
choice == "Stoch RSI" or choice == "MACD" and close > open ? close :
choice == "Stoch RSI" or choice == "MACD" and close < open ? open :
choice == "RSI" and close > open ? close : na
nwl =
choice == "Stoch RSI" or choice == "MACD" and close > open ? open :
choice == "Stoch RSI" or choice == "MACD" and close < open ? close :
choice == "RSI" and close < open ? close : na
nwh_ = close > open ? high : close < open ? high : na
nwl_ = close > open ? low : close < open ? low : na
show_div = input(true, title="Show Regular Divergences")
show_hiddiv = input(true, title="Show Hidden Divergences")
arrows = input(true, title="Just Show Arrows (No Lines)")
dot = input(title="[Divergences] Line Type", defval="solid", options=["solid", "dashed", "dotted"])
lw = input(1, minval=0, maxval=10, title="[Divergences] linewidth")
_dot = line.style_dotted
if dot == "solid"
_dot := line.style_solid
else
if dot == "dashed"
_dot := line.style_dashed
else
if dot == "dotted"
_dot := line.style_dotted
//solid = input(title="Line Type [Connections]", defval="solid", options=["solid", "dashed", "dotted"])
//_solid = line.style_solid
//if solid == "solid"
// _solid := line.style_solid
//else
// if solid == "dashed"
// _solid := line.style_dashed
// else
// if solid == "dotted"
// _solid := line.style_dotted
symbl = input(title="[Divergences] Bullish Labels", defval="Label Up",
options=["Label Up", "Arrow Up", "Triangle Up", "Circle", "Square", "Xcross", "Cross", "Flag", "Diamond"])
_symbl = label.style_labelup
if symbl == "Label Up"
_symbl := label.style_labelup
else
if symbl == "Arrow Up"
_symbl := label.style_arrowup
else
if symbl == "Triangle Up"
_symbl := label.style_triangleup
else
if symbl == "Circle"
_symbl := label.style_circle
else
if symbl == "Square"
_symbl := label.style_square
else
if symbl == "Xcross"
_symbl := label.style_xcross
else
if symbl == "Cross"
_symbl := label.style_cross
else
if symbl == "Flag"
_symbl := label.style_flag
else
if symbl == "Diamond"
_symbl := label.style_diamond
symbr = input(title="[Divergences] Bearish Labels", defval="Label Down",
options=["Label Down", "Arrow Down", "Triangle Down", "Circle", "Square", "Xcross", "Cross", "Flag", "Diamond"])
_symbr = label.style_labeldown
if symbr == "Label Down"
_symbr := label.style_labeldown
else
if symbr == "Arrow Down"
_symbr := label.style_arrowdown
else
if symbr == "Triangle Down"
_symbr := label.style_triangledown
else
if symbr == "Circle"
_symbr := label.style_circle
else
if symbr == "Square"
_symbr := label.style_square
else
if symbr == "Xcross"
_symbr := label.style_xcross
else
if symbr == "Cross"
_symbr := label.style_cross
else
if symbr == "Flag"
_symbr := label.style_flag
else
if symbr == "Diamond"
_symbr := label.style_diamond
size = input(title="[Divergences] Label Size", defval="Auto",
options=["Tiny", "Small", "Normal", "Auto"])
_size = size.auto
if size == "Tiny"
_size := size.tiny
else
if size == "Small"
_size := size.small
else
if size == "Normal"
_size := size.normal
else
if size == "Auto"
_size := size.auto
Bars = input(false, title="-<><><><><><>[LeftBars RightBars]<><><><><><>-")
LB = input(5, title = "LeftBars 'MACD & Stoch'")
RB = input(0, title = "RightBars 'MACD & Stoch'")
LB_ = input(10, title = "LeftBars 'RSI'")
RB_ = input(1, title = "RightBars 'RSI'")
_x = input(false, title="Custom Div Period?")
x = _x ? input(7, minval=7, title="Custom Div Period:") : na
rsi = 0.0
if Period and choice == "MACD" and input == "macd"
rsi := macd
else
if Period and choice == "MACD" and input == "signal"
rsi := signal
else
if Period and choice == "MACD" and input == "hist"
rsi := hist
else
if Period and choice == "Stoch RSI"
rsi := k
else
if Period and choice == "RSI"
rsi := rsi_
ph =
Period and choice == "MACD" ? pivothigh(nwh, LB, RB) :
Period and choice == "Stoch RSI" ? pivothigh(nwh, LB, RB) :
Period and choice == "RSI" ? pivothigh(nwh, LB_, RB_) : na
_ph = Period ? pivothigh(nwh_, LB_, RB_) : na
pl =
Period and choice == "MACD" ? pivotlow(nwl, LB, RB) :
Period and choice == "Stoch RSI" ? pivotlow(nwl, LB, RB) :
Period and choice == "RSI" ? pivotlow(nwl, LB_, RB_) : na
_pl = Period ? pivothigh(nwl_, LB_, RB_) : na
rh =
Period and choice == "Stoch RSI" and (k<d) and (k[1]>=d[1]) and (k[2]>=d[2]) ? pivothigh(rsi, LB, RB) :
Period and choice == "MACD" and macd <= macd[1] and macd[1] >= macd[2] ? pivothigh(rsi, LB, RB) :
Period and choice == "RSI" ? pivothigh(rsi, LB_, RB_) : na
rl =
Period and choice == "Stoch RSI" and (k>d) and (k[1]<=d[1]) and (k[2]<=d[2]) ? pivotlow(rsi, LB, RB) :
Period and choice == "MACD" and macd >= macd[1] and macd[1] <= macd[2] ? pivotlow(rsi, LB, RB) :
Period and choice == "RSI" ? pivotlow(rsi, LB_, RB_) : na
//plot(rl, color=color.lime, linewidth=2, offset=-1)
//plot(rh, color=color.red, linewidth=2, offset=-1)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rsih0 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 0) : na
rsil0 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 0) : na
hh0 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih0 ? valuewhen(rh, nwh[1], 0) : na
ll0 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil0 ? valuewhen(rl, nwl[1], 0) : na
rsih1 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 1) : na
rsil1 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 1) : na
hh1 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih1 ? valuewhen(rh, nwh[1], 1) : na
ll1 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil1 ? valuewhen(rl, nwl[1], 1) : na
rsih2 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 2) : na
rsil2 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 2) : na
hh2 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih2 ? valuewhen(rh, nwh[1], 2) : na
ll2 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil2 ? valuewhen(rl, nwl[1], 2) : na
rsih3 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 3) : na
rsil3 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 3) : na
hh3 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih3 ? valuewhen(rh, nwh[1], 3) : na
ll3 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil3 ? valuewhen(rl, nwl[1], 3) : na
rsih4 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 4) : na
rsil4 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 4) : na
hh4 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih4 ? valuewhen(rh, nwh[1], 4) : na
ll4 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil4 ? valuewhen(rl, nwl[1], 4) : na
rsih5 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 5) : na
rsil5 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 5) : na
hh5 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih5 ? valuewhen(rh, nwh[1], 5) : na
ll5 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil5 ? valuewhen(rl, nwl[1], 5) : na
rsih6 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], 6) : na
rsil6 = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], 6) : na
hh6 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsih6 ? valuewhen(rh, nwh[1], 6) : na
ll6 = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsil6 ? valuewhen(rl, nwl[1], 6) : na
rsihx = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rh, rsi[1], x) : na
rsilx = Period and choice == "Stoch RSI" or Period and choice == "MACD" ? valuewhen(rl, rsi[1], x) : na
hhx = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsihx ? valuewhen(rh, nwh[1], x) : na
llx = Period and choice == "Stoch RSI" or Period and choice == "MACD" and rsilx ? valuewhen(rl, nwl[1], x) : na
hh0_ = Period and choice == "RSI" ? valuewhen(ph, nwh[1], 0) : na
ll0_ = Period and choice == "RSI" ? valuewhen(pl, nwl[1], 0) : na
rsih0_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 0) : na
rsil0_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 0) : na
hh1_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 1) : na
ll1_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 1) : na
_hh1 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 1) : na
_ll1 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 1) : na
rsih1_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 1) : na
rsil1_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 1) : na
hh2_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 2) : na
ll2_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 2) : na
_hh2 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 2) : na
_ll2 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 2) : na
rsih2_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 2) : na
rsil2_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 2) : na
hh3_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 3) : na
ll3_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 3) : na
_hh3 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 3) : na
_ll3 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 3) : na
rsih3_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 3) : na
rsil3_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 3) : na
hh4_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 4) : na
ll4_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 4) : na
_hh4 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 4) : na
_ll4 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 4) : na
rsih4_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 4) : na
rsil4_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 4) : na
hh5_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 5) : na
ll5_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 5) : na
_hh5 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 5) : na
_ll5 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 5) : na
rsih5_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 5) : na
rsil5_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 5) : na
hh6_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 6) : na
ll6_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 6) : na
_hh6 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 6) : na
_ll6 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 6) : na
rsih6_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 6) : na
rsil6_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 6) : na
hh7_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 7) : na
ll7_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 7) : na
_hh7 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1],7) : na
_ll7 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 7) : na
rsih7_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 7) : na
rsil7_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 7) : na
hh8_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh[1], 8) : na
ll8_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl[1], 8) : na
_hh8 = Period and choice == "RSI" and hh0_ ? valuewhen(ph, nwh_[1], 8) : na
_ll8 = Period and choice == "RSI" and ll0_ ? valuewhen(pl, nwl_[1], 8) : na
rsih8_ = Period and choice == "RSI" and hh0_ ? valuewhen(ph, rsi[1], 8) : na
rsil8_ = Period and choice == "RSI" and ll0_ ? valuewhen(pl, rsi[1], 8) : na
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_m = input(title="Margin", defval="0", options=["0", "0.236", "0.382", "0.5", "0.618", "0.786", "1", "1.272", "1.618", "1.786", "2"])
m = 0.5
if choice == "RSI" and _m == "0"
m := 0
else
if choice == "RSI" and _m == "0.236"
m := 0.236
else
if choice == "RSI" and _m == "0.382"
m := 0.382
else
if choice == "RSI" and _m == "0.5"
m := 0.5
else
if choice == "RSI" and _m == "0.618"
m := 0.618
else
if choice == "RSI" and _m == "0.786"
m := 0.786
else
if choice == "RSI" and _m == "1"
m := 1
else
if choice == "RSI" and _m == "1.272"
m := 1.272
else
if choice == "RSI" and _m == "1.618"
m := 1.618
else
if choice == "RSI" and _m == "1.786"
m := 1.786
else
if choice == "RSI" and _m == "2"
m := 2
////////// Bullish Divergences = close LL - RSI HL /////////////////////////////
//////////////////// MACD - Stoch RSI //////////////////////////////////////////
rsi_HL1 = rsil0 > rsil1
close_LL1 = ll0 < ll1
rsi_HL2 = rsil0 > rsil2
and rsil1 > (rsil2 + ((rsil0 - rsil2) / 2))
close_LL2 = ll0 < ll2
rsi_HL3 = rsil0 > rsil3
and rsil1 > (rsil3 + ((rsil0 - rsil3) / 2))
and rsil2 > (rsil3 + ((rsil0 - rsil3) / 2))
close_LL3 = ll0 < ll3
rsi_HL4 = rsil0 > rsil4
and rsil1 > (rsil4 + ((rsil0 - rsil4) / 2))
and rsil2 > (rsil4 + ((rsil0 - rsil4) / 2))
and rsil3 > (rsil4 + ((rsil0 - rsil4) / 2))
close_LL4 = ll0 < ll4
rsi_HL5 = rsil0 > rsil5
and rsil1 > (rsil5 + ((rsil0 - rsil5) / 2))
and rsil2 > (rsil5 + ((rsil0 - rsil5) / 2))
and rsil3 > (rsil5 + ((rsil0 - rsil5) / 2))
and rsil4 > (rsil5 + ((rsil0 - rsil5) / 2))
close_LL5 = ll0 < ll5
rsi_HL6 = rsil0 > rsil6
and rsil1 > (rsil6 + ((rsil0 - rsil6) / 2))
and rsil2 > (rsil6 + ((rsil0 - rsil6) / 2))
and rsil3 > (rsil6 + ((rsil0 - rsil6) / 2))
and rsil4 > (rsil6 + ((rsil0 - rsil6) / 2))
and rsil5 > (rsil6 + ((rsil0 - rsil6) / 2))
close_LL6 = ll0 < ll6
rsi_HLx = rsil0 > rsilx
and rsil1 > (rsilx + ((rsil0 - rsilx) / 2))
and rsil2 > (rsilx + ((rsil0 - rsilx) / 2))
and rsil3 > (rsilx + ((rsil0 - rsilx) / 2))
and rsil4 > (rsilx + ((rsil0 - rsilx) / 2))
and rsil5 > (rsilx + ((rsil0 - rsilx) / 2))
and rsil6 > (rsilx + ((rsil0 - rsilx) / 2))
close_LLx = ll0 < llx
////////// Bullish Divergences = close LL - RSI HL /////////////////////////////
//////////////////////// RSI ///////////////////////////////////////////////////
close_LL1_ = ll0_ < ll1_
rsi_HL1_ = rsil0_ > rsil1_
close_LL2_ = ll0_ < ll2_
and ll1_ > ll0_ and ll1_ > (_ll2 - ((_ll2 - ll0_) * m))
rsi_HL2_ = rsil0_ > rsil2_
and rsil1_ > rsil2_
and rsil1_ > (rsil2_ + ((rsil0_ - rsil2_) / 2))
close_LL3_ = ll0_ < ll3_
and ll1_ > ll0_ and ll1_ > (_ll3 - ((_ll3 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll3 - ((_ll3 - ll0_) * m))
rsi_HL3_ = rsil0_ > rsil3_
and rsil1_ > (rsil3_ + ((rsil0_ - rsil3_) / 2))
and rsil2_ > (rsil3_ + ((rsil0_ - rsil3_) / 2))
close_LL4_ = ll0_ < ll4_
and ll1_ > ll0_ and ll1_ > (_ll4 - ((_ll4 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll4 - ((_ll4 - ll0_) * m))
and ll3_ > ll0_ and ll3_ > (_ll4 - ((_ll4 - ll0_) * m))
rsi_HL4_ = rsil0_ > rsil4_
and rsil1_ > (rsil4_ + ((rsil0_ - rsil4_) / 2))
and rsil2_ > (rsil4_ + ((rsil0_ - rsil4_) / 2))
and rsil3_ > (rsil4_ + ((rsil0_ - rsil4_) / 2))
close_LL5_ = ll0_ < ll5_
and ll1_ > ll0_ and ll1_ > (_ll5 - ((_ll5 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll5 - ((_ll5 - ll0_) * m))
and ll3_ > ll0_ and ll3_ > (_ll5 - ((_ll5 - ll0_) * m))
and ll4_ > ll0_ and ll4_ > (_ll5 - ((_ll5 - ll0_) * m))
rsi_HL5_ = rsil0_ > rsil5_
and rsil1_ > (rsil5_ + ((rsil0_ - rsil5_) / 2))
and rsil2_ > (rsil5_ + ((rsil0_ - rsil5_) / 2))
and rsil3_ > (rsil5_ + ((rsil0_ - rsil5_) / 2))
and rsil4_ > (rsil5_ + ((rsil0_ - rsil5_) / 2))
close_LL6_ = ll0_ < ll6_
and ll1_ > ll0_ and ll1_ > (_ll6 - ((_ll6 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll6 - ((_ll6 - ll0_) * m))
and ll3_ > ll0_ and ll3_ > (_ll6 - ((_ll6 - ll0_) * m))
and ll4_ > ll0_ and ll4_ > (_ll6 - ((_ll6 - ll0_) * m))
and ll5_ > ll0_ and ll5_ > (_ll6 - ((_ll6 - ll0_) * m))
rsi_HL6_ = rsil0_ > rsil6_
and rsil1_ > (rsil6_ + ((rsil0_ - rsil6_) / 2))
and rsil2_ > (rsil6_ + ((rsil0_ - rsil6_) / 2))
and rsil3_ > (rsil6_ + ((rsil0_ - rsil6_) / 2))
and rsil4_ > (rsil6_ + ((rsil0_ - rsil6_) / 2))
and rsil5_ > (rsil6_ + ((rsil0_ - rsil6_) / 2))
close_LL7_ = ll0_ < ll7_
and ll1_ > ll0_ and ll1_ > (_ll7 - ((_ll7 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll7 - ((_ll7 - ll0_) * m))
and ll3_ > ll0_ and ll3_ > (_ll7 - ((_ll7 - ll0_) * m))
and ll4_ > ll0_ and ll4_ > (_ll7 - ((_ll7 - ll0_) * m))
and ll5_ > ll0_ and ll5_ > (_ll7 - ((_ll7 - ll0_) * m))
and ll6_ > ll0_ and ll6_ > (_ll7 - ((_ll7 - ll0_) * m))
rsi_HL7_ = rsil0_ > rsil7_
and rsil1_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
and rsil2_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
and rsil3_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
and rsil4_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
and rsil5_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
and rsil6_ > (rsil7_ + ((rsil0_ - rsil7_) / 2))
close_LL8_ = ll0_ < ll8_
and ll1_ > ll0_ and ll1_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll2_ > ll0_ and ll2_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll3_ > ll0_ and ll3_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll4_ > ll0_ and ll4_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll5_ > ll0_ and ll5_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll6_ > ll0_ and ll6_ > (_ll8 - ((_ll8 - ll0_) * m))
and ll7_ > ll0_ and ll7_ > (_ll8 - ((_ll8 - ll0_) * m))
rsi_HL8_ = rsil0_ > rsil8_
and rsil1_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil2_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil3_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil4_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil5_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil6_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
and rsil7_ > (rsil8_ + ((rsil0_ - rsil8_) / 2))
div_bull_1 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL1 and rsi_HL1 :
choice == "RSI" ? close_LL1_ and rsi_HL1_ : na
div_bull_2 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL2 and rsi_HL2 :
choice == "RSI" ? close_LL2_ and rsi_HL2_ : na
div_bull_3 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL3 and rsi_HL3 :
choice == "RSI" ? close_LL3_ and rsi_HL3_ : na
div_bull_4 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL4 and rsi_HL4 :
choice == "RSI" ? close_LL4_ and rsi_HL4_ : na
div_bull_5 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL5 and rsi_HL5 :
choice == "RSI" ? close_LL5_ and rsi_HL5_ : na
div_bull_6 =
choice == "Stoch RSI" or choice == "MACD" ? close_LL6 and rsi_HL6 :
choice == "RSI" ? close_LL6_ and rsi_HL6_ : na
div_bull_x =
choice == "Stoch RSI" or choice == "MACD" ? close_LLx and rsi_HLx : na
div_bull_7 = close_LL7_ and rsi_HL7_
div_bull_8 = close_LL8_ and rsi_HL8_
////////// Bearish Divergences = close HH - RSI LH /////////////////////////////
//////////////////// MACD - Stoch RSI //////////////////////////////////////////
rsi_LH1 = rsih0 < rsih1
close_HH1 = hh0 > hh1
rsi_LH2 = rsih0 < rsih2
and rsih1 < (rsih2 - ((rsih2 - rsih0) / 2))
close_HH2 = hh0 > hh2
rsi_LH3 = rsih0 < rsih3
and rsih1 < (rsih3 - ((rsih3 - rsih0) / 2))
and rsih2 < (rsih3 - ((rsih3 - rsih0) / 2))
close_HH3 = hh0 > hh3
rsi_LH4 = rsih0 < rsih4
and rsih1 < (rsih4 - ((rsih4 - rsih0) / 2))
and rsih2 < (rsih4 - ((rsih4 - rsih0) / 2))
and rsih3 < (rsih4 - ((rsih4 - rsih0) / 2))
close_HH4 = hh0 > hh4
rsi_LH5 = rsih0 < rsih5
and rsih1 < (rsih5 - ((rsih5 - rsih0) / 2))
and rsih2 < (rsih5 - ((rsih5 - rsih0) / 2))
and rsih3 < (rsih5 - ((rsih5 - rsih0) / 2))
and rsih4 < (rsih5 - ((rsih5 - rsih0) / 2))
close_HH5 = hh0 > hh5
rsi_LH6 = rsih0 < rsih6
and rsih1 < (rsih6 - ((rsih6 - rsih0) / 2))
and rsih2 < (rsih6 - ((rsih6 - rsih0) / 2))
and rsih3 < (rsih6 - ((rsih6 - rsih0) / 2))
and rsih4 < (rsih6 - ((rsih6 - rsih0) / 2))
and rsih5 < (rsih6 - ((rsih6 - rsih0) / 2))
close_HH6 = hh0 > hh6
rsi_LHx = rsih0 < rsihx
and rsih1 < (rsihx - ((rsihx - rsih0) / 2))
and rsih2 < (rsihx - ((rsihx - rsih0) / 2))
and rsih3 < (rsihx - ((rsihx - rsih0) / 2))
and rsih4 < (rsihx - ((rsihx - rsih0) / 2))
and rsih5 < (rsihx - ((rsihx - rsih0) / 2))
and rsih6 < (rsihx - ((rsihx - rsih0) / 2))
close_HHx = hh0 > hhx
////////// Bearish Divergences = close HH - RSI LH /////////////////////////////
//////////////////////// RSI ///////////////////////////////////////////////////
close_HH1_ = hh0_ > hh1_
rsi_LH1_ = rsih0_ < rsih1_
close_HH2_ = hh0_ > hh2_
and hh1_ < hh0_ and hh1_ < (_hh2 + ((hh0_ - _hh2) * m))
rsi_LH2_ = rsih0_ < rsih2
and rsih1_ < (rsih2_ - ((rsih2_ - rsih0_) / 2))
close_HH3_ = hh0_ > hh3_
and hh1_ < hh0_ and hh1_ < (_hh3 + ((hh0_ - _hh3) * m))
and hh2_ < hh0_ and hh2_ < (_hh3 + ((hh0_ - _hh3) * m))
rsi_LH3_ = rsih0_ < rsih3_
and rsih1_ < (rsih3_ - ((rsih3_ - rsih0_) / 2))
and rsih2_ < (rsih3_ - ((rsih3_ - rsih0_) / 2))
close_HH4_ = hh0_ > hh4_
and hh1_ < hh0_ and hh1_ < (_hh4 + ((hh0_ - _hh4) * m))
and hh2_ < hh0_ and hh2_ < (_hh4 + ((hh0_ - _hh4) * m))
and hh3_ < hh0_ and hh3_ < (_hh4 + ((hh0_ - _hh4) * m))
rsi_LH4_ = rsih0_ < rsih4_
and rsih1_ < (rsih4_ - ((rsih4_ - rsih0_) / 2))
and rsih2_ < (rsih4_ - ((rsih4_ - rsih0_) / 2))
and rsih3_ < (rsih4_ - ((rsih4_ - rsih0_) / 2))
close_HH5_ = hh0_ > hh5_
and hh1_ < hh0_ and hh1_ < (_hh5 + ((hh0_ - _hh5) * m))
and hh2_ < hh0_ and hh2_ < (_hh5 + ((hh0_ - _hh5) * m))
and hh3_ < hh0_ and hh3_ < (_hh5 + ((hh0_ - _hh5) * m))
and hh4_ < hh0_ and hh4_ < (_hh5 + ((hh0_ - _hh5) * m))
rsi_LH5_ = rsih0_ < rsih5_
and rsih1_ < (rsih5_ - ((rsih5_ - rsih0_) / 2))
and rsih2_ < (rsih5_ - ((rsih5_ - rsih0_) / 2))
and rsih3_ < (rsih5_ - ((rsih5_ - rsih0_) / 2))
and rsih4_ < (rsih5_ - ((rsih5_ - rsih0_) / 2))
close_HH6_ = hh0_ > hh6_
and hh1_ < hh0_ and hh1_ < (_hh6 + ((hh0_ - _hh6) * m))
and hh2_ < hh0_ and hh2_ < (_hh6 + ((hh0_ - _hh6) * m))
and hh3_ < hh0_ and hh3_ < (_hh6 + ((hh0_ - _hh6) * m))
and hh4_ < hh0_ and hh4_ < (_hh6 + ((hh0_ - _hh6) * m))
and hh5_ < hh0_ and hh5_ < (_hh6 + ((hh0_ - _hh6) * m))
rsi_LH6_ = rsih0_ < rsih6_
and rsih1_ < (rsih6_ - ((rsih6_ - rsih0_) / 2))
and rsih2_ < (rsih6_ - ((rsih6_ - rsih0_) / 2))
and rsih3_ < (rsih6_ - ((rsih6_ - rsih0_) / 2))
and rsih4_ < (rsih6_ - ((rsih6_ - rsih0_) / 2))
and rsih5_ < (rsih6_ - ((rsih6_ - rsih0_) / 2))
close_HH7_ = hh0_ > hh7_
and hh1_ < hh0_ and hh1_ < (_hh7 + ((hh0_ - _hh7) * m))
and hh2_ < hh0_ and hh2_ < (_hh7 + ((hh0_ - _hh7) * m))
and hh3_ < hh0_ and hh3_ < (_hh7 + ((hh0_ - _hh7) * m))
and hh4_ < hh0_ and hh4_ < (_hh7 + ((hh0_ - _hh7) * m))
and hh5_ < hh0_ and hh5_ < (_hh7 + ((hh0_ - _hh7) * m))
and hh6_ < hh0_ and hh6_ < (_hh7 + ((hh0_ - _hh7) * m))
rsi_LH7_ = rsih0_ < rsih7_
and rsih1_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
and rsih2_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
and rsih3_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
and rsih4_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
and rsih5_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
and rsih6_ < (rsih7_ - ((rsih7_ - rsih0_) / 2))
close_HH8_ = hh0_ > hh8_
and hh1_ < hh0_ and hh1_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh2_ < hh0_ and hh2_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh3_ < hh0_ and hh3_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh4_ < hh0_ and hh4_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh5_ < hh0_ and hh5_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh6_ < hh0_ and hh6_ < (_hh8 + ((hh0_ - _hh8) * m))
and hh7_ < hh0_ and hh7_ < (_hh8 + ((hh0_ - _hh8) * m))
rsi_LH8_ = rsih0_ < rsih8_
and rsih1_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih2_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih3_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih4_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih5_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih6_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
and rsih7_ < (rsih8_ - ((rsih8_ - rsih0_) / 2))
div_bear_1 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH1 and rsi_LH1 :
choice == "RSI" ? close_HH1_ and rsi_LH1_ : na
div_bear_2 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH2 and rsi_LH2 :
choice == "RSI" ? close_HH2_ and rsi_LH2_ : na
div_bear_3 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH3 and rsi_LH3 :
choice == "RSI" ? close_HH3_ and rsi_LH3_ : na
div_bear_4 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH4 and rsi_LH4 :
choice == "RSI" ? close_HH4_ and rsi_LH4_ : na
div_bear_5 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH5 and rsi_LH5 :
choice == "RSI" ? close_HH5_ and rsi_LH5_ : na
div_bear_6 =
choice == "Stoch RSI" or choice == "MACD" ? close_HH6 and rsi_LH6 :
choice == "RSI" ? close_HH6_ and rsi_LH6_ : na
div_bear_x =
choice == "Stoch RSI" or choice == "MACD" ? close_HHx and rsi_LHx : na
div_bear_7 = close_HH7_ and rsi_LH7_
div_bear_8 = close_HH8_ and rsi_LH8_
////////// Hidden Bullish Divergences = close HL - RSI LL //////////////////////
//////////////////// MACD - Stoch RSI //////////////////////////////////////////
rsi_LL1 = rsil0 < rsil1
close_HL1 = ll0 > ll1
rsi_LL2 = rsil0 < rsil2
and rsil1 > (rsil2 + ((rsil2 - rsil0) / 2))
close_HL2 = ll0 > ll2
rsi_LL3 = rsil0 < rsil3
and rsil1 > (rsil3 + ((rsil3 - rsil0) / 2))
and rsil2 > (rsil3 + ((rsil3 - rsil0) / 2))
close_HL3 = ll0 > ll3
rsi_LL4 = rsil0 < rsil4
and rsil1 > (rsil4 + ((rsil4 - rsil0) / 2))
and rsil2 > (rsil4 + ((rsil4 - rsil0) / 2))
and rsil3 > (rsil4 + ((rsil4 - rsil0) / 2))
close_HL4 = ll0 > ll4
rsi_LL5 = rsil0 < rsil5
and rsil1 > (rsil5 + ((rsil5 - rsil0) / 2))
and rsil2 > (rsil5 + ((rsil5 - rsil0) / 2))
and rsil3 > (rsil5 + ((rsil5 - rsil0) / 2))
and rsil4 > (rsil5 + ((rsil5 - rsil0) / 2))
close_HL5 = ll0 > ll5
rsi_LL6 = rsil0 < rsil6
and rsil1 > (rsil6 + ((rsil6 - rsil0) / 2))
and rsil2 > (rsil6 + ((rsil6 - rsil0) / 2))
and rsil3 > (rsil6 + ((rsil6 - rsil0) / 2))
and rsil4 > (rsil6 + ((rsil6 - rsil0) / 2))
and rsil5 > (rsil6 + ((rsil6 - rsil0) / 2))
close_HL6 = ll0 > ll6
rsi_LLx = rsil0 < rsilx
and rsil1 > (rsilx + ((rsilx - rsil0) / 2))
and rsil2 > (rsilx + ((rsilx - rsil0) / 2))
and rsil3 > (rsilx + ((rsilx - rsil0) / 2))
and rsil4 > (rsilx + ((rsilx - rsil0) / 2))
and rsil5 > (rsilx + ((rsilx - rsil0) / 2))
and rsil6 > (rsilx + ((rsilx - rsil0) / 2))
close_HLx = ll0 > llx
////////// Hidden Bullish Divergences = close HL - RSI LL //////////////////////
/////////////////////////// RSI ////////////////////////////////////////////////
close_HL1_ = ll0_ > ll1_
rsi_LL1_ = rsil0_ < rsil1_
close_HL2_ = ll0_ > ll2_
and ll1_ > ll0_ and ll1_ > (_ll2 - ((ll0_ - _ll2) * m))
rsi_LL2_ = rsil0_ < rsil2_
and rsil1_ > (rsil2_ - ((rsil2_ - rsil0_) / 2))
close_HL3_ = ll0_ > ll3_
and ll1_ > ll0_ and ll1_ > (_ll3 - ((ll0_ - _ll3) * m))
and ll2_ > ll0_ and ll2_ > (_ll3 - ((ll0_ - _ll3) * m))
rsi_LL3_ = rsil0_ < rsil3_
and rsil1_ > (rsil3_ - ((rsil3_ - rsil0_) / 2))
and rsil2_ > (rsil3_ - ((rsil3_ - rsil0_) / 2))
close_HL4_ = ll0_ > ll4_
and ll1_ > ll0_ and ll1_ > (_ll4 - ((ll0_ - _ll4) * m))
and ll2_ > ll0_ and ll2_ > (_ll4 - ((ll0_ - _ll4) * m))
and ll3_ > ll0_ and ll3_ > (_ll4 - ((ll0_ - _ll4) * m))
rsi_LL4_ = rsil0_ < rsil4_
and rsil1_ > (rsil4_ - ((rsil4_ - rsil0_) / 2))
and rsil2_ > (rsil4_ - ((rsil4_ - rsil0_) / 2))
and rsil3_ > (rsil4_ - ((rsil4_ - rsil0_) / 2))
close_HL5_ = ll0_ > ll5_
and ll1_ > ll0_ and ll1_ > (_ll5 - ((ll0_ - _ll5) * m))
and ll2_ > ll0_ and ll2_ > (_ll5 - ((ll0_ - _ll5) * m))
and ll3_ > ll0_ and ll3_ > (_ll5 - ((ll0_ - _ll5) * m))
and ll4_ > ll0_ and ll4_ > (_ll5 - ((ll0_ - _ll5) * m))
rsi_LL5_ = rsil0_ < rsil5_
and rsil1_ > (rsil5_ - ((rsil5_ - rsil0_) / 2))
and rsil2_ > (rsil5_ - ((rsil5_ - rsil0_) / 2))
and rsil3_ > (rsil5_ - ((rsil5_ - rsil0_) / 2))
and rsil4_ > (rsil5_ - ((rsil5_ - rsil0_) / 2))
close_HL6_ = ll0_ > ll6_
and ll1_ > ll0_ and ll1_ > (_ll6 - ((ll0_ - _ll6) * m))
and ll2_ > ll0_ and ll2_ > (_ll6 - ((ll0_ - _ll6) * m))
and ll3_ > ll0_ and ll3_ > (_ll6 - ((ll0_ - _ll6) * m))
and ll4_ > ll0_ and ll4_ > (_ll6 - ((ll0_ - _ll6) * m))
and ll5_ > ll0_ and ll5_ > (_ll6 - ((ll0_ - _ll6) * m))
rsi_LL6_ = rsil0_ < rsil6_
and rsil1_ > (rsil6_ - ((rsil6_ - rsil0_) / 2))
and rsil2_ > (rsil6_ - ((rsil6_ - rsil0_) / 2))
and rsil3_ > (rsil6_ - ((rsil6_ - rsil0_) / 2))
and rsil4_ > (rsil6_ - ((rsil6_ - rsil0_) / 2))
and rsil5_ > (rsil6_ - ((rsil6_ - rsil0_) / 2))
close_HL7_ = ll0_ > ll7_
and ll1_ > ll0_ and ll1_ > (_ll7 - ((ll0_ - _ll7) * m))
and ll2_ > ll0_ and ll2_ > (_ll7 - ((ll0_ - _ll7) * m))
and ll3_ > ll0_ and ll3_ > (_ll7 - ((ll0_ - _ll7) * m))
and ll4_ > ll0_ and ll4_ > (_ll7 - ((ll0_ - _ll7) * m))
and ll5_ > ll0_ and ll5_ > (_ll7 - ((ll0_ - _ll7) * m))
and ll6_ > ll0_ and ll6_ > (_ll7 - ((ll0_ - _ll7) * m))
rsi_LL7_ = rsil0_ < rsil7_
and rsil1_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
and rsil2_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
and rsil3_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
and rsil4_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
and rsil5_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
and rsil6_ > (rsil7_ - ((rsil7_ - rsil0_) / 2))
close_HL8_ = ll0_ > ll8_
and ll1_ > ll0_ and ll1_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll2_ > ll0_ and ll2_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll3_ > ll0_ and ll3_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll4_ > ll0_ and ll4_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll5_ > ll0_ and ll5_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll6_ > ll0_ and ll6_ > (_ll8 - ((ll0_ - _ll8) * m))
and ll7_ > ll0_ and ll7_ > (_ll8 - ((ll0_ - _ll8) * m))
rsi_LL8_ = rsil0_ < rsil8_
and rsil1_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil2_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil3_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil4_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil5_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil6_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
and rsil7_ > (rsil8_ - ((rsil8_ - rsil0_) / 2))
hid_div_bull_1 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL1 and rsi_LL1 :
choice == "RSI" ? close_HL1_ and rsi_LL1_ : na
hid_div_bull_2 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL2 and rsi_LL2 :
choice == "RSI" ? close_HL2_ and rsi_LL2_ : na
hid_div_bull_3 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL3 and rsi_LL3 :
choice == "RSI" ? close_HL3_ and rsi_LL3_ : na
hid_div_bull_4 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL4 and rsi_LL4 :
choice == "RSI" ? close_HL4_ and rsi_LL4_ : na
hid_div_bull_5 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL5 and rsi_LL5 :
choice == "RSI" ? close_HL5_ and rsi_LL5_ : na
hid_div_bull_6 =
choice == "Stoch RSI" or choice == "MACD" ? close_HL6 and rsi_LL6 :
choice == "RSI" ? close_HL6_ and rsi_LL6_ : na
hid_div_bull_x =
choice == "Stoch RSI" or choice == "MACD" ? close_HLx and rsi_LLx : na
hid_div_bull_7 = close_HL7_ and rsi_LL7_
hid_div_bull_8 = close_HL8_ and rsi_LL8_
////////// Hidden Bearish Divergences = close LH - RSI HH //////////////////////
//////////////////// MACD - Stoch RSI //////////////////////////////////////////
rsi_HH1 = rsih0 > rsih1
close_LH1 = hh0 < hh1
rsi_HH2 = rsih0 > rsih2
and rsih1 < (rsih2 + ((rsih0 - rsih2) / 2))
close_LH2 = hh0 < hh2
rsi_HH3 = rsih0 > rsih3
and rsih1 < (rsih3 + ((rsih0 - rsih3) / 2))
and rsih2 < (rsih3 + ((rsih0 - rsih3) / 2))
close_LH3 = hh0 < hh3
rsi_HH4 = rsih0 > rsih4
and rsih1 < (rsih4 + ((rsih0 - rsih4) / 2))
and rsih2 < (rsih4 + ((rsih0 - rsih4) / 2))
and rsih3 < (rsih4 + ((rsih0 - rsih4) / 2))
close_LH4 = hh0 < hh4
rsi_HH5 = rsih0 > rsih5
and rsih1 < (rsih5 + ((rsih0 - rsih5) / 2))
and rsih2 < (rsih5 + ((rsih0 - rsih5) / 2))
and rsih3 < (rsih5 + ((rsih0 - rsih5) / 2))
and rsih4 < (rsih5 + ((rsih0 - rsih5) / 2))
close_LH5 = hh0 < hh5
rsi_HH6 = rsih0 > rsih6
and rsih1 < (rsih6 + ((rsih0 - rsih6) / 2))
and rsih2 < (rsih6 + ((rsih0 - rsih6) / 2))
and rsih3 < (rsih6 + ((rsih0 - rsih6) / 2))
and rsih4 < (rsih6 + ((rsih0 - rsih6) / 2))
and rsih5 < (rsih6 + ((rsih0 - rsih6) / 2))
close_LH6 = hh0 < hh6
rsi_HHx = rsih0 > rsihx
and rsih1 < (rsihx + ((rsih0 - rsihx) / 2))
and rsih2 < (rsihx + ((rsih0 - rsihx) / 2))
and rsih3 < (rsihx + ((rsih0 - rsihx) / 2))
and rsih4 < (rsihx + ((rsih0 - rsihx) / 2))
and rsih5 < (rsihx + ((rsih0 - rsihx) / 2))
and rsih6 < (rsihx + ((rsih0 - rsihx) / 2))
close_LHx = hh0 < hhx
////////// Hidden Bearish Divergences = close LH - RSI HH //////////////////////
/////////////////////////// RSI ////////////////////////////////////////////////
close_LH1_ = hh0_ < hh1_
rsi_HH1_ = rsih0_ > rsih1_
close_LH2_ = hh0_ < hh2_
and hh1_ < hh0_ and hh1_ < (_hh2 + ((_hh2 - hh0_) * m))
rsi_HH2_ = rsih0_ > rsih2_
and rsih1_ < (rsih2_ + ((rsih0_ - rsih2_) / 2))
close_LH3_ = hh0_ < hh3_
and hh1_ < hh0_ and hh1_ < (_hh3 + ((_hh3 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh3 + ((_hh3 - hh0_) * m))
rsi_HH3_ = rsih0_ > rsih3_
and rsih1_ < (rsih3_ + ((rsih0_ - rsih3_) / 2))
and rsih2_ < (rsih3_ + ((rsih0_ - rsih3_) / 2))
close_LH4_ = hh0_ < hh4_
and hh1_ < hh0_ and hh1_ < (_hh4 + ((_hh4 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh4 + ((_hh4 - hh0_) * m))
and hh3_ < hh0_ and hh3_ < (_hh4 + ((_hh4 - hh0_) * m))
rsi_HH4_ = rsih0_ > rsih4_
and rsih1_ < (rsih4_ + ((rsih0_ - rsih4_) / 2))
and rsih2_ < (rsih4_ + ((rsih0_ - rsih4_) / 2))
and rsih3_ < (rsih4_ + ((rsih0_ - rsih4_) / 2))
close_LH5_ = hh0_ < hh5_
and hh1_ < hh0_ and hh1_ < (_hh5 + ((_hh5 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh5 + ((_hh5 - hh0_) * m))
and hh3_ < hh0_ and hh3_ < (_hh5 + ((_hh5 - hh0_) * m))
and hh4_ < hh0_ and hh4_ < (_hh5 + ((_hh5 - hh0_) * m))
rsi_HH5_ = rsih0_ > rsih5_
and rsih1_ < (rsih5_ + ((rsih0_ - rsih5_) / 2))
and rsih2_ < (rsih5_ + ((rsih0_ - rsih5_) / 2))
and rsih3_ < (rsih5_ + ((rsih0_ - rsih5_) / 2))
and rsih4_ < (rsih5_ + ((rsih0_ - rsih5_) / 2))
close_LH6_ = hh0_ < hh6_
and hh1_ < hh0_ and hh1_ < (_hh6 + ((_hh6 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh6 + ((_hh6 - hh0_) * m))
and hh3_ < hh0_ and hh3_ < (_hh6 + ((_hh6 - hh0_) * m))
and hh4_ < hh0_ and hh4_ < (_hh6 + ((_hh6 - hh0_) * m))
and hh5_ < hh0_ and hh5_ < (_hh6 + ((_hh6 - hh0_) * m))
rsi_HH6_ = rsih0_ > rsih6_
and rsih1_ < (rsih6_ + ((rsih0_ - rsih6_) / 2))
and rsih2_ < (rsih6_ + ((rsih0_ - rsih6_) / 2))
and rsih3_ < (rsih6_ + ((rsih0_ - rsih6_) / 2))
and rsih4_ < (rsih6_ + ((rsih0_ - rsih6_) / 2))
and rsih5_ < (rsih6_ + ((rsih0_ - rsih6_) / 2))
close_LH7_ = hh0_ < hh7_
and hh1_ < hh0_ and hh1_ < (_hh7 + ((_hh7 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh7 + ((_hh7 - hh0_) * m))
and hh3_ < hh0_ and hh3_ < (_hh7 + ((_hh7 - hh0_) * m))
and hh4_ < hh0_ and hh4_ < (_hh7 + ((_hh7 - hh0_) * m))
and hh5_ < hh0_ and hh5_ < (_hh7 + ((_hh7 - hh0_) * m))
and hh6_ < hh0_ and hh6_ < (_hh7 + ((_hh7 - hh0_) * m))
rsi_HH7_ = rsih0_ > rsih7_
and rsih1_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
and rsih2_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
and rsih3_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
and rsih4_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
and rsih5_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
and rsih6_ < (rsih7_ + ((rsih0_ - rsih7_) / 2))
close_LH8_ = hh0_ < hh8_
and hh1_ < hh0_ and hh1_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh2_ < hh0_ and hh2_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh3_ < hh0_ and hh3_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh4_ < hh0_ and hh4_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh5_ < hh0_ and hh5_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh6_ < hh0_ and hh6_ < (_hh8 + ((_hh8 - hh0_) * m))
and hh7_ < hh0_ and hh7_ < (_hh8 + ((_hh8 - hh0_) * m))
rsi_HH8_ = rsih0_ > rsih8_
and rsih1_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))
and rsih2_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))
and rsih3_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))
and rsih4_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))
and rsih5_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))
and rsih6_ < (rsih8_ + ((rsih0_ - rsih8_) / 2))