@@ -76,10 +76,6 @@ class DeferredFlipProbe : public BacktestEngine {
7676 slippage_ = 0 ;
7777 commission_value_ = 0 ;
7878 pyramiding_ = 1 ;
79- // The deferred-flip rule is gated on the script having a
80- // ``strategy.close`` call at compile time. The codegen would
81- // set this flag from an AST scan; here we set it directly.
82- script_has_strategy_close_ = true ;
8379 }
8480
8581 void on_bar (const Bar& bar) override {
@@ -126,6 +122,50 @@ class DeferredFlipProbe : public BacktestEngine {
126122 int last_bar_index_seen = -1 ;
127123};
128124
125+ // Metamorphic probe for issue #141. Both instances execute exactly the same
126+ // broker commands: a bracket closes the source long, then a pre-armed priced
127+ // short entry fires from flat. The only difference is the legacy codegen AST
128+ // bit that says whether the Pine source contains any strategy.close call.
129+ // An unreachable call can change that bit but cannot change runtime behavior.
130+ class BracketExitDeferredFlipProbe : public BacktestEngine {
131+ public:
132+ explicit BracketExitDeferredFlipProbe (bool ast_has_strategy_close) {
133+ initial_capital_ = 1'000'000 ;
134+ default_qty_type_ = QtyType::FIXED ;
135+ default_qty_value_ = 1.0 ;
136+ slippage_ = 0 ;
137+ commission_value_ = 0 ;
138+ pyramiding_ = 1 ;
139+ syminfo_mintick_ = 0.01 ;
140+ script_has_strategy_close_ = ast_has_strategy_close;
141+ }
142+
143+ void on_bar (const Bar&) override {
144+ if (bar_index_ == 0 ) {
145+ strategy_entry (" L" , true ,
146+ std::numeric_limits<double >::quiet_NaN (),
147+ std::numeric_limits<double >::quiet_NaN (), 1.0 );
148+ }
149+ if (bar_index_ == 1 && position_side_ == PositionSide::LONG ) {
150+ strategy_entry (" S" , false ,
151+ /* limit=*/ 105.0 ,
152+ std::numeric_limits<double >::quiet_NaN (), 1.0 );
153+ strategy_exit (" XL" , " L" ,
154+ std::numeric_limits<double >::quiet_NaN (),
155+ /* stop=*/ 95.0 );
156+ }
157+ if (bar_index_ == 3 ) {
158+ final_side = position_side_;
159+ final_qty = position_qty_;
160+ closed_trade_count = trade_count ();
161+ }
162+ }
163+
164+ PositionSide final_side = PositionSide::FLAT ;
165+ double final_qty = 0.0 ;
166+ int closed_trade_count = -1 ;
167+ };
168+
129169} // namespace
130170
131171// Scenario 1: deferred-flip oracle (probe 95-style). Cycle qty grows
@@ -170,6 +210,36 @@ static void test_deferred_flip_chain_grows() {
170210 CHECK (max_qty >= 2 );
171211}
172212
213+ // Scenario 1b: a strategy.exit bracket is itself sufficient to close the
214+ // source position before a priced opposite entry fires. The global AST scan
215+ // for strategy.close must not decide whether the captured carry applies.
216+ static void test_unreachable_strategy_close_is_semantically_inert () {
217+ std::printf (" test_unreachable_strategy_close_is_semantically_inert\n " );
218+ Bar bars[4 ] = {
219+ {100 , 101 , 99 , 100 , 1000 , 60'000 },
220+ {100 , 101 , 99 , 100 , 1000 , 120'000 }, // L fills; arm S + XL
221+ {100 , 101 , 94 , 95 , 1000 , 180'000 }, // XL closes L; S untouched
222+ {100 , 106 , 99 , 105 , 1000 , 240'000 }, // S limit fires from flat
223+ };
224+
225+ BracketExitDeferredFlipProbe without_dead_close (false );
226+ BracketExitDeferredFlipProbe with_dead_close (true );
227+ without_dead_close.run (bars, 4 );
228+ with_dead_close.run (bars, 4 );
229+
230+ CHECK (without_dead_close.last_error ().empty ());
231+ CHECK (with_dead_close.last_error ().empty ());
232+ CHECK (without_dead_close.final_side == PositionSide::SHORT );
233+ CHECK (with_dead_close.final_side == PositionSide::SHORT );
234+ CHECK (near (without_dead_close.final_qty , 2.0 ));
235+ CHECK (near (with_dead_close.final_qty , 2.0 ));
236+ CHECK (near (without_dead_close.final_qty , with_dead_close.final_qty ));
237+ CHECK (without_dead_close.closed_trade_count == 1 );
238+ CHECK (with_dead_close.closed_trade_count == 1 );
239+ CHECK (without_dead_close.closed_trade_count ==
240+ with_dead_close.closed_trade_count );
241+ }
242+
173243// Scenario 2: deferred-flip is gated on opposite direction. A
174244// pre-armed SAME-direction priced entry (long stop placed during a
175245// long, position closes, long stop later fires from flat) should NOT
@@ -191,7 +261,6 @@ static void test_same_direction_no_carry() {
191261 slippage_ = 0 ;
192262 commission_value_ = 0 ;
193263 pyramiding_ = 1 ;
194- script_has_strategy_close_ = true ;
195264 }
196265 void on_bar (const Bar& bar) override {
197266 if (bar_index_ == 0 ) {
@@ -323,7 +392,6 @@ static void test_two_cycle_siblings_independent_carry() {
323392 slippage_ = 0 ;
324393 commission_value_ = 0 ;
325394 pyramiding_ = 5 ;
326- script_has_strategy_close_ = true ;
327395 }
328396 void on_bar (const Bar& bar) override {
329397 (void )bar;
@@ -487,7 +555,6 @@ static void test_per_leg_fifo_pnl_three_legs() {
487555 slippage_ = 0 ;
488556 commission_value_ = 0 ;
489557 pyramiding_ = 3 ;
490- script_has_strategy_close_ = true ;
491558 }
492559 void on_bar (const Bar& bar) override {
493560 (void )bar;
@@ -689,6 +756,7 @@ static void test_overcap_same_id_reissue_removes_old_pending_order() {
689756
690757int main () {
691758 test_deferred_flip_chain_grows ();
759+ test_unreachable_strategy_close_is_semantically_inert ();
692760 test_same_direction_no_carry ();
693761 test_two_cycle_siblings_independent_carry ();
694762 test_per_bar_pending_close_resets_in_script_tf_run ();
0 commit comments