-
Notifications
You must be signed in to change notification settings - Fork 2
/
-Backtesting System.pine
998 lines (996 loc) · 64 KB
/
-Backtesting System.pine
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
Script Name: -Backtesting System
Author: HALDRO
Description: ⚉ OVERVIEW ⚉
One of the best Systems for Backtesting your Strategies.
Incredibly flexible, simple, fast and feature-rich system — will solve most of your queries without much effort.
Many systems for setting StopLoss, TakeProfit, Risk Management and advanced Filters.
All you need to do is plug in your indicator and start Backtesting .
I intentionally left the...
PineScript code:
Pine Script™ strategy
*Backtesting System
Copy code
This document contains many ambiguous unicode characters
Disable Ambiguous Highlight
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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @HALDRO Project
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
//@version=5
strategy('*Backtesting System',
overlay = true,
initial_capital = 10000,
commission_value = 0.04,
default_qty_value = 10,
slippage = 1,
pyramiding = 0,
max_lines_count = 500,
max_labels_count = 500,
currency = currency.USD,
default_qty_type = strategy.percent_of_equity)
MA(string _maType, float _maSource, simple int _maLength) =>
float ma = switch _maType
'ALMA' => ta.alma (_maSource, _maLength, 0.85, 6)
'EMA' => ta.ema (_maSource, _maLength)
'HMA' => ta.hma (_maSource, _maLength)
'RMA' => ta.rma (_maSource, _maLength)
'SMA' => ta.sma (_maSource, _maLength)
'VWMA' => ta.vwma (_maSource, _maLength)
'WMA' => ta.wma (_maSource, _maLength)
ma
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// —————— ToolTips
TText_source_ = '𝐄𝐱𝐭𝐞𝐫𝐧𝐚𝐥 𝐒𝐨𝐮𝐫𝐜𝐞 ➖ Here you must select your Indicator, having previously connected the "Connector" to it.\nIf the "Close" option is selected then you can test the backtest on regular MA crosses EMA 200 and EMA 50.'
TTreversesignal = '𝐖𝐚𝐢𝐭 𝐄𝐧𝐝 𝐃𝐞𝐚𝐥 ➖ Enable/Disable waiting for a trade to close at Stop Loss/Take Profit. Until the trade closes on the Stop Loss or Take Profit, no new trade will open. \n'
+ '𝐑𝐞𝐯𝐞𝐫𝐬𝐞 𝐃𝐞𝐚𝐥𝐬 ➖ If true strategy will go in the opposite direction from the signal.'
TTreOpenDeal = '𝐑𝐞𝐄𝐧𝐭𝐫𝐲 𝐃𝐞𝐚𝐥 ➖ If true trade was long and SL/TP was reached then go long again and vice versa. \n'
+ '𝐑𝐞𝐎𝐩𝐞𝐧 𝐃𝐞𝐚𝐥 ➖ If true and Wait End Deal is false then in case you are in a Long position and a new signal to Long, you will reEnter a new Long position and vice versa for Shorts.'
TTslType = '𝐅𝐈𝐗𝐄𝐃 % ➖ Fixed SL/TP in percent. \n'
+ '𝐅𝐈𝐗𝐄𝐃 $ ➖ Fixed SL/TP in cash. \n'
+ '𝐓𝐑𝐀𝐈𝐋𝐈𝐍𝐆 ➖ Trailing stop in % like on exchanges. Regulated by the "𝗧𝗿𝗮𝗶𝗹𝗶𝗻𝗴 %" parameter. \n'
+ '𝐅𝐀𝐒𝐓 𝐓𝐑𝐀𝐈𝐋 ➖ If in a long, it immediately follows the price when it rises otherwise it stands still. Vice versa for shorts. Regulated by the "𝗧𝗿𝗮𝗶𝗹𝗶𝗻𝗴 %" parameter. \n'
+ '𝐀𝐓𝐑 ➖ Fixed TP/SL depending on the current ATR. It is regulated by the "𝗔𝗧𝗥 𝗣𝗲𝗿𝗶𝗼𝗱" | "𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗶𝗲𝗿" parameters. \n'
+ '𝗔𝗧𝗥 𝐓𝐑𝐀𝐈𝐋 ➖ Trailing Stop calculated on the basis of the ATR. It is regulated by the "𝗔𝗧𝗥 𝗣𝗲𝗿𝗶𝗼𝗱" | "𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗶𝗲𝗿" parameters. \n'
+ '𝗥:𝗥 ➖ Risk Reward sets the TP depending on the size of the SL. For example, if SL is $100, and R:R = 2, then the TP is $200. \n'
+ '𝐇𝐇 / 𝐋𝐋 ➖ Searches for the last Extremum (High/Low) for the specified number of bars and sets a fixed Take. \n'
+ '𝐋𝐎 / 𝐇𝐈 ➖ Sets the SL for High/Low candles. "𝗟𝗢/𝗛𝗜" = 1 Sets the SL for the last High/Low. \n'
+ '𝐌𝐀 ➖ Movieng Average SL. A very diverse type of SL.'
// —————— Main Inputs
groupset = '➤ MAIN SETTINGS'
ext_source_ = input.source (close, ' 𝐄𝐱𝐭𝐞𝐫𝐧𝐚𝐥 𝐒𝐨𝐮𝐫𝐜𝐞 ', group=groupset, inline='esrc', tooltip=TText_source_)
bullDeal = input.bool (true, ' Long Deals ', group=groupset, inline='deal')
bearDeal = input.bool (true, ' Short Deals', group=groupset, inline='deal')
waitEndDeal = input.bool (true, ' Wait End Deal ', group=groupset, inline='ord1')
reversesignal = input.bool (false, ' Reverse Deals', group=groupset, inline='ord1', tooltip=TTreversesignal)
reEntryDeal = input.bool (false, ' ReEntry Deal ', group=groupset, inline='ord2')
reOpenDeal = input.bool (false, ' ReOpen Deal', group=groupset, inline='ord2', tooltip=TTreOpenDeal)
tpType = input.string ('R:R', ' 𝐓𝐚𝐤𝐞 𝐏𝐫𝐨𝐟𝐢𝐭�', group=groupset, inline='type', options=['None', 'FIXED %', 'FIXED $', 'ATR', 'R:R', 'HH / LL'])
slType = input.string ('ATR', ' 𝐒𝐭𝐨𝐩 𝐋𝐨𝐬𝐬 ', group=groupset, inline='type', options=['None', 'FIXED %', 'FIXED $', 'TRAILING', 'FAST TRAIL', 'ATR', 'ATR TRAIL', 'LO / HI', 'MA'], tooltip=TTslType)
grouptakes = ' ➤ Take Profit Levels'
ontake1 = input.bool (false, '🇹🇵¹', group=grouptakes, inline='take')
qtake1 = input.int (20, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
ontake2 = input.bool (false, '🇹🇵²', group=grouptakes, inline='take')
qtake2 = input.int (20, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
ontake3 = input.bool (false, '🇹🇵³', group=grouptakes, inline='take')
qtake3 = input.int (50, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
ontake4 = input.bool (false, '🇹🇵⁴', group=grouptakes, inline='take')
qtake4 = input.int (25, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
ontake5 = input.bool (false, '🇹🇵⁵ ', group=grouptakes, inline='take')
qtake5 = input.int (25, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
onstop0 = input.bool (false, '🇸🇱⁰ ', group=grouptakes, inline='take')
qstop0 = input.int (30, '�', group=grouptakes, inline='take', minval=1, step=5, maxval=100)
// Example Connector
//Signal = buy ? +1 : sell ? -1 : 0
//plot(Signal, "🔌Connector🔌", display = display.none)
// —————— Variables Initialisation
ext_source = nz(ext_source_)
bull = ext_source == +1 // +1 is bull signal
bear = ext_source == -1 // -1 is bear signal
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// ————————————————————————————————————————————————— DATE RANGE ——————————————————————————————————————————————————————————— \\ // Credit @jason5480
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
grouptime = '➤ TIME FILTERS'
src_timezone = input.string ('Exchange', 'Timezones Src->Dst', group=grouptime, inline='Timezone', options=['Exchange', 'UTC', 'America/Los_Angeles', 'America/Phoenix', 'America/Vancouver', 'America/El_Salvador', 'America/Bogota', 'America/Chicago', 'America/New_York', 'America/Toronto', 'America/Argentina/Buenos_Aires', 'America/Sao_Paulo', 'Etc/UTC', 'Europe/London', 'Europe/Berlin', 'Europe/Madrid', 'Europe/Paris', 'Europe/Warsaw', 'Europe/Athens', 'Europe/Moscow', 'Asia/Tehran', 'Asia/Dubai', 'Asia/Ashkhabad', 'Asia/Kolkata', 'Asia/Almaty', 'Asia/Bangkok', 'Asia/Hong_Kong', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Taipei', 'Asia/Seoul', 'Asia/Tokyo', 'Australia/ACT', 'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Sydney', 'Pacific/Auckland', 'Pacific/Fakaofo', 'Pacific/Chatham', 'Pacific/Honolulu'])
dst_timezone = input.string ('Exchange', '->', group=grouptime, inline='Timezone', options=['Exchange', 'UTC', 'America/Los_Angeles', 'America/Phoenix', 'America/Vancouver', 'America/El_Salvador', 'America/Bogota', 'America/Chicago', 'America/New_York', 'America/Toronto', 'America/Argentina/Buenos_Aires', 'America/Sao_Paulo', 'Etc/UTC', 'Europe/London', 'Europe/Berlin', 'Europe/Madrid', 'Europe/Paris', 'Europe/Warsaw', 'Europe/Athens', 'Europe/Moscow', 'Asia/Tehran', 'Asia/Dubai', 'Asia/Ashkhabad', 'Asia/Kolkata', 'Asia/Almaty', 'Asia/Bangkok', 'Asia/Hong_Kong', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Taipei', 'Asia/Seoul', 'Asia/Tokyo', 'Australia/ACT', 'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Sydney', 'Pacific/Auckland', 'Pacific/Fakaofo', 'Pacific/Chatham', 'Pacific/Honolulu'], tooltip='The Src is the timezone to be used as a reference for the time settings. The Dst is the timezone to convert into (e.g. the charslPrice\' timezone)')
usefromDate = input.bool (true, 'Srart From ', group=grouptime, inline='From Date')
fromDate = input.time (timestamp('01 Jan 2020 00:00'), '', group=grouptime, inline='From Date')
usetoDate = input.bool (false, 'End To ', group=grouptime, inline='To Date')
toDate = input.time (timestamp('01 Jul 2025 00:00'), '', group=grouptime, inline='To Date')
useSessionDay = input.bool (false, 'Session Days', group=grouptime, inline='Session Days')
mon = input.bool (true, 'Mon', group=grouptime, inline='Session Days')
tue = input.bool (true, 'Tue', group=grouptime, inline='Session Days')
wed = input.bool (true, 'Wed', group=grouptime, inline='Session Days')
thu = input.bool (true, 'Thu', group=grouptime, inline='Session Days')
fri = input.bool (true, 'Fri', group=grouptime, inline='Session Days')
sat = input.bool (false, 'Sat', group=grouptime, inline='Session Days')
sun = input.bool (false, 'Sun', group=grouptime, inline='Session Days')
useSessionStart = input.bool (false, 'Session Start', group=grouptime, inline='Session Start')
sessionStartHour = input.int (12, '', group=grouptime, inline='Session Start', minval=0, maxval=23, step=1)
sessionStartMinute = input.int (00, ':', group=grouptime, inline='Session Start', minval=0, maxval=59, step=1, tooltip='Start time of the session.')
useSessionEnd = input.bool (false, 'Session End ', group=grouptime, inline='Session End')
sessionEndHour = input.int (20, '', group=grouptime, inline='Session End', minval=0, maxval=23, step=1)
sessionEndMinute = input.int (00, ':', group=grouptime, inline='Session End', minval=0, maxval=59, step=1, tooltip='End time of the session.')
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
ex_timezone(simple string tz) =>
switch tz
'Exchange' => syminfo.timezone
=> tz
if_in_date_range(simple bool usefromDate, simple int fromDate, simple bool usetoDate, simple int toDate, simple string src_timezone = 'Exchange', simple string dst_timezone = 'Exchange', int t = time_close) =>
var src_tz = ex_timezone(src_timezone)
var dst_tz = ex_timezone(dst_timezone)
var fromDateTz = timestamp(src_tz, year(fromDate, dst_tz), month(fromDate, dst_tz), dayofmonth(fromDate, dst_tz), hour(fromDate, dst_tz), minute(fromDate, dst_tz), second(fromDate, dst_tz))
var toDateTz = timestamp(src_tz, year(toDate, dst_tz), month(toDate, dst_tz), dayofmonth(toDate, dst_tz), hour(toDate, dst_tz), minute(toDate, dst_tz), second(toDate, dst_tz))
(usefromDate ? t >= fromDateTz : true) and (usetoDate ? t < toDateTz : true)
if_in_session(simple bool useSessionStart, simple int sessionStartHour, simple int sessionStartMinute, simple bool useSessionEnd, simple int sessionEndHour, simple int sessionEndMinute, simple bool useSessionDay = false, simple bool mon = true, simple bool tue = true, simple bool wed = true, simple bool thu = true, simple bool fri = true, simple bool sat = false, simple bool sun = false, simple string src_timezone = 'Exchange', simple string dst_timezone = 'Exchange', int t = time_close) =>
var one_day = 86400000
var utc_tz = 'UTC'
var src_tz = ex_timezone(src_timezone)
var dst_tz = ex_timezone(dst_timezone)
start_hr = sessionStartHour + (hour(t, dst_tz) - hour(t, src_tz))
start_min = sessionStartMinute + (minute(t, dst_tz) - minute(t, src_tz))
end_hr = sessionEndHour + (hour(t, dst_tz) - hour(t, src_tz))
end_min = sessionEndMinute + (minute(t, dst_tz) - minute(t, src_tz))
time_start_session = timestamp(dst_tz, year(t, src_tz), month(t, src_tz), dayofmonth(t, src_tz), start_hr, start_min, second(t, src_tz))
time_end_session = timestamp(dst_tz, year(t, src_tz), month(t, src_tz), dayofmonth(t, src_tz), end_hr, end_min, second(t, src_tz))
var bool isOvernight = time_start_session >= time_end_session
// in overnight sessions increase end time by one day
if (useSessionStart and useSessionEnd and isOvernight)
time_end_session := time_end_session + one_day
isSessionDay = switch dayofweek(t, src_tz)
dayofweek.monday => mon
dayofweek.tuesday => tue
dayofweek.wednesday => wed
dayofweek.thursday => thu
dayofweek.friday => fri
dayofweek.saturday => sat
dayofweek.sunday => sun
=> false
(useSessionDay ? isSessionDay : true) and (useSessionStart ? t >= time_start_session : true) and (useSessionEnd ? t < time_end_session : true)
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
bool dateFilterApproval = if_in_date_range(usefromDate, fromDate, usetoDate, toDate, src_timezone, dst_timezone)
bool sessionFilterApproval = if_in_session(useSessionStart, sessionStartHour, sessionStartMinute, useSessionEnd, sessionEndHour, sessionEndMinute, useSessionDay, mon, tue, wed, thu, fri, sat, sun, src_timezone, dst_timezone)
bool timeFilterApproval = dateFilterApproval and sessionFilterApproval
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// ————————————————————————————————————————————— Signal Filters ——————————————————————————————————————————————————————————— \\ // Credit @pAulseperformance
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
groupsignal = '➤ SIGNAL FILTERS'
FilterType1 = input.bool (false, '1. ADX', group=groupsignal, inline='FT1')
FilterType1Len = input.int (17, '', group=groupsignal, inline='FT1', minval=1 )
FilterType1Smt = input.int (14, '', group=groupsignal, inline='FT1', minval=1, maxval=50)
FilterType1Tresh = input.int (20, '', group=groupsignal, inline='FT1', minval=1, maxval=50, tooltip='ADX Lenght, Smooth, Level. \nTrade If ADX < Level')
FilterType2 = input.bool (false, '2. MACD', group=groupsignal, inline='FT2')
FilterType2Len1 = input.int (12, '', group=groupsignal, inline='FT2')
FilterType2Len2 = input.int (26, '', group=groupsignal, inline='FT2')
FilterType2Len3 = input.int (9, '', group=groupsignal, inline='FT2', tooltip='Fast Len, Slow Len, Smooth. \nLong Filter: MACD > 0\nShort Filter: MACD < 0')
FilterType3 = input.bool (false, '3. Bar direction=Trade direction', group=groupsignal, tooltip='Long filter: close>open\nShort Filter: close<open')
FilterType4 = input.bool (false, '4. Rising Volume', group=groupsignal, tooltip='Long Filter: Volume Increasing\nShort Filter: Volume Decreasing')
FilterType5 = input.bool (false, '5. Rising/Falling MA', group=groupsignal, inline='FT5', tooltip='Long Filter: SMA is Rising\nShort Filter: SMA Falling \nRising/Falling - For N Bars')
FilterType5Len = input.int (50, 'Length', group=groupsignal, inline='FT5', minval=2)
FilterType5Bars = input.int (1, 'Bars', group=groupsignal, inline='FT5', minval=1)
FilterType6 = input.bool (false, '6. High/Low Filter', group=groupsignal, inline='FT6')
FilterType6Bars = input.int (10, 'Lookback', group=groupsignal, inline='FT6', tooltip='If true then the signal bar must be the highest/lovest bar over X bars')
FilterType7 = input.bool (false, '7. Maximum close change (%) allowed on entry', group=groupsignal, inline='FT7')
FilterType7IncPct = input.float (10.0, '', group=groupsignal, inline='FT7', minval=0.0, step=0.5)
FilterType8 = input.bool (false, '8. Minimum close change (%) allowed on entry', group=groupsignal, inline='FT8')
FilterType8IncPct = input.float (1.0, '', group=groupsignal, inline='FT8', minval=0.0, step=0.5)
FilterType9 = input.bool (false, '9. RSI OS/OB', group=groupsignal, inline='FT9')
FilterType9Len = input.int (20, '', group=groupsignal, inline='FT9', minval=2)
FilterType9OS = input.int (25, '', group=groupsignal, inline='FT9', minval=0, maxval=100)
FilterType9OB = input.int (75, '', group=groupsignal, inline='FT9', minval=0, maxval=100, tooltip='RSI Length, OS Level, OB Level \nLong = RSI < OB\nShort = RSI > OS')
FilterType10 = input.bool (false, '10. MA', group=groupsignal, inline='FT10')
FilterType10MAType = input.string ('SMA', '', group=groupsignal, inline='FT10', options=['ALMA', 'EMA', 'HMA', 'RMA', 'SMA', 'VWMA', 'WMA'])
FilterType10Len = input.int (200, '', group=groupsignal, inline='FT10', minval=1, tooltip='MA Type, MA Source, MA Length\nLong = close > MA\nShort = close < MA')
// —————— Filter 1: ADX
[dip, dim, adx] = ta.dmi(FilterType1Len, FilterType1Smt)
Filter1Long = FilterType1 ? adx < FilterType1Tresh : true
Filter1Short = Filter1Long
// —————— Filter 2: MACD Hist
_macdLine = FilterType2 ? nz(MA('EMA', close, FilterType2Len1) - MA('EMA', close, FilterType2Len2)) : 0.
_macdSignal = FilterType2 ? nz(MA('EMA', _macdLine, FilterType2Len3)) : 0.
_macdHist = FilterType2 ? _macdLine - _macdSignal : 0.
Filter2Long = FilterType2 ? _macdHist > 0 : true
Filter2Short = FilterType2 ? _macdHist < 0 : true
// —————— Filter 3: Bar direction
Filter3Long = FilterType3 ? close > open : true
Filter3Short = FilterType3 ? close < open : true
// —————— Filter 4: Rising volume
Filter4Long = FilterType4 ? ta.rising(volume, 1) : true
Filter4Short = Filter4Long
// —————— Filter 5: Rising/Falling MA
Filter5Ma = FilterType5 ? nz(MA('SMA', close, FilterType5Len)) : 0
Filter5Long = FilterType5 ? ta.rising(Filter5Ma, FilterType5Bars) : true
Filter5Short = FilterType5 ? ta.falling(Filter5Ma, FilterType5Bars) : true
// —————— Filter 6: Check high & close filter
Filter6Long = FilterType6 ? high >= ta.highest(high, FilterType6Bars) : true
Filter6Short = FilterType6 ? low <= ta.lowest(low, FilterType6Bars) : true
// —————— Filter 7: Maximum delta with previous close allowed at entry
Filter7Long = FilterType7 ? close < close[1] * (1 + FilterType7IncPct / 100) : true
Filter7Short = FilterType7 ? close > close[1] * (1 - FilterType7IncPct / 100) : true
// —————— Filter 8: Minimum delta with previous close allowed at entry
Filter8Long = FilterType8 ? close > close[1] * (1 + FilterType8IncPct / 100) : true
Filter8Short = FilterType8 ? close < close[1] * (1 - FilterType8IncPct / 100) : true
// —————— Filter 9: RSI OS/OB
Rsi = FilterType9 ? ta.rsi(close, FilterType9Len) : 0
RsiOB = Rsi > FilterType9OB
RsiOS = Rsi < FilterType9OS
Filter9Long = FilterType9 ? not RsiOB : true
Filter9Short = FilterType9 ? not RsiOS : true
// —————— Filter 10: Moving Average
Filter10Ma = FilterType10 ? MA(FilterType10MAType, close, FilterType10Len) : 0.
Filter10Long = FilterType10 ? close > Filter10Ma : true
Filter10Short = FilterType10 ? close < Filter10Ma : true
// —————— Assemble Filters
FilterLongOK = Filter1Long and Filter2Long and Filter3Long and Filter4Long and Filter5Long
and Filter6Long and Filter7Long and Filter8Long and Filter9Long and Filter10Long
FilterShortOK = Filter1Short and Filter2Short and Filter3Short and Filter4Short and Filter5Short
and Filter6Short and Filter7Short and Filter8Short and Filter9Short and Filter10Short
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// ————————————————————————————————————————————————— Risk Managment ——————————————————————————————————————————————————————— \\ // Credit @Daveatt
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
grouprisk = '➤ RISK MANAGEMENT'
setmaxLosingStreak = input.bool (false, '', group=grouprisk, inline='1')
maxLosingStreak = input.int (15, 'Loss Streak ', group=grouprisk, inline='1', minval=1)
setmaxWinStreak = input.bool (false, '', group=grouprisk, inline='1')
maxWinStreak = input.int (15, 'Win Streak ', group=grouprisk, inline='1', minval=1, tooltip='𝐋𝐨𝐬𝐬 𝐒𝐭𝐫𝐞𝐚𝐤 ➖ Set Max number of consecutive loss trades. \n𝐖𝐢𝐧 𝐒𝐭𝐫𝐞𝐚𝐤 ➖ Max Winning Streak Length.')
setmaxLosingDaysStreak = input.bool (false, '', group=grouprisk, inline='2')
maxLosingDaysStreak = input.int (3, 'Row Loss InDay', group=grouprisk, inline='2', minval=1)
setMaxDrawdown = input.bool (false, '', group=grouprisk, inline='2')
maxPercDd = input.int (10, 'DrawDown %', group=grouprisk, inline='2', minval=1, maxval=100, tooltip='𝐑𝐨𝐰 𝐋𝐨𝐬𝐬 𝐈𝐧𝐃𝐚𝐲 ➖ Max of consecutive days with a loss in a row. \n𝐃𝐫𝐚𝐰𝐃𝐨𝐰𝐧% ➖ Max DrawDown (in % of strategy equity).')
setMaxIntradayLoss = input.bool (false, '', group=grouprisk, inline='3')
maxIntradayLoss = input.int (3, 'InDay Loss % ', group=grouprisk, inline='3', minval=1, maxval=100)
setNumberDailyTrades = input.bool (false, '', group=grouprisk, inline='3')
maxDailyTrades = input.int (10, 'Daily Trades ', group=grouprisk, inline='3', minval=1, maxval=100, tooltip='𝐈𝐧𝐃𝐚𝐲 𝐋𝐨𝐬𝐬 % ➖ Set Max Intraday Loss. \n𝐃𝐚𝐢𝐥𝐲 𝐓𝐫𝐚𝐝𝐞𝐬 ➖ Limit the number of MAX trades per day.')
setNumberWeeklyTrades = input.bool (false, '', group=grouprisk, inline='4')
maxWeeklyTrades = input.int (50, 'Weekly Trades', group=grouprisk, inline='4', minval=1, maxval=100, tooltip='𝐖𝐞𝐞𝐤𝐥𝐲 𝐓𝐫𝐚𝐝𝐞𝐬 ➖ Limit the number of MAX trades per week.')
QTYMethod = input.string ('EQUITY', ' Order Size', group=grouprisk, inline=' ', options=['NONE', 'EQUITY', 'SIZE', 'CONTRACTS'])
useNetProfit = input.bool (true, 'Use Net Profit', group=grouprisk, inline=' ', tooltip='𝐔𝐬𝐞 𝐍𝐞𝐭 𝐏𝐫𝐨𝐟𝐢𝐭 ➖ On/Off the use of profit in the following trades. *Only works if the type is EQUITY')
riskPerc = input.int (10, '🇪🇶🇺🇮🇹🇾', group=grouprisk, inline='.', minval=1, maxval=100)
riskSize = input.int (1000, '🇸🇮🇿🇪', group=grouprisk, inline='.', minval=1)
riskCntr = input.int (1, '🇨🇴🇳🇹🇷🇦🇨🇹🇸', group=grouprisk, inline='.', minval=1, tooltip='𝐎𝐫𝐝𝐞𝐫 𝐒𝐢𝐳𝐞: \n𝐍𝐎𝐍𝐄 ➖ Use the default position size settings in Tab "Properties". \n𝐄𝐐𝐔𝐈𝐓𝐘 ➖ % per trade from the initial capital. \n𝐒𝐈𝐙𝐄 ➖ Fixed size amount of trade. \n𝐂𝐎𝐍𝐓𝐑𝐀𝐂𝐓𝐒 ➖ The fixed amount of the deal in contracts. \n')
// —————— Order Size
eqty = switch QTYMethod
'NONE' => na
'EQUITY' => riskPerc / close
'SIZE' => riskSize / close
'CONTRACTS' => riskCntr
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// —————— Intraday Loss %
condintradayloss = setMaxIntradayLoss ? maxIntradayLoss : 100
strategy.risk.max_intraday_loss(value=condintradayloss, type=strategy.percent_of_equity)
// —————— Max Drawdown %
condmaxdrawdown = setMaxDrawdown ? maxPercDd : 100
strategy.risk.max_drawdown(value=condmaxdrawdown, type=strategy.percent_of_equity)
// —————— Daily trades calculation
tradesIntradayCount = setNumberDailyTrades ? maxDailyTrades : 1000
strategy.risk.max_intraday_filled_orders(count=tradesIntradayCount)
// —————— Weekly trades calculation
tradesLastWeek = 0
tradesLastWeek := dayofweek == dayofweek.monday and dayofweek != dayofweek[1] ? strategy.closedtrades[1] + strategy.opentrades[1] : tradesLastWeek[1]
// —————— Calculate number of trades this week
weeklyTrades = strategy.closedtrades + strategy.opentrades - tradesLastWeek
okToTradeWeekly = setNumberWeeklyTrades ? weeklyTrades < maxWeeklyTrades : true
// —————— Consecutive loss days in a row
countConsLossDays = setmaxLosingDaysStreak ? maxLosingDaysStreak : 1000
strategy.risk.max_cons_loss_days(countConsLossDays)
// —————— Calculate the total losing streaks
newLoss = strategy.losstrades > strategy.losstrades[1] and strategy.wintrades == strategy.wintrades[1] and strategy.eventrades == strategy.eventrades[1]
// —————— Determine current losing streak length
streakLossLen = 0
streakLossLen := newLoss ? nz(streakLossLen[1]) + 1 : strategy.wintrades > strategy.wintrades[1] or strategy.eventrades > strategy.eventrades[1] ? 0 : nz(streakLossLen[1])
// —————— Check if losing streak is under max allowed
okToTradeLossStreak = setmaxLosingStreak ? streakLossLen < maxLosingStreak : true
// —————— Calculate the total winning streaks
newWin = strategy.wintrades > strategy.wintrades[1] and strategy.losstrades == strategy.losstrades[1] and strategy.eventrades == strategy.eventrades[1]
// —————— Figure out current winning streak length
streakWinLen = 0
streakWinLen := newWin ? nz(streakWinLen[1]) + 1 : strategy.losstrades > strategy.losstrades[1] or strategy.eventrades > strategy.eventrades[1] ? 0 : nz(streakWinLen[1])
// —————— Check if winning streak is under max allowed
okToTradeWinStreak = setmaxWinStreak ? streakWinLen < maxWinStreak : true
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// ————————————————————————————————————————————————— STRATEGY ————————————————————————————————————————————————————————————— \\
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
percentTP = input.float (4.5, ' Profit % ', group=groupset, inline='fix', minval=0.1, step=0.1)
percentSL = input.float (1.5, ' Stop % ', group=groupset, inline='fix', minval=0.1, step=0.1)
moneyTP = input.float (333, ' Profit $ ', group=groupset, inline='mon', minval=1, step=5)
moneySL = input.float (100, ' Stop $ ', group=groupset, inline='mon', minval=1, step=5)
ATRLen = input.int (40, ' ATR Period ', group=groupset, inline='atr', minval=1, step=1)
ATRMult = input.float (4, ' Multiplier', group=groupset, inline='atr', minval=0.1, step=0.1)
trailPerc = input.float (3, ' Trailing % ', group=groupset, inline='trl', minval=0.1, step=0.1)
riskReward = input.float (2, ' R:R 1:', group=groupset, inline='trl', minval=0.5, step=0.1)
fixedRR = input.bool (true, ' Fixed RR', group=groupset, inline='trl', tooltip='𝐅𝐢𝐱𝐞𝐝 𝗥:𝗥 ➖ If the stop loss is Dynamic (Trailing or MA) then R:R can also be made Dynamic')
HHLL = input.int (200, ' HH / LL ', group=groupset, inline='hls', minval=1, step=5)
LoHi = input.int (5, ' LO / HI ', group=groupset, inline='hls', minval=1, step=1)
addPerc = input.bool (false, ' Add %', group=groupset, inline='hls', tooltip='𝐀𝐝𝐝 % ➖ If true, then with the "𝗦𝘁𝗼𝗽 %" parameter you can add percentages to any of the current SL. \nCan be especially useful when using Stop - 𝗔𝗧𝗥 or 𝗠𝗔 or 𝗟𝗢/𝗛𝗜. \nFor example with 𝗟𝗢/𝗛𝗜 to put a stop for the last High/Low and add 0.5% additional Stoploss')
maStopType = input.string ('SMA', ' MA Stop', group=groupset, inline='mas', options=['ALMA', 'EMA', 'HMA', 'RMA', 'SMA', 'VWMA', 'WMA'])
maStopLen = input.int (15, ' Len', group=groupset, inline='mas', minval=1, step=2)
maPerc = input.float (1.5, ' Step', group=groupset, inline='mas', minval=0, step=0.1, tooltip='𝐌𝐀 𝐒𝐭𝐨𝐩 ➖ allows to choose which MA to use. \n𝐋𝐞𝐧 ➖ Length of chosen MA. \n𝐒𝐭𝐞𝐩 ➖% step of change MA. Step = 0 will use the selected MA without changes. \nWARNING: When 𝐀𝐝𝐝 % is on, the MA calculation changes..')
bullcolor = input.color (#00bbd49a, 'Colors: FG', group=grouptakes, inline='col')
bearcolor = input.color (#c2185c9a, '', group=grouptakes, inline='col')
bullcolorb = input.color (#00bbd426, 'BG', group=grouptakes, inline='col')
bearcolorb = input.color (#c2185c27, '', group=grouptakes, inline='col')
TipLvl = input.string ('——⤴—⤴—⤴', '𝐓𝐈𝐏 𝐅𝐨𝐫 𝐓𝐏', group=grouptakes, inline='col', tooltip='Here you can set up intermediate Takes Profit.\nIn each line next to the TP/SL activation, you specify what % of the current position you want to close. \nFor example, if there is a checkmark next to TP 3 and its value = 50, then at this level 50% of the size of the current position will be closed.')
slNumber = input.int (3, ' SL 0 Position', group=grouptakes, inline='pos', minval=1, maxval=5)
ontpBE = input.bool (false, '', group=grouptakes, inline='pos')
tpBEnumber = input.int (3, 'Breakeven on TP', group=grouptakes, inline='pos', minval=1, maxval=5, tooltip='𝐒𝐋 𝟎 𝐏𝐨𝐬𝐢𝐭𝐢𝐨𝐧 ➖ Changes the position of the intermediate Stop Loss.\nThe value = 3 - the middle. \n𝐁𝐫𝐞𝐚𝐤𝐞𝐯𝐞𝐧 𝐨𝐧 𝐓𝐏 ➖ If true Set StopLoss to Breakeven after the specified TakeProfit is reached.')
barCoolDwn = input.int (0, ' CoolDown # Bars', group=grouptakes, inline=' ', minval=0, tooltip='Do Not open a new position until # bars have passed since the last trade.\nValue=0 - disables the function.')
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// —————— Trade variables
entry = strategy.position_avg_price
sizePos = strategy.position_size
inLong = sizePos > 0
inShort = sizePos < 0
inTrade = inLong or inShort
inPos = (inLong and not inShort[1]) or (inShort and not inLong[1])
var ID = 'TradeID'
var tpPrice = float(na)
var slPrice = float(na)
// —————— Signal
bull := ext_source_ == close ? ta.crossover (ta.ema(close, 50), ta.ema(close, 200)) : bull
bear := ext_source_ == close ? ta.crossunder(ta.ema(close, 50), ta.ema(close, 200)) : bear
bull := reversesignal ? bear : bull
bear := reversesignal ? bull : bear
// —————— Entry solutions
direction = 0
directionL = bull and (nz(direction[1]) == 0 or nz(direction[1]) == -1)
directionS = bear and (nz(direction[1]) == 0 or nz(direction[1]) == +1)
direction := directionL ? +1 : directionS ? -1 : (reEntryDeal ? direction[1] : direction[0])
checkCoolDwn = barCoolDwn >0 ? not inTrade[barCoolDwn] : true
OkToTrade = timeFilterApproval and okToTradeLossStreak and okToTradeWinStreak and checkCoolDwn
goLong = ( waitEndDeal ? not inTrade and direction==+1 : direction==+1 ) and bullDeal and OkToTrade and FilterLongOK
goShort = ( waitEndDeal ? not inTrade and direction==-1 : direction==-1 ) and bearDeal and OkToTrade and FilterShortOK
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
DynamicStops = (slType == 'TRAILING' or slType == 'FAST TRAIL' or slType == 'ATR TRAIL' or slType == 'MA')
// —————— Fixed SL/TP
if tpType == 'FIXED %'
tpPrice := inLong and not inTrade[1] ? entry * (1 + percentTP/100) : inShort and not inTrade[1] ? entry * (1 - percentTP/100) : tpPrice
if slType == 'FIXED %'
slPrice := inLong and not inTrade[1] ? entry * (1 - percentSL/100) : inShort and not inTrade[1] ? entry * (1 + percentSL/100) : slPrice
// —————— Money SL/TP
if tpType == 'FIXED $'
tpPrice := inLong and not inTrade[1] ? entry + (moneyTP / math.abs(sizePos)) : inShort and not inTrade[1] ? entry - (moneyTP / math.abs(sizePos)) : tpPrice
if slType == 'FIXED $'
slPrice := inLong and not inTrade[1] ? entry - (moneySL / math.abs(sizePos)) : inShort and not inTrade[1] ? entry + (moneySL / math.abs(sizePos)) : slPrice
// —————— Trailing Stop
if slType == 'TRAILING'
StopL = 0., StopS = 0.
StopL := inLong ? math.max(close * (1 - trailPerc/100), StopL[1]) : 0
StopS := inShort ? math.min(close * (1 + trailPerc/100), StopS[1]) : 999999
slPrice := inLong ? StopL : inShort ? StopS : slPrice
// —————— Fast Trailing Stop
if slType == 'FAST TRAIL'
var stop = float(na)
stop := inLong and not inTrade[1] ? entry * (1 - trailPerc/100) : inShort and not inTrade[1] ? entry * (1 + trailPerc/100) : stop
stop := inLong and close > close[1] ? stop + (close - close[1]) : inShort and close < close[1] ? stop + (close - close[1]) : stop
slPrice := stop
// —————— ATR Fixed
StopATR = ta.atr(ATRLen) * ATRMult
if tpType == 'ATR'
tpPrice := inLong and not inTrade[1] ? entry + StopATR : inShort and not inTrade[1] ? entry - StopATR : tpPrice
if slType == 'ATR'
slPrice := inLong and not inTrade[1] ? entry - StopATR : inShort and not inTrade[1] ? entry + StopATR : slPrice
// —————— ATR Trail Stop
if slType == 'ATR TRAIL'
StopL = 0., StopS = 0.
StopL := inLong ? math.max(close - StopATR, StopL[1]) : 0
StopS := inShort ? math.min(close + StopATR, StopS[1]) : 999999
slPrice := inLong ? StopL : inShort ? StopS : slPrice
// —————— LO / HI Stop
if slType == 'LO / HI'
HiStop = ta.highest(high, LoHi)
LoStop = ta.lowest (low, LoHi)
slPrice := inLong and not inTrade[1] ? LoStop : inShort and not inTrade[1] ? HiStop : slPrice
// —————— HH / LL Take
if tpType == 'HH / LL'
HiStop = ta.highest(high, HHLL)
LoStop = ta.lowest (low, HHLL)
tpPrice := inLong and not inTrade[1] ? HiStop : inShort and not inTrade[1] ? LoStop : tpPrice
// —————— MA's Stops
if slType == 'MA'
maSType = MA(maStopType, close, maStopLen)
Stop = maSType * (maPerc / 100)
slPrice := inLong ? math.max(nz(slPrice[1], 0), maSType - Stop) : inShort ? math.min(nz(slPrice[1], 999999), maSType + Stop) : na
// —————— Risk Reward Take
if tpType == 'R:R'
check = fixedRR ? not inTrade[1] : true
tpPrice := inLong and check ? entry + (entry - slPrice) * riskReward : inShort and check ? entry - (slPrice - entry) * riskReward : tpPrice
// —————— Add Percents to SL
if addPerc
check = DynamicStops ? true : not inTrade[1]
slPrice := inLong and check ? slPrice * (1 - percentSL/100) : inShort and check ? slPrice * (1 + percentSL/100) : slPrice
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// —————— TPSL's
TypeStop = tpType != 'None' or slType == 'FIXED %' or slType == 'FIXED $' or slType == 'ATR' or slType == 'LO / HI'
BuildLeveles(take, nTake) => // Creating Intermediate Levels of the SL/TP - by shifting the current TP/SL to the entry level
nTakes = 6
var offset = float(na)
offset := not inPos[1] ? (entry - take) : offset
xentry = take + offset
levels = array.new_float()
for i = 1 to nTakes - 1
array.push(levels, xentry + (take - xentry) * i / (nTakes))
result = nTake >= 0 and nTake <= nTakes ? array.get(levels, nTake) : na
CheckLevels(lvl, isTake) => // Check if the SL/TP is reached
var float level = na
if isTake and ((inLong and high >= lvl) or (inShort and low <= lvl))
level := entry
if not isTake and ((inLong and low <= lvl) or (inShort and high >= lvl))
level := entry
if not inTrade
level := na
level
// —————— Get and Check Levels
stop0 = tpType != 'None' ? BuildLeveles(slPrice, slNumber-1) : slPrice , checkStop0 = TypeStop ? CheckLevels(stop0, false) : CheckLevels(stop0, true )
take1 = BuildLeveles(TypeStop ? tpPrice : slPrice, 0) , checkTake1 = TypeStop ? CheckLevels(take1, true ) : CheckLevels(take1, false)
take2 = BuildLeveles(TypeStop ? tpPrice : slPrice, 1) , checkTake2 = TypeStop ? CheckLevels(take2, true ) : CheckLevels(take2, false)
take3 = BuildLeveles(TypeStop ? tpPrice : slPrice, 2) , checkTake3 = TypeStop ? CheckLevels(take3, true ) : CheckLevels(take3, false)
take4 = BuildLeveles(TypeStop ? tpPrice : slPrice, 3) , checkTake4 = TypeStop ? CheckLevels(take4, true ) : CheckLevels(take4, false)
take5 = BuildLeveles(TypeStop ? tpPrice : slPrice, 4) , checkTake5 = TypeStop ? CheckLevels(take5, true ) : CheckLevels(take5, false)
// —————— Move Stop to Breakeven
slbecheck = (tpBEnumber==1 and ontake1) or (tpBEnumber==2 and ontake2) or (tpBEnumber==3 and ontake3) or (tpBEnumber==4 and ontake4) or (tpBEnumber==5 and ontake5)
if ontpBE and slbecheck and not DynamicStops
slbe = TypeStop ? CheckLevels(BuildLeveles(tpPrice, tpBEnumber-1), true) : CheckLevels(BuildLeveles(slPrice, tpBEnumber-1), false)
slPrice := slbe > 0 ? slbe : slPrice
stop0 := slPrice == entry ? slPrice : stop0
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
// —————— Entry's
eqty(qty) => QTYMethod=='EQUITY' ? qty / 100 * (strategy.initial_capital + (useNetProfit ? strategy.netprofit : 0)) : QTYMethod=='SIZE' ? qty / syminfo.pointvalue : qty
if goLong
ID := 'Long'
if reOpenDeal and inLong and goLong
strategy.close(ID, comment='reOpen')
strategy.entry(ID, strategy.long, qty=eqty(eqty), comment=ID, alert_message=ID + 'Entry')
if goShort
ID := 'Short'
if reOpenDeal and inShort and goShort
strategy.close(ID, comment='reOpen')
strategy.entry(ID, strategy.short, qty=eqty(eqty), comment=ID, alert_message=ID + 'Entry')
// —————— Exit's
qty(perc) => math.abs(sizePos*perc/100)
if inTrade
strategy.exit('End_0', onstop0 ? ID : 'na', qty=qty(qstop0), limit=tpPrice, stop=stop0, comment_profit='TP 0', comment_loss='SL 0', alert_message=ID + 'SL/TP 0 Trigger')
strategy.exit('End_1', ontake1 ? ID : 'na', qty=qty(qtake1), limit=TypeStop ? take1 : tpPrice, stop=TypeStop ? slPrice : take1, comment_profit='TP 1', comment_loss='SL 1', alert_message=ID + 'SL/TP 1 Trigger')
strategy.exit('End_2', ontake2 ? ID : 'na', qty=qty(qtake2), limit=TypeStop ? take2 : tpPrice, stop=TypeStop ? slPrice : take2, comment_profit='TP 2', comment_loss='SL 2', alert_message=ID + 'SL/TP 2 Trigger')
strategy.exit('End_3', ontake3 ? ID : 'na', qty=qty(qtake3), limit=TypeStop ? take3 : tpPrice, stop=TypeStop ? slPrice : take3, comment_profit='TP 3', comment_loss='SL 3', alert_message=ID + 'SL/TP 3 Trigger')
strategy.exit('End_4', ontake4 ? ID : 'na', qty=qty(qtake4), limit=TypeStop ? take4 : tpPrice, stop=TypeStop ? slPrice : take4, comment_profit='TP 4', comment_loss='SL 4', alert_message=ID + 'SL/TP 4 Trigger')
strategy.exit('End_5', ontake5 ? ID : 'na', qty=qty(qtake5), limit=TypeStop ? take5 : tpPrice, stop=TypeStop ? slPrice : take5, comment_profit='TP 5', comment_loss='SL 5', alert_message=ID + 'SL/TP 5 Trigger')
strategy.exit('End_6', ID, limit=tpPrice, stop=slPrice, comment_profit='TP X', comment_loss='SL X', alert_message=ID + 'SL/TP X Trigger')
// ========================================================================================================================
// Plotting and Debugging
// ========================================================================================================================
plotColx = inLong ? bullcolor : inShort ? bearcolor : na
plotColy = inLong ? bullcolorb : inShort ? bearcolorb : na
tpcolor = tpType != 'None' ? bullcolor : plotColx
tp = plot(inPos ? tpPrice : na, 'Take', color=tpType != 'None' ? bullcolor : plotColx, style=plot.style_linebr, editable=false)
sl = plot(inPos ? slPrice : na, 'Stop', color=tpType != 'None' ? bearcolor : plotColx, style=plot.style_linebr, editable=false)
en = plot(TypeStop ? (inLong and slPrice > entry or inShort and slPrice < entry ? slPrice : entry) : inPos ? close : na, 'Price', color=TypeStop ? #787b86 : na, style=plot.style_linebr)
fill(tp, en, color=TypeStop ? bullcolorb : plotColy, editable=false)
fill(sl, en, color=TypeStop ? bearcolorb : plotColy, editable=false)
plotchar(inPos and not inPos[1]? tpPrice : na, 'Take Start', color=tpType != 'None' ? bullcolor : plotColx, char='➤', location=location.absolute, size=size.tiny)
plotchar(inPos and not inPos[1]? slPrice : na, 'Stop Start', color=tpType != 'None' ? bearcolor : plotColx, char='◉', location=location.absolute, size=size.tiny)
plot(inPos and ontake1 and not checkTake1[1] ? take1 : na, 'TP 1', color=tpcolor, style=plot.style_linebr, editable=false)
plot(inPos and ontake2 and not checkTake2[1] ? take2 : na, 'TP 2', color=tpcolor, style=plot.style_linebr, editable=false)
plot(inPos and ontake3 and not checkTake3[1] ? take3 : na, 'TP 3', color=tpcolor, style=plot.style_linebr, editable=false)
plot(inPos and ontake4 and not checkTake4[1] ? take4 : na, 'TP 4', color=tpcolor, style=plot.style_linebr, editable=false)
plot(inPos and ontake5 and not checkTake5[1] ? take5 : na, 'TP 5', color=tpcolor, style=plot.style_linebr, editable=false)
plot(inPos and onstop0 and not checkStop0[1] ? stop0 : na, 'SL 0', color=bearcolor, style=plot.style_linebr, editable=false)
plot(ext_source_ == close ? ta.ema(close, 50) : na, 'MA 1', color=#ffeb3b93, editable=false)
plot(ext_source_ == close ? ta.ema(close, 200) : na, 'MA 2', color=#ffeb3b93, editable=false)
//plotchar(goLong and not inPos, 'goLong' , char='✟', size=size.tiny, location=location.belowbar, color=#4caf4f )
//plotchar(goShort and not inPos, 'goShort', char='✟', size=size.tiny, location=location.abovebar, color=#ff5252 )
// Panel
//var table panel = table.new(position = position.bottom_right, columns = 2, rows = 1, bgcolor = #363A45, border_width = 1)
//table.cell(panel, 1, 0, text=str.tostring(0), text_color = #ffa726, bgcolor = #363A45)
// ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— \\
Expand (513 lines)