-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriple_supertrend_v4_champion_r3.pine
More file actions
1176 lines (1114 loc) · 123 KB
/
Copy pathtriple_supertrend_v4_champion_r3.pine
File metadata and controls
1176 lines (1114 loc) · 123 KB
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
// ????????????????????????????????????????????????????????????????????????????
// Triple SuperTrend v11-Evo-A-ASCEND ?? 3ST-RGVS-MTF (Regime-Gated Vol-Targeted)
// (Regime-Gated, true Vol-Targeted, Symmetric-Exit, contiguous-stack,
// peak-anchored right-tail lock, regime-fade de-risk, MTF maturity gate,
// 瘜W??芷???? / TP ?0撘??砍??/ ?餌?郭??閬?/ 頞典撟湧翩????唳
// + 頝典偕摨?ATR ?折?甇X? / ?脣???孵榆蝣箄???/ 銝?游漲?Ⅳ靽∪艙??// + ?R ??漲?Ⅳ??/ ?葉撠箏漲?脣蝣箄???
//
// ?祆???v9-Evo-A-CONV ?脣????極蝔璅?(a)蝬剜??X?????芣晾??/ ?箏鈭?抒??游董
// 靽桀儔(璉?? stageLive==strategy.opentrades)嚗?b)?豢撠?韐振?券瘥瑼撥????// (頝典偕摨?ATR ?折?甇X? / ?脣???孵榆蝣箄???/ 銝?游漲?Ⅳ靽∪艙??/ ?唳靘?甇詨???葫??嚗?// 銝行??Ⅳ addOK ???孵?撠惇 addOKLong/addOKShort 蝬剜??湔?∪?嚗?c)?冽迨?芣晾?箇?銝??啣?
// ?拇?韐振瘝????X?閮?頠詨?賊??迤鈭文????啣??航矽 input??閮剝?? not usePureA??// 憭征?湔?∪?)??//
// ??蝮暹?隤除?脫?嚗?潮摰??∪?皜砍????????????????????????????????????????????
// ?祆???葫撘??????嗥???銝敺???嗆撠?閮剛??牧嚗???? walk-forward
// ????撽??????胯雿?蜀?瑼??恐蝔曹遙雿?頞?/ ?? / 銝??撘?/
// 瘙箏??扳???/ ???? Sharpe X???詨?蝮暹??擃摮?隢迨憿??飛??嗅?皜砌??⊥?
// ??嚗歇?冽蝘駁?遙雿撠銝蝑摮啣摮啣 / ?芣?璈?舀捱摰批?鞎????賡?嚗???// ?撽??牧???誑 walk-forward 撠蝯祕皜砍??孵閮???//
// ?? ????芣晾靽格迤 1嚚ntrabar hardstop ?菜葫?箸? ?????????????????????????????????
// process_orders_on_close=true?alc_on_order_fills=false 銝??祆????strategy.exit ????// 閬?銝璉?撠銝剜?鈭斤????詨??祆??迤????live stop ?胯?銝璉?committed ??// trailStop??= trailStop[1])?ardstop ?支葉?漱?菜葫銝敺?朣?trailStop[1]嚗??詨??漱?箸?銝?氬?//
// ?? ????芣晾靽格迤 2嚚?游??鈭 + 銝??單撅斗頝?蝵????????????????????????
// brokerStopHit ??蝡?剛楝?祆????????TP/cut/fade/belowAvg)?擗?斤Ⅱ隤??// (閮?exitedThisBar/blockPartials=true)嚗???璉摮 hX ?典?鈭?蝯??找???嚗?????// ?刻?砍惜??銝剖董??蝵栽?歇?芾?撟喳?銝?璉?禮6 not inPos ?芰摰?蔭?X 銝?摰?// from_entry嚗蝙?嗥瘨菔??券 tranche ?擃雿????//
// ?? ????芣晾靽格迤 3嚚ntryBar ?蔭瘙⊥? ??????????????????????????????????????????
// ?典像??entryBar ?寥?蝵桃 na(??0)嚗imeStop 璇辣??not na(entryBar) 摰? + na ??閮?嚗?// 敺??剜?蝯?(bar_index-0)>=effTimeStop ???鈭活撟喳?//
// ?? ???砍?靽格迤 4嚚?折?銵虜?豢??脩?舀???input ?????????????????????????????
// ???潛?撘Ⅳ???摰虜?訾?敺??脩 input(?臬 walk-forward ??/ablation)嚗?// ??cohRelaxK (??1.15) / stallShrinkMin (??0.5) / pctlGaugeLo/Hi (??1/2) /
// eqBlendInit / eqBlendSmooth(??0.5 / 0.99)??銝????tooltip 璅酉撅祆?撠征?雁摨艾?//
// ?? ?餌?耨甇?5嚚?朣璇迤鈭斗郭??撟湧翩撽?嚗? brakeK ?桅??摹??????????????????
// useDecorrVolGauge(atrPct ?曉?雿??breathPctl)/useStallRatchet(撜啣澆?皛?stallShrink)??//
// ?? ?航??找耨甇?6嚚?銵冽?餃?蝣澆? ?????????????????????????????????????????????????
// 鈭粹??航?銝剜???皜 + 銝剜??剛??? + ????+ 璉 fresh stageLive 憿舐內/撠董??//
// ?? ?豢韐振璈嚗1~A4嚗?韐振蝑嚗?啣??航矽 input??閮剝?? not usePureA??蝛粹????
// [NEW-A1嚚楊撠箏漲 ATR ?折?甇X??⊥?] 撌?BE 敺敹? trail ??ATR 撠箏漲?寧
// atrScale=max(atrChand, |close-st3|)嚗?W偕摨?ST3 band 撖砍漲雿?瑼◢?芣?撠偕摨血?選?蝺抵圾
// ?Z隅?W low-vol pocket 鋡怠翰 ATR trail ?典摨瑕?瑼葉???premature-stopout 憭望?璅∪???曉之
// rawStop 撠箏漲?唳?atchet 隞?脖???eActive ??雿(銝憭批?憪?R)????st3/atrChand??// [NEW-A2嚚脣???孵榆蝣箄?? 擐 L1/S1 ??瘙?|close?t1| ??kBuf?atrChand嚗?典?蝧餉?撠
// ?渲??脣嚗?瞈曇蕭擃?餈賜征???? R 撌脰◤????祇◢?芣??∪?)?a 摰????銵?踹???隤日???// [NEW-A3嚚??游漲?Ⅳ靽∪艙? addOK ???孵?撠惇 addOKLong/addOKShort 銝血? AND coherence>=2(憭?// coherenceLong/蝛?coherenceShort)嚗甇W翰撠箏漲撌脰??X?隞頞典銵啁垠暺憭扳??芥? convMult(隤?// size)甇?漱??矽 go/no-go??// [NEW-A4嚚?蹂?皞飛?皜砍?] 蝝閫皜祆扼鈭斗?銵?寡?嚗?銵冽??嗡?撖阡? binding ?敹?// 靽?唳靘?(BE/?0/撌脣祕?暸/撜啣?撟湧翩??)嚗蝙 walk-forward ?臬?瑟?璇?踵?血???binding
// ??芣????冽??*FloorL/S ??effGiveback???潔遙雿?strategy.* ?澆??//
// ?? ??頞?堆?韐振瘝?嚗?啣??航矽 input??閮剝?? not usePureA??蝛箏?潮????????????
// [NEW-C1嚚R ??漲?Ⅳ? ?Ⅳ addOK ??AND (er >= er[1])嚗?瘙?蝣潛銝??????冽??// ?銵?L2/L3???渡?瑽?隤迎?A3 ???胯??瑽?血?隤?coherence)?onvMult ???胯??// ???抒?撠偌皞?er/erThr 隤?size)???抵??箝偌皞?蝯???嚗??????????榆??// (??漲)????ER 隞?>?瑼颯?撠箏漲隞??? ER 撌脤??銝??隅?g?甇???澆??質※皜?
// late-trend ?挾嚗迨??蝣潭?券???皜?頞典撠暹挾????Calmar 畾箸?)???榆???偌皞迤鈭歹?
// 擃偌皞???典???皜??冽??er(禮5) 銋?[1] 甇瑕嚗?啣? input?ffErAccelAdd ??// usePureA?? A3(蝯?)/convMult(size) 銝迤鈭歹??恣 go/no-go ???賣?雁摨艾?蝛箏?蝔?// (ER ?箇?孵?蝝???憭征?梁??璇?er嚗??漲??憭征憭拍?∪?)???閮剛??牧嚗?撽???// [NEW-C2嚚葉撠箏漲?脣蝣箄?? 擐 L1/S1 ??瘙葉撠箏漲 ST2 ??(憭?st2Bull / 蝛?st2Bear)????// 蝯??牧嚗egime gate ??蟡函 ER/ADX/ST3嚗??瑽?乩??撠箏漲 ST3??敹怠偕摨衣蕃頧?ST1嚗?// ?蝻箔葉撠箏漲 ST2???潭 ST1 ?蕃憭T3 撌脣??? ST2 隞征(swing 撠箏漲?◢)?冗敹擐??// ?◤?曇?? 1:2.5:6 銝偕摨衣?瑽葉???whipsaw ?脣??(敹怠偕摨血?喟蕃頧撠箏漲皛臬??葉
// 撠箏漲? swing ?????? coherence ????Ⅳ?瘜?蝘餃???桅脣?釭??閬??脣
// ?嗡?銝偕摨血??(蝑 coherence==3 ?脣??嚗?銝?regime gate ?葉撠箏漲?孵??脤??? A2(??// ?Z??撌格???甇?漱嚗???葉撠箏漲?孵??臬蝣箄????冽??st2Bull/st2Bear 蝝??瑽???// ?啣? input?雿擐(銝蔣?踹?蝣???蝛箏?潮?ffMidConfirm ??usePureA?? walk-forward
// 撽?嚗酉嚗????閬?銝剖偕摨衣Ⅱ隤???雿漱?嚗?隞?usePureA 蝝蝺???鞎Y撠?閬?// 擃?銝銵?// ??銝膩?剜???A1~A4 + C1/C2)銝敺?a)銴?X? input ?嗆憓隤輯?勗漲嚗?b)?身 false ??// not usePureA嚗?c)憭征?∪?嚗?d)???箸??嗆撠?閮剛??牧/? walk-forward 撽?嚗?隞颱??詨?
// 摮啣?瑁?????皜祆摮?//
// ???芰摨西?撖西??銝?憭改????剝嚗?詨?蝮暹??瑁?嚗?????????????????????????????
// ???啣??航矽 input(撅祆?撠征?雁摨佗?walk-forward ????摨行???嚗tfCapBars / fadeK /
// fadeCoolBars / cohRelaxK / stallShrinkMin / pctlGaugeLo / pctlGaugeHi / eqBlendInit /
// eqBlendSmooth?EW-A1/A2/A3/A4 ???EW-C1/C2 ???冽??input嚗?啣??航矽?芰摨艾?// ???桅??摹?改?擃血?嚗ultBE 銝?撽?銝???BE 蝺抵? + peakR ????+ TP ?0?唳)嚗?// relVol(蝬?brakeK clamp)?舫?????頠?+ 瘜W??? + 瘜W?銝?trail + 瘜W??瑕(??賊???
// 敺郭?蜓 trail 閫??????銝???嚗rThr 撽? convMult/regime-fade/adaptive-TS 憭楝敺?// (NEW-C1 ?寧 er 銝?榆??蝣?erThr嚗??賊???erThr 隞仿??嗉???嚗en3 ????ST3
// ?望???MTF ?瑼颱?皞銵??? multBE/brakeK/erThr ?? 簣2 step 閫撖?????// ???脤?璈??憭???????勗漲??閮剖??粹???靘?usePureA 銋暹楊?箇?嚗alk-forward
// ?????箇? vs ?????嗚??甜?餃??改??芾?擃?(plateau)霅?銝??曇???//
// ???砍?嚗? ??ST ? ??1 ?箇??望? + 瘣曄???(1:2.5:6)嚗頝隞?ATR% 甇????
// 憭征?湔?∪?嚗??臭?撌桃嚗???瞈??箏/?Ⅳ/?脤?璈?? toggle 靘?ablation??// 瘜冽?嚗瑼?葫撘?嚗??????箝??嗆撠?閮剛??牧嚗? walk-forward ????撽???
// ?渡??嗡??X?蝮暹???閮剖潛蝛拙韏琿?嚗?甇?per-symbol 隤踹???// ????????????????????????????????????????????????????????????????????????????
strategy("Triple SuperTrend v11-Evo-A-ASCEND", shorttitle="3ST-v11EAa", overlay=true,
initial_capital=10000, default_qty_type=strategy.fixed, pyramiding=3,
commission_type=strategy.commission.percent, commission_value=0.05,
slippage=2, calc_on_order_fills=false, process_orders_on_close=true)
// ????????????????????????????????????????????????????????????????????????????
// 禮1 頛詨?嚗?蝯?/ 銝剜? tooltip / min-max-step / ?函????塚?
// ????????????????????????????????????????????????????????????????????????????
// ?? 頞典嚗uperTrend嚗?1 ?箇??望? + 瘣曄???嚗?蝯?10/11/12 ?脩??砍? ??????????
gTrend = "頞典 SuperTrend"
baseLen = input.int(10, "?箇? ATR ?望?", minval=7, maxval=20, group=gTrend,
tooltip="銝? ST ?梁?蝷?ATR ?望?嚗T2/ST3 ?勗?瘣曄?嚗??圈?銵(10/11/12)?敺株矽??瞈?蝢抬?摰儔??剛隅?X??偕摨艾遣霅圈???8~12?? timeframe嚗?擃望?(?梁?)?舫???7~9??)
midMult = input.float(2.5, "銝剖偕摨阡望???", minval=2.0, maxval=3.0, step=0.1, group=gTrend,
tooltip="ST2 ?望? = ?箇??甇文??遣霅?ST1:ST2:ST3 ??1:2.5:6 隞亙???intraday noise / swing / regime嚗蝙銝? ST ?孵??賊? <0.7 ???函??Ⅳ靘??矽憭芸?銝? ST ?梁???蝣潸????孵?銝?游漲 ratchet 憭勗?儔嚗?撠?midMult/slowMult ??摨行???)
slowMult = input.float(6.0, "?W偕摨阡望???", minval=5.0, maxval=7.0, step=0.5, group=gTrend,
tooltip="ST3 ?望? = ?箇??甇文??T3 雿?a?regime ?孵????孵?嚗T1 摰?暺? timeframe 銝??對??摰?詨? baseLen ???TF ?瑼?persistBars ??len3 瘣曄??? mtfCapBars clamp?en3 ????ST3 ?望???MTF ?瑼颱?皞??桅??摹??????游漲 ratchet 靘陷銝偕摨血??W漲嚗銝?ST ?梁????)
f1 = input.float(1.5, "ST1 ??(敹?", minval=1.3, maxval=2.0, step=0.1, group=gTrend,
tooltip="敹恍?ST ??ATR ??????1.3 ??斗 whipsaw嚗??1.0 ??車?餌?蝧餉?嚗?憭扯???撠?蝛押?)
f2 = input.float(2.5, "ST2 ??(銝?", minval=2.0, maxval=3.5, step=0.1, group=gTrend,
tooltip="銝剝?ST ??嚗?望???隞亙?甇交????偕摨艾?)
f3 = input.float(3.5, "ST3 ??(??", minval=3.0, maxval=5.0, step=0.5, group=gTrend,
tooltip="?a?ST ????蕪摰???? ST1 摰?暺郭?? size ?隅?V?畾萇?瑽?)
// ?? 瘥???銝?萇??箇?嚗?閮剖尿???湔?/ 銋暹楊甇詨?嚗??????????????????????????????
gMaster = "瘥???Master"
usePureA = input.bool(false, "蝝蝺芋撘?銝?菟????脤?璈嚗?, group=gMaster,
tooltip="??敺撥?園?????脤?璈嚗eak-R ????瘜W??芷????Regime-fade/ER ?芷??time-stop/MTF ??摨阡?/TP ?0靽?唳/瘜W??芷?蜓 trail/憭偕摨行???游漲 ratchet/?餌?郭??閬?頞典撟湧翩????唳/撠迂?澆撘脣?瑕/撌脣祕?曄?拚?唳/頝典偕摨?ATR ?折?甇X? A1/?脣???孵榆??A2/銝?游漲?Ⅳ靽∪艙??A3/ER ??漲?Ⅳ??C1/銝剖偕摨阡脣蝣箄???C2嚗?雿輻??仿?嗾瘛函??箇????walk-forward ???箇? vs +璈???甜?餃??改??踹??????? toggle ??甇詨??航炊???脤?璈? not usePureA嚗??箇?銝??孵神?詨????唳/?瑕/trail/?脣嚗?蝯情?蝺飛?rThr/multBE/brakeK ??撽?憭?頝臬??箏暺?撘望改??祇??噶?潮??X葫閰艾?)
// ?? Regime ??嚗???(ER) ?箔蜓?DX ?箄??T3 ?X???誨?箏? MA200 ???????????
gReg = "Regime ??"
useRegime = input.bool(true, "? Regime Gate嚗????殷?", group=gReg,
tooltip="頞典蝑??撖?edge ?芸??冽?孵??扳?蝥? regime嚗??雿R/雿DX)?◤?????迨?雿?潮???L1/S1嚗?敶梢?Ⅳ/?箏????蝝?ST 撠蝯?靘?ablation)???閮剛??牧嚗? walk-forward 撽???)
useER = input.bool(true, "??Kaufman ??瘥?ER) ?巨", group=gReg,
tooltip="ER = |close-close[n]| / 峉|close-close[1]|?身閮?隤迎?ER ?瑼餅頞典蝑?詨?蝛拙?銝?蕪?剁???ADX 甇?漱?折???Choppiness嚗??函???湧??撠?? walk-forward 撽???)
erLen = input.int(20, "ER ?望?", minval=10, maxval=40, group=gReg,
tooltip="??瘥??摨艾誑 baseLen ??閮剖?嚗望??⊿?嚗?瘥?嚗楊???芷????)
erThr = input.float(0.30, "ER ?瑼?, minval=0.20, maxval=0.50, step=0.05, group=gReg,
tooltip="ER > ?瑼?閬?孵???regime??.30 ?箔葉?扯絲暺???鈭斗??訾?頞喋?雿蕪銝??斗??閬迨?瑼餃????暨靽∪艙??][???[regime-fade][?芷??time-stop]??典皞偕摨色惇?桅??摹?改?walk-forward 敹?撠?erThr ?????摨行葫閰艾酉嚗EW-C1 ??漲?Ⅳ????er 銝?榆??銝1 erThr)嚗誑?餌??雿?瑼餉??瑯?)
useADX = input.bool(true, "??ADX ?巨嚗皛舫??曉潘?", group=gReg,
tooltip="ADX ?箄??拇?蟡具dxOn/adxOff 敶X??脫趙嚗????芸?閬???regime?? ER ??甇?漱蝣箄???)
adxLen = input.int(14, "ADX / DI ?望?", minval=10, maxval=20, group=gReg,
tooltip="ADX ??DI ?孵????望?????澆翰 ATR 隡啗???)
adxOn = input.float(23.0, "ADX ???曉?, minval=20.0, maxval=30.0, step=1.0, group=gReg,
tooltip="ADX ?甇文?regime 頧?On??)
adxOff = input.float(18.0, "ADX ???曉?, minval=14.0, maxval=22.0, step=1.0, group=gReg,
tooltip="ADX 頝甇文?regime 頧?Off?? < ???曉潔誑敶X??脫趙撣塚??踹???????)
useSlowST = input.bool(true, "??ST3 ???巨嚗?孵???", group=gReg,
tooltip="隞交??ST3 ??銝忽????regime ?孵?蟡剁??誨蝖祉楊蝣?MA200?望??⊿???拇???瘜W???)
minVotes = input.int(2, "?撠?蟡冽", minval=1, maxval=3, group=gReg,
tooltip="ER / ADX / ST3 銝巨銝剝??迨蟡冽?銵??柴? 蟡函蝛拙韏琿??釣??撖阡??瑼餃歇?? clamp ?喋歇?蟡冽???踹??巨?券???甇鳴?銝??澆?巨?豢??銵冽?誑??瑼餃歇蝮柴?蝷箝?)
useDIgate = input.bool(true, "? DI ?孵???diPlus>diMinus嚗?, group=gReg,
tooltip="擐?斗?蟡典???瘙?DI ?孵?銝?氬?????航?蕪?(?虫?蝔桅??砍?)嚗?閮剔?舫?嚗? walk-forward 撠?園??甜?鳴??身?inVotes 隞?2??)
useMTFgate = input.bool(false, "? MTF ?Y?瑽??漲???身?? ablation嚗?, group=gReg,
tooltip="擐??瘙?ST3 蝧餃?敺歇??蝛???persistBars 璉?persistBars=clamp(len3,3,mtfCapBars))??隤祕?剝嚗?憓?1 ?隤?input mtfCapBars??閬甈?)嚗蒂??啣??芰摨佗?mtfCapBars ?湔瘙箏?擐?曇??瑼鳴?撅祆?撠征?雁摨佗?walk-forward ???亙?皜祈?勗漲?身閮?隤迎??隅?W憭偕摨血????Y?瑽歇??嚗?蝒?芸敹?TF ?祆??箇嚗誑?T3 蝧餃?敺?瘣餅??詻?隡?HTF ??摨佗???request.security ?鼓/??憸券?sePureA=true ?撥?園???)
mtfCapBars = input.int(20, "MTF ??摨虫???璉?[?啣??芰摨因", minval=5, maxval=60, group=gReg,
tooltip="persistBars ????persistBars = clamp(len3, 3, 甇文?????憓??航矽 input?????脣?撣豢嚗??嗅潛?交捱摰?MTF 擐?曇??瑼鳴??迨撅祆?撠征?雁摨佗?敹?閮?祕?蝛粹?銝血???摨行葫閰艾en3=baseLen?slowMult ?券?閮剖??~60 璉?閬? ST3 蝡帘 60 璉??曇?擐?扔摨血辣?脤脣?之撟?鈭斗??賂?甇支??甇X晷?潮???曉之?啣嗾銋???20 璉蝛拙韏琿??? useMTFgate=true ????)
// ?? ??嚗?繚瘜W??璅?嚗T-Core嚗? ER 靽∪艙???Ⅳ + ??頠?+ 瑽▼銝? ??????
gPos = "?? Vol-Target"
useVolTarget= input.bool(true, "?瘜W??璅???", group=gPos,
tooltip="????摰????抒?(瘥?=1)???? qty=(targetVol/instVolAnn)*equity*weight/close嚗楊??/頝?regime 憸券鞎Y銝?湛??舀??敹身閮?隤迎??孵? Sharpe嚗? walk-forward 撽???)
targetVol = input.float(15.0, "撟游??格?瘜W???%", minval=8.0, maxval=30.0, step=1.0, group=gPos,
tooltip="瘥雿??芰??格?撟游?瘜W?(憒?15%)??瘜W????憭扼?瘜W????葬撠??迎?雿踹???憸券鞎Y蝑??? timeframe 銝???撟游?撌脩 barsPerYear ??)??) / 100.0
volEstMode = input.string("ATR%", "瘜W?隡啗?瘜?, options=["ATR%","Stdev"], group=gPos,
tooltip="ATR%嚗a.atr/close 撟游?(撠?gap 頛帘??嚗tdev嚗? N ?亙??詨?祆?皞榆撟游?(頛票餈?蝘摰儔)????詨???瘨 close ?寞??瘙⊥???)
volLen = input.int(20, "瘜W?隡啗???", minval=10, maxval=40, group=gPos,
tooltip="instVol 隡啗?閬??? 20 璉瘜W??????漲?像銵∴?????????瑕????)
barsPerYear = input.float(365.0, "撟游?璉(撟游???)", minval=24.0, maxval=8760.0, step=1.0, group=gPos,
tooltip="撟游??冽?撟?K 璉?蝺?撖?365?蝺蟡兩?252??H??190??H??760???望????郊隤踵嚗??vol-target 撟游?憭梁????芰頂蝯望批?憭扳????釣??commission/slippage/timeStopBars/cooldownBars 蝑誑璉閮漲??望????郊(?∠?撘??撅砌蝙?刻痊隞???)
wL1 = input.float(0.40, "L1 甈?(擐挾)", minval=0.20, maxval=0.60, step=0.05, group=gPos,
tooltip="擐挾?甈???畾菜???蝮格?格??嚗?蝯??蜇瑽▼銝?蝝???)
wL2 = input.float(0.30, "L2 甈?(?Ⅳ)", minval=0.15, maxval=0.50, step=0.05, group=gPos,
tooltip="蝚砌?畾萄?蝣潭???蝣澆??典?畾菜筑??R>0)銝隅?W???閫貊(winners-add-only)嚗蒂??ER 靽∪艙????瘜W???蝝???)
wL3 = input.float(0.30, "L3 甈?(?Ⅳ)", minval=0.15, maxval=0.50, step=0.05, group=gPos,
tooltip="蝚砌?畾萄?蝣潭???)
useConvAdd = input.bool(true, "? ER 靽∪艙???Ⅳ", group=gPos,
tooltip="L2/L3 ?甈? ?clamp(er/erThr,0.5,1.0)嚗隅?X?蝥扯?擃??遛蝣潘?皞隅?W???蝣潦身閮?隤迎????怎垢???游?蝣潭??芥??Calmar???冽??er/erThr嚗?啣??芰摨佗???=?箏?甈?撠??)
maxGross = input.float(1.5, "蝮賣?獢蹂???? equity)", minval=1.0, maxval=3.0, step=0.1, group=gPos,
tooltip="??惜???? / equity ?′銝???瘜W???vol-target ?憭扳??迎?甇支???漲瑽▼???活?貉? debug capHits??)
useVolBrake = input.bool(true, "?擃郭????頠?, group=gPos,
tooltip="??instVol > ?瑟?銝凋??蜆 ???怠? L2/L3 ?Ⅳ?身閮?隤迎?頞典?怎垢擃郭??蝣潭 Calmar 畾箸?嚗誑??????誨鈭??斗?? walk-forward 撽???)
brakeLen = input.int(100, "瘜W?銝凋??詨???, minval=50, maxval=300, group=gPos,
tooltip="閮? instVol ?瑟?銝凋??貊?閬?嚗??箏???頠? vol floor ?箸???賊?瘜W???鈭西??冽迨閬?雿?atrPct ?曉?雿妝???摨??嗆憓?input)??)
brakeK = input.float(1.8, "??? k", minval=1.2, maxval=3.0, step=0.1, group=gPos,
tooltip="instVol 擃銝凋??豢迨????蝣潦?撠?靽????刻郎蝷甄瑕暺?撘望扼迨?澆??撽????塚?(1)??頠?瑼鳴?(2)瘜W??芷????銝? clamp(relVol,1,brakeK)嚗?3)瘜W??芷?蜓 trail 銝?嚗?4)瘜W??芷?脣?瑕銝?????賊?瘜W???敺?瘜W?銝?trail ?寧 atrPct ?曉?雿????血? brakeK嚗? brakeK ?梢???璈??銝??嗚alk-forward ?? brakeK ?????摨行???簣2 step 閫撖?????)
// ?? ?箏嚗atchet Chandelier(high/low) + ?典?甇Y? + ?T摨? + ???? ?????????
gExit = "?箏 Exit"
useChandelier= input.bool(true, "? Chandelier 蝘餃???", group=gExit,
tooltip="隞?ta.highest(high)-mult*ATR嚗征?桅??銝?ratchet ?芷脖??嚗?雿◤?格?瑕蔣蝺?蝒????????芷???ST 摨??箏(靘?ablation)??)
chandLen = input.int(22, "Chandelier ATR ?望?", minval=14, maxval=30, group=gExit,
tooltip="蝘餃??????ATR ?望????脣 R ????璇?ATR嚗Ⅱ靽??桅◢?芸偕摨西? R ?0撠箏漲銝?氬?)
chandMult = input.float(3.0, "Chandelier ATR ?(??/R ??)", minval=2.0, maxval=4.0, step=0.5, group=gExit,
tooltip="蝘餃???頝 = mult?ATR嚗????粹脣??銋?憪迫???U皞?雿?R=1 = 1 ??撖血?憪◢?芥???R ?0???嗥?? R-multiple ?摩??綽?????sizing 銝?氬?)
useRStep = input.bool(true, "? R ?0?嗥?", group=gExit,
tooltip="靘筑??R ????嗥??嚗<1 撖?=??mult)??~2 銝准?2 蝺???曉之?喳偏??瑕椰撠?閮剛??牧)?????箏? chandMult?eak-R ratchet ?冽迨?X銝?銋???????唳嚗郭??拇?銝?trail 撠葉/蝺??郭?凝隤踴?)
trailMidR = input.float(2.0, "銝剜??(1~2R)", minval=1.5, maxval=3.0, step=0.5, group=gExit,
tooltip="瘚桃? 1~2R ?? Chandelier ??? ??chandMult?郭??拇?銝?trail ??迨瑼???relVol(??atrPct ?曉?雿???[trailTightR, chandMult] ??詻?)
trailTightR = input.float(1.5, "蝺??(>2R)", minval=1.0, maxval=2.5, step=0.5, group=gExit,
tooltip="瘚桃? >2R ?蝺? Chandelier ??? ??銝剜??嚗雁?隤踵蝺郭??拇?銝?trail ??迨瑼??冽郭???擃郭?撖研?瘜W??嗥?)嚗漲?箸???游漲銵啁垠?蝺??蝺?踴?)
useBE = input.bool(true, "?靽(break-even)", group=gExit,
tooltip="瘚桃???kBE?R 敺???銝宏?單??砌???multBE?R嚗???蝥祥+皛???R 閫貊暺???甇X?頝銝?氬?)
kBE = input.float(1.0, "靽閫貊 R", minval=0.5, maxval=2.0, step=0.1, group=gExit,
tooltip="瘚桃??迨 R ???靽??)
multBE = input.float(0.3, "靽蝺抵? R", minval=0.1, maxval=0.6, step=0.1, group=gExit,
tooltip="靽??蝵格???+/- 甇?R ?嚗? ??.1 隞亥?????皛?????頛郎蝷甄瑕暺?撘望扼迨?澆??撽?銝??塚?(1)BE 蝺抵?嚗?2)peakR ????givebackR/effGiveback 撜啣澆???嚗?3)TP ?0?唳瘥挾?砍???啣? input 雿血?摨阡??酉嚗歇撖衣?脣?典?踹???bankedQty/openInitQty 瘣曄???蝣?multBE嚗誑?餌??雿?鞎嚗blation ???漲皜祈岫??瘜冽?甇文暺?撘望扼?)
// ?? ?挾 Partial TP嚗?澆擗雿移蝣箄鋡???????????????????????????????????????
usePartialTP= input.bool(true, "??挾?典?甇Y?(R ?0?質?)", group=gExit,
tooltip="R?另p1R 撟喟蜇??tp1Pct??另p2R ?像蝮賢?tp2Pct嚗ne-shot ??閫貊銝甈~身閮?隤迎?銝餃??芣?之鞈箏???像?撠整像皛??誑 posLive(???嗅??拚??其?)?瘥???qty 蝎曄Ⅱ?質?嚗P1 敺?posLive ?喟葬撠?TP2 ??閫貊?芸?隞亦葬撠??拚??其?閮??畾萇敞閮祕?鋡?< tp1Pct+tp2Pct ???debug/?銵冽憿舐內?敞閮鋡? vs 摰?迂%??撠董嚗偶銝??像?敹?L1??)
tp1R = input.float(1.0, "TP1 閫貊 R", minval=0.5, maxval=2.0, step=0.1, group=gExit,
tooltip="瘚桃??迨 R ?撟單?蝮賢?tp1Pct??R = 1 ??撖血?憪迫???U孛?澆??郊???砌??砍?踴?)
tp1Pct = input.float(25.0, "TP1 撟喳?靘?%嚗蜇??", minval=10.0, maxval=50.0, step=5.0, group=gExit,
tooltip="TP1 閫貊?像??擗雿?posLive??甇斤??(?銝 id)嚗鋡??押?)
tp2R = input.float(2.0, "TP2 閫貊 R", minval=1.5, maxval=4.0, step=0.5, group=gExit,
tooltip="瘚桃??迨 R ??像蝮賢?tp2Pct?? > tp1R??)
tp2Pct = input.float(25.0, "TP2 撟喳?靘?%嚗蜇??", minval=10.0, maxval=50.0, step=5.0, group=gExit,
tooltip="TP2 閫貊??撟喋P1 ?質?敺葬撠??拚??其? posLive?迨?曉?瘥?靽??詨????喳偏?p1+tp2 撱箄降 ??0% 隞乩??隅?X敹?)
useBEonTP1 = input.bool(true, "TP1 ?質??喲?靽", group=gExit,
tooltip="銝?西鋡?1R(TP1)嚗?擗敹??撥?????(憭?/???(蝛??身閮?隤迎?撌脣祕??1R 敺敹???霈?嚗?瑯?頧?椰撠整???Calmar????BE 璈嚗?啣? input嚗???蝝?trail??)
useTPstairBE= input.bool(true, "TP ?0撘??砍??, group=gExit,
tooltip="BE-on-TP1 ?芷??桐?瑼??+multBE?R)嚗P2 ?質?敺??擃?踴璈霈??砍?輸?質?畾菜?0銝嚗P1 敺??+1?multBE?rDenom?P2 敺??+2?multBE?rDenom(蝛箏?∪?)嚗? peakR ????唳 math.max ??蝺身閮?隤迎?瘥祕?曆?畾萄瞏歹??詨???銝?霈瞏文?蝺??株矽銝宏????multBE ??tp1Done/tp2Done嚗?啣? input嚗?????瑼?BE?祕????璅?effTPstair = useTPstairBE and not usePureA嚗sePureA ?撥?園??誑靽嗾瘛典蝺飛??)
usePeakRatchet= input.bool(false, "? Peak-R ?????身?? ablation嚗?, group=gExit,
tooltip="蝬剛風 peakR(? high-water R)嚗??詨?????????唳嚗railStop ??rBasePx+(peakR?ffGiveback)?rDenom(蝛箏?∪?)?身閮?隤迎???R ?0?X銝??嗥????粹?? peak-anchored ?嗥?嚗???芣憭扯??桀??撠整?閮剝?(?踹???箇?)???? effGiveback ?身?望郭??拇?隤踵?蒂?舐頞典撟湧翩?蝺????撟湧翩??摰?multBE?sePureA=true ?撥?園???)
useBreathGiveback= input.bool(true, "瘜W??芷????", group=gExit,
tooltip="????givebackR=multBE ??箏??潘?擃郭??regime 甇?虜??撟漲?砍停?游之嚗摰??????憭芰???亙熒憭批????◤??瑕撠整璈霈????函撠郭??賂?effGiveback=multBE?clamp(relVol,1.0,brakeK)?身閮?隤迎?擃?relVol 摰孵??游之??(?唳?曉祝?踹???)?? relVol(瘜W??嗆?頞典撟喲?)???嗥??憭瞏扎???relVol/brakeK/multBE嚗?啣? input嚗?????摰?givebackR=multBE?? usePeakRatchet=true ????effBreathGB=useBreathGiveback and not usePureA??)
useStallRatchet= input.bool(false, "頞典撟湧翩????唳嚗?閮剝?嚗?, group=gExit,
tooltip="閮剛??牧嚗eakR ?????芰?瘜W?嚗???銝活?瑟撜啣澆歇??撠???畾菔隅?W?萄 high-water R 敺?瑟?銝??瑟撜啣?barsSincePeak 憓之)嚗?賣?銵啁垠/??斗閮?嚗?銝餃??嗥????唳???嚗??瑟撜啣??隞)?策?游??澆?璈霈????典陸?澆?皛舀??貊??扳蝺?effGiveback ?? stallShrink=clamp(1?arsSincePeak/timeStopBars, stallShrinkMin, 1.0)嚗?皛航?銋???撠??唳頞?)??憭??stallShrinkMin ??瘜W?/撌脣祕?曄??蝬剖漲甇?漱???冽??peakR ?瑟鈭辣 + timeStopBars + multBE嚗???stallShrinkMin ?箸憓隤?input?ffStallRatchet=useStallRatchet and not usePureA嚗? usePeakRatchet=true ?????閮剛??牧嚗? walk-forward 撽???)
stallShrinkMin= input.float(0.5, "撟湧翩??銝? stallShrinkMin[?啣??芰摨因", minval=0.2, maxval=0.9, step=0.1, group=gExit,
tooltip="???箇?撘Ⅳ?折撣豢 0.5嚗??箏隤?input 隞乩噶 walk-forward ??/ablation?隅?W僑朣∪?皛舀蝺???????stallShrink=clamp(1?arsSincePeak/timeStopBars, 甇文? 1.0)??撠??趙銋????嗅?頞?(?唳頞???敹恍??拐?銋??◤甇?虜???)?惇??蝛粹?蝬剖漲嚗?閮?葫?芰摨艾? useStallRatchet=true ????)
useBreathTrail= input.bool(false, "瘜W??芷?蜓 trail嚗?閮剝?嚗?, group=gExit,
tooltip="R ?0 multStep 銝?蝺???箏??潘?撠郭??regime ?脩?璈撠葉/蝺?(??憪祝瑼??誑瘜W???拇?敺株矽??瘜W?(頞典撟喲?)?渡?璆菜蝺蜓 trail ???瘜W?(??曉之)?曉祝?踹??亙熒??鋡急??祕雿?effMultStep=multStepBase??澆靽嚗lamp ??[trailTightR, chandMult] 蝬剜??株矽嚗???R>=1(撌脤??憪祝瑼?????訾??賊?閮剛???clamp(relVol,1,brakeK)嚗???餌?郭??閬??寧 atrPct ?曉?雿??breathPctl(銝血? brakeK)????relVol/brakeK ??atrPct嚗?啣? input?身閮?隤迎?雿??Chandelier 銝?trail(?? peak ?唳)嚗????芷?peak-ratchet 雿歇??1~2R??銝剜挾?喳偏?ffBreathTrail=useBreathTrail and not usePureA??)
useDecorrVolGauge= input.bool(false, "?餌?郭??閬?ATR% ?曉?雿?嚗?閮剝?嚗?, group=gExit,
tooltip="relVol(蝬?brakeK clamp)?券???璈敺??箏暺?撘望扼璈??銝璇?靘陷 relVol/brakeK??甇?漱瘜W???嚗 atrPct ?刻? brakeLen 璉??曉?雿妝(ta.percentrank)????[pctlGaugeLo, pctlGaugeHi] ??訾???breathPctl嚗??箸郭?蜓 trail ?隞???身閮?隤迎?percentrank ?胯?雿?蝵柴????撳?瑼颯?撠?撠?頝喟征?渡帘?乓??銝銝凋??詨皞? brakeK ???血?瘙⊥?????瘜W?銝?trail ?寧 breathPctl 撽?嚗蝙?嗉???頠???/?瑕?餌????brakeK 敺???璈??銝??嗚???atrPct/brakeLen嚗?撠???pctlGaugeLo/Hi ?箸憓隤?input?ffDecorrGauge=useDecorrVolGauge and not usePureA嚗?瘜W?銝?trail ??蔣?踹?澆靘???)
pctlGaugeLo = input.float(1.0, "?曉?雿?閬???pctlGaugeLo[?啣??芰摨因", minval=0.5, maxval=1.5, step=0.1, group=gExit,
tooltip="???箇?撘Ⅳ?折撣豢(???????1)嚗??箏隤?input 隞乩噶 walk-forward ??/ablation??賊?????atrPct ?曉?雿妝(0~100%)蝺扳?撠 [pctlGaugeLo, pctlGaugeHi]嚗?? 0% ??pctlGaugeLo??00% ??pctlGaugeHi?? < pctlGaugeHi 隞亦雁?隤??亙?蝵桃?撘??芸? swap ?脣?)?惇??蝛粹?蝬剖漲嚗? useDecorrVolGauge=true ????)
pctlGaugeHi = input.float(2.0, "?曉?雿?閬???pctlGaugeHi[?啣??芰摨因", minval=1.5, maxval=3.0, step=0.1, group=gExit,
tooltip="???箇?撘Ⅳ?折撣豢(???????2)嚗??箏隤?input 隞乩噶 walk-forward ??/ablation?? pctlGaugeLo???曉?雿?擃郭????撠甇支????曉祝銝?trail?惇??蝛粹?蝬剖漲嚗? useDecorrVolGauge=true ????)
useCoherenceTrail= input.bool(false, "憭偕摨行???游漲 ratchet嚗?閮剝?嚗?, group=gExit,
tooltip="銝?trail ?芰? R/瘜W?嚗???撠箏漲?孵?蝯??璈 coherence=?? ST ??憭?st1Bull+st2Bull+st3Bull)嚗oherence==3(銝偕摨血??)?曉祝銝?trail 銝瑼?靽 cohRelaxK嚗???chandMult)?踹??撘瑁隅?Z◤蝺?trail ??oherence<=1(敹思葉撠箏漲撌脰??X敹??嗅?蝺?(trailTightR)????身閮?隤迎?銝? ST ??1:2.5:6 瘣曄?閬? noise/swing/regime嚗???舀?蝵?????撠曄???蝯行?憭批?賂?敹怠偕摨血???航隅?Z※蝡剜??拇迤鈭方????????瘜W?銝?trail 甇?漱嚗???math.min ??蝺?????st1/2/3 ?孵?嚗撖砍 cohRelaxK ?箸憓隤?input?ffCoherenceTrail=useCoherenceTrail and not usePureA??靘陷銝偕摨血??W漲嚗? ST ?梁?????? midMult/slowMult ??摨行???)
cohRelaxK = input.float(1.15, "銝?游漲?曉祝? cohRelaxK[?啣??芰摨因", minval=1.0, maxval=1.5, step=0.05, group=gExit,
tooltip="???箇?撘Ⅳ?折撣豢 1.15嚗??箏隤?input 隞乩噶 walk-forward ??/ablation?oherence==3(銝偕摨血??)?蜓 trail ? ?甇文?銝? chandMult)隞亦策?撘瑁隅?X憭?詻?憭扳敺?撖?頞??◤?雿???憭??惇??蝛粹?蝬剖漲嚗? useCoherenceTrail=true ????)
useBankedFloor= input.bool(false, "撌脣祕?曄?拚靽?唳嚗?閮剝?嚗?, group=gExit,
tooltip="TP ?0?唳隞 multBE 撽?(?? multBE ?桅??摹???璈?寧??蝣?multBE???餌??選?bankFrac=bankedQty/openInitQty(撌脰鋡???蝮賢??格?靘?嚗rofitFloorL=rBasePx+bankFrac?rDenom(蝛粹??嚗? BE/peak/?0?唳 math.max/min ??蝺身閮?隤迎?瘥祕?曆?畾萄歇撖衣?脣嚗敹??舫霈??拙?蝺??株矽銝宏銝迤瘥撌脰鋡?靘? peakR(?撖衣擃偌雿?甇?漱嚗摰歇撖衣?暸?瘚???bankedQty/openInitQty嚗?啣? input?ffBankedFloor=useBankedFloor and not usePureA????posLive 撘瑁血?嚗ankedQty 撠董甇?Ⅱ?臬?踵迤蝣箏???)
usePartialST= input.bool(true, "? ST ???挾皜?cut)", group=gExit,
tooltip="ST1/ST2 ?????畾菔鋡?靽??詨????喳偏?? Partial TP 鈭?嚗P 蝞∠?抵鋡ut 蝞∟隅?Z?撘望?蝣潦ut 銝敺?top-first ???怠惜嚗??????詨?嚗?蝯撅扎?)
useRegimeFade= input.bool(false, "? Regime-fade ?詨??駁◢?迎??身?? ablation嚗?, group=gExit,
tooltip="?葉 er < erThr?fadeK ?? cutTop ???怠?蝣澆惜????tranche(銝??詨? L1嚗? contiguous-stack + fresh 瘣?stage 靽?)?身閮?隤迎?regime gate ?芰恣?脣嚗??葉 ER 撏抵??詨??????鋡怠??箏?迨?粹脣靽∪艙?Ⅳ?典?游??e-arm ?寧摰?脫趙(er>=erThr)+?撠????賊??風甈????瑼駁?餈??????隤祕?剝嚗璈?憓?2 ?隤?input fadeK ??fadeCoolBars??銝阡??嗆憓?勗漲?sePureA=true ?撥?園???)
fadeK = input.float(0.6, "Regime-fade 閫貊瘥?(?erThr)[?啣??芰摨因", minval=0.3, maxval=0.9, step=0.1, group=gExit,
tooltip="?葉 er < erThr?fadeK 閬靽∪艙瘨?憸券??.6 ??ER 頝?瑼?60% ??雿??踹?甇?虜??隤方孛????憓??航矽 input??仿???regime-fade 閫貊?曉潘?撅祆?撠征?雁摨佗?walk-forward ????摨行葫閰佗???erThr ?血?嚗? useRegimeFade=true ????)
fadeCoolBars= input.int(5, "Regime-fade ?撠???[?啣??芰摨因", minval=1, maxval=20, group=gExit,
tooltip="?拇活 regime-fade ?惜銋??喳???甇斗??賂??? re-arm ? er>=erThr ???湧皛臬葆嚗??甇?ER ?券?瑼駁?餈?????????游?蝣澆?????憓??航矽 input?惇??蝛粹?蝬剖漲???乩蒂???漲皜祈岫?? 璉蝛拙韏琿??? useRegimeFade=true ????)
useBelowAvg = input.bool(false, "? belowAvg ?怠惜皜Ⅳ嚗?閮剝?嚗?挾嚗?, group=gExit,
tooltip="????stage?? 銝?close 頝?-蝺抵??喋撟喋??甇?虜????頞典?詨?? Calmar???箏?皜??思?撅扎? close_all嚗??身??靘??扼?)
kBuf = input.float(0.5, "belowAvg 蝺抵? / ?脣?孵榆??ATR)", minval=0.3, maxval=1.0, step=0.1, group=gExit,
tooltip="belowAvg 皜Ⅳ閫貊蝺抵?嚗誑 chandLen ATR 閮? useBelowAvg=true ???EW-A2 ?脣???孵榆蝣箄??漲銴甇文潔?????close ??st1 蝧餉??寞?憭批捆閮梯??U?|close?t1|?女Buf?atrChand)嚗?啣? input??)
useTimeStop = input.bool(true, "?????(甇餃???", group=gExit,
tooltip="??頞? N 璉? R<1 銝皛蹂?畾蛛?閬甇餃??湛???湔?瘝????)
timeStopBars= input.int(30, "????璉", minval=15, maxval=60, group=gExit,
tooltip="甇餃??渡??憭扳????詻????脣?嚗??撖祉?????? timeframe 瘜冽?嚗誑璉閮?頛??望???皜??∠?撘??雿輻?痊隞??隅?W僑朣∪???蹂漲銴甇文潔?撜啣澆?皛舀蝺?甇???偕摨?barsSincePeak/timeStopBars)嚗?啣? input??)
useAdaptiveTS= input.bool(false, "ER ?芷??time-stop嚗?閮剝?靘?ablation嚗?, group=gExit,
tooltip="?摰?timeStopBars ?寧 round(timeStopBars ? clamp(er/erThr,0.5,1.0))嚗R 頞?(?斗)甇餃??渲?敹怒R 頞?(撘瑁隅?W摨瑕?瑼?蝯西?憭??身閮?隤迎?撘瑁隅?Z絲甇亦?渲?撘梯隅?W?甇餅???閰脩??璉?瑼颯???er/erThr/timeStopBars嚗?啣? input?sePureA=true ?撥?園???)
useHardStop = input.bool(true, "?鈭斗??蝝??????脰歲蝛?", group=gExit,
tooltip="撠?Chandelier ??銝?撖?stop ?桃銝剛孛?潘??脰歲蝛?敹怠???頞?ATR 皛??????芣晾+鈭??0 ?銝剜?鈭文皜砌?敺?朣?璉?甇???? live stop = 銝?璉?committed ??trailStop(trailStop[1])?????鈭文皞??湛??菜葫?啗孛?閬?詨?撌脣?像???單?祆??剛楝???????園??嗥蝣箄??箏(?湔鈭)??銝???砍惜?葉撣喳??蔭(?踹??嗉???撟賡??其?)嚗?像??銝?璉?禮6 ?芰摰?蔭嚗?霅?stageLive==opentrades 璉????)
cooldownBars= input.int(3, "?箏敺?餅???, minval=0, maxval=10, group=gExit,
tooltip="?典像敺?敺?N 璉??迂?圈??殷??踹??箏璉??喳??蕭?桅???? whipsaw??蝔勗?詨??脣?瑕????effCooldown=round(?砍撳lamp(relVol,1,brakeK))嚗?瘜W? whipsaw ??餅銋?瘜W???敹恍??脯?)
useAdaptiveCooldown= input.bool(false, "撠迂?澆撘脣?瑕嚗?閮剝?嚗?, group=gExit,
tooltip="?箏? cooldownBars 撠郭??柴璈 effCooldown=round(cooldownBars?clamp(relVol,1.0,brakeK))嚗撟喳? cooldownUntil:=bar_index+effCooldown?身閮?隤迎?擃郭??whipsaw ??游??瑕?港??踹?餈賢??瘜W???敹恍??莎???ER ?芷??time-stop 撠迂????cooldownBars/relVol/brakeK嚗?啣? input?ffAdaptiveCooldown=useAdaptiveCooldown and not usePureA??)
// ?? ?豢韐振璈嚗1~A3 鈭斗?銵嚗?啣??航矽 input??閮剝?? not usePureA??蝛粹????????
gAbsorb = "?豢韐振 Absorb-A"
useMultiScaleStop= input.bool(false, "NEW-A1 頝典偕摨?ATR ?折?甇X?嚗?閮剝?嚗?, group=gAbsorb,
tooltip="閮剛??牧嚗handelier trail ??R ?0銝敺?桐? chandLen ??atrChand ?嗚迫??撠偕摨血皞???郭??拇?(breathPctl/breathClamp/coherence)?芾矽?氬撠?蝺 multStep???芣霈迫???TR 撠箏漲?祈澈?敹???W偕摨西隅??ST3)嚗頞典甇?虜?澆撟漲?之?澆翰撠箏漲 ATR嚗 low-vol pocket ?典翰 ATR 閮?trail ???詨???亙熒??銝剜???頞典蝑 well-known premature-stopout 憭望?璅∪?)?璈??典歇 BE 敺??詨???beActive)?? trail ??ATR 撠箏漲?寧 atrScale=max(atrChand, |close?t3|)?誑?W偕摨?ST3 band 撖砍漲雿?瑼◢?芣?撠偕摨血?選?雿踵敹?貊征?摰撖阡??撅祆??偕摨艾?曉之 rawStop(撠箏漲?唳)?atchet 隞?霅?trailStop ?芷脖??嚗??游之?? R(beActive ??雿)???餌??閬?breathPctl(??雿?蝵?甇?漱嚗璈?楊撠箏漲蝯?瘜W?撠箏漲????st3/atrChand ?嗆憓?input??蝛粹?ffMultiScaleStop=useMultiScaleStop and not usePureA嚗? useChandelier=true ?蔣??trail???閮剛??牧嚗? walk-forward 撽???)
useEntryDistGate= input.bool(false, "NEW-A2 ?脣???孵榆蝣箄????身??", group=gAbsorb,
tooltip="閮剛??牧嚗??桀閬?st1FlipUp + regime gate ?喲莎?銝炎?乓蝧餉??寡?憭??隅?Y??亦?撖?edge ?刻??????憭改??Z????脣?嗅?憪?R 憸券撌脰◤????祇◢?芣??∪??撠暹??????Z??????桀撖祆迫??????? R ???釭???瘙???L1/S1 ?脣璉? |close?t1| ??kBuf?atrChand(銴?X? kBuf ????啣? input)嚗?具?蝧餉????芣銵??脣嚗?瞈?st1 蝧餉?敺歇韏唳??K ?◤閫貊(靘??文? gap ??regime gate 撱園?曇?)?蕭擃?a 摰?嚗t1/atrChand 隞颱? na ??atrChand<=0 ???餅?(???銵)嚗??璈?隤日??? regime gate(?隅?W?鞈?甇?漱嚗???脣暺頧??撌格??研雿擐(銝蔣?踹?蝣???蝛箏?潮?ffEntryDistGate=useEntryDistGate and not usePureA???閮剛??牧嚗? walk-forward 撽???)
useCoherenceAdd= input.bool(false, "NEW-A3 銝?游漲?Ⅳ靽∪艙???身??", group=gAbsorb,
tooltip="閮剛??牧嚗?蝣潭?隞?addOK=not volBrakeOn and R>0 銝炎?乩?撠箏漲?孵?蝯???蝣潭?典歇?筑??頞典銝????迎??芣??嗅?撠箏漲?孵?蝯?隞?隤?coherence>=2)??蝣潛?璇辣???甇???亙翰撠箏漲撌脰???coherence<=1)???Ⅳ嚗??澆頞典銵啁垠暺憭扳???Calmar 畾箸?)??? 禮11 ?Ⅳ addOK(撌脫??箸??撅?addOKLong/addOKShort)??AND (coherence>=2)嚗???coherenceLong?征??coherenceShort?? ER 靽∪艙??(convMult 隤踹?蝣?size)甇?漱嚗?矽?Ⅳ??go/no-go gate??隤?size???冽??coherenceLong/Short ?嗆憓?input??蝛箏?潮?ffCoherenceAdd=useCoherenceAdd and not usePureA???閮剛??牧嚗? walk-forward 撽???)
// ?? ??頞?堆?韐振瘝?嚗?啣??航矽 input??閮剝?? not usePureA??蝛粹????????????
gNew = "??頞??Ascend-New"
useErAccelAdd= input.bool(false, "?EW-C1 ER ??漲?Ⅳ???身??", group=gNew,
tooltip="閮剛??牧嚗?蝣潮?瑼?addOK ??A3 銝?游漲????胯偌皞?蝯????onvMult ??ER 蝯?瘞湔?(隤?size)?3 ??撠箏漲?孵?蝯?(coherence)??銝??ER 隞?>?瑼颯?撠箏漲隞??撌脤???豢?銝??隅?g?甇???潦??賢??漲?箄??? late-trend 銵唳?畾蛛?甇斗??Ⅳ?臬?????隅?W偏畾萇??(Calmar 畾箸?)????Ⅳ addOK ??AND (er >= er[1])嚗?瘙??????榆???箄?(??芸?∪?)???曇? L2/L3???榆???漲)?偌皞?蝯??迤鈭歹?擃偌皞???典???皜?撠箏漲??鈭血?? ER 銝??????冽??er(禮5) 銋?[1] 甇瑕嚗?啣? input???蝣?erThr 隞亙?賊???erThr ?桅??摹?扼R ?箇?孵?蝝???憭征?梁??璇?er嚗??祇?撠?蝛箏予?園?ffErAccelAdd=useErAccelAdd and not usePureA嚗? A3(蝯?)/convMult(size) 銝迤鈭歹??祇?隤?go/no-go ???賣?雁摨艾??閮剛??牧嚗? walk-forward 撽???)
useMidConfirm= input.bool(false, "?EW-C2 銝剖偕摨阡脣蝣箄????身??", group=gNew,
tooltip="閮剛??牧嚗egime gate 銝巨??ER/ADX/ST3嚗??瑽?乩??撠箏漲 ST3??敹怠偕摨衣蕃頧?ST1嚗蝻箔葉撠箏漲 ST2(swing 撠箏漲)???ST1 ?蕃憭T3 撌脣??? ST2 隞征(swing ?◢)?冗敹擐??鋡急銵 1:2.5:6 銝偕摨衣?瑽葉???whipsaw ?脣??(敹怠偕摨血?喟蕃頧撠箏漲皛臬??葉撠箏漲? swing ?????? coherence ????Ⅳ?冽??宏?啜??桅脣?釭??閬?擐 L1/S1 ?脣?嗡?銝剖偕摨血???憭?st2Bull / 蝛?st2Bear)嚗??寡?瘙脣??撠箏漲?典???coherence==3 ?脣??嚗?銝?regime gate ?葉撠箏漲?孵??脤??? A2(?頧??孵榆?)甇?漱嚗???葉撠箏漲?孵??臬蝣箄????冽??st2Bull/st2Bear 蝝??瑽??嗆憓?input?雿擐(銝蔣?踹?蝣???蝛箏?潮?ffMidConfirm=useMidConfirm and not usePureA?酉嚗???湔???鈭斗??賂??誑 usePureA 蝝蝺???鞎Y撠?閬????曇????閮剛??牧嚗? walk-forward 撽???)
// ?? 頝臬?? sizing 瘛瑕?嚗??折撣豢嚗??脩?航矽 input嚗??????????????????????????
gSize = "Sizing 頝臬??"
eqBlendInit = input.float(0.5, "sizing ?箸?繚??鞈甈? eqBlendInit[?啣??芰摨因", minval=0.0, maxval=1.0, step=0.1, group=gSize,
tooltip="???箇?撘Ⅳ?折撣豢 0.5嚗??箏隤?input?izing ?箸? = eqBlendInit???鞈 + (1?qBlendInit)?撟單? equity嚗隞仿?雿??抵楝敺憭扼?.0=摰?典?憪????∟??抵楝敺?鞈???.0=摰?典像皛?equity?惇??蝛粹?蝬剖漲嚗?閮?葫?芰摨艾?)
eqBlendSmooth= input.float(0.99, "撟單? equity繚??找???eqBlendSmooth[?啣??芰摨因", minval=0.80, maxval=0.999, step=0.001, group=gSize,
tooltip="???箇?撘Ⅳ?折撣豢 0.99嚗??箏隤?input?像皛?equity ??EMA ??改?avgEq := eqBlendSmooth?avgEq + (1?qBlendSmooth)?equity???亥? 1 頞像皛?頞票餈?憪???撠?鞎潸??單? equity?惇??蝛粹?蝬剖漲嚗?閮?葫?芰摨艾?)
// ?? 閬死????????????????????????????????????????????????????????????????????
gViz = "閬死??
showST = input.bool(true, "憿舐內銝? SuperTrend", group=gViz)
showAvg = input.bool(true, "憿舐內?蝺?, group=gViz)
showStop = input.bool(true, "憿舐內蝘餃???蝺?, group=gViz)
showSignals = input.bool(true, "憿舐內?脣?湔?閮?, group=gViz)
showBg = input.bool(true, "憿舐內 Regime / ?挾???, group=gViz)
showTable = input.bool(true, "憿舐內?喃?閫???銵冽", group=gViz)
showDebug = input.bool(false, "憿舐內?日甈?votes/floor/cap/TP 撠董/peakR/?質?%/?澆/coherence/?瑕/?曉?雿??趙/頝典偕摨??脣??ER ?漲)", group=gViz)
// ?? 瘥?????蝝蝺?撘瑕?????脤?璈 ????????????????????????????????
effPeakRatchet = usePeakRatchet and not usePureA
effRegimeFade = useRegimeFade and not usePureA
effAdaptiveTS = useAdaptiveTS and not usePureA
effMTFgate = useMTFgate and not usePureA
effBreathGB = useBreathGiveback and not usePureA // 瘜W??芷????
effTPstair = useTPstairBE and not usePureA // TP ?0?唳
effBreathTrail = useBreathTrail and not usePureA // 瘜W??芷?蜓 trail
effCoherenceTrail = useCoherenceTrail and not usePureA // 憭偕摨行???游漲 ratchet
effBankedFloor = useBankedFloor and not usePureA // 撌脣祕?曄?拚靽?唳
effAdaptiveCooldown= useAdaptiveCooldown and not usePureA // 撠迂?澆撘脣?瑕
effDecorrGauge = useDecorrVolGauge and not usePureA // ?餌?郭??閬?ATR% ?曉?雿?
effStallRatchet = useStallRatchet and not usePureA // 頞典撟湧翩????唳
effMultiScaleStop = useMultiScaleStop and not usePureA // NEW-A1 頝典偕摨?ATR ?折?甇X?
effEntryDistGate = useEntryDistGate and not usePureA // NEW-A2 ?脣???孵榆蝣箄???effCoherenceAdd = useCoherenceAdd and not usePureA // NEW-A3 銝?游漲?Ⅳ靽∪艙??effErAccelAdd = useErAccelAdd and not usePureA // ?EW-C1 ER ??漲?Ⅳ??effMidConfirm = useMidConfirm and not usePureA // ?EW-C2 銝剖偕摨阡脣蝣箄???
// ?曉?雿?閬?撠?????乩蝙?刻? Lo/Hi ?蔭嚗??swap 蝬剜??株矽
pctlLo = math.min(pctlGaugeLo, pctlGaugeHi)
pctlHi = math.max(pctlGaugeLo, pctlGaugeHi)
// ????????????????????????????????????????????????????????????????????????????
// 禮2 銝惜 SuperTrend嚗銝?箇??望?瘣曄?嚗a.supertrend ? [value,direction]嚗?// direction < 0 ?箏???// ????????????????????????????????????????????????????????????????????????????
len1 = baseLen
len2 = math.max(int(math.round(baseLen * midMult)), 2)
len3 = math.max(int(math.round(baseLen * slowMult)), 3)
[st1, dir1] = ta.supertrend(f1, len1)
[st2, dir2] = ta.supertrend(f2, len2)
[st3, dir3] = ta.supertrend(f3, len3)
st1Bull = dir1 < 0
st2Bull = dir2 < 0
st3Bull = dir3 < 0
st1Bear = not st1Bull
st2Bear = not st2Bull
st3Bear = not st3Bull
// 蝧餉?鈭辣嚗??格?暺???
st1FlipUp = st1Bull and not st1Bull[1]
st2FlipUp = st2Bull and not st2Bull[1]
st3FlipUp = st3Bull and not st3Bull[1]
st1FlipDn = st1Bear and not st1Bear[1]
st2FlipDn = st2Bear and not st2Bear[1]
st3FlipDn = st3Bear and not st3Bear[1]
// ST3 蝧餃?敺?瘣餅??賂??Y?瑽??漲嚗?璉閮? request.security ?鼓憸券嚗?var int st3BullBars = 0
var int st3BearBars = 0
st3BullBars := st3Bull ? nz(st3BullBars[1]) + 1 : 0
st3BearBars := st3Bear ? nz(st3BearBars[1]) + 1 : 0
// persistBars ?望??len3 瘣曄?嚗?baseLen?slowMult嚗? clamp ??[3, mtfCapBars]嚗?// ??撖行?莎?mtfCapBars ?箝憓??航矽 input???桃??脣?撣豢??潛?交捱摰?MTF 擐
// ?曇??瑼?persistBars嚗惇??蝛粹?蝬剖漲嚗?閮?祕?蝛粹?銝行 walk-forward ???漲皜祈岫??persistBars = math.min(math.max(len3, 3), mtfCapBars)
mtfBullOK = not effMTFgate or (st3Bull and st3BullBars >= persistBars)
mtfBearOK = not effMTFgate or (st3Bear and st3BearBars >= persistBars)
// 憭偕摨行???游漲嚗???ST ?賂?憭征??嚗?啣? input???孵?蝯?嚗?coherenceLong = (st1Bull?1:0) + (st2Bull?1:0) + (st3Bull?1:0)
coherenceShort = (st1Bear?1:0) + (st2Bear?1:0) + (st3Bear?1:0)
// ????????????????????????????????????????????????????????????????????????????
// 禮3 ATR% 甇???皞???????瑼颱?敺撠?嚗?頝典???蝝情??
// ????????????????????????????????????????????????????????????????????????????
atrAdx = ta.atr(adxLen)
atrChand = ta.atr(chandLen)
atrPct = atrAdx / close // ATR%嚗撠??箸?嚗?
// ????????????????????????????????????????????????????????????????????????????
// 禮4 瘜W??摯閮?撟游??撠?嚗? VT-Core ??
// instVolAnn嚗僑??郭???冽??甇????relVol嚗撠葉雿嚗?澆???頠?+
// 瘜W??? + 瘜W?銝?trail + 瘜W??瑕
// breathPctl嚗trPct ?曉?雿妝?澆靽嚗??箸郭?蜓 trail ??賊?(銝1 brakeK)?蹂誨撽?
// ????????????????????????????????????????????????????????????????????????????
logRet = math.log(close / close[1])
stdevDaily = ta.stdev(logRet, volLen)
volAnnATR = atrPct * math.sqrt(barsPerYear) // ATR% 撟游?
volAnnStd = stdevDaily * math.sqrt(barsPerYear) // ?梢 std 撟游?
instVolAnn = volEstMode == "Stdev" ? volAnnStd : volAnnATR
// ?詨??????頨怨? brakeLen 璉僑?郭??雿??嚗楊???芷???踹?蝯? floor 憭梁?
// fallback 0.02 ?箝僑??2%??蝝?摰絲暺??詨??仃????撽?摰?潘??撣 magic bp嚗?volFloor = ta.percentile_linear_interpolation(instVolAnn, brakeLen, 5)
volFloor := na(volFloor) ? 0.02 : math.max(volFloor, 0.001)
instVolAnn := math.max(nz(instVolAnn, volFloor), volFloor)
// ??頠皞?instVolAnn ?詨??園?葉雿
volMedian = ta.median(instVolAnn, brakeLen)
volMedian := na(volMedian) ? instVolAnn : math.max(volMedian, volFloor)
relVol = instVolAnn / volMedian
volBrakeOn = useVolBrake and relVol > brakeK // true=擃郭???怠??Ⅳ
// 蝯曹??澆靽嚗???relVol/brakeK嚗?瘜W???/瘜W?銝?trail/瘜W??瑕?梁嚗lamp ??[1, brakeK]嚗?breathClamp = math.min(math.max(relVol, 1.0), brakeK)
// ?餌?郭??閬?atrPct ?刻? brakeLen 璉??曉?雿妝(0~100) ???澆靽 [pctlLo, pctlHi]??// 銝?鞈?relVol/brakeK嚗???頠???/?瑕?餌????brakeK ?桅??摹?扼?// percentrank ?箏?雿?蝵????撳?瑼?嚗??偏/頝喟征?渡帘?乓???atrPct/brakeLen嚗?// ?????[pctlLo, pctlHi] ?箸??脩??航矽 input(??勗虜??1/2)??atrPctRank = ta.percentrank(atrPct, brakeLen) // 0~100
breathPctl = pctlLo + nz(atrPctRank, 50.0) / 100.0 * (pctlHi - pctlLo) // [pctlLo, pctlHi]
var int floorHits = 0
if instVolAnn <= volFloor + 1e-12
floorHits += 1
// ????????????????????????????????????????????????????????????????????????????
// 禮5 Regime ??嚗R / ADX ?脫趙 / ST3 ?X??嚗? ?巨?券??脤?甇?// ????????????????????????????????????????????????????????????????????????????
// Kaufman ??瘥??駁?日嚗 math.sum ??math.sum嚗?erChange = math.abs(close - close[erLen])
erVolSum = math.sum(math.abs(close - close[1]), erLen)
er = erVolSum > 0 ? erChange / erVolSum : 0.0
er := nz(er, 0.0)
// ER 銝?榆????漲嚗迤=??瘥?????=?∪?嚗?nz 摰?????// ?EW-C1 ?Ⅳ???剁?er >= er[1] 閬??芣?rSlope 蝝?閫皜????erSlope = er - nz(er[1], er)
// ADX / DI嚗皛舫??曉潘?
[diPlus, diMinus, adx] = ta.dmi(adxLen, adxLen)
var bool adxOn_state = false
if adx > adxOn
adxOn_state := true
else if adx < adxOff
adxOn_state := false
// ST3 ?X??
slowBull = st3Bull
slowBear = st3Bear
// ?巨嚗?蟡函??舐蝡??????巨銝??亙?瘥???嚗?voteER_long = useER and er > erThr
voteADX_long = useADX and adxOn_state
voteSlow_long = useSlowST and slowBull
voteER_short = useER and er > erThr
voteADX_short = useADX and adxOn_state
voteSlow_short= useSlowST and slowBear
votesLong = (voteER_long?1:0) + (voteADX_long?1:0) + (voteSlow_long?1:0)
votesShort = (voteER_short?1:0) + (voteADX_short?1:0) + (voteSlow_short?1:0)
// 撌脣??函巨?賂????瑼?= min(minVotes, ?蟡冽)嚗??蟡典??甇?enabledVotes = (useER?1:0) + (useADX?1:0) + (useSlowST?1:0)
effMinVotes = math.min(minVotes, enabledVotes)
votesUnreachable = minVotes > enabledVotes // ?瑼?> ?舫?蟡冽 ??撌脤??clamp嚗?銵冽?內
// ?孵?蟡剁?憭征?湔?∪?嚗??臭?撌桃嚗?DI ?孵????dirLong = diPlus > diMinus
dirShort = diMinus > diPlus
diLongOK = not useDIgate or dirLong
diShortOK = not useDIgate or dirShort
// ?巨???文?嚗??函巨??0 ??? ST 撠(true)嚗???Z炊???香
voteLongPass = enabledVotes == 0 ? true : votesLong >= effMinVotes
voteShortPass = enabledVotes == 0 ? true : votesShort >= effMinVotes
// MTF ?Y?瑽??漲?蔥?仿???gate嚗ND嚗?閮剝?靘?ablation嚗ffMTFgate 撌脣 usePureA嚗?regimeLongOK = not useRegime or (voteLongPass and diLongOK and mtfBullOK)
regimeShortOK = not useRegime or (voteShortPass and diShortOK and mtfBearOK)
// regime 撘瑕摹嚗??/?銵冽嚗?隞颱??孵???瑼餉???trend-on
regimeOn = (voteLongPass and dirLong) or (voteShortPass and dirShort)
// ????????????????????????????????????????????????????????????????????????????
// 禮6 ???嚗銝?嚗誑?惜?典 bool ?遣 stage嚗?蝯???皜?頝喳惜嚗?// 銝?撘?l3On?2On?1On嚗?嚗? s3On?2On?1On嚗征嚗???禮10 contiguous-stack
// 皜?隤?霅?stage 瘞貊??? 0/1/2/3嚗摮文?/?游惜??// ??[?] 甇方? stage ?箝?擐?reductions 銋?)?潘??? 禮9 ?箏閫貊(?典像頝臬???// reductions 銋?閰摯嚗?摨迤蝣?雿輻??0 皜?????stage-gate(fade/belowAvg/
// 禮11 ?Ⅳ)銝敺?具??? fresh l*On/s*On ?單?????瘣?stage嚗?2/禮14 憿舐內/撠董
// ?寧 禮11 敺?撱箇? stageLive(璉 fresh)??// ????????????????????????????????????????????????????????????????????????????
var bool l1On = false
var bool l2On = false
var bool l3On = false
var bool s1On = false
var bool s2On = false
var bool s3On = false
var int entryBar = na // ?典像?寥?蝵桃 na嚗? 0嚗??? bar_index-0 瘙⊥? timeStop
var float entryPx = na // 擐?嚗 ?箸??典?嚗??典?蝣潭?蝘鳴?
var float entryRisk = na // ???脣??甇X?頝嚗 ??嚗? chandMult?atrChand
var float trailExt = na // 璆萄潛敞蝛?high/low嚗?var float trailStop = na
var float cooldownUntil = 0.0
// Partial TP one-shot ??銝甈⊥改??箏/???蔭嚗?var bool tp1Done = false
var bool tp2Done = false
// ? high-water R嚗eak-anchored ????嚗nline ?蔭甇賊
var float peakR = 0.0
// 銝活?瑟 peakR ??蝝W?嚗隅?W僑朣??趙閮??剁?嚗?啣陸?澆?湔????脣?蔭
var int peakBar = 0
// [撠董] TP 蝝航?撖阡??質????脣蝮賡?嚗??敞閮鋡? vs 摰?迂%??撣?+ 撌脣祕?暸?唳?箸
var float bankedQty = 0.0
var float openInitQty = 0.0
// regime-fade 銝甈⊥扯風甈?+ ?撠??????脫趙嚗?蝯?瑼駁?餈????嚗?var bool fadeArmed = true
var int lastFadeBar = -100000
// ?惜銝?⊥閮董嚗? Partial TP / cut / regime-fade 蝝蝎曄Ⅱ撠董嚗脣撖怠????扎lat 甇賊嚗?var float notL1 = 0.0
var float notL2 = 0.0
var float notL3 = 0.0
var float notS1 = 0.0
var float notS2 = 0.0
var float notS3 = 0.0
inLong = strategy.position_size > 0
inShort = strategy.position_size < 0
inPos = strategy.position_size != 0
posSize = math.abs(strategy.position_size)
avgPx = strategy.position_avg_price
// ?箏蝯?敺??單飛?塚?瘨迨??銝?鞈港?銝璉辣?莎?
// entryBar ?寥?蝵桃 na嚗?a)?撟喳摰?蔭??霈???嚗?b)timeStop 隞?na(entryBar) 摰?嚗?// ?? bar_index-0 撠厭???啁征?炊閮???if not inPos
l1On := false
l2On := false
l3On := false
s1On := false
s2On := false
s3On := false
entryRisk := na
entryPx := na
entryBar := na
trailExt := na
trailStop := na
tp1Done := false
tp2Done := false
peakR := 0.0
peakBar := bar_index // ?趙閮??冽飛???∪?撠??嗅?璉?銝活?脣??撖?
bankedQty := 0.0
openInitQty := 0.0
fadeArmed := true
notL1 := 0.0
notL2 := 0.0
notL3 := 0.0
notS1 := 0.0
notS2 := 0.0
notS3 := 0.0
longStage = (l1On?1:0) + (l2On?1:0) + (l3On?1:0)
shortStage = (s1On?1:0) + (s2On?1:0) + (s3On?1:0)
stage = inLong ? longStage : inShort ? shortStage : 0 // 璉??潘??? 禮9 ?典像閫貊(reductions ??雿輻
// ????????????????????????????????????????????????????????????????????????????
// 禮7 ??閮?嚗T-Core嚗僑?郭?璅? ??? ??蝮賣?獢?clamp ??qty嚗?// qty = (targetVol / instVolAnn) * equity * weight / close
// ?Ⅳ撅斗????clamp(er/erThr,0.5,1.0)嚗R 靽∪艙??
// ????????????????????????????????????????????????????????????????????????????
// 頝臬?靘陷?嚗??鞈?像皛?equity 瘛瑕?雿 sizing ?箸?嚗?銴頝臬??曉之??// 瘛瑕?靽 eqBlendInit / eqBlendSmooth 撌脫??脩?航矽 input(???0.5 / 0.99)??var float avgEq = na
avgEq := na(avgEq) ? strategy.equity : avgEq * eqBlendSmooth + strategy.equity * (1.0 - eqBlendSmooth)
sizeBase = eqBlendInit * strategy.initial_capital + (1.0 - eqBlendInit) * avgEq
// vol-target 瘥?嚗?????摰????改?瘥?=1嚗?vtScale = useVolTarget ? targetVol / instVolAnn : 1.0
// ER 靽∪艙靽嚗??嚗lamp 0.5~1.0嚗????冽?Ⅳ撅?L2/L3嚗???L1 銝葬??convMult = useConvAdd and erThr > 0 ? math.min(math.max(er / erThr, 0.5), 1.0) : 1.0
wL2eff = wL2 * convMult
wL3eff = wL3 * convMult
totW = wL1 + wL2 + wL3 // 蝮賣?獢踹??誑?甈?(銝靽∪艙蝮格)?箏皞?靽?銝?蝛拙?
var int capHits = 0
// ?桐??箏嚗策摰惜甈? ???格?? ???府撅文???瑽▼銝? clamp ??qty(??/?aN)
qtyFor(w) =>
float notion = vtScale * sizeBase * w
// 閰脣惜???啁??銝?嚗蜇瑽▼???格???靘???
float nCap = totW > 0 ? sizeBase * maxGross * (w / totW) : sizeBase * maxGross
bool capped = notion > nCap
notion := math.min(notion, nCap)
float q = close > 0 ? notion / close : 0.0
q := math.max(nz(q, 0.0), 0.0)
[q, capped]
[qty1, cap1] = qtyFor(wL1)
[qty2, cap2] = qtyFor(wL2eff)
[qty3, cap3] = qtyFor(wL3eff)
// ????????????????????????????????????????????????????????????????????????????
// 禮8 R-multiple ??Chandelier ratchet ??嚗?蝛粹??only-up / only-down嚗?// R ?? = entryRisk嚗脣??銋?chandMult?atrChand ??甇X?頝嚗?// entryPx/rBasePx 銋 L1 敹 L2/L3 ?券?游??◤閫詨?嚗 ?摰祕摮敹?// ????????????????????????????????????????????????????????????????????????????
rDenom = na(entryRisk) ? (chandMult * atrChand) : entryRisk // ?祕??甇X?頝
rBasePx = na(entryPx) ? avgPx : entryPx
R = inPos and rDenom > 0 ? (inLong ? (close - rBasePx) / rDenom : (rBasePx - close) / rDenom) : 0.0
// ?湔? high-water R嚗?脖??嚗ot inPos 撌脫 禮6 甇賊嚗?// ?瑟撜啣澆?璉???peakBar嚗??箄隅?W僑朣??趙閮??函??箸???if inPos
if R > peakR
peakR := R
peakBar := bar_index // ?瑟 high-water R ???蔭?趙閮?
// 撜啣澆?皛舀??賂?頝?甈∪??peakR 撌脤?撟暹?嚗??∪???0
barsSincePeak = inPos ? math.max(bar_index - peakBar, 0) : 0
// ?? R ?0?嚗祝?葉??嚗隤踵蝺???曉之?喳偏??瑕椰撠橘???
// ?箇?銝?嚗<1 撖?=??mult)??~2 銝准?2 蝺?multStepBase = not useRStep ? chandMult : (R < 1 ? chandMult : R < 2 ? trailMidR : trailTightR)
multStep = multStepBase
// 瘜W??芷?蜓 trail嚗?銝?蝺?(R>=1嚗歇?a???撖祆?)隞交郭???芷?凝隤踴?// ?澆靘?嚗?閮剛???breathClamp(relVol/brakeK)嚗??賊??????breathPctl
// (atrPct ?曉?雿?銝1 brakeK)??瘜W? ??靽?? ??蝬剜??嗥?嚗?瘜W? ???曉祝??// clamp ??[trailTightR, chandMult] 蝬剜??株矽??trailBreath = effDecorrGauge ? breathPctl : breathClamp
if effBreathTrail and useRStep and inPos and R >= 1
float bt = multStepBase * trailBreath
multStep := math.min(math.max(bt, trailTightR), chandMult)
// 憭偕摨行???游漲 ratchet嚗誑銝偕摨行??瑽?撖思蜓 trail 擛?(?郭?蜓 trail ??蝺?????// coherence==3(?典????亙熒) ???曉祝 cohRelaxK(銝? chandMult) 蝯行?憭批?賂?
// coherence<=1(敹思葉撠箏漲?) ???嗅?蝺? trailTightR ???嚗oherence==2 ??銝???if effCoherenceTrail and useRStep and inPos and R >= 1
int coh = inLong ? coherenceLong : coherenceShort
if coh >= 3
multStep := math.min(multStep * cohRelaxK, chandMult)
else if coh <= 1
multStep := math.min(multStep, trailTightR)
// TP1 ?質?敺??詨??撥?嗡??穿?蝑??BE 閫貊??歇?質????
beActive = (useBE and R >= kBE) or (useBEonTP1 and tp1Done)
// ?? 瘜W??芷????嚗ffGiveback = multBE ? clamp(relVol,1.0,brakeK) ??
// 銴 relVol(禮4)/brakeK/multBE嚗?啣? input??瘜W??曉祝??(?踹???)??// 雿郭?蝺????憭瞏????澆??摰?givebackR=multBE??breathMult = effBreathGB ? breathClamp : 1.0
// 頞典撟湧翩??趙?嗥?嚗陸?澆?皛航?銋???????撠??唳頞?)嚗?憭??stallShrinkMin ??// stallShrink = clamp(1 ??barsSincePeak/timeStopBars, stallShrinkMin, 1.0)??// 閮剛??牧嚗ong-since-new-high ?舫??函???銵啁垠???雁摨西????郭??撌脣祕?曄??甇?漱??stallFrac = barsSincePeak * 1.0 / math.max(timeStopBars, 1) // 撘瑕 float ?斗?嚗??豢??stallShrink = effStallRatchet and timeStopBars > 0 ? math.min(math.max(1.0 - stallFrac, stallShrinkMin), 1.0) : 1.0
effGiveback= multBE * breathMult * stallShrink
// Peak-R ???唳嚗???peakR 撌脩?銝?effGiveback 銋??????踹????????// ?銋??ffPeakRatchet 撌脣 usePureA??peakFloorOn= effPeakRatchet and inPos and peakR > effGiveback
// ?? TP ?0撘??砍?選?瘥鋡?畾萄?蹂???multBE?rDenom ??
// stairRungs嚗歇?質?畾菜(TP1???P2??)嚗??= rBasePx 簣 stairRungs?multBE?rDenom??// ??peakR ????唳 / BE ?唳 / R ?0 銝敺?math.max/min ??蝺???multBE/tp ????stairRungs = (tp1Done ? 1 : 0) + (tp2Done ? 1 : 0)
tpStairOn = effTPstair and inPos and stairRungs > 0
tpStairFloorL= rBasePx + stairRungs * multBE * rDenom
tpStairFloorS= rBasePx - stairRungs * multBE * rDenom
// ?? 撌脣祕?曄?拚靽?唳嚗ankFrac=bankedQty/openInitQty嚗??rBasePx簣bankFrac?rDenom ??
// ?餌? multBE(銝1)嚗?歇?質???憪蜇?瘥???交撠敹?雿??拐???// bankFrac?0,1] ???唳?憭??rBasePx+1?rDenom(=+1R)嚗迤瘥撌脣祕?暹?靘??株矽銝宏??bankFrac = openInitQty > 1e-9 ? math.min(bankedQty / openInitQty, 1.0) : 0.0
bankedFloorOn = effBankedFloor and inPos and bankFrac > 1e-9
bankedFloorL = rBasePx + bankFrac * rDenom
bankedFloorS = rBasePx - bankFrac * rDenom
// ?? [NEW-A1] 頝典偕摨?ATR ?折?甇X?撠箏漲?唳嚗??beActive 敺敹??剁?憭征?∪?嚗??
// 閮剛??牧嚗敹??W偕摨西隅?g??Z隅?X迤撣詨?詨?摨阡?憭扳敹怠偕摨?atrChand嚗敹?ATR 閮?trail
// ? low-vol pocket ?敹?亙熒??銝剜??箝璈??trail ??ATR 撠箏漲?寧
// atrScale=max(atrChand, |close?t3|)?誑?W偕摨?ST3 band 撖砍漲雿?瑼◢?芣?撠偕摨血?選?雿踹??// 蝛粹??典??詨??祕??撅祆??偕摨艾?曉之 rawStop(撠箏漲?唳)?atchet 隞?霅?trailStop ?芷脖??嚗?// 銝憭批?憪?R(beActive ??雿)????st3/atrChand ?嗆憓?input?? breathPctl(??雿蔭)甇?漱??st3Band = na(st3) ? 0.0 : math.abs(close - st3)
atrScaleX = effMultiScaleStop and beActive ? math.max(atrChand, st3Band) : atrChand
if useChandelier and inLong
trailExt := na(trailExt) ? high : math.max(trailExt, high)
rawStop = trailExt - multStep * atrScaleX
trailStop := na(trailStop) ? rawStop : math.max(trailStop, rawStop) // ratchet ?芷脖??
if beActive
trailStop := math.max(trailStop, rBasePx + multBE * rDenom)
if tpStairOn
trailStop := math.max(trailStop, tpStairFloorL) // ?0?唳
if bankedFloorOn
trailStop := math.max(trailStop, bankedFloorL) // 撌脣祕?曄?拚?唳
if peakFloorOn
trailStop := math.max(trailStop, rBasePx + (peakR - effGiveback) * rDenom) // ?澆/撟湧翩????唳
else if useChandelier and inShort
trailExt := na(trailExt) ? low : math.min(trailExt, low)
rawStop = trailExt + multStep * atrScaleX
trailStop := na(trailStop) ? rawStop : math.min(trailStop, rawStop) // ratchet ?芷脖??
if beActive
trailStop := math.min(trailStop, rBasePx - multBE * rDenom)
if tpStairOn
trailStop := math.min(trailStop, tpStairFloorS) // ?0?唳(?∪?)
if bankedFloorOn
trailStop := math.min(trailStop, bankedFloorS) // 撌脣祕?曄?拚?唳(?∪?)
if peakFloorOn
trailStop := math.min(trailStop, rBasePx - (peakR - effGiveback) * rDenom) // ?澆/撟湧翩????唳(?∪?)
// ????????????????????????????????????????????????????????????????????????????
// 禮9 ?箏閫貊嚗??蝔梧?頞典???箔蜓?箏嚗elowAvg ?孵?畾萎??舫?嚗?// time-stop 璉靘?ER ?芷??effTimeStop = round(timeStopBars?clamp(er/erThr,0.5,1.0))
// 閮鳴??祆挾?典像閫貊??禮10 隞颱? reductions 銋?閰摯嚗?撘 禮6 璉? stage ??甇?Ⅱ??// ????????????????????????????????????????????????????????????????????????????
trailHitLong = useChandelier and inLong and not na(trailStop) and close < trailStop
trailHitShort = useChandelier and inShort and not na(trailStop) and close > trailStop
// ?賡摨?嚗翰+銝?ST ????嚗? ST 蝧餃?嚗?twoSTRevLong = inLong and st1Bear and st2Bear
twoSTRevShort = inShort and st1Bull and st2Bull
// ER ?芷??time-stop 璉嚗???er/erThr/timeStopBars嚗?啣? input嚗ffAdaptiveTS 撌脣 usePureA嚗?tsScale = effAdaptiveTS and erThr > 0 ? math.min(math.max(er / erThr, 0.5), 1.0) : 1.0
effTimeStop = math.max(math.round(timeStopBars * tsScale), 1)
// timeStop ??not na(entryBar) 摰?(憭征撠迂)嚗ntryBar 撌脤?蝵桃 na ?厭???芷脣???閮?嚗?// ?? (bar_index-0) ???鈭活撟喳?tsAge = na(entryBar) ? 0 : (bar_index - entryBar)
timeStopLong = useTimeStop and inLong and not na(entryBar) and tsAge >= effTimeStop and R < 1 and stage < 3
timeStopShort = useTimeStop and inShort and not na(entryBar) and tsAge >= effTimeStop and R < 1 and stage < 3
// ????????????????????????????????????????????????????????????????????????????
// 禮10 銝?瑁?嚗??箏敺脣嚗?璉????喳?甇伐?
// ????????????????????????????????????????????????????????????????????????????
var bool exitedThisBar = false
exitedThisBar := false
// ?? ??摨??暑?????? stale 敹怎 ??
// 禮6 ??stage/posSize ?冽???????銝甈~蝔?????0 ??TP/cut/fade/belowAvg ???// l*On/s*On 銝血祕?葬撠雿?隞?posLive 餈質馱???嗅??拚??其?(瘥活 drain 敺?文祕??鈭日? d)
// 雿???drain ??posAbs 銝? + TP ?質??箸嚗???stage-gate ?寧??fresh l*On/s*On
// ?單????暑 stage嚗?????stale stage??var float posLive = 0.0
posLive := posSize // 璉?隞亙祕?雿?憪?嚗?甈?drain 敺 global scope ??撖阡??漱??d
bool fullExitNow = false
// ????????????????????????????????????????????????????????????????????????????
// [intrabar hardstop ????芣晾 + ?箏?芸?摨??包 ????????????????????????????????
// (a) ?隞??璉?committed ??trailStop?? strategy.exit(?脰歲蝛箇銝剜?鈭歹?銝?璉???嚗?// ?餅?銝?摰?from_entry 雿踹?箸項???tranche ?擃雿????// (b) ?支葉?漱?菜葫??敺?朣?璉?甇???? live stop = trailStop[1]??
// brokerStopHit = (憭? low<=trailStop[1] / 蝛? high>=trailStop[1])??// (c) 銝??brokerStopHit嚗??喟頝舀璉??????TP/cut/fade/belowAvg)?擗?斤Ⅱ隤?// ?箏(閮?exitedThisBar/blockPartials=true)嚗???璉摮 hX ?典?鈭?蝯??找?????// (d) ????單撅文? brokerStopHit ??銝剖董??蝵栽?歇?芾?撟喳?銝?璉?禮6 not inPos
// ?芰摰?蔭(??entryBar:=na)嚗???璉?? stageLive==opentrades??// ????????????????????????????????????????????????????????????????????????????
// ?詨??祆??迤????live stop = 銝?璉?committed ??trailStop(trailStop[1])
liveStopLong = useHardStop and inLong and not na(trailStop[1])
liveStopShort = useHardStop and inShort and not na(trailStop[1])
// ?嚗??祆? committed ??trailStop 銝漱??蝝?????脰歲蝛綽?銝?璉??支葉?漱??)??if useHardStop and inLong and not na(trailStop)
strategy.exit("hX-L", stop=trailStop, comment="HardStopL")
if useHardStop and inShort and not na(trailStop)
strategy.exit("hX-S", stop=trailStop, comment="HardStopS")
// ?支葉?漱?菜葫嚗?朣?璉??? live stop(trailStop[1])
bool brokerStopHit = (liveStopLong and low <= trailStop[1]) or (liveStopShort and high >= trailStop[1])
// brokerStopHit ???祆??其?撌脩 hX ?典像?????剛楝?祆????砍惜皜??嗥蝣箄??箏(鈭)
bool blockPartials = brokerStopHit
if brokerStopHit
exitedThisBar := true // ?餅迫?祆?銝??蝥?close/close_all/?典?皜??詨?撌脣?像??
// ??銝餃??A嚗handelier trailStop嚗?斤Ⅱ隤? ??// 瘜冽?嚗rokerStopHit ??exitedThisBar 撌?true嚗???not exitedThisBar ?券?剛楝??if not exitedThisBar and trailHitLong
strategy.close_all(comment="TrailL")
fullExitNow := true
else if not exitedThisBar and trailHitShort
strategy.close_all(comment="TrailS")
fullExitNow := true
// ???箏 B嚗? ST ??摨? ??if not exitedThisBar and not fullExitNow and twoSTRevLong
strategy.close_all(comment="2STrevL")
fullExitNow := true
else if not exitedThisBar and not fullExitNow and twoSTRevShort
strategy.close_all(comment="2STrevS")
fullExitNow := true
// ???箏 C嚗???????if not exitedThisBar and not fullExitNow and (timeStopLong or timeStopShort)
strategy.close_all(comment="TimeStop")
fullExitNow := true
// [inline ?典像?蔭] ??單撅?close_all ?箏(trail/2ST/timeStop)閫貊嚗rokerStopHit 頝臬?
// 銝粥甇方?(?詨??芾?撟喳?銝?璉?禮6 ?芰?蔭)??// 撠迂?澆撘脣?瑕嚗ffCooldown=round(cooldownBars?clamp(relVol,1,brakeK))??if fullExitNow
l1On := false
l2On := false
l3On := false
s1On := false
s2On := false
s3On := false
entryRisk := na
entryPx := na
entryBar := na // 摰?蔭??entryBar:=na嚗?蝯?bar_index-0 瘙⊥?
trailExt := na
trailStop := na
tp1Done := false
tp2Done := false
peakR := 0.0
peakBar := bar_index // ?趙閮??冽飛?? bankedQty := 0.0
openInitQty := 0.0
fadeArmed := true
notL1 := 0.0
notL2 := 0.0
notL3 := 0.0
notS1 := 0.0
notS2 := 0.0
notS3 := 0.0
exitedThisBar := true
float effCooldown = effAdaptiveCooldown ? math.max(math.round(cooldownBars * breathClamp), 0) : cooldownBars
cooldownUntil := bar_index + effCooldown
posLive := 0.0 // ?典像敺?璉擗雿飛?塚?敺? drain ??posAbs 銝???TP ?箸?喟 0
// ?? contiguous-stack ?臭?皜?隤?蝝撘?top-first drain + global-scope 閫???神嚗??
// [Pine v5 蝖祉?? ?賢??找??舀撖怠?撅?scope var???寧蝝撘????撅文????典??誑
// ??喳??霈?撠撘????賊?蝞???湔敺?[done, ?啣??捌?, ?啣?棺?] tuple嚗?// ?迤???mutation 銝敺 global scope ?刻圾瑽釵?澆??trategy.close(id, qty=) ?澆撘
// ????蝢抬?隞?newest-first(L3?2?1) ???摰?撅斗???撅扎畾???遣?典 bool嚗?// 銝?撘?l3On?2On?1On ??top-first ??蝡?drainLong(reqQty, posAbs, n3, n2, n1) =>
float want = math.min(math.max(nz(reqQty, 0.0), 0.0), posAbs)
float done = 0.0
float r3 = n3
float r2 = n2
float r1 = n1
if r3 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r3)
if take > 1e-9
strategy.close("L3", qty=take, comment="cut-L3")
r3 := r3 - take
done := done + take
if r2 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r2)
if take > 1e-9
strategy.close("L2", qty=take, comment="cut-L2")
r2 := r2 - take
done := done + take
if r1 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r1)
if take > 1e-9
strategy.close("L1", qty=take, comment="cut-L1")
r1 := r1 - take
done := done + take
[done, r3, r2, r1, r3 > 1e-9, r2 > 1e-9, r1 > 1e-9]
drainShort(reqQty, posAbs, n3, n2, n1) =>
float want = math.min(math.max(nz(reqQty, 0.0), 0.0), posAbs)
float done = 0.0
float r3 = n3
float r2 = n2
float r1 = n1
if r3 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r3)
if take > 1e-9
strategy.close("S3", qty=take, comment="cut-S3")
r3 := r3 - take
done := done + take
if r2 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r2)
if take > 1e-9
strategy.close("S2", qty=take, comment="cut-S2")
r2 := r2 - take
done := done + take
if r1 > 1e-9 and want - done > 1e-9
float take = math.min(want - done, r1)
if take > 1e-9
strategy.close("S1", qty=take, comment="cut-S1")
r1 := r1 - take
done := done + take
[done, r3, r2, r1, r3 > 1e-9, r2 > 1e-9, r1 > 1e-9]
// trancheTopLong/Short嚗??賢?嚗??喋???怠?游惜???柴???cutTop ????tranche
// 嚗???惜?嚗?璉?甈?cut ?勗??芸?怎垢??drain 敺?蝞??踹? stale ?嚗?trancheTopLong(on3, on2, on1, n3, n2, n1) =>
on3 ? n3 : on2 ? n2 : on1 ? n1 : 0.0
trancheTopShort(on3, on2, on1, n3, n2, n1) =>
on3 ? n3 : on2 ? n2 : on1 ? n1 : 0.0
// ?? ?挾 R-multiple Partial TP嚗擗雿?posLive?瘥?蝎曄Ⅱ?質?嚗? contiguous-stack嚗??
// tpQty 銝敺誑 posLive(???嗅??拚??其?)?箏?賂?drain ??posAbs 銝?鈭血 posLive嚗?甈∟鋡?// 敺?posLive -= d?P1 ??TP2 ????閫貊??TP2 ?芸?隞亦葬撠??拚??其?閮??拇挾??瘞訾?頞??嗅?
// 蝮賡雿敹?L1 ??newest-first + posAbs clamp ??靽風?ankedQty 蝝航?撖阡??質???撠董??// not blockPartials嚗rokerStopHit 璉?敺??潮??close嚗? hX ?典?鈭支??乓?tpHitL1 = usePartialTP and not exitedThisBar and not blockPartials and inLong and R >= tp1R and not tp1Done
tpHitL2 = usePartialTP and not exitedThisBar and not blockPartials and inLong and R >= tp2R and not tp2Done
tpHitS1 = usePartialTP and not exitedThisBar and not blockPartials and inShort and R >= tp1R and not tp1Done
tpHitS2 = usePartialTP and not exitedThisBar and not blockPartials and inShort and R >= tp2R and not tp2Done
if tpHitL1
[d, n3, n2, n1, o3, o2, o1] = drainLong(posLive * tp1Pct / 100.0, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
bankedQty := bankedQty + d
posLive := math.max(posLive - d, 0.0)
tp1Done := true
if tpHitL2
[d, n3, n2, n1, o3, o2, o1] = drainLong(posLive * tp2Pct / 100.0, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
bankedQty := bankedQty + d
posLive := math.max(posLive - d, 0.0)
tp2Done := true
if tpHitS1
[d, n3, n2, n1, o3, o2, o1] = drainShort(posLive * tp1Pct / 100.0, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
bankedQty := bankedQty + d
posLive := math.max(posLive - d, 0.0)
tp1Done := true
if tpHitS2
[d, n3, n2, n1, o3, o2, o1] = drainShort(posLive * tp2Pct / 100.0, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
bankedQty := bankedQty + d
posLive := math.max(posLive - d, 0.0)
tp2Done := true
// ???箏 D嚗T ???挾皜?cut嚗??蝬?contiguous-stack top-first嚗偶銝撅??香嚗?// 閫貊璇辣嚗??嚗T1 ??嚗敹隅?Z?撘梧???撅歹?ST2 ??嚗葉撠箏漲頧摹嚗???撅扎?// 瘜冽?嚗t1 ??st2 ???箏??? 禮9 twoSTRevLong/Short 銋?close_all 撌脣?銵撟喃蒂閮?// exitedThisBar嚗?甇方? cut ??銝 ST ???銝 ST 隞???皜Ⅳ??????// cutL2/cutS2 閬??喳????Ⅳ撅文?湛?l2On or l3On嚗?蝣箔? ST2 頝臬?銝?刻孛?敹?L1??// 閫貊 bool 撌脖誑 fresh l*On/s*On ?斗(??stale stage)嚗rain ??posAbs 銝??寧 posLive??// not blockPartials嚗rokerStopHit 璉?敺??潮??close??cutL1 = usePartialST and not exitedThisBar and not blockPartials and inLong and st1FlipDn and (l1On or l2On or l3On)
cutL2 = usePartialST and not exitedThisBar and not blockPartials and inLong and st2FlipDn and (l2On or l3On)
cutS1 = usePartialST and not exitedThisBar and not blockPartials and inShort and st1FlipUp and (s1On or s2On or s3On)
cutS2 = usePartialST and not exitedThisBar and not blockPartials and inShort and st2FlipUp and (s2On or s3On)
// cutTop ??global-scope inline嚗??惜 tranche ??drain ??閫???神?典???// ?? cutL1 敺?cutL2 ?誑??啣??? notL*/l*On ?? tranche嚗??stale ?嚗?if cutL1
float tr = trancheTopLong(l3On, l2On, l1On, notL3, notL2, notL1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainLong(tr, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
posLive := math.max(posLive - d, 0.0)
if cutL2
float tr = trancheTopLong(l3On, l2On, l1On, notL3, notL2, notL1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainLong(tr, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
posLive := math.max(posLive - d, 0.0)
if cutS1
float tr = trancheTopShort(s3On, s2On, s1On, notS3, notS2, notS1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainShort(tr, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
posLive := math.max(posLive - d, 0.0)
if cutS2
float tr = trancheTopShort(s3On, s2On, s1On, notS3, notS2, notS1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainShort(tr, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
posLive := math.max(posLive - d, 0.0)
// ???箏 E嚗egime-fade ?詨??駁◢?迎??箏?游?蝔望?脣靽∪艙?Ⅳ嚗???// ?葉 er < erThr?fadeK嚗縑敹菜??嚗?嚗? contiguous-stack ???怠?蝣澆惜????tranche嚗?// 閬?瘣?stage>=2嚗撠?銝?Ⅳ撅文?湛?隞乩?霅?閫詨??詨? L1???皛航風甈?
// (1) re-arm ??er >= erThr嚗??湧皛臬葆嚗?(2) ?拇活?惜?喳??? fadeCoolBars 璉?// effRegimeFade ??usePureA?ot blockPartials??if not inPos or er >= erThr
fadeArmed := true // ER ??頞?摰?瑼?erThr ???唳郎鋆??脫趙撣塚?
fadeTrig = effRegimeFade and not exitedThisBar and not blockPartials and erThr > 0 and er < erThr * fadeK
fadeReady = fadeArmed and (bar_index - lastFadeBar) >= fadeCoolBars
// 隞乓迨??fresh l*On/s*On ?單?????瘣?stage ??>=2 霅瑟?嚗?蝯???禮6 璉? stale stage??fadeStageLong = (l1On?1:0) + (l2On?1:0) + (l3On?1:0)
fadeStageShort = (s1On?1:0) + (s2On?1:0) + (s3On?1:0)
regimeFadeLong = fadeTrig and inLong and fadeStageLong >= 2 and fadeReady
regimeFadeShort = fadeTrig and inShort and fadeStageShort >= 2 and fadeReady
if regimeFadeLong
float tr = trancheTopLong(l3On, l2On, l1On, notL3, notL2, notL1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainLong(tr, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
posLive := math.max(posLive - d, 0.0)
fadeArmed := false
lastFadeBar := bar_index
if regimeFadeShort
float tr = trancheTopShort(s3On, s2On, s1On, notS3, notS2, notS1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainShort(tr, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
posLive := math.max(posLive - d, 0.0)
fadeArmed := false
lastFadeBar := bar_index
// ???箏 F嚗elowAvg/aboveAvg ?怠惜皜Ⅳ嚗?挾??閮剝?嚗??撟喟??詨??? ??// 蝬?contiguous-stack嚗?????怠?游惜????tranche嚗op-first嚗?靽??詨? L1 頝撠橘?
// 瘣?stage>=2 璇辣蝣箔??喳?????蝣澆惜?臬?嚗1 瘞訾?鋡急迨頝臬?閫詨???// stage>=2 霅瑟??寧甇文 fresh l*On/s*On ?單????暑 stage?ot blockPartials??belowStageLong = (l1On?1:0) + (l2On?1:0) + (l3On?1:0)
aboveStageShort = (s1On?1:0) + (s2On?1:0) + (s3On?1:0)
belowAvgLong = useBelowAvg and not exitedThisBar and not blockPartials and inLong and belowStageLong >= 2 and close < avgPx - kBuf * atrChand
aboveAvgShort = useBelowAvg and not exitedThisBar and not blockPartials and inShort and aboveStageShort >= 2 and close > avgPx + kBuf * atrChand
if belowAvgLong
float tr = trancheTopLong(l3On, l2On, l1On, notL3, notL2, notL1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainLong(tr, posLive, notL3, notL2, notL1)
notL3 := n3
notL2 := n2
notL1 := n1
l3On := o3
l2On := o2
l1On := o1
posLive := math.max(posLive - d, 0.0)
if aboveAvgShort
float tr = trancheTopShort(s3On, s2On, s1On, notS3, notS2, notS1)
if tr > 1e-9
[d, n3, n2, n1, o3, o2, o1] = drainShort(tr, posLive, notS3, notS2, notS1)
notS3 := n3
notS2 := n2
notS1 := n1
s3On := o3
s2On := o2
s1On := o1
posLive := math.max(posLive - d, 0.0)
// ????????????????????????????????????????????????????????????????????????????
// 禮11 ?脣嚗?蝛箏?潮??擐憟?regime gate嚗?蝣澆? winners-add-only + vol-brake嚗?// ??禮10 靽? stage ???嚗1Cond(stage==0) ??蝣?l1On==true) 瘞詨???⊿?蝝?甇颯?// brokerStopHit 璉?exitedThisBar=true ???祆?銝脣(canEnter ??not exitedThisBar)??// ????????????????????????????????????????????????????????????????????????????
canEnter = bar_index >= cooldownUntil and not exitedThisBar
// ?脣 stage ??敺誑??0 皜? fresh l*On/s*On ?單?????瘣?stage ?斗嚗?????禮6 璉?
// stale stage?1Cond/s1Cond 銝血?撠? stage==0 ??瘙⊥??脰風??curLongStage = (l1On?1:0) + (l2On?1:0) + (l3On?1:0)
curShortStage = (s1On?1:0) + (s2On?1:0) + (s3On?1:0)
// [NEW-A2] ?脣???孵榆蝣箄???擐 |close?t1| ??kBuf?atrChand嚗???kBuf/atrChand ?嗆憓?input嚗?// ?芸??蝧餉????芣銵??脤??殷??蕪?Z??云??餈賡?/餈賜征?柴?蝛粹?ffEntryDistGate 撌脣 usePureA??// na 摰?嚗t1/atrChand 隞颱? na ??atrChand<=0 ???餅?(???銵)嚗??璈?隤日???entryDistOK = not effEntryDistGate or na(st1) or na(atrChand) or atrChand <= 0 or math.abs(close - st1) <= kBuf * atrChand
// [?EW-C2] 銝剖偕摨阡脣蝣箄???擐??瘙葉撠箏漲 ST2 ??嚗? st2Bull / 蝛?st2Bear嚗?// 鋆?regime gate ?葉撠箏漲?孵??脤?(銝巨 ER/ADX/ST3 銝 ST2)????st2Bull/st2Bear ?嗆憓?input??// 憭征?∪??雿擐?ffMidConfirm 撌脣 usePureA嚗????拇? OK ??true(???銵)??midConfirmLongOK = not effMidConfirm or st2Bull
midConfirmShortOK = not effMidConfirm or st2Bear
// 擐嚗T1 蝧餃? + regime gate嚗 MTF ?Y?瑽??漲嚗? ?孵榆??A2) + 銝剖偕摨衣Ⅱ隤?(C2)
l1Cond = curLongStage == 0 and curShortStage == 0 and st1FlipUp and regimeLongOK and canEnter and entryDistOK and midConfirmLongOK
s1Cond = curLongStage == 0 and curShortStage == 0 and st1FlipDn and regimeShortOK and canEnter and entryDistOK and midConfirmShortOK
// ?Ⅳ嚗?畾萄歇瘚桃?(R>0)?隅?W??T ?亙?蝧餃????芾孛??vol-brake嚗?瘜W???嚗?// [NEW-A3] 銝?游漲?Ⅳ靽∪艙??addOK ???撅砌蒂??AND coherence>=2嚗? coherenceLong / 蝛?coherenceShort嚗?// [?EW-C1] ER ??漲?Ⅳ????AND (er >= er[1])(??芣??????er[1] ?嗆憓?input??蝣?erThr??// ?拚???convMult(隤?size)銝迤鈭歹?A3 ??瑽1 ???賢??漲?onvMult ?偌皞?蝛粹??addCohLongOK = not effCoherenceAdd or coherenceLong >= 2
addCohShortOK = not effCoherenceAdd or coherenceShort >= 2
addErAccelOK = not effErAccelAdd or er >= nz(er[1], er) // er 銝?榆????(??芣??嚗?蝛箏?典?銝璇?er
addOKLong = not volBrakeOn and R > 0 and addCohLongOK and addErAccelOK
addOKShort = not volBrakeOn and R > 0 and addCohShortOK and addErAccelOK
l2Cond = curLongStage == 1 and l1On and inLong and st2FlipUp and st1Bull and addOKLong
l3Cond = curLongStage == 2 and l1On and l2On and inLong and st3FlipUp and st1Bull and st2Bull and addOKLong
s2Cond = curShortStage == 1 and s1On and inShort and st2FlipDn and st1Bear and addOKShort
s3Cond = curShortStage == 2 and s1On and s2On and inShort and st3FlipDn and st1Bear and st2Bear and addOKShort
if l1Cond
strategy.entry("L1", strategy.long, qty=qty1, comment="L1")
entryRisk := chandMult * atrChand // ???祕??甇X?頝??R ??
entryPx := close
trailExt := high
trailStop := na
entryBar := bar_index
peakR := 0.0
peakBar := bar_index // ?啣???趙閮??? bankedQty := 0.0
openInitQty := qty1
fadeArmed := true
l1On := true
notL1 := qty1 // 閮?閰脣惜銝?⊥嚗? TP/cut/fade 蝝蝎曄Ⅱ撠董
if l2Cond
strategy.entry("L2", strategy.long, qty=qty2, comment="L2")
l2On := true
notL2 := qty2
openInitQty := openInitQty + qty2
if l3Cond
strategy.entry("L3", strategy.long, qty=qty3, comment="L3")
l3On := true
notL3 := qty3
openInitQty := openInitQty + qty3
if s1Cond
strategy.entry("S1", strategy.short, qty=qty1, comment="S1")
entryRisk := chandMult * atrChand // ???祕??甇X?頝??R ??
entryPx := close
trailExt := low
trailStop := na
entryBar := bar_index
peakR := 0.0
peakBar := bar_index // ?啣???趙閮??? bankedQty := 0.0
openInitQty := qty1
fadeArmed := true
s1On := true
notS1 := qty1
if s2Cond
strategy.entry("S2", strategy.short, qty=qty2, comment="S2")
s2On := true
notS2 := qty2
openInitQty := openInitQty + qty2
if s3Cond
strategy.entry("S3", strategy.short, qty=qty3, comment="S3")
s3On := true
notS3 := qty3
openInitQty := openInitQty + qty3
// nCap ??閮嚗??vol-target ?臬鋡急?獢蹂???頝荔?
if cap1 or cap2 or cap3
capHits += 1
// 璉 fresh 瘣?stage嚗?0 皜?+ 禮11 ?Ⅳ敺?蝯?l*On/s*On ?遣嚗? 禮12 閬死?? 禮14
// stage==opentrades 撠董雿輻?? ??stage ?箸?擐?靘?禮9 timeStop 甇?Ⅱ閮?)嚗*蝷?撠董
// ?寧 stageLive(璉 fresh)隞亙??璉?祕撅斗??// 閮鳴?brokerStopHit 璉?銝剖歇撟喳?雿?strategy.position_size 閬?銝璉??? flat嚗??祆?
// inLong/inShort 隞?true?tageLive 隞*蝷箸?鈭文?撅斗嚗??詨?敺?蝞? opentrades 銝?湛?閰脫?
// 禮14 銝炊?梁撣喉?銝?璉?Ⅱ隤?flat 敺?禮6 ?芰摰?蔭??蝑敺押?stageLive = inLong ? ((l1On?1:0) + (l2On?1:0) + (l3On?1:0)) : inShort ? ((s1On?1:0) + (s2On?1:0) + (s3On?1:0)) : 0
// [NEW-A4] ?詨????拙?蹂?皞飛??蝝閫皜祆扼鈭斗?銵?寡?嚗?瘥????唳撖阡? binding ??// 憭征?∪?嚗???擃??蝺??征??雿??蝺?嚗??梁??蝺?輻?靘??迂嚗? walk-forward
// ?斗???唳?臬敺 binding(?臬?????冽??*FloorL/S ??effGiveback嚗?啣? input??floorBE_L = beActive ? rBasePx + multBE * rDenom : na
floorBE_S = beActive ? rBasePx - multBE * rDenom : na
floorPeak_L = peakFloorOn ? rBasePx + (peakR - effGiveback) * rDenom : na
floorPeak_S = peakFloorOn ? rBasePx - (peakR - effGiveback) * rDenom : na
floorStair_L = tpStairOn ? tpStairFloorL : na
floorStair_S = tpStairOn ? tpStairFloorS : na
floorBank_L = bankedFloorOn ? bankedFloorL : na
floorBank_S = bankedFloorOn ? bankedFloorS : na
// ??祕??蝺?binding)?唳靘?嚗??格?蝺??擃征?格?蝺??雿?var string activeFloorTxt = "??
activeFloorTxt := "??
if inPos
float best = na
string src = "蝝rail"
if inLong
if not na(floorBE_L) and (na(best) or floorBE_L > best)
best := floorBE_L
src := "BE靽"
if not na(floorStair_L) and (na(best) or floorStair_L > best)
best := floorStair_L
src := "?0?唳"
if not na(floorBank_L) and (na(best) or floorBank_L > best)
best := floorBank_L
src := "撌脣祕?暸"
if not na(floorPeak_L) and (na(best) or floorPeak_L > best)
best := floorPeak_L
src := "撜啣?撟湧翩??"
else
if not na(floorBE_S) and (na(best) or floorBE_S < best)
best := floorBE_S
src := "BE靽"
if not na(floorStair_S) and (na(best) or floorStair_S < best)
best := floorStair_S
src := "?0?唳"
if not na(floorBank_S) and (na(best) or floorBank_S < best)
best := floorBank_S