FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pineforge-engine/src/engine_orders.cpp at main · pineforge-4pass/pineforge-engine · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pineforge-4pass
/
pineforge-engine
Public
Notifications
You must be signed in to change notification settings
Fork
42
Star
179
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pineforge-engine
/
src
/
engine_orders.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1081 lines (993 loc) · 55.4 KB
Breadcrumbs
pineforge-engine
/
src
/
engine_orders.cpp
Copy path
File metadata and controls
1081 lines (993 loc) · 55.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* engine_orders.cpp — execute_market_* and partial-exit fill mechanics
*/
#
include
"
engine_internal.hpp
"
#
include
<
algorithm
>
#
include
<
cctype
>
#
include
<
cmath
>
#
include
<
stdexcept
>
#
include
<
unordered_set
>
#
include
<
utility
>
namespace
pineforge
{
using
namespace
internal
;
//
Risk management + per-trade extreme tracking moved to engine_risk.cpp.
double
BacktestEngine::calc_qty_for_type
(
double
fill_price,
double
qty_value,
int
qty_type)
const
{
if
(
std::isnan
(qty_value)) {
return
calc_qty
(fill_price);
}
//
qty_step_ lot-size flooring applies uniformly regardless of how the
//
caller's qty was derived — including this FIXED branch, which is the
//
common ``strategy.entry(qty=someComputedExpr)`` shape (e.g. a DCA base/
//
safety-order qty = orderSizeUsd/close). See apply_qty_step's doc
//
comment (engine.hpp) for the verified TV behavior this mirrors.
if
(qty_type <
0
|| qty_type ==
static_cast
<
int
>(QtyType::
FIXED
)) {
return
apply_qty_step
(qty_value);
}
if
(qty_type ==
static_cast
<
int
>(QtyType::
PERCENT_OF_EQUITY
)) {
const
double
equity =
percent_commission_live_equity
(current_bar_.
close
);
if
(!
std::isfinite
(equity))
return
0.0
;
double
cash =
reserve_percent_commission
(equity * (qty_value /
100.0
));
//
Reject (qty 0) on a non-finite / non-positive fill price — a degenerate
//
$0/NaN print must NOT size as the raw % number (silent wrong-qty bug).
//
One contract's currency exposure is fill_price × pointvalue (1.0 for
//
crypto/equity — legacy math unchanged; futures divide the budget by
//
the full per-contract notional). cash is account-currency (equity is);
//
convert to quote currency via account_currency_fx_ before dividing by
//
the quote-currency fill_price — same convention as calc_qty() in
//
engine.hpp. fx=1.0 is a no-op.
return
(
std::isfinite
(fill_price) && fill_price >
0.0
)
?
apply_qty_step
((cash /
active_account_currency_fx
())
/ (fill_price * syminfo_.
pointvalue
)) :
0.0
;
}
if
(qty_type ==
static_cast
<
int
>(QtyType::
CASH
)) {
return
(
std::isfinite
(fill_price) && fill_price >
0.0
)
?
apply_qty_step
((qty_value /
active_account_currency_fx
())
/ (fill_price * syminfo_.
pointvalue
)) :
0.0
;
}
return
apply_qty_step
(qty_value);
}
//
Internal helper: execute a market entry (handles reverse-and-open).
//
//
Dispatches to one of five case-helpers based on the current position
//
state and the entry's close_only_opposite / later_same_tick_entry flags:
//
1. enter_market_from_flat — position FLAT
//
2. add_to_pyramid_market — position is same direction as requested
//
3. close_opposite_then_enter — close-only-opposite branch
//
4. sequential_same_tick_reversal_fill — opposite direction, another
//
same-direction market entry fills later this same tick (TV rule R*)
//
5. flip_market_position_to — opposite direction (close-and-flip)
void
BacktestEngine::execute_market_entry
(
const
std::string& id,
bool
is_long,
double
fill_price,
double
explicit_qty,
int
explicit_qty_type,
PositionSide created_position_side,
bool
close_only_opposite,
bool
is_priced_entry,
double
tv_carry_qty,
int
created_bar,
bool
later_same_tick_entry,
bool
paired_flat_market_transaction,
bool
explicit_qty_prequantized,
uint64_t
entry_incarnation) {
//
Degenerate-bar guard: never open a position at a non-finite fill price
//
(e.g. a NaN/Inf print). Dropping the fill keeps trade output finite and
//
a single bad tick from poisoning the backtest. Clean feeds never hit this.
if
(!
std::isfinite
(fill_price))
return
;
PositionSide requested = is_long ? PositionSide::
LONG
: PositionSide::
SHORT
;
bool
is_opposite_entry = position_side_ != PositionSide::
FLAT
&& position_side_ != requested;
bool
direction_blocked =
(risk_direction_ == RiskDirection::
LONG_ONLY
&& !is_long)
|| (risk_direction_ == RiskDirection::
SHORT_ONLY
&& is_long);
if
(is_opposite_entry && direction_blocked) {
double
exit_fill =
apply_slippage
(fill_price, position_side_ == PositionSide::
SHORT
);
execute_market_exit
(exit_fill);
if
(!paired_flat_market_transaction)
purge_exit_orders
();
return
;
}
//
Check risk rules before allowing entry
if
(!
check_risk_allow_entry
(is_long))
return
;
//
Apply slippage: buy fills higher, sell fills lower. LIMIT-triggered
//
fills (current_fill_is_limit_) take the unslipped limit-or-better
//
path instead — TV does not slip limit fills.
fill_price =
apply_fill_slippage
(fill_price, is_long);
if
(position_side_ == PositionSide::
FLAT
) {
enter_market_from_flat
(id, is_long, fill_price, explicit_qty, explicit_qty_type,
created_position_side, is_priced_entry, tv_carry_qty,
created_bar,
/*
explicit_qty_prequantized=
*/
(explicit_qty_prequantized
|| paired_flat_market_transaction),
entry_incarnation);
return
;
}
if
(position_side_ == requested) {
add_to_pyramid_market
(id, is_long, fill_price, explicit_qty, explicit_qty_type,
created_position_side, is_priced_entry,
entry_incarnation);
return
;
}
if
(created_position_side == PositionSide::
FLAT
&& close_only_opposite) {
close_opposite_then_enter
(
id, is_long, fill_price, explicit_qty, explicit_qty_type,
/*
purge_pending_exits=
*/
!paired_flat_market_transaction,
/*
explicit_qty_prequantized=
*/
(explicit_qty_prequantized
|| paired_flat_market_transaction),
entry_incarnation);
return
;
}
if
(later_same_tick_entry) {
sequential_same_tick_reversal_fill
(id, is_long, fill_price, explicit_qty,
explicit_qty_type,
entry_incarnation);
return
;
}
//
``close_only_opposite`` reaches here for a created_position_side != FLAT
//
reduce-only flip (the FLAT bracket case returned above via
//
close_opposite_then_enter). This is either a deferred-flip carry that
//
reverses a later position cycle, or the equality-only same-cycle frozen
//
transaction whose whole broker movement is consumed by the close. Both
//
close the live opposite position without opening their own leg.
flip_market_position_to
(id, is_long, fill_price, explicit_qty, explicit_qty_type,
explicit_qty_prequantized,
/*
close_only=
*/
close_only_opposite,
entry_incarnation);
}
//
Internal helper: execute a market exit (close position at fill price)
void
BacktestEngine::execute_market_exit
(
double
fill_price) {
if
(position_side_ == PositionSide::
FLAT
) {
return
;
}
//
Apply slippage: closing long = sell (lower), closing short = buy
//
(higher). LIMIT-triggered exits (TP brackets) take the unslipped
//
limit-or-better path via apply_fill_slippage.
bool
is_buy = (position_side_ == PositionSide::
SHORT
);
fill_price =
apply_fill_slippage
(fill_price, is_buy);
bool
was_long = (position_side_ == PositionSide::
LONG
);
//
Emit one Trade per pyramid entry (matches TradingView reporting)
for
(
auto
& pe : pyramid_entries_) {
emit_close_trade
(pe, pe.
qty
, fill_price, was_long);
}
reset_position_state_to_flat
();
}
//
FIFO-drain up to qty_limit from pyramid_entries_, optionally restricted to a
//
single from_entry id. See engine.hpp for the contract. Mirrors TradingView's
//
per-pyramid trade reporting: one Trade per drained slice. Returns total qty
//
drained so callers can assert / log if needed.
double
BacktestEngine::fifo_drain
(
const
std::string* from_entry,
double
qty_limit,
double
fill_price,
bool
was_long) {
double
qty_closed =
0.0
;
std::vector<PyramidEntry> remaining;
for
(
auto
& pe : pyramid_entries_) {
bool
eligible = (from_entry ==
nullptr
) || (pe.
entry_id
== *from_entry);
if
(!eligible || qty_closed >= qty_limit -
kQtyEpsilon
) {
remaining.
push_back
(pe);
continue
;
}
double
close_qty =
std::min
(pe.
qty
, qty_limit - qty_closed);
double
keep_qty = pe.
qty
- close_qty;
qty_closed += close_qty;
emit_close_trade
(pe, close_qty, fill_price, was_long);
if
(keep_qty >
kQtyEpsilon
) {
//
Scale the accumulated USD excursion to the kept slice so the
//
remaining entry's extremes stay consistent with its reduced
//
qty (update_per_trade_extremes accumulates (diff) * pe.qty).
double
keep_scale = keep_qty / pe.
qty
;
PyramidEntry kept = pe;
kept.
qty
= keep_qty;
kept.
max_runup
*= keep_scale;
kept.
max_drawdown
*= keep_scale;
//
Percent and per-contract fees follow the surviving quantity.
//
CASH_PER_ORDER remains one fee for the accepted entry order,
//
matching calc_commission's established qty-independent shape.
if
(
std::isfinite
(kept.
entry_commission_account
)
&& commission_type_ != CommissionType::
CASH_PER_ORDER
) {
kept.
entry_commission_account
*= keep_scale;
}
remaining.
push_back
(
std::move
(kept));
}
}
pyramid_entries_ =
std::move
(remaining);
position_qty_ -= qty_closed;
return
qty_closed;
}
//
Internal helper: execute a partial exit (reduce position by qty, create trade records)
//
TradingView creates individual trade records for each partial exit.
void
BacktestEngine::execute_partial_exit_qty
(
double
fill_price,
double
qty_to_close, PositionReductionCause cause) {
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
;
const
double
qty_before = position_qty_;
qty_to_close =
std::clamp
(qty_to_close,
0.0
, position_qty_);
if
(qty_to_close <=
kQtyEpsilon
)
return
;
bool
is_buy = (position_side_ == PositionSide::
SHORT
);
fill_price =
apply_fill_slippage
(fill_price, is_buy);
bool
was_long = (position_side_ == PositionSide::
LONG
);
//
Close FIFO across all pyramid entries, creating trade records.
fifo_drain
(
/*
from_entry=
*/
nullptr
, qty_to_close, fill_price, was_long);
settle_position_after_partial_exit
(qty_before, cause);
}
void
BacktestEngine::execute_partial_exit
(
double
fill_price,
double
qty_percent,
PositionReductionCause cause) {
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
;
double
pct =
std::clamp
(qty_percent,
0.0
,
100.0
);
double
qty_to_close = position_qty_ * (pct /
100.0
);
//
Percent-derived partial exit resolved at FILL time (an exit placed
//
while still FLAT carries no reserved qty): floor the lot to the
//
instrument qty step exactly like the placement-time path in
//
compute_exit_reserved_qty — see apply_exit_qty_step for the TV
//
dust-remainder evidence. Full exits (pct == 100%) stay exact.
if
(pct <
100.0
-
kFullPercentEps
) {
qty_to_close =
apply_exit_qty_step
(qty_to_close);
}
execute_partial_exit_qty
(fill_price, qty_to_close, cause);
}
//
Internal helper: close only entries matching from_entry (close_entries_rule="ANY")
void
BacktestEngine::execute_partial_exit_by_entry
(
double
fill_price,
const
std::string& from_entry,
PositionReductionCause cause) {
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
;
const
double
qty_before = position_qty_;
bool
is_buy = (position_side_ == PositionSide::
SHORT
);
fill_price =
apply_fill_slippage
(fill_price, is_buy);
bool
was_long = (position_side_ == PositionSide::
LONG
);
std::vector<PyramidEntry> remaining;
for
(
auto
& pe : pyramid_entries_) {
if
(pe.
entry_id
== from_entry) {
emit_close_trade
(pe, pe.
qty
, fill_price, was_long);
position_qty_ -= pe.
qty
;
}
else
{
remaining.
push_back
(pe);
}
}
pyramid_entries_ =
std::move
(remaining);
settle_position_after_partial_exit
(qty_before, cause);
}
//
Internal helper: close an exact quantity only from entries matching
//
from_entry. Live-position strategy.exit calls freeze their percent-derived
//
reservations into PendingOrder::qty; when layered siblings fill on one bar,
//
that absolute reservation must survive earlier reductions of the position.
void
BacktestEngine::execute_partial_exit_by_entry_qty
(
double
fill_price,
const
std::string& from_entry,
double
qty_to_close,
PositionReductionCause cause) {
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
;
if
(!
std::isfinite
(qty_to_close) || qty_to_close <=
kQtyEpsilon
)
return
;
const
double
qty_before = position_qty_;
bool
is_buy = (position_side_ == PositionSide::
SHORT
);
fill_price =
apply_fill_slippage
(fill_price, is_buy);
bool
was_long = (position_side_ == PositionSide::
LONG
);
fifo_drain
(&from_entry, qty_to_close, fill_price, was_long);
settle_position_after_partial_exit
(qty_before, cause);
}
//
Internal helper: resolve a genuinely deferred percentage at fill time, then
//
close that quantity only from entries matching from_entry.
void
BacktestEngine::execute_partial_exit_by_entry_percent
(
double
fill_price,
const
std::string& from_entry,
double
qty_percent,
PositionReductionCause cause) {
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
;
double
matched_qty =
0.0
;
for
(
const
auto
& pe : pyramid_entries_) {
if
(pe.
entry_id
== from_entry) matched_qty += pe.
qty
;
}
if
(matched_qty <=
kQtyEpsilon
)
return
;
double
pct =
std::clamp
(qty_percent,
0.0
,
100.0
);
double
qty_to_close = matched_qty * (pct /
100.0
);
if
(qty_to_close <=
kQtyEpsilon
)
return
;
execute_partial_exit_by_entry_qty
(fill_price, from_entry, qty_to_close, cause);
}
//
KI-62: after a from_entry PRICED bracket exit fills, scratch (close dur-0)
//
any same-bar same-id MARKET pyramid-add slice still open — it filled earlier
//
this bar, ahead of the exit in TV's open-tick fill sequence, so TV's exit
//
covers it. Targets ONLY flagged same-bar (entry_bar_index == bar_index_)
//
market-add slices of this from_entry: the frozen pre-add lot was already
//
drained by the normal close, and prior-bar slices (entry_bar_index <
//
bar_index_) are never touched — so multi-bar pyramids stay untouched. A
//
strict no-op when no such slice exists (the KEEP cell fills the exit first,
//
so the add is not yet open; non-collision shapes flag no add). Emits each
//
covered slice as its own dur-0 trade (entry at the add's fill price, exit at
//
this exit's fill price), matching TV's per-pyramid scratch reporting.
double
BacktestEngine::cover_samebar_market_adds_on_exit
(
const
PendingOrder& order,
double
fill_price,
PositionReductionCause cause) {
if
(order.
from_entry
.
empty
())
return
0.0
;
if
(position_side_ == PositionSide::
FLAT
|| pyramid_entries_.
empty
())
return
0.0
;
//
Scope to a PRICED bracket (stop/limit/trail). A plain market close /
//
close_all already flattens the whole position through its own path.
bool
priced_bracket = !
std::isnan
(order.
stop_price
)
|| !
std::isnan
(order.
limit_price
)
|| !
std::isnan
(order.
trail_points
)
|| !
std::isnan
(order.
trail_price
);
if
(!priced_bracket)
return
0.0
;
const
double
qty_before = position_qty_;
bool
is_buy = (position_side_ == PositionSide::
SHORT
);
double
slipped =
apply_fill_slippage
(fill_price, is_buy);
bool
was_long = (position_side_ == PositionSide::
LONG
);
std::vector<PyramidEntry> remaining;
remaining.
reserve
(pyramid_entries_.
size
());
double
closed =
0.0
;
for
(
auto
& pe : pyramid_entries_) {
if
(pe.
market_pyramid_add
&& pe.
entry_bar_index
== bar_index_
&& pe.
entry_id
== order.
from_entry
) {
emit_close_trade
(pe, pe.
qty
, slipped, was_long);
closed += pe.
qty
;
}
else
{
remaining.
push_back
(pe);
}
}
if
(closed <=
kQtyEpsilon
)
return
0.0
;
//
nothing covered
pyramid_entries_ =
std::move
(remaining);
position_qty_ -= closed;
settle_position_after_partial_exit
(qty_before, cause);
return
closed;
}
//
Internal helper: cancel OCA group members (except the one that just filled)
void
BacktestEngine::cancel_oca_group
(
const
std::string& oca_name,
const
std::string& exclude_id) {
if
(oca_name.
empty
())
return
;
pending_orders_.
erase
(
std::remove_if
(pending_orders_.
begin
(), pending_orders_.
end
(),
[&](
const
PendingOrder& o) {
return
o.
oca_name
== oca_name && o.
id
!= exclude_id;
}),
pending_orders_.
end
());
}
//
Pine v6 strategy.oca.reduce: when one sibling fills qty Q, every other
//
sibling's remaining qty is reduced by Q. Siblings whose remaining qty
//
reaches <= 0 are cancelled outright (matches TradingView behaviour and
//
degenerates to oca.cancel when the filling order's qty >= sibling qty).
//
Siblings using default sizing (qty == NaN) cannot have a meaningful
//
per-order qty applied at place time, so we conservatively cancel them
//
(this matches the prior, blanket-cancel behaviour for that subset).
void
BacktestEngine::reduce_oca_group
(
const
std::string& oca_name,
const
std::string& exclude_id,
double
filled_qty) {
if
(oca_name.
empty
())
return
;
if
(!(filled_qty >
0.0
))
return
;
//
nothing to subtract
pending_orders_.
erase
(
std::remove_if
(pending_orders_.
begin
(), pending_orders_.
end
(),
[&](PendingOrder& o) {
if
(o.
oca_name
!= oca_name || o.
id
== exclude_id)
return
false
;
if
(
std::isnan
(o.
qty
))
return
true
;
//
default-sized: cancel
o.
qty
-= filled_qty;
return
o.
qty
<=
kOcaQtyEpsilon
;
}),
pending_orders_.
end
());
}
void
BacktestEngine::purge_exit_orders
(
bool
retain_for_pending_entries) {
if
(retain_for_pending_entries) {
//
End-of-bar flat-purge: the position is flat, but a from_entry-bound
//
EXIT bracket whose parent ENTRY is still a PENDING order (e.g. a limit
//
entry that could not fill on its creation bar) must be RETAINED — once
//
the entry fills on a later bar the bracket fires, matching TV. Only
//
brackets with no live/pending parent entry are stale and dropped.
std::unordered_set<std::string> pending_entry_ids;
for
(
const
auto
& o : pending_orders_) {
if
(o.
type
== OrderType::
ENTRY
|| o.
type
== OrderType::
MARKET
) {
pending_entry_ids.
insert
(o.
id
);
}
}
pending_orders_.
erase
(
std::remove_if
(pending_orders_.
begin
(), pending_orders_.
end
(),
[&](
const
PendingOrder& o) {
return
o.
type
== OrderType::
EXIT
&& !(!o.
from_entry
.
empty
()
&& pending_entry_ids.
count
(o.
from_entry
));
}),
pending_orders_.
end
());
return
;
}
pending_orders_.
erase
(
std::remove_if
(pending_orders_.
begin
(), pending_orders_.
end
(),
[](
const
PendingOrder& o) {
return
o.
type
== OrderType::
EXIT
; }),
pending_orders_.
end
());
}
//
────────────────────────────────────────────────────────────────────
//
Shared close-side / position-state helpers
//
────────────────────────────────────────────────────────────────────
//
Emit one Trade record for closing close_qty of pyramid entry pe at
//
fill_price (already slippage-adjusted). Updates trades_, profit/loss
//
aggregates, intraday PnL, and consecutive-loss-day tracking exactly the
//
same way every closing path does — the body was previously inlined in
//
execute_market_exit, execute_partial_exit_qty, execute_partial_exit_by_entry,
//
execute_partial_exit_by_entry_percent, and the flip branch of
//
execute_market_entry. Mirrors TradingView's per-pyramid trade reporting.
void
BacktestEngine::emit_close_trade
(
const
PyramidEntry& pe,
double
close_qty,
double
fill_price,
bool
was_long) {
//
Realized PnL scales by the instrument point value ($ per point per
//
contract). Crypto/equity (pointvalue=1) is unchanged; futures (e.g. ES=50)
//
multiply the price-difference PnL. The price-difference component is in
//
the symbol's QUOTE currency; multiply by account_currency_fx_ (default
//
1.0, no-op for the corpus) to report in the strategy's ACCOUNT currency
//
— same conversion the margin gate (engine_strategy_commands.cpp) already
//
applies. Commission is already account-currency-native (calc_commission
//
applies the same fx factor internally for its PERCENT case; cash-per-*
//
is account-currency by construction), so it is NOT scaled again here.
const
double
pv = syminfo_.
pointvalue
;
double
pnl = (was_long ? (fill_price - pe.
price
) : (pe.
price
- fill_price))
* close_qty * pv *
active_account_currency_fx
();
const
double
entry_commission =
calc_commission
(pe.
price
, close_qty);
const
double
exit_commission =
calc_commission
(fill_price, close_qty);
pnl -= entry_commission + exit_commission;
//
TV "Net P&L %" convention (arbitrated 2026-06-12 vs TV export,
//
trade #258 short: 102.44 USD on 2276.66 entry => 4.50%): NET pnl
//
as a percent of entry cost (entry_price * qty * pointvalue, same
//
account_currency_fx_ conversion as pnl above so the ratio is
//
currency-invariant). Long/no-commission degenerates to the old
//
(exit/entry-1) form; shorts diverge on large moves ((entry/exit-1)
//
was wrong). Computed AFTER the commission subtraction above — order
//
matters.
const
double
entry_cost = pe.
price
* close_qty * pv
*
active_account_currency_fx
();
double
pnl_pct = (entry_cost >
0.0
) ? (pnl / entry_cost) *
100.0
:
0.0
;
Trade trade;
trade.
entry_time
= pe.
time
;
trade.
exit_time
= current_bar_.
timestamp
;
trade.
entry_price
= pe.
price
;
trade.
exit_price
= fill_price;
trade.
qty
= close_qty;
trade.
pnl
= pnl;
trade.
pnl_pct
= pnl_pct;
trade.
is_long
= was_long;
trade.
entry_bar_index
= pe.
entry_bar_index
;
trade.
exit_bar_index
= bar_index_;
trade.
entry_id
= pe.
entry_id
;
trade.
entry_incarnation
= pe.
entry_incarnation
;
trade.
entry_comment
= pe.
entry_comment
;
trade.
commission
= entry_commission + exit_commission;
//
Excursions: TV's per-trade excursion includes the exit fill itself —
//
a stop-out's adverse excursion is at least the loss at the SL fill and
//
a take-profit's favorable excursion includes the move to the TP fill.
//
The per-bar sampler (update_per_trade_extremes) cannot see this: exit
//
fills happen inside process_pending_orders and the pyramid entry is
//
removed before the next sample, so same-bar entry+exit trades would
//
otherwise report 0/0. Fold the fill price in here. The carried
//
per-entry extreme is scaled to the closed slice (close_qty/pe.qty) so
//
a partial close reports the slice's USD excursion, matching TV's
//
per-trade-record qty. Both fields stay >= 0 (Pine accessor convention);
//
the TV-export sign flip happens only in the CSV writer.
double
slice = (pe.
qty
>
kQtyEpsilon
) ? (close_qty / pe.
qty
) :
1.0
;
double
fill_fav = (was_long ? (fill_price - pe.
price
) : (pe.
price
- fill_price))
* close_qty;
double
runup =
std::max
(pe.
max_runup
* slice, fill_fav);
double
drawdown =
std::max
(pe.
max_drawdown
* slice, -fill_fav);
//
Priced (stop/limit/trail) exits fill mid-bar: the bar-path extremes the
//
assumed OHLC path reaches BEFORE the exit fill belong to this trade's
//
excursion, but per-bar sampling never sees them (the entry is removed
//
before the next update_per_trade_extremes). Fold them in here, honoring
//
the entry-side masks when the trade opened on this same bar (an extreme
//
that precedes the ENTRY fill is not part of the trade either).
//
TRAIL fills: the peak that armed the trail (fill +/- offset) is a
//
pre-fill favorable excursion no bar-boundary sample sees (TV reports
//
MFE == peak for trail exits).
if
(!
std::isnan
(fold_exit_trail_peak_)) {
double
peak_fav = (was_long ? (fold_exit_trail_peak_ - pe.
price
)
: (pe.
price
- fold_exit_trail_peak_))
* close_qty;
runup =
std::max
(runup, peak_fav);
}
if
(fold_exit_path_extremes_) {
double
fill_pos =
0.0
;
if
(
internal::first_touch_position
(current_bar_, fill_price, &fill_pos)) {
const
bool
high_first =
internal::bar_path_uses_high_first
(current_bar_);
const
double
high_pos = high_first ?
1.0
:
2.0
;
const
double
low_pos = high_first ?
2.0
:
1.0
;
const
bool
same_bar = (pe.
entry_bar_index
== bar_index_);
if
(high_pos < fill_pos && !(same_bar && pe.
skip_entry_bar_high
)) {
double
hi_fav = (was_long ? (current_bar_.
high
- pe.
price
)
: (pe.
price
- current_bar_.
high
)) * close_qty;
runup =
std::max
(runup, hi_fav);
drawdown =
std::max
(drawdown, -hi_fav);
}
if
(low_pos < fill_pos && !(same_bar && pe.
skip_entry_bar_low
)) {
double
lo_fav = (was_long ? (current_bar_.
low
- pe.
price
)
: (pe.
price
- current_bar_.
low
)) * close_qty;
runup =
std::max
(runup, lo_fav);
drawdown =
std::max
(drawdown, -lo_fav);
}
}
}
//
TV reports excursions on the NET OPEN-PROFIT basis: the entry-leg
//
commission is deducted from the favorable/adverse extremes (verified
//
numerically on pyramid-cash-fractional-commission-01 — TV's exported
//
excursions differ from the gross price excursion by exactly
//
qty * cash_per_contract on every trade, both columns). Favorable is
//
floored at 0 (TV never exports a negative favorable excursion —
//
confirmed across all 757k corpus rows); adverse grows by the entry
//
commission (open profit at the entry tick is already -commission).
//
Both fields remain >= 0 here (Pine positive-drawdown convention).
//
runup/drawdown are quote-currency (price-diff × qty); convert to
//
account currency via account_currency_fx_ (default 1.0, no-op) before
//
combining with entry_commission, which is already account-currency
//
(see calc_commission) — same convention as pnl above.
trade.
max_runup
=
std::max
(
0.0
, runup * pv *
active_account_currency_fx
() - entry_commission);
trade.
max_drawdown
= drawdown * pv *
active_account_currency_fx
()
+ entry_commission;
const
double
trade_pnl = trade.
pnl
;
trades_.
push_back
(
std::move
(trade));
net_profit_sum_ += trade_pnl;
if
(trade_pnl >
0
) { gross_profit_sum_ += trade_pnl; win_trades_count_++; }
else
if
(trade_pnl <
0
) { gross_loss_sum_ += trade_pnl; loss_trades_count_++; }
else
{ ++eventrades_count_; }
//
strategy.eventrades: exact zero P&L (TV uses == 0)
//
Update risk state: intraday PnL and consecutive loss day tracking
intraday_pnl_ += pnl;
if
(pnl <
0.0
) {
BarTime bt =
_decompose_bar_time_chart_tz
();
int
cur_day = bt.
dayofmonth
*
100
+ bt.
month
;
if
(cur_day != last_loss_day_) {
last_loss_day_ = cur_day;
cons_loss_day_count_++;
}
}
else
if
(pnl >
0.0
) {
cons_loss_day_count_ =
0
;
}
}
//
Reset all per-position state after the position is fully closed. Used by
//
every full-close path (execute_market_exit) and by partial-exit settlement
//
when the FIFO loop drained the position.
void
BacktestEngine::reset_position_state_to_flat
() {
position_side_ = PositionSide::
FLAT
;
position_cycle_seq_ =
0
;
position_entry_price_ =
0.0
;
opening_affordability_pending_ =
false
;
opening_affordability_eligible_ =
false
;
commissioned_all_in_market_long_opening_affordability_ =
false
;
opening_affordability_default_long_reversal_ =
false
;
close_then_short_opening_requires_adverse_retry_ =
false
;
commissioned_all_in_market_short_lifecycle_ =
false
;
default_market_direct_short_reversal_lifecycle_ =
false
;
opening_affordability_raw_fill_base_ =
std::numeric_limits<
double
>::
quiet_NaN
();
position_entry_time_ =
0
;
position_qty_ =
0.0
;
position_entry_count_ =
0
;
position_open_bar_ = -
1
;
trail_best_price_ = std::numeric_limits<
double
>::
quiet_NaN
();
pyramid_entries_.
clear
();
id_unclosed_qty_.
clear
();
//
Bracket legs live for the POSITION cycle, so the provenance that keeps
//
them alive dies exactly here — going flat is what makes a from_entry
//
bracket stale and un-fireable against a future same-id position.
cycle_filled_entry_ids_.
clear
();
close_reserved_qty_.
clear
();
close_two_call_first_qty_.
clear
();
callsite_close_reserved_qty_.
clear
();
callsite_close_two_call_first_qty_.
clear
();
consumed_partial_exit_ids_.
clear
();
}
//
After a partial exit potentially empties pyramid_entries_, either reset
//
position state to FLAT (no entries left or qty effectively zero) or
//
recompute volume-weighted average entry price across surviving entries.
//
Body was previously inlined identically at the end of every partial-exit
//
path.
void
BacktestEngine::settle_position_after_partial_exit
(
double
qty_before, PositionReductionCause cause) {
if
(position_qty_ <=
kQtyEpsilon
|| pyramid_entries_.
empty
()) {
reset_position_state_to_flat
();
}
else
{
double
total_qty =
0
, weighted_sum =
0
;
for
(
auto
& pe : pyramid_entries_) {
weighted_sum += pe.
price
* pe.
qty
;
total_qty += pe.
qty
;
}
position_entry_price_ = weighted_sum / total_qty;
//
TV returns a pyramid slot when the entry is retired by a close-path
//
order — the grid-bot family depends on it (3commas-ena: 1021 fills
//
over 64 reused ids, 776 entries between flats under a cap of 200,
//
never more than 50 CONCURRENT entries). TV does NOT return the slot
//
when the entry is drained by strategy.exit bracket fills
//
(thulashimohanr 2026-03-29: the 03-26 entry was fully retired by two
//
T1 fills and TV still refused the third entry).
if
(cause == PositionReductionCause::
BRACKET_EXIT
) {
position_entry_count_ =
std::max
(position_entry_count_, (
int
)pyramid_entries_.
size
());
}
else
{
position_entry_count_ = (
int
)pyramid_entries_.
size
();
}
//
The one-contract floor-zero rule belongs to an otherwise unmodified
//
commissioned all-in short lifecycle. Any script-driven surviving
//
reduction changes that shape. Broker margin-call reductions are the
//
sole exception: TV preserves the lifecycle across its own cascade.
if
(cause != PositionReductionCause::
MARGIN_CALL
&& position_side_ == PositionSide::
SHORT
&& position_qty_ +
kQtyEpsilon
< qty_before) {
commissioned_all_in_market_short_lifecycle_ =
false
;
default_market_direct_short_reversal_lifecycle_ =
false
;
}
}
}
//
Establish a fresh position at fill_price/qty after a transition from FLAT
//
or a same-bar close. Resets all per-position state and seeds the first
//
pyramid entry. Used by every entry path that opens a brand-new position
//
(FLAT entry, close-only-opposite remainder, opposite flip).
void
BacktestEngine::open_fresh_position
(PositionSide requested,
double
fill_price,
double
qty,
const
std::string& id,
uint64_t
entry_incarnation) {
position_side_ = requested;
position_cycle_seq_ = next_position_cycle_seq_++;
position_entry_price_ = fill_price;
//
The shared post-dispatch lifecycle hook queues the new fill's event.
//
Clear prior-cycle provenance now so reversals cannot expose it even
//
transiently.
opening_affordability_pending_ =
false
;
opening_affordability_eligible_ =
false
;
commissioned_all_in_market_long_opening_affordability_ =
false
;
opening_affordability_default_long_reversal_ =
false
;
close_then_short_opening_requires_adverse_retry_ =
false
;
commissioned_all_in_market_short_lifecycle_ =
false
;
default_market_direct_short_reversal_lifecycle_ =
false
;
opening_affordability_raw_fill_base_ =
std::numeric_limits<
double
>::
quiet_NaN
();
position_entry_time_ = current_bar_.
timestamp
;
position_qty_ = qty;
position_entry_count_ =
1
;
position_open_bar_ = bar_index_;
trail_best_price_ = fill_price;
pyramid_entries_.
clear
();
id_unclosed_qty_.
clear
();
cycle_filled_entry_ids_.
clear
();
close_reserved_qty_.
clear
();
close_two_call_first_qty_.
clear
();
callsite_close_reserved_qty_.
clear
();
callsite_close_two_call_first_qty_.
clear
();
consumed_partial_exit_ids_.
clear
();
pyramid_entries_.
push_back
({fill_price, current_bar_.
timestamp
, qty, id, bar_index_});
pyramid_entries_.
back
().
entry_incarnation
= entry_incarnation;
snapshot_entry_commission
(pyramid_entries_.
back
());
id_unclosed_qty_[id] += qty;
cycle_filled_entry_ids_.
insert
(id);
}
//
────────────────────────────────────────────────────────────────────
//
execute_market_entry case helpers
//
────────────────────────────────────────────────────────────────────
//
Drop the TV deferred-flip carry on every other pending priced entry that
//
was placed during the same source position cycle. TV consumes the carry
//
from the now-closed source position exactly once, so siblings (matching
//
``created_position_side``) must fire later with their own explicit qty
//
rather than re-applying the same carry growth.
//
//
Probe 93 (pyramiding=2, two opposite-direction stops armed during a long
//
cycle): the first sibling grows by |old|+qty=2, the second sibling fires
//
fresh at qty=1. Without this, both siblings would grow and the engine
//
emits an extra-qty pyramid add (or, after the cleanup-loop wipes the
//
survivor, an entire extra round-trip the next day).
//
//
Cycle scoping: TV consumes carry only when the triggering sibling fires
//
from FLAT. That means a sibling armed in a LATER position cycle (whose
//
own ``tv_carry_qty`` was captured from a different source position
//
later than this firing entry's ``created_bar``) must keep its carry
//
independent — it will apply its own carry when it later fires from
//
flat. Without this scoping, consuming a future-cycle sibling's carry
//
drops one cycle's worth of qty (validation/52, 63, 72, 92, 93, 95, 96
//
pre-fix: row count + qty match TV exactly, but per-leg PnL drifts
//
because the chain qty schedule shifts by one cycle when a sibling is
//
pre-emptively wiped by an earlier-cycle fire).
void
BacktestEngine::consume_tv_carry_from_siblings
(
const
std::string& id,
PositionSide created_position_side,
int
created_bar) {
for
(
auto
& other : pending_orders_) {
if
(other.
id
== id)
continue
;
if
(other.
created_position_side
!= created_position_side)
continue
;
if
(other.
tv_carry_qty
<=
0.0
)
continue
;
//
Cycle-scope: only consume siblings placed no later than the
//
firing order's own placement. Siblings placed in a LATER bar
//
captured carry from a DIFFERENT source position cycle and own
//
their carry — TV does not pre-emptively wipe them.
if
(other.
created_bar
> created_bar)
continue
;
other.
tv_carry_qty
=
0.0
;
}
}
//
Open a new position from FLAT.
//
//
TradingView's deferred-flip growth rule (probes 52, 63, 72, 92): a priced
//
(stop/limit) entry that was placed while the strategy held an
//
OPPOSITE-direction position carries that position's qty forward. If the
//
original position is later closed (by strategy.close, close_all, or any
//
exit) and the priced entry now fires from FLAT, the new position opens at
//
``qty + tv_carry_qty`` rather than just ``qty`` — as if it had been a
//
true flip of the original (now-closed) position.
//
//
The carry persists across bars: in probe 92 the daily cleanup
//
(``strategy.close_all``) closes the long at chart 12:15 and the SE stop
//
fires hours later at 21:30, still applying the carry. So this helper
//
reads ``tv_carry_qty`` from the pending order itself (snapshotted at
//
placement, see PendingOrder struct in engine.hpp) rather than a per-bar
//
transient state.
//
//
Conditions:
//
(a) is_priced_entry -- stop/limit, not market
//
(b) tv_carry_qty > 0 -- order was placed while a position
//
was open
//
(c) requested != created direction
//
-- new entry is opposite to the carry
//
position
//
//
Do not gate this on the compile-time ``script_has_strategy_close_`` AST
//
scan. A bracket from ``strategy.exit`` can close the source position too,
//
and adding an unreachable ``strategy.close`` must be semantically inert.
//
//
After applying the carry, ``consume_tv_carry_from_siblings`` zeroes the
//
same-source-cycle siblings so probe 93 doesn't double-grow.
//
//
Final guard: TradingView margin check. required_margin = qty * fill_price
//
* margin_pct / 100. If required_margin > available equity, TV silently
//
rejects the fill (the order simply does not appear in the trade list).
//
With default margin_long_/margin_short_ = 100 (1x leverage) this is just
//
"position value <= equity". Reproduces the IES/VCP/ies-probe-08 entry-skip
//
behaviour where dynamic-qty strategies over-leverage on low-ATR bars and
//
TV silently drops the entry while the engine fires it (matched-trade qty
//
ratio in probe 08 was empirically equal to engine_equity / TV_equity,
//
proving the math is right but the gate was missing).
void
BacktestEngine::enter_market_from_flat
(
const
std::string& id,
bool
is_long,
double
fill_price,
double
explicit_qty,
int
explicit_qty_type,
PositionSide created_position_side,
bool
is_priced_entry,
double
tv_carry_qty,
int
created_bar,
bool
explicit_qty_prequantized,
uint64_t
entry_incarnation) {
const
bool
carry_was_long =
created_position_side == PositionSide::
LONG
;
const
bool
tv_deferred_flip =
is_priced_entry
&& tv_carry_qty >
0.0
&& (carry_was_long ? !is_long : is_long);
double
base_qty = explicit_qty_prequantized
? explicit_qty
:
calc_qty_for_type
(fill_price, explicit_qty, explicit_qty_type);
double
qty = tv_deferred_flip ? (tv_carry_qty + base_qty) : base_qty;
if
(tv_deferred_flip) {
consume_tv_carry_from_siblings
(id, created_position_side, created_bar);
}
//
NOTE: for EXPLICIT-qty market entries the margin check is performed at
//
SIGNAL time inside strategy_entry / queue_deferred_close_order, NOT here
//
at fill time. This matches TV's broker emulator, which rejects entries
//
whose qty * SIGNAL_BAR_CLOSE exceeds equity. By the time we reach this
//
fill-side helper such an order has already been admitted (or rejected)
//
at signal time, and the next-bar slippage between signal close and fill
//
open should NOT flip a TV-accepted entry into a reject. The empirical
//
base — parity-probe-{03..06} + ies-probe-08 — is entirely explicit-qty /
//
pct<100 / headroom sizing, so the claim is scoped to it. The one FROZEN
//
default-sized carve-out that TV DOES re-check and drop at fill (a
//
percent==100, zero-commission, true-flat above-lot gap) is handled by
//
the gap-reject gate in apply_filled_order_to_state, upstream of this
//
helper — a dropped order never reaches enter_market_from_flat.
PositionSide requested = is_long ? PositionSide::
LONG
: PositionSide::
SHORT
;
open_fresh_position
(requested, fill_price, qty, id, entry_incarnation);
}
//
Add to an existing same-direction position (pyramiding).
//
//
Pyramiding limit applies at fill time, EXCEPT for priced (stop/limit)
//
entries that were placed while the position was either FLAT or holding
//
the OPPOSITE direction. Such entries were "armed" pre-position (probe
//
80's morning short stop firing on top of the afternoon's short entry
//
confirms the flat-armed case) or placed as a flip-prep stop during a
//
previous opposite-direction cycle that has since closed (probe 72's S2
//
placed while LONG'ing via L2 and TV emits the second-sibling short trade
//
despite pyramiding=1). Market entries — and entries placed while already
//
in the SAME direction — still respect the limit (probe 54's two same-bar
//
same-direction market entries with pyramiding=1 must keep only the first
//
one).
void
BacktestEngine::add_to_pyramid_market
(
const
std::string& id,
bool
is_long,
double
fill_price,
double
explicit_qty,
int
explicit_qty_type,
PositionSide created_position_side,
bool
is_priced_entry,
uint64_t
entry_incarnation) {
PositionSide requested = is_long ? PositionSide::
LONG
: PositionSide::
SHORT
;
bool
flat_armed_priced =
is_priced_entry && created_position_side == PositionSide::
FLAT
;
bool
pre_armed_opposite_priced =
is_priced_entry
&& created_position_side != PositionSide::
FLAT
&& created_position_side != requested;
if
(!flat_armed_priced && !pre_armed_opposite_priced
&& position_entry_count_ >= pyramiding_) {
return
;
}
double
new_qty =
calc_qty_for_type
(fill_price, explicit_qty, explicit_qty_type);
//
Zero-lot add safety net. The fill kernel (apply_filled_order_to_state's
//
zero-lot decline) consumes such an order before it reaches here; should
//
any path bypass that gate, never materialize a qty-0 pyramid lot nor
//
spend a pyramiding slot on it — TV does not place the order at all.
if
(!(new_qty >
kQtyEpsilon
))
return
;
double
total_qty = position_qty_ + new_qty;
position_entry_price_ = (position_entry_price_ * position_qty_ + fill_price * new_qty) / total_qty;
position_qty_ = total_qty;
position_entry_count_++;
trail_best_price_ = fill_price;
pyramid_entries_.
push_back
({fill_price, current_bar_.
timestamp
, new_qty, id, bar_index_});
pyramid_entries_.
back
().
entry_incarnation
= entry_incarnation;
snapshot_entry_commission
(pyramid_entries_.
back
());
//
KI-62: only a same-direction MARKET add is scratched by a same-bar
//
from_entry bracket exit; a priced pyramid add is not this collision.
pyramid_entries_.
back
().
market_pyramid_add
= !is_priced_entry;
id_unclosed_qty_[id] += new_qty;
cycle_filled_entry_ids_.
insert
(id);
}
//
close_only_opposite branch: TV semantic for opposite-direction entries
//
where the strategy.entry call was placed with ``close_only_opposite=true``
//
— close part of the existing opposite position by tx_qty, then open the
//
requested-direction remainder if any.
void
BacktestEngine::close_opposite_then_enter
(
const
std::string& id,
bool
is_long,
double
fill_price,
double
explicit_qty,
int
explicit_qty_type,
bool
purge_pending_exits,
bool
explicit_qty_prequantized,
uint64_t
entry_incarnation) {
double
tx_qty = explicit_qty_prequantized
? explicit_qty
:
calc_qty_for_type
(fill_price, explicit_qty, explicit_qty_type);
double
close_qty =
std::min
(tx_qty, position_qty_);
//
execute_partial_exit_qty applies slippage internally (mirrors its other
//
callers, e.g. execute_partial_exit_by_percent). Pass the RAW fill_price —
//
pre-slipping here would double-slip the close leg (issue #27).
execute_partial_exit_qty
(fill_price, close_qty);
//
The ordinary close-only-opposite path historically purges stale exits.
//
A confirmed same-source flat MARKET pair is different: both transaction
//
legs execute inside process_pending_orders, where erasing the vector
//
would invalidate the active order reference and filled-index ledger.
//
Its stale exits follow flip_market_position_to's safe next-pass cleanup.
if
(purge_pending_exits)
purge_exit_orders
();
double
remainder = tx_qty - close_qty;
if
(remainder <=
kQtyEpsilon
) {
return
;
}
fill_price =
apply_fill_slippage
(fill_price, is_long);
PositionSide requested = is_long ? PositionSide::
LONG
: PositionSide::
SHORT
;
open_fresh_position
(
requested, fill_price, remainder, id, entry_incarnation);
}
//
Opposite direction: close current position trade-by-pyramid then open new
//
position in requested direction at the entry-slipped fill_price.
//
//
Standard Pine semantic: an in-position flip (``strategy.entry`` while
//
holding the opposite side) closes the existing position and opens a fresh
//
position with ``qty`` from ``strategy.entry``'s ``qty`` parameter — not
//
``|old| + qty``. Verified empirically with probe 92: TV produces 328
//
qty=1 in-position flips (SE stop firing while still long) and only 20
//
qty=2 flips that all happen AFTER a same-day cleanup closed the long.
//
The qty=2 cases are handled by the paired-close growth rule in
//
``enter_market_from_flat``; this branch keeps the standard
//
``new_size = qty`` contract.
//
//
We deliberately do NOT purge exit orders here. Mutating pending_orders_
//
mid-iteration of process_pending_orders shifts indices and corrupts the
//
filled_indices accounting. Stale exits targeting the old entry id get
//
cleaned up on the next bar by the "from_entry doesn't match any pyramid
//
entry" check in process_pending_orders. Newly-placed exits that target
//
the incoming entry id stay and evaluate correctly on the current bar's
//
remaining iterations.
void
BacktestEngine::flip_market_position_to
(
const
std::string& id,
bool
is_long,
double
fill_price,
double
explicit_qty,
int
explicit_qty_type,
bool
explicit_qty_prequantized,
bool
close_only,
uint64_t
entry_incarnation) {
//
For the close we need exit slippage based on closing direction.
//
Closing a long = sell (price - slip); closing a short = buy (price + slip).
//
fill_price already has entry slippage applied; un-slip it before
//
re-applying with the exit direction.
double
raw_price = fill_price;
if
(slippage_ !=
0
&& !current_fill_is_limit_) {
//
LIMIT-triggered entry fills were never slipped (limit-or-better
//
path), so there is no entry slip to back out for the close leg.
double
slip = slippage_ * syminfo_mintick_;
raw_price = is_long ? (fill_price - slip) : (fill_price + slip);
}
//
Flag-aware close leg: a limit-triggered flip's close leg follows the
//
entry leg's limit semantics (unslipped, limit-or-better) for internal
//
consistency with the sibling close_opposite_then_enter path — both
//
legs land at the identical unslipped snapped price. No direct TV
//
evidence yet (needs a limit-flip slippage>0 export) — corpus provably
//
indifferent at slippage=0.
double
exit_fill =
apply_fill_slippage
(raw_price, position_side_ == PositionSide::
SHORT
);
bool
was_long = (position_side_ == PositionSide::
LONG
);
//
Emit one Trade per pyramid entry (matches TradingView reporting)
for
(
auto
& pe : pyramid_entries_) {
emit_close_trade
(pe, pe.
qty
, exit_fill, was_long);
}
//
The old lots are fully realized above. Clear their live position/PnL
//
and entry-fee snapshots before sizing the incoming leg; otherwise a
//
percent-typed reversal adds stale open PnL and debits the already-
//
realized entry commission a second time.
reset_position_state_to_flat
();
if
(close_only) {
//
Priced-entry reduce-only cases: either this order was armed during a
//
prior cycle and flips a later opposite position, or its same-cycle
//
frozen transaction exactly equals the grown live opposite position.
//
In both cases close the whole position and stay flat; do NOT open.
//
See apply_entry_order_fill's close_only_opposite predicates.
return
;
}
//
Default-sized MARKET quantities are frozen and exchange-quantized at
//
signal time. Every sibling dispatch path preserves that provenance;
//
ordinary flips must not feed the frozen contracts through qty_step a
//
second time (binary64 can turn 1.3410 into 1.3409 on the second floor).
//
Explicit/FIXED quantities keep their normal single fill-side floor.
double
new_qty = explicit_qty_prequantized
? explicit_qty
:
calc_qty_for_type
(fill_price, explicit_qty, explicit_qty_type);
PositionSide requested = is_long ? PositionSide::
LONG
: PositionSide::
SHORT
;
open_fresh_position
(requested, fill_price, new_qty, id, entry_incarnation);
}
//
TradingView same-tick multi-entry sequential-fill semantics (audit rule
//
R*, jevondijefferson-big-breakout-strategy, 2026-07-02 tv-ceiling audit —
//
validated 26/26 against every in-window race in the TV export):
//
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL