-
Notifications
You must be signed in to change notification settings - Fork 1
/
Profit Plus Pro Strategy.txt
1588 lines (1287 loc) · 68.7 KB
/
Profit Plus Pro Strategy.txt
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=5
strategy('Profit Pulse Pro', overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, currency='USD', commission_type=strategy.commission.percent, commission_value=0.03, max_bars_back=3000, max_lines_count=100, calc_on_every_tick=false, max_labels_count=100, process_orders_on_close =true)
var initialcapital = strategy.equity
//Truncate Function
truncate(number, decimals) =>
factor = math.pow(10, decimals)
int(number * factor) / factor
//=======================================================================================
// Trade Time
daysback = input(14, title='Backtest Days, For Defining Best TF, Max: 20 Days')
millisecondinxdays = 1000 * 60 * 60 * 24 * daysback
leftbar = timenow - time < millisecondinxdays
bool backtest = leftbar
//=======================================================================================
et1 = 'Supertrend'
bool longside = input.bool(true, title='Long Side', group='Strategy Option')
bool shortside = input.bool(true, title='Short Side', group='Strategy Option')
filter1 = 'Filter with Atr'
filter2 = 'Filter with RSI'
filter3 = 'Atr or RSI'
filter4 = 'Atr and RSI'
filter5 = 'No Filtering'
// filter6 = 'Entry Only in sideways market(By ATR or RSI)'
// filter7 = 'Entry Only in sideways market(By ATR and RSI)'
typefilter = input.string(filter5, title='Sideways Filtering Input', options=[filter1, filter2, filter3, filter4, filter5], group='Strategy Options')
withsl = input.bool(false, title='Use Dynamic SL?', group='Strategy Options')
RSI = truncate(ta.rsi(close, input.int(14, group='RSI Filterring')), 2)
toplimitrsi = input.float(55.5, title='TOP Limit', group='RSI Filterring')
botlimitrsi = input.float(45.5, title='BOT Limit', group='RSI Filterring')
// TP SL Option
ex1 = 'TP & SL by Percentage Price'
ex2 = 'TP & SL by atr Value'
et = input.string(ex2, title='TP SL Type', options=[ex1, ex2], group='Strategy Options')
ST = input.bool(true, title='Show Supertrend?', group='Supertrend Indicator')
period = input.int(15, group='Supertrend Indicator')
mult = input.int(5, group='Supertrend Indicator')
atrLen = input.int(13, minval=1, title='atr Length', group='Sideways Filtering Input')
atrMaType = input.string('EMA', options=['SMA', 'EMA'], group='Sideways Filtering Input', title='atr Moving Average Type')
atrMaLen = input.int(13, minval=1, title='atr MA Length', group='Sideways Filtering Input')
//adxLen = input(5, minval = 1, maxval = 50, title = "ADX SMOOTHing", group='Sideways Filtering Input')
//diLen = input(14, minval = 1, title = "DI Length", group='Sideways Filtering Input')
//adxLim = input(22, minval = 1, title = "ADX Limit", group='Sideways Filtering Input')
//SMOOTH = input(3, minval = 1, maxval = 5, title = "SMOOTHing Factor", group='Sideways Filtering Input')
//lag = input(8, minval = 0, maxval = 15, title = "Lag", group='Sideways Filtering Input')
round_num = close < 1 ? 5 : close < 99 ? 3 : 2
TP1 = input.float(1.5, title='TP1 Atr Multiplier', group='TP/SL by ATR')
TP2 = input.int(3, title='TP2 Atr Multiplier', group='TP/SL by ATR')
TP3 = input.int(5, title='TP3 Atr Multiplier', group='TP/SL by ATR')
TP4 = input.float(title='TP 4', minval=0.0, step=0.1, defval=9, group='TP/SL by ATR')
SL = input.int(4, title='SL Atr Multiplier', group='TP/SL by ATR')
ptp1 = input.float(2, title='Take Profit 1 (%)', minval=0.0, step=0.1, group='TP/SL Percentage Price') * 0.01
ptp2 = input.float(4, title='Take Profit 2 (%)', minval=0.0, step=0.1, group='TP/SL Percentage Price') * 0.01
ptp3 = input.float(6, title='Take Profit 3 (%)', minval=0.0, step=0.1, group='TP/SL Percentage Price') * 0.01
ptp4 = input.float(12, title='Take Profit 4 (%)', minval=0.0, step=0.1, group='TP/SL Percentage Price') * 0.01
psl = input.float(3, title='StopLoss (%)', minval=0.0, step=0.1) * 0.01
qtytp1 = input.int(30, title='QTY TP 1', group='Qty for TP')
qtytp2 = input.int(30, title='QTY TP 2', group='Qty for TP')
qtytp3 = input.int(30, title='QTY TP 3', group='Qty for TP')
qtytp4 = input.int(10, title='QTY TP 4', group='Qty for TP')
Qtytp1 = qtytp1 / 100
Qtytp2 = qtytp2 / 100
Qtytp3 = qtytp3 / 100
Qtytp4 = qtytp4 / 100
///
//===================================================================
//===================================================================
//Timeframe
//Supertrend Indicator
src = hl2
atr2 = ta.sma(ta.tr, period)
atr = request.security(syminfo.tickerid, '', ta.atr(period))
up = src - mult * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + mult * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
st = trend == 1 ? up : dn
//filtering
atra = request.security(syminfo.tickerid, '', ta.atr(atrLen))
atrMa = atrMaType == 'EM' ? ta.ema(atra, atrMaLen) : ta.sma(atra, atrMaLen)
updm = ta.change(high)
downdm = -ta.change(low)
plusdm = na(updm) ? na : updm > downdm and updm > 0 ? updm : 0
minusdm = na(downdm) ? na : downdm > updm and downdm > 0 ? downdm : 0
//trur = rma(tr, diLen)
//plus = fixnan(100 * rma(plusdm, diLen) / trur)
//minus = fixnan(100 * rma(minusdm, diLen) / trur)
//sum = plus + minus
//adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxLen)
cndSidwayss1 = atra >= atrMa
cndSidwayss2 = RSI > toplimitrsi or RSI < botlimitrsi
cndSidways = cndSidwayss1 or cndSidwayss2
cndSidways1 = cndSidwayss1 and cndSidwayss2
Sidwayss1 = atra <= atrMa
Sidwayss2 = RSI < toplimitrsi and RSI > botlimitrsi
Sidways = Sidwayss1 or Sidwayss2
Sidways1 = Sidwayss1 and Sidwayss2
bool trendType = typefilter == filter1 ? cndSidwayss1 : typefilter == filter2 ? cndSidwayss2 : typefilter == filter3 ? cndSidways : typefilter == filter4 ? cndSidways1 : typefilter == filter5 ? RSI > 0 : na
// Entry FIX
buysignal1 = ta.crossover(close, st)
sellsignal1 = ta.crossunder(close, st)
bool buy = buysignal1 and longside
bool sell = sellsignal1 and shortside
//Tp & Sl by atr Mult
cb = ta.valuewhen(buy, close, 0)
ratr = ta.valuewhen(buy, ta.atr(14), 0)
tpb1 = cb + ratr * TP1
tpb2 = cb + ratr * TP2
tpb3 = cb + ratr * TP3
tpb4 = cb + ratr * TP4
slb = cb - ratr * SL //withsl?up:valuewhen(buy, up,0)
cs = ta.valuewhen(sell, close, 0)
ratrs = ta.valuewhen(sell, ta.atr(14), 0)
tps1 = cs - ratrs * TP1
tps2 = cs - ratrs * TP2
tps3 = cs - ratrs * TP3
tps4 = cs - ratrs * TP4
sls = cs + ratrs * SL //withsl?dn:valuewhen(sell, dn, 0)
Ptpb1 = cb * (1 + ptp1)
Ptpb2 = cb * (1 + ptp2)
Ptpb3 = cb * (1 + ptp3)
Ptpb4 = et == ex1 ? cb * (1 + ptp4) : tpb4
Pslb = cb * (1 - psl)
Ptps1 = cs * (1 - ptp1)
Ptps2 = cs * (1 - ptp2)
Ptps3 = cs * (1 - ptp3)
Ptps4 = et == ex1 ? cs * (1 - ptp4) : tps4
Psls = cs * (1 + psl)
//Variable Fix TP SL
float tpb1t = et == ex1 ? Ptpb1 : tpb1
float tpb2t = et == ex1 ? Ptpb2 : tpb2
float tpb3t = et == ex1 ? Ptpb3 : tpb3
float tpb4t = Ptpb4
float slbt = et == ex1 ? Pslb : slb
float tps1t = et == ex1 ? Ptps1 : tps1
float tps2t = et == ex1 ? Ptps2 : tps2
float tps3t = et == ex1 ? Ptps3 : tps3
float tps4t = Ptps4
float slst = et == ex1 ? Psls : sls
//===================================================================
slbm = et == ex1 ? Psls : slb
slsm = et == ex1 ? Psls : sls
slbmessage = '","stopLossPriority":"price","stopLossPrice":"' + str.tostring(slbm)
slsmessage = '","stopLossPriority":"price","stopLossPrice":"' + str.tostring(slsm)
//signal provider
tpb1tsp = et == ex1 ? ptp1 * 100 : truncate((tpb1 - cb) * 100 / cb, 2)
tpb2tsp = et == ex1 ? ptp2 * 100 : truncate((tpb2 - cb) * 100 / cb, 2)
tpb3tsp = et == ex1 ? ptp3 * 100 : truncate((tpb3 - cb) * 100 / cb, 2)
tpb4tsp = ptp4 * 100
slbtsp = truncate((cb - slb) * 100 / cb, 2)
tps1tsp = et == ex1 ? ptp1 * 100 : truncate((cs - tps1) * 100 / cs, 2)
tps2tsp = et == ex1 ? ptp2 * 100 : truncate((cs - tps2) * 100 / cs, 2)
tps3tsp = et == ex1 ? ptp3 * 100 : truncate((cs - tps3) * 100 / cs, 2)
tps4tsp = ptp4 * 100
slstsp = truncate((sls - cs) * 100 / cs, 2)
slbmsp = et == ex1 ? psl * 100 : slbtsp
slsmsp = et == ex1 ? psl * 100 : slstsp
slbmessagesp = '","stopLossPercentage":"-' + str.tostring(slbmsp)
slsmessagesp = '","stopLossPercentage":"-' + str.tostring(slsmsp)
//===================================================================
//MTF Analysis
//===================================================================
//===================================================================
//Source MTF
tf1 = input.timeframe('15', group='MTF Analysis')
tf2 = input.timeframe('30', group='MTF Analysis')
tf3 = input.timeframe('45', group='MTF Analysis')
tf4 = input.timeframe('60', group='MTF Analysis')
hl2A = request.security(syminfo.tickerid, tf1, hl2)
hl2B = request.security(syminfo.tickerid, tf2, hl2)
hl2C = request.security(syminfo.tickerid, tf3, hl2)
hl2D = request.security(syminfo.tickerid, tf4, hl2)
trA = request.security(syminfo.tickerid, tf1, ta.tr)
trB = request.security(syminfo.tickerid, tf2, ta.tr)
trC = request.security(syminfo.tickerid, tf3, ta.tr)
trD = request.security(syminfo.tickerid, tf4, ta.tr)
highA = request.security(syminfo.tickerid, tf1, high)
highB = request.security(syminfo.tickerid, tf2, high)
highC = request.security(syminfo.tickerid, tf3, high)
highD = request.security(syminfo.tickerid, tf4, high)
lowA = request.security(syminfo.tickerid, tf1, low)
lowB = request.security(syminfo.tickerid, tf2, low)
lowC = request.security(syminfo.tickerid, tf3, low)
lowD = request.security(syminfo.tickerid, tf4, low)
closeA = request.security(syminfo.tickerid, tf1, close)
closeB = request.security(syminfo.tickerid, tf2, close)
closeC = request.security(syminfo.tickerid, tf3, close)
closeD = request.security(syminfo.tickerid, tf4, close)
//===================================================================
//===================================================================
//Timeframe A
//Supertrend Indicator
srcA = hl2A
atr2A = ta.sma(trA, period)
atrA = request.security(syminfo.tickerid, tf1, ta.atr(period))
upA = srcA - mult * atrA
up1A = nz(upA[1], upA)
upA := closeA[1] > up1A ? math.max(upA, up1A) : upA
dnA = srcA + mult * atrA
dn1A = nz(dnA[1], dnA)
dnA := closeA[1] < dn1A ? math.min(dnA, dn1A) : dnA
trendA = 1
trendA := nz(trendA[1], trendA)
trendA := trendA == -1 and closeA > dn1A ? 1 : trendA == 1 and closeA < up1A ? -1 : trendA
stA = trendA == 1 ? upA : dnA
//filtering
atraA = request.security(syminfo.tickerid, tf1, ta.atr(atrLen))
atrMaA = atrMaType == 'EMA' ? ta.ema(atraA, atrMaLen) : ta.sma(atraA, atrMaLen)
updmA = ta.change(highA)
downdmA = -ta.change(lowA)
plusdmA = na(updmA) ? na : updmA > downdmA and updmA > 0 ? updmA : 0
minusdmA = na(downdmA) ? na : downdmA > updmA and downdmA > 0 ? downdmA : 0
//trurA = rma(trA, diLen)
//plusA = fixnan(100 * rma(plusdmA, diLen) / trurA)
//minusA = fixnan(100 * rma(minusdmA, diLen) / trurA)
//sumA = plusA + minusA
//adxA = 100 * rma(abs(plusA - minusA) / (sumA == 0 ? 1 : sumA), adxLen)
cndSidwayss1A = atraA >= atrMaA
cndSidwayss2A = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysA = cndSidwayss1A or cndSidwayss2A
cndSidways1A = cndSidwayss1A and cndSidwayss2A
Sidwayss1A = atraA <= atrMaA
Sidwayss2A = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysA = Sidwayss1A or Sidwayss2A
Sidways1A = Sidwayss1A and Sidwayss2A
trendTypeA = typefilter == filter1 ? cndSidwayss1A : typefilter == filter2 ? cndSidwayss2A : typefilter == filter3 ? cndSidwaysA : typefilter == filter4 ? cndSidways1A : typefilter == filter5 ? RSI > 0 : na
// Entry FIX
buysignal1A = ta.crossover(closeA, stA) and backtest and trendTypeA
sellsignal1A = ta.crossunder(closeA, stA) and backtest and trendTypeA
buyA = buysignal1A and longside
sellA = sellsignal1A and shortside
//Tp & Sl by atr Mult
cbA = ta.valuewhen(buyA, closeA, 0)
ratrA = ta.valuewhen(buyA, request.security(syminfo.tickerid, tf1, ta.atr(14)), 0)
tpb1A = cbA + ratrA * TP1
tpb2A = cbA + ratrA * TP2
tpb3A = cbA + ratrA * TP3
tpb4A = cbA + ratrA * TP4
slbA = cbA - ratrA * SL //withsl?upA:valuewhen(buyA, upA,0)
csA = ta.valuewhen(sellA, closeA, 0)
ratrsA = ta.valuewhen(sellA, request.security(syminfo.tickerid, tf1, ta.atr(14)), 0)
tps1A = csA - ratrsA * TP1
tps2A = csA - ratrsA * TP2
tps3A = csA - ratrsA * TP3
tps4A = csA - ratrsA * TP4
slsA = csA + ratrsA * SL //withsl?dnA:valuewhen(sellA, dnA, 0)
Ptpb1A = cbA * (1 + ptp1)
Ptpb2A = cbA * (1 + ptp2)
Ptpb3A = cbA * (1 + ptp3)
Ptpb4A = et == ex1 ? cbA * (1 + ptp4) : tpb4A
PslbA = cbA * (1 - psl)
Ptps1A = csA * (1 - ptp1)
Ptps2A = csA * (1 - ptp2)
Ptps3A = csA * (1 - ptp3)
Ptps4A = et == ex1 ? csA * (1 - ptp4) : tps4A
PslsA = csA * (1 + psl)
//Variable Fix TP SL
tpb1tA = et == ex1 ? Ptpb1A : tpb1A
tpb2tA = et == ex1 ? Ptpb2A : tpb2A
tpb3tA = et == ex1 ? Ptpb3A : tpb3A
tpb4tA = Ptpb4A
slbtA = et == ex1 ? PslbA : slbA
tps1tA = et == ex1 ? Ptps1A : tps1A
tps2tA = et == ex1 ? Ptps2A : tps2A
tps3tA = et == ex1 ? Ptps3A : tps3A
tps4tA = Ptps4A
slstA = et == ex1 ? PslsA : slsA
sinceentrybA = int(math.max(1, nz(ta.barssince(buyA))))
openedbA = ta.barssince(buyA) >= 0 and trendA[1] == 1 and longside
highestbarbA = ta.highest(sinceentrybA)
lowestbarbA = ta.lowest(sinceentrybA)
stillopenb1A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb1tA and lowestbarbA[1] > slbtA
stillopenb2A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb2tA and lowestbarbA[1] > slbtA
stillopenb3A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb3tA and lowestbarbA[1] > slbtA
stillopenb4A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb4tA and lowestbarbA[1] > slbtA
hittpb1A = ta.cross(highA, tpb1tA) and stillopenb1A and openedbA
plotshape(hittpb1A)
hittpb2A = ta.cross(highA, tpb2tA) and stillopenb2A and openedbA
hittpb3A = ta.cross(highA, tpb3tA) and stillopenb3A and openedbA
hittpb4A = ta.cross(highA, tpb4tA) and stillopenb4A and openedbA
hitslbA = lowA < slbtA and stillopenb4A and openedbA
winreversebA = closeA > cbA and backtest and ta.crossunder(closeA, stA) and stillopenb4A and trendA == -1 and openedbA
losereversebA = closeA < cbA and backtest and ta.crossunder(closeA, stA) and stillopenb4A and trendA == -1 and openedbA
sinceentrysA = int(math.max(1, nz(ta.barssince(sellA))))
openedsA = ta.barssince(sellA) >= 0 and trendA[1] == -1 and shortside
highestbarsA = ta.highest(sinceentrysA)
lowestbarsA = ta.lowest(sinceentrysA)
stillopens1A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and lowestbarsA[1] > tps1tA
stillopens2A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and lowestbarsA[1] > tps2tA
stillopens3A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and lowestbarsA[1] > tps3tA
stillopens4A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and lowestbarsA[1] > tps4tA
hittps1A = ta.cross(lowA, tps1tA) and stillopens1A and openedsA
hittps2A = ta.cross(lowA, tps2tA) and stillopens2A and openedsA
hittps3A = ta.cross(lowA, tps3tA) and stillopens3A and openedsA
hittps4A = ta.cross(lowA, tps4tA) and stillopens4A and openedsA
hitslsA = highA > slstA and stillopens4A and openedsA
winreversesA = closeA <= csA and backtest and ta.crossover(closeA, stA) and stillopens4A and trendA == 1 and openedsA
losereversesA = closeA >= csA and backtest and ta.crossover(closeA, stA) and stillopens4A and trendA == 1 and openedsA
//============================================
cumbuyA = ta.cum(buyA ? 1 : 0)
cumsellA = ta.cum(sellA ? 1 : 0)
cumsignalA = cumbuyA + cumsellA
//net profit
sisaqtybA = stillopenb1A ? 1 : stillopenb2A ? 1 - Qtytp1 : stillopenb3A ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysA = stillopens1A ? 1 : stillopens2A ? 1 - Qtytp1 : stillopens3A ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
ptpb1A = (tpb1tA - cbA) * 100 / cbA * Qtytp1 //-0.06
ptpb2A = (tpb2tA - cbA) * 100 / cbA * Qtytp2 //-0.06
ptpb3A = (tpb3tA - cbA) * 100 / cbA * Qtytp3 //-0.06
ptpb4A = (tpb4tA - cbA) * 100 / cbA * Qtytp4 //-0.06
pslbA = (closeA - cbA) * 100 / cbA * sisaqtybA //-0.06
prbA = (closeA - cbA) * 100 / cbA * sisaqtybA //-0.06
ptps1A = (csA - tps1tA) * 100 / csA * Qtytp1 //-0.06
ptps2A = (csA - tps2tA) * 100 / csA * Qtytp2 //-0.06
ptps3A = (csA - tps3tA) * 100 / csA * Qtytp3 //-0.06
ptps4A = (csA - tps4tA) * 100 / csA * Qtytp4 //-0.06
pslsA = (csA - closeA) * 100 / csA * sisaqtysA //-0.06
prsA = (csA - closeA) * 100 / csA * sisaqtysA //-0.06
prtpb1A = ta.cum(hittpb1A ? ptpb1A : 0)
prtpb2A = ta.cum(hittpb2A ? ptpb2A : 0)
prtpb3A = ta.cum(hittpb3A ? ptpb3A : 0)
prtpb4A = ta.cum(hittpb4A ? ptpb4A : 0)
prslbA = ta.cum(hitslbA ? pslbA : 0)
prwrbA = ta.cum(winreversebA ? prbA : 0)
prlrbA = ta.cum(losereversebA ? prbA : 0)
prtps1A = ta.cum(hittps1A ? ptps1A : 0)
prtps2A = ta.cum(hittps2A ? ptps2A : 0)
prtps3A = ta.cum(hittps3A ? ptps3A : 0)
prtps4A = ta.cum(hittps4A ? ptps4A : 0)
prslsA = ta.cum(hitslsA ? pslsA : 0)
prwrsA = ta.cum(winreversesA ? prsA : 0)
prlrsA = ta.cum(losereversesA ? prsA : 0)
netcapitalgainA = prtpb1A + prtpb2A + prtpb3A + prtpb4A + prslbA + prtps1A + prtps2A + prtps3A + prtps4A + prslsA + prwrbA + prlrbA + prwrsA + prlrsA
///winrate
wintpb1A = ta.cum(hittpb1A ? 1 : 0)
wintpb2A = ta.cum(hittpb2A ? 1 : 0)
wintpb3A = ta.cum(hittpb3A ? 1 : 0)
wintpb4A = ta.cum(hittpb4A ? 1 : 0)
wrslbA = ta.cum(hitslbA ? 1 : 0)
wintps1A = ta.cum(hittps1A ? 1 : 0)
wintps2A = ta.cum(hittps2A ? 1 : 0)
wintps3A = ta.cum(hittps3A ? 1 : 0)
wintps4A = ta.cum(hittps4A ? 1 : 0)
wrslsA = ta.cum(hitslsA ? 1 : 0)
cumtp1A = wintpb1A + wintps1A
cumtp2A = wintpb2A + wintps2A
cumtp3A = wintpb3A + wintps3A
cumtp4A = wintpb4A + wintps4A
cumslA = wrslbA + wrslsA
cumpositionA = cumtp1A + cumtp2A + cumtp3A + cumtp4A + cumslA //+cumwinreverseA//+cumlosereverseA
wrtp1A = truncate(cumtp1A * 100 / cumsignalA, 0)
wrtp2A = truncate(cumtp2A * 100 / cumsignalA, 0)
wrtp3A = truncate(cumtp3A * 100 / cumsignalA, 0)
wrtp4A = truncate(cumtp4A * 100 / cumsignalA, 0)
wrslA = truncate(cumslA * 100 / cumsignalA, 0)
winallA = cumtp1A + cumtp2A + cumtp3A + cumtp4A //+cumwinreverseA
wrallA = winallA * 100 / cumpositionA
//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe B
//Supertrend Indicator
srcB = hl2B
atr2B = ta.sma(trB, period)
atrB = request.security(syminfo.tickerid, tf2, ta.atr(period))
upB = srcB - mult * atrB
up1B = nz(upB[1], upB)
upB := closeB[1] > up1B ? math.max(upB, up1B) : upB
dnB = srcB + mult * atrB
dn1B = nz(dnB[1], dnB)
dnB := closeB[1] < dn1B ? math.min(dnB, dn1B) : dnB
trendB = 1
trendB := nz(trendB[1], trendB)
trendB := trendB == -1 and closeB > dn1B ? 1 : trendB == 1 and closeB < up1B ? -1 : trendB
stB = trendB == 1 ? upB : dnB
//filtering
atraB = request.security(syminfo.tickerid, tf2, ta.atr(atrLen))
atrMaB = atrMaType == 'EMB' ? ta.ema(atraB, atrMaLen) : ta.sma(atraB, atrMaLen)
updmB = ta.change(highB)
downdmB = -ta.change(lowB)
plusdmB = na(updmB) ? na : updmB > downdmB and updmB > 0 ? updmB : 0
minusdmB = na(downdmB) ? na : downdmB > updmB and downdmB > 0 ? downdmB : 0
//trurB = rma(trB, diLen)
//plusB = fixnan(100 * rma(plusdmB, diLen) / trurB)
//minusB = fixnan(100 * rma(minusdmB, diLen) / trurB)
//sumB = plusB + minusB
//adxB = 100 * rma(abs(plusB - minusB) / (sumB == 0 ? 1 : sumB), adxLen)
cndSidwayss1B = atraB >= atrMaB
cndSidwayss2B = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysB = cndSidwayss1B or cndSidwayss2B
cndSidways1B = cndSidwayss1B and cndSidwayss2B
Sidwayss1B = atraB <= atrMaB
Sidwayss2B = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysB = Sidwayss1B or Sidwayss2B
Sidways1B = Sidwayss1B and Sidwayss2B
trendTypeB = typefilter == filter1 ? cndSidwayss1B : typefilter == filter2 ? cndSidwayss2B : typefilter == filter3 ? cndSidwaysB : typefilter == filter4 ? cndSidways1B : typefilter == filter5 ? RSI > 0 : na
// Entry FIX
buysignal1B = ta.crossover(closeB, stB) and backtest and trendTypeB
sellsignal1B = ta.crossunder(closeB, stB) and backtest and trendTypeB
buyB = buysignal1B and longside
sellB = sellsignal1B and shortside
//Tp & Sl by atr Mult
cbB = ta.valuewhen(buyB, closeB, 0)
ratrB = ta.valuewhen(buyB, request.security(syminfo.tickerid, tf2, ta.atr(14)), 0)
tpb1B = cbB + ratrB * TP1
tpb2B = cbB + ratrB * TP2
tpb3B = cbB + ratrB * TP3
tpb4B = cbB + ratrB * TP4
slbB = cbB - ratrB * SL //withsl?upB:valuewhen(buyB, upB,0)
csB = ta.valuewhen(sellB, closeB, 0)
ratrsB = ta.valuewhen(sellB, request.security(syminfo.tickerid, tf2, ta.atr(14)), 0)
tps1B = csB - ratrsB * TP1
tps2B = csB - ratrsB * TP2
tps3B = csB - ratrsB * TP3
tps4B = csB - ratrsB * TP4
slsB = csB + ratrsB * SL //withsl?dnB:valuewhen(sellB, dnB, 0)
Ptpb1B = cbB * (1 + ptp1)
Ptpb2B = cbB * (1 + ptp2)
Ptpb3B = cbB * (1 + ptp3)
Ptpb4B = et == ex1 ? cbB * (1 + ptp4) : tpb4B
PslbB = cbB * (1 - psl)
Ptps1B = csB * (1 - ptp1)
Ptps2B = csB * (1 - ptp2)
Ptps3B = csB * (1 - ptp3)
Ptps4B = et == ex1 ? csB * (1 - ptp4) : tps4B
PslsB = csB * (1 + psl)
//Variable Fix TP SL
tpb1tB = et == ex1 ? Ptpb1B : tpb1B
tpb2tB = et == ex1 ? Ptpb2B : tpb2B
tpb3tB = et == ex1 ? Ptpb3B : tpb3B
tpb4tB = Ptpb4B
slbtB = et == ex1 ? PslbB : slbB
tps1tB = et == ex1 ? Ptps1B : tps1B
tps2tB = et == ex1 ? Ptps2B : tps2B
tps3tB = et == ex1 ? Ptps3B : tps3B
tps4tB = Ptps4B
slstB = et == ex1 ? PslsB : slsB
sinceentrybB = int(math.max(1, nz(ta.barssince(buyB))))
openedbB = ta.barssince(buyB) >= 0 and trendB[1] == 1 and longside
highestbarbB = ta.highest(sinceentrybB)
lowestbarbB = ta.lowest(sinceentrybB)
stillopenb1B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb1tB and lowestbarbB[1] > slbtB
stillopenb2B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb2tB and lowestbarbB[1] > slbtB
stillopenb3B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb3tB and lowestbarbB[1] > slbtB
stillopenb4B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb4tB and lowestbarbB[1] > slbtB
hittpb1B = ta.cross(highB, tpb1tB) and stillopenb1B and openedbB
plotshape(hittpb1B)
hittpb2B = ta.cross(highB, tpb2tB) and stillopenb2B and openedbB
hittpb3B = ta.cross(highB, tpb3tB) and stillopenb3B and openedbB
hittpb4B = ta.cross(highB, tpb4tB) and stillopenb4B and openedbB
hitslbB = lowB < slbtB and stillopenb4B and openedbB
winreversebB = closeB > cbB and backtest and ta.crossunder(closeB, stB) and stillopenb4B and trendB == -1 and openedbB
losereversebB = closeB < cbB and backtest and ta.crossunder(closeB, stB) and stillopenb4B and trendB == -1 and openedbB
sinceentrysB = int(math.max(1, nz(ta.barssince(sellB))))
openedsB = ta.barssince(sellB) >= 0 and trendB[1] == -1 and shortside
highestbarsB = ta.highest(sinceentrysB)
lowestbarsB = ta.lowest(sinceentrysB)
stillopens1B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and lowestbarsB[1] > tps1tB
stillopens2B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and lowestbarsB[1] > tps2tB
stillopens3B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and lowestbarsB[1] > tps3tB
stillopens4B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and lowestbarsB[1] > tps4tB
hittps1B = ta.cross(lowB, tps1tB) and stillopens1B and openedsB
hittps2B = ta.cross(lowB, tps2tB) and stillopens2B and openedsB
hittps3B = ta.cross(lowB, tps3tB) and stillopens3B and openedsB
hittps4B = ta.cross(lowB, tps4tB) and stillopens4B and openedsB
hitslsB = highB > slstB and stillopens4B and openedsB
winreversesB = closeB <= csB and backtest and ta.crossover(closeB, stB) and stillopens4B and trendB == 1 and openedsB
losereversesB = closeB >= csB and backtest and ta.crossover(closeB, stB) and stillopens4B and trendB == 1 and openedsB
//============================================
cumbuyB = ta.cum(buyB ? 1 : 0)
cumsellB = ta.cum(sellB ? 1 : 0)
cumsignalB = cumbuyB + cumsellB
//net profit
sisaqtybB = stillopenb1B ? 1 : stillopenb2B ? 1 - Qtytp1 : stillopenb3B ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysB = stillopens1B ? 1 : stillopens2B ? 1 - Qtytp1 : stillopens3B ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
ptpb1B = (tpb1tB - cbB) * 100 / cbB * Qtytp1 //-0.06
ptpb2B = (tpb2tB - cbB) * 100 / cbB * Qtytp2 //-0.06
ptpb3B = (tpb3tB - cbB) * 100 / cbB * Qtytp3 //-0.06
ptpb4B = (tpb4tB - cbB) * 100 / cbB * Qtytp4 //-0.06
pslbB = (closeB - cbB) * 100 / cbB * sisaqtybB //-0.06
prbB = (closeB - cbB) * 100 / cbB * sisaqtybB //-0.06
ptps1B = (csB - tps1tB) * 100 / csB * Qtytp1 //-0.06
ptps2B = (csB - tps2tB) * 100 / csB * Qtytp2 //-0.06
ptps3B = (csB - tps3tB) * 100 / csB * Qtytp3 //-0.06
ptps4B = (csB - tps4tB) * 100 / csB * Qtytp4 //-0.06
pslsB = (csB - close) * 100 / csB * sisaqtysB //-0.06
prsB = (csB - closeB) * 100 / csB * sisaqtysB //-0.06
prtpb1B = ta.cum(hittpb1B ? ptpb1B : 0)
prtpb2B = ta.cum(hittpb2B ? ptpb2B : 0)
prtpb3B = ta.cum(hittpb3B ? ptpb3B : 0)
prtpb4B = ta.cum(hittpb4B ? ptpb4B : 0)
prslbB = ta.cum(hitslbB ? pslbB : 0)
prwrbB = ta.cum(winreversebB ? prbB : 0)
prlrbB = ta.cum(losereversebB ? prbB : 0)
prtps1B = ta.cum(hittps1B ? ptps1B : 0)
prtps2B = ta.cum(hittps2B ? ptps2B : 0)
prtps3B = ta.cum(hittps3B ? ptps3B : 0)
prtps4B = ta.cum(hittps4B ? ptps4B : 0)
prslsB = ta.cum(hitslsB ? pslsB : 0)
prwrsB = ta.cum(winreversesB ? prsB : 0)
prlrsB = ta.cum(losereversesB ? prsB : 0)
netcapitalgainB = prtpb1B + prtpb2B + prtpb3B + prtpb4B + prslbB + prtps1B + prtps2B + prtps3B + prtps4B + prslsB + prwrbB + prlrbB + prwrsB + prlrsB
///winrate
wintpb1B = ta.cum(hittpb1B ? 1 : 0)
wintpb2B = ta.cum(hittpb2B ? 1 : 0)
wintpb3B = ta.cum(hittpb3B ? 1 : 0)
wintpb4B = ta.cum(hittpb4B ? 1 : 0)
wrslbB = ta.cum(hitslbB ? 1 : 0)
wintps1B = ta.cum(hittps1B ? 1 : 0)
wintps2B = ta.cum(hittps2B ? 1 : 0)
wintps3B = ta.cum(hittps3B ? 1 : 0)
wintps4B = ta.cum(hittps4B ? 1 : 0)
wrslsB = ta.cum(hitslsB ? 1 : 0)
cumtp1B = wintpb1B + wintps1B
cumtp2B = wintpb2B + wintps2B
cumtp3B = wintpb3B + wintps3B
cumtp4B = wintpb4B + wintps4B
cumslB = wrslbB + wrslsB
cumpositionB = cumtp1B + cumtp2B + cumtp3B + cumtp4B + cumslB //+cumwinreverseB//+cumlosereverseB
wrtp1B = truncate(cumtp1B * 100 / cumsignalB, 0)
wrtp2B = truncate(cumtp2B * 100 / cumsignalB, 0)
wrtp3B = truncate(cumtp3B * 100 / cumsignalB, 0)
wrtp4B = truncate(cumtp4B * 100 / cumsignalB, 0)
wrslB = truncate(cumslB * 100 / cumsignalB, 0)
winallB = cumtp1B + cumtp2B + cumtp3B + cumtp4B //+cumwinreverseB
wrallB = winallB * 100 / cumpositionB
//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe C
//Supertrend Indicator
srcC = hl2C
atr2C = ta.sma(trC, period)
atrC = request.security(syminfo.tickerid, tf3, ta.atr(period))
upC = srcC - mult * atrC
up1C = nz(upC[1], upC)
upC := closeC[1] > up1C ? math.max(upC, up1C) : upC
dnC = srcC + mult * atrC
dn1C = nz(dnC[1], dnC)
dnC := closeC[1] < dn1C ? math.min(dnC, dn1C) : dnC
trendC = 1
trendC := nz(trendC[1], trendC)
trendC := trendC == -1 and closeC > dn1C ? 1 : trendC == 1 and closeC < up1C ? -1 : trendC
stC = trendC == 1 ? upC : dnC
//filtering
atraC = request.security(syminfo.tickerid, tf3, ta.atr(atrLen))
atrMaC = atrMaType == 'EMC' ? ta.ema(atraC, atrMaLen) : ta.sma(atraC, atrMaLen)
updmC = ta.change(highC)
downdmC = -ta.change(lowC)
plusdmC = na(updmC) ? na : updmC > downdmC and updmC > 0 ? updmC : 0
minusdmC = na(downdmC) ? na : downdmC > updmC and downdmC > 0 ? downdmC : 0
//trurC = rma(trC, diLen)
//plusC = fixnan(100 * rma(plusdmC, diLen) / trurC)
//minusC = fixnan(100 * rma(minusdmC, diLen) / trurC)
//sumC = plusC + minusC
//adxC = 100 * rma(abs(plusC - minusC) / (sumC == 0 ? 1 : sumC), adxLen)
cndSidwayss1C = atraC >= atrMaC
cndSidwayss2C = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysC = cndSidwayss1C or cndSidwayss2C
cndSidways1C = cndSidwayss1C and cndSidwayss2C
Sidwayss1C = atraC <= atrMaC
Sidwayss2C = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysC = Sidwayss1C or Sidwayss2C
Sidways1C = Sidwayss1C and Sidwayss2C
trendTypeC = typefilter == filter1 ? cndSidwayss1C : typefilter == filter2 ? cndSidwayss2C : typefilter == filter3 ? cndSidwaysC : typefilter == filter4 ? cndSidways1C : typefilter == filter5 ? RSI > 0 : na
// Entry FIX
buysignal1C = ta.crossover(closeC, stC) and backtest and trendTypeC
sellsignal1C = ta.crossunder(closeC, stC) and backtest and trendTypeC
buyC = buysignal1C and longside
sellC = sellsignal1C and shortside
//Tp & Sl by atr Mult
cbC = ta.valuewhen(buyC, closeC, 0)
ratrC = ta.valuewhen(buyC, request.security(syminfo.tickerid, tf3, ta.atr(14)), 0)
tpb1C = cbC + ratrC * TP1
tpb2C = cbC + ratrC * TP2
tpb3C = cbC + ratrC * TP3
tpb4C = cbC + ratrC * TP4
slbC = cbC - ratrC * SL //withsl?upC:valuewhen(buyC, upC,0)
csC = ta.valuewhen(sellC, closeC, 0)
ratrsC = ta.valuewhen(sellC, request.security(syminfo.tickerid, tf3, ta.atr(14)), 0)
tps1C = csC - ratrsC * TP1
tps2C = csC - ratrsC * TP2
tps3C = csC - ratrsC * TP3
tps4C = csC - ratrsC * TP4
slsC = csC + ratrsC * SL //withsl?dnC:valuewhen(sellC, dnC, 0)
Ptpb1C = cbC * (1 + ptp1)
Ptpb2C = cbC * (1 + ptp2)
Ptpb3C = cbC * (1 + ptp3)
Ptpb4C = et == ex1 ? cbC * (1 + ptp4) : tpb4C
PslbC = cbC * (1 - psl)
Ptps1C = csC * (1 - ptp1)
Ptps2C = csC * (1 - ptp2)
Ptps3C = csC * (1 - ptp3)
Ptps4C = et == ex1 ? csC * (1 - ptp4) : tps4C
PslsC = csC * (1 + psl)
//Variable Fix TP SL
tpb1tC = et == ex1 ? Ptpb1C : tpb1C
tpb2tC = et == ex1 ? Ptpb2C : tpb2C
tpb3tC = et == ex1 ? Ptpb3C : tpb3C
tpb4tC = Ptpb4C
slbtC = et == ex1 ? PslbC : slbC
tps1tC = et == ex1 ? Ptps1C : tps1C
tps2tC = et == ex1 ? Ptps2C : tps2C
tps3tC = et == ex1 ? Ptps3C : tps3C
tps4tC = Ptps4C
slstC = et == ex1 ? PslsC : slsC
sinceentrybC = int(math.max(1, nz(ta.barssince(buyC))))
openedbC = ta.barssince(buyC) >= 0 and trendC[1] == 1 and longside
highestbarbC = ta.highest(sinceentrybC)
lowestbarbC = ta.lowest(sinceentrybC)
stillopenb1C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb1tC and lowestbarbC[1] > slbtC
stillopenb2C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb2tC and lowestbarbC[1] > slbtC
stillopenb3C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb3tC and lowestbarbC[1] > slbtC
stillopenb4C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb4tC and lowestbarbC[1] > slbtC
hittpb1C = ta.cross(highC, tpb1tC) and stillopenb1C and openedbC
plotshape(hittpb1C)
hittpb2C = ta.cross(highC, tpb2tC) and stillopenb2C and openedbC
hittpb3C = ta.cross(highC, tpb3tC) and stillopenb3C and openedbC
hittpb4C = ta.cross(highC, tpb4tC) and stillopenb4C and openedbC
hitslbC = lowC < slbtC and stillopenb4C and openedbC
winreversebC = closeC > cbC and backtest and ta.crossunder(closeC, stC) and stillopenb4C and trendC == -1 and openedbC
losereversebC = closeC < cbC and backtest and ta.crossunder(closeC, stC) and stillopenb4C and trendC == -1 and openedbC
sinceentrysC = int(math.max(1, nz(ta.barssince(sellC))))
openedsC = ta.barssince(sellC) >= 0 and trendC[1] == -1 and shortside
highestbarsC = ta.highest(sinceentrysC)
lowestbarsC = ta.lowest(sinceentrysC)
stillopens1C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and lowestbarsC[1] > tps1tC
stillopens2C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and lowestbarsC[1] > tps2tC
stillopens3C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and lowestbarsC[1] > tps3tC
stillopens4C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and lowestbarsC[1] > tps4tC
hittps1C = ta.cross(lowC, tps1tC) and stillopens1C and openedsC
hittps2C = ta.cross(lowC, tps2tC) and stillopens2C and openedsC
hittps3C = ta.cross(lowC, tps3tC) and stillopens3C and openedsC
hittps4C = ta.cross(lowC, tps4tC) and stillopens4C and openedsC
hitslsC = highC > slstC and stillopens4C and openedsC
winreversesC = closeC <= csC and backtest and ta.crossover(closeC, stC) and stillopens4C and trendC == 1 and openedsC
losereversesC = closeC >= csC and backtest and ta.crossover(closeC, stC) and stillopens4C and trendC == 1 and openedsC
//============================================
cumbuyC = ta.cum(buyC ? 1 : 0)
cumsellC = ta.cum(sellC ? 1 : 0)
cumsignalC = cumbuyC + cumsellC
//net profit
sisaqtybC = stillopenb1C ? 1 : stillopenb2C ? 1 - Qtytp1 : stillopenb3C ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysC = stillopens1C ? 1 : stillopens2C ? 1 - Qtytp1 : stillopens3C ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
ptpb1C = (tpb1tC - cbC) * 100 / cbC * Qtytp1 //-0.06
ptpb2C = (tpb2tC - cbC) * 100 / cbC * Qtytp2 //-0.06
ptpb3C = (tpb3tC - cbC) * 100 / cbC * Qtytp3 //-0.06
ptpb4C = (tpb4tC - cbC) * 100 / cbC * Qtytp4 //-0.06
pslbC = (closeC - cbC) * 100 / cbC * sisaqtybC //-0.06
prbC = (closeC - cbC) * 100 / cbC * sisaqtybC //-0.06
ptps1C = (csC - tps1tC) * 100 / csC * Qtytp1 //-0.06
ptps2C = (csC - tps2tC) * 100 / csC * Qtytp2 //-0.06
ptps3C = (csC - tps3tC) * 100 / csC * Qtytp3 //-0.06
ptps4C = (csC - tps4tC) * 100 / csC * Qtytp4 //-0.06
pslsC = (csC - closeC) * 100 / csC * sisaqtysC //-0.06
prsC = (csC - closeC) * 100 / csC * sisaqtysC //-0.06
prtpb1C = ta.cum(hittpb1C ? ptpb1C : 0)
prtpb2C = ta.cum(hittpb2C ? ptpb2C : 0)
prtpb3C = ta.cum(hittpb3C ? ptpb3C : 0)
prtpb4C = ta.cum(hittpb4C ? ptpb4C : 0)
prslbC = ta.cum(hitslbC ? pslbC : 0)
prwrbC = ta.cum(winreversebC ? prbC : 0)
prlrbC = ta.cum(losereversebC ? prbC : 0)
prtps1C = ta.cum(hittps1C ? ptps1C : 0)
prtps2C = ta.cum(hittps2C ? ptps2C : 0)
prtps3C = ta.cum(hittps3C ? ptps3C : 0)
prtps4C = ta.cum(hittps4C ? ptps4C : 0)
prslsC = ta.cum(hitslsC ? pslsC : 0)
prwrsC = ta.cum(winreversesC ? prsC : 0)
prlrsC = ta.cum(losereversesC ? prsC : 0)
netcapitalgainC = prtpb1C + prtpb2C + prtpb3C + prtpb4C + prslbC + prtps1C + prtps2C + prtps3C + prtps4C + prslsC + prwrbC + prlrbC + prwrsC + prlrsC
///winrate
wintpb1C = ta.cum(hittpb1C ? 1 : 0)
wintpb2C = ta.cum(hittpb2C ? 1 : 0)
wintpb3C = ta.cum(hittpb3C ? 1 : 0)
wintpb4C = ta.cum(hittpb4C ? 1 : 0)
wrslbC = ta.cum(hitslbC ? 1 : 0)
wintps1C = ta.cum(hittps1C ? 1 : 0)
wintps2C = ta.cum(hittps2C ? 1 : 0)
wintps3C = ta.cum(hittps3C ? 1 : 0)
wintps4C = ta.cum(hittps4C ? 1 : 0)
wrslsC = ta.cum(hitslsC ? 1 : 0)
cumtp1C = wintpb1C + wintps1C
cumtp2C = wintpb2C + wintps2C
cumtp3C = wintpb3C + wintps3C
cumtp4C = wintpb4C + wintps4C
cumslC = wrslbC + wrslsC
cumpositionC = cumtp1C + cumtp2C + cumtp3C + cumtp4C + cumslC //+cumwinreverseC//+cumlosereverseC
wrtp1C = truncate(cumtp1C * 100 / cumsignalC, 0)
wrtp2C = truncate(cumtp2C * 100 / cumsignalC, 0)
wrtp3C = truncate(cumtp3C * 100 / cumsignalC, 0)
wrtp4C = truncate(cumtp4C * 100 / cumsignalC, 0)
wrslC = truncate(cumslC * 100 / cumsignalC, 0)
winallC = cumtp1C + cumtp2C + cumtp3C + cumtp4C //+cumwinreverseC
wrallC = winallC * 100 / cumpositionC
//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe D
//Supertrend Indicator
srcD = hl2D
atr2D = ta.sma(trD, period)
atrD = request.security(syminfo.tickerid, tf4, ta.atr(period))
upD = srcD - mult * atrD
up1D = nz(upD[1], upD)
upD := closeD[1] > up1D ? math.max(upD, up1D) : upD
dnD = srcD + mult * atrD
dn1D = nz(dnD[1], dnD)
dnD := closeD[1] < dn1D ? math.min(dnD, dn1D) : dnD
trendD = 1
trendD := nz(trendD[1], trendD)
trendD := trendD == -1 and closeD > dn1D ? 1 : trendD == 1 and closeD < up1D ? -1 : trendD
stD = trendD == 1 ? upD : dnD
//filtering
atraD = request.security(syminfo.tickerid, tf4, ta.atr(atrLen))
atrMaD = atrMaType == 'EMD' ? ta.ema(atraD, atrMaLen) : ta.sma(atraD, atrMaLen)
updmD = ta.change(highD)
downdmD = -ta.change(lowD)
plusdmD = na(updmD) ? na : updmD > downdmD and updmD > 0 ? updmD : 0
minusdmD = na(downdmD) ? na : downdmD > updmD and downdmD > 0 ? downdmD : 0
//trurD = rma(trD, diLen)
//plusD = fixnan(100 * rma(plusdmD, diLen) / trurD)
//minusD = fixnan(100 * rma(minusdmD, diLen) / trurD)
//sumD = plusD + minusD
//adxD = 100 * rma(abs(plusD - minusD) / (sumD == 0 ? 1 : sumD), adxLen)
cndSidwayss1D = atraD >= atrMaD
cndSidwayss2D = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysD = cndSidwayss1D or cndSidwayss2D
cndSidways1D = cndSidwayss1D and cndSidwayss2D
Sidwayss1D = atraD <= atrMaD
Sidwayss2D = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysD = Sidwayss1D or Sidwayss2D
Sidways1D = Sidwayss1D and Sidwayss2D
trendTypeD = typefilter == filter1 ? cndSidwayss1D : typefilter == filter2 ? cndSidwayss2D : typefilter == filter3 ? cndSidwaysD : typefilter == filter4 ? cndSidways1D : typefilter == filter5 ? RSI > 0: na
// Entry FIX
buysignal1D = ta.crossover(closeD, stD) and backtest and trendTypeD
sellsignal1D = ta.crossunder(closeD, stD) and backtest and trendTypeD
buyD = buysignal1D and longside
sellD = sellsignal1D and shortside
//Tp & Sl by atr Mult
cbD = ta.valuewhen(buyD, closeD, 0)
ratrD = ta.valuewhen(buyD, request.security(syminfo.tickerid, tf4, ta.atr(14)), 0)
tpb1D = cbD + ratrD * TP1
tpb2D = cbD + ratrD * TP2
tpb3D = cbD + ratrD * TP3
tpb4D = cbD + ratrD * TP4
slbD = cbD - ratrD * SL //withsl?upD:valuewhen(buyD, upD,0)
csD = ta.valuewhen(sellD, closeD, 0)
ratrsD = ta.valuewhen(sellD, request.security(syminfo.tickerid, tf4, ta.atr(14)), 0)
tps1D = csD - ratrsD * TP1
tps2D = csD - ratrsD * TP2
tps3D = csD - ratrsD * TP3
tps4D = csD - ratrsD * TP4
slsD = csD + ratrsD * SL //withsl?dnD:valuewhen(sellD, dnD, 0)
Ptpb1D = cbD * (1 + ptp1)
Ptpb2D = cbD * (1 + ptp2)
Ptpb3D = cbD * (1 + ptp3)
Ptpb4D = et == ex1 ? cbD * (1 + ptp4) : tpb4D
PslbD = cbD * (1 - psl)
Ptps1D = csD * (1 - ptp1)
Ptps2D = csD * (1 - ptp2)
Ptps3D = csD * (1 - ptp3)
Ptps4D = et == ex1 ? csD * (1 - ptp4) : tps4D
PslsD = csD * (1 + psl)
//Variable Fix TP SL
tpb1tD = et == ex1 ? Ptpb1D : tpb1D
tpb2tD = et == ex1 ? Ptpb2D : tpb2D
tpb3tD = et == ex1 ? Ptpb3D : tpb3D
tpb4tD = Ptpb4D
slbtD = et == ex1 ? PslbD : slbD
tps1tD = et == ex1 ? Ptps1D : tps1D
tps2tD = et == ex1 ? Ptps2D : tps2D
tps3tD = et == ex1 ? Ptps3D : tps3D
tps4tD = Ptps4D
slstD = et == ex1 ? PslsD : slsD
sinceentrybD = int(math.max(1, nz(ta.barssince(buyD))))
openedbD = ta.barssince(buyD) >= 0 and trendD[1] == 1 and longside
highestbarbD = ta.highest(sinceentrybD)
lowestbarbD = ta.lowest(sinceentrybD)
stillopenb1D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb1tD and lowestbarbD[1] > slbtD
stillopenb2D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb2tD and lowestbarbD[1] > slbtD
stillopenb3D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb3tD and lowestbarbD[1] > slbtD
stillopenb4D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb4tD and lowestbarbD[1] > slbtD
hittpb1D = ta.cross(highD, tpb1tD) and stillopenb1D and openedbD
plotshape(hittpb1D)
hittpb2D = ta.cross(highD, tpb2tD) and stillopenb2D and openedbD
hittpb3D = ta.cross(highD, tpb3tD) and stillopenb3D and openedbD
hittpb4D = ta.cross(highD, tpb4tD) and stillopenb4D and openedbD
hitslbD = lowD < slbtD and stillopenb4D and openedbD
winreversebD = closeD > cbD and backtest and ta.crossunder(closeD, stD) and stillopenb4D and trendD == -1 and openedbD
losereversebD = closeD < cbD and backtest and ta.crossunder(closeD, stD) and stillopenb4D and trendD == -1 and openedbD
sinceentrysD = int(math.max(1, nz(ta.barssince(sellD))))
openedsD = ta.barssince(sellD) >= 0 and trendD[1] == -1 and shortside
highestbarsD = ta.highest(sinceentrysD)
lowestbarsD = ta.lowest(sinceentrysD)
stillopens1D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and lowestbarsD[1] > tps1tD
stillopens2D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and lowestbarsD[1] > tps2tD
stillopens3D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and lowestbarsD[1] > tps3tD
stillopens4D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and lowestbarsD[1] > tps4tD
hittps1D = ta.cross(lowD, tps1tD) and stillopens1D and openedsD
hittps2D = ta.cross(lowD, tps2tD) and stillopens2D and openedsD
hittps3D = ta.cross(lowD, tps3tD) and stillopens3D and openedsD
hittps4D = ta.cross(lowD, tps4tD) and stillopens4D and openedsD
hitslsD = highD > slstD and stillopens4D and openedsD
winreversesD = closeD <= csD and backtest and ta.crossover(closeD, stD) and stillopens4D and trendD == 1 and openedsD
losereversesD = closeD >= csD and backtest and ta.crossover(closeD, stD) and stillopens4D and trendD == 1 and openedsD
//============================================
cumbuyD = ta.cum(buyD ? 1 : 0)
cumsellD = ta.cum(sellD ? 1 : 0)
cumsignalD = cumbuyD + cumsellD
//net profit
sisaqtybD = stillopenb1D ? 1 : stillopenb2D ? 1 - Qtytp1 : stillopenb3D ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysD = stillopens1D ? 1 : stillopens2D ? 1 - Qtytp1 : stillopens3D ? 1 - (Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
ptpb1D = (tpb1tD - cbD) * 100 / cbD * Qtytp1 //-0.06
ptpb2D = (tpb2tD - cbD) * 100 / cbD * Qtytp2 //-0.06
ptpb3D = (tpb3tD - cbD) * 100 / cbD * Qtytp3 //-0.06
ptpb4D = (tpb4tD - cbD) * 100 / cbD * Qtytp4 //-0.06
pslbD = (closeD - cbD) * 100 / cbD * sisaqtybD //-0.06
prbD = (closeD - cbD) * 100 / cbD * sisaqtybD //-0.06
ptps1D = (csD - tps1tD) * 100 / csD * Qtytp1 //-0.06
ptps2D = (csD - tps2tD) * 100 / csD * Qtytp2 //-0.06
ptps3D = (csD - tps3tD) * 100 / csD * Qtytp3 //-0.06
ptps4D = (csD - tps4tD) * 100 / csD * Qtytp4 //-0.06
pslsD = (csD - closeD) * 100 / csD * sisaqtysD //-0.06
prsD = (csD - closeD) * 100 / csD * sisaqtysD //-0.06
prtpb1D = ta.cum(hittpb1D ? ptpb1D : 0)
prtpb2D = ta.cum(hittpb2D ? ptpb2D : 0)
prtpb3D = ta.cum(hittpb3D ? ptpb3D : 0)
prtpb4D = ta.cum(hittpb4D ? ptpb4D : 0)
prslbD = ta.cum(hitslbD ? pslbD : 0)
prwrbD = ta.cum(winreversebD ? prbD : 0)
prlrbD = ta.cum(losereversebD ? prbD : 0)
prtps1D = ta.cum(hittps1D ? ptps1D : 0)