@@ -492,25 +492,6 @@ struct PendingOrder {
492492 // LONG and must not be mistaken for an order born in the later LONG
493493 // cycle. Zero means the order was created while broker-flat.
494494 int64_t created_position_cycle_seq = 0 ;
495- // True when a successful strategy.close/close_all call earlier in this
496- // same on_bar already targeted live quantity. This remains distinct from
497- // created_position_side: an immediate full close makes the engine truly
498- // FLAT before a paired reentry is placed, but that reentry is not an
499- // independent true-flat opening for KI-61 affordability purposes.
500- bool created_after_position_close_in_bar = false ;
501- // True when this SAME-direction MARKET/ENTRY order was OVER the pyramiding
502- // cap at PLACEMENT — i.e. the position was already held in this order's
503- // direction with position_entry_count_ >= pyramiding_ at the moment it was
504- // placed. Snapshotted at every entry placement site so it mirrors the
505- // fill-time pyramiding gate (add_to_pyramid_market / the strategy.order add
506- // gate) exactly. The post-full-close same-direction wipe reads this to
507- // distinguish a TV-admissible (within-cap) co-queue — which survives a
508- // deferred full close that flattens on the fill bar — from one TradingView
509- // rejects at placement (over cap), which must still be cancelled even though
510- // the co-queued close zeroed position_entry_count_ before the add's fill-time
511- // gate ran. See classify_order_eligibility / compact_filled_pending_orders
512- // and test_close_all_coqueued_entry.cpp.
513- bool over_pyramiding_cap_at_placement = false ;
514495 // Call-bar provenance for the one deferred close_all whose post-fill
515496 // cleanup may preserve this order. Set only on a PRIOR-bar, pure-STOP
516497 // strategy.entry that was under the pyramiding cap and reused the id of a
@@ -907,6 +888,27 @@ struct PendingOrder {
907888 ShortSeedCollisionRole::NONE ;
908889};
909890
891+ // These views derive historical placement facts from the original command.
892+ // No current position, current configuration or mutable sizing participates.
893+ // The close fact describes previously accepted close claims, not physical
894+ // flatness; even an immediate close can leave that source-time fact positive.
895+ // The capacity view uses the original direction/count/cap (including cap0),
896+ // which remains meaningful after fills or a later configuration change.
897+ // Missing observations retain the historical default false for manual orders.
898+ // The comparison matches the broker's existing quantity tolerance exactly.
899+ inline bool placement_has_prior_close (const PendingOrder& order) {
900+ const auto & observation = order.market_admission .observation ();
901+ return observation && observation->prior_close_quantity > 1e-10 ;
902+ }
903+ inline bool placement_at_entry_capacity (const PendingOrder& order) {
904+ const auto & observation = order.market_admission .observation ();
905+ if (!observation) return false ;
906+ const auto requested_side = observation->buy ? PositionSide::LONG : PositionSide::SHORT ;
907+ return observation->placement_side != static_cast <int >(PositionSide::FLAT )
908+ && observation->placement_side == static_cast <int >(requested_side)
909+ && observation->held_entries >= observation->configuration .pyramiding ;
910+ }
911+
910912 } // inline namespace engine_script_run_v9 (PendingOrder)
911913
912914// default_qty_type constants (matches TradingView)
@@ -1956,7 +1958,7 @@ class BacktestEngine {
19561958 || order.type != OrderType::MARKET || order.incarnation == 0
19571959 || order.created_bar != bar_index_
19581960 || order.created_position_side != PositionSide::FLAT
1959- || order. created_after_position_close_in_bar
1961+ || placement_has_prior_close ( order)
19601962 || (order.replaced_order_incarnation != 0 )
19611963 || order.oca_type != 0 || !order.oca_name .empty ()
19621964 || position_side_ != PositionSide::FLAT
@@ -2000,7 +2002,7 @@ class BacktestEngine {
20002002 || !order.is_long || order.incarnation == 0
20012003 || order.created_bar != bar_index_
20022004 || order.created_position_side != PositionSide::FLAT
2003- || order. created_after_position_close_in_bar
2005+ || placement_has_prior_close ( order)
20042006 || order.birth .from_fill () || (order.replaced_order_incarnation != 0 )
20052007 || !order.oca_name .empty () || order.oca_type != 0
20062008 || position_side_ != PositionSide::FLAT || position_entry_count_ != 0
@@ -2039,7 +2041,7 @@ class BacktestEngine {
20392041 || (order.pine_frozen_market_instruction .transaction ()
20402042 && order.pine_frozen_market_instruction .transaction ()->transaction_units
20412043 == order.pine_frozen_market_instruction .transaction ()->own_units
2042- && !order. over_pyramiding_cap_at_placement ));
2044+ && !placement_at_entry_capacity ( order) ));
20432045 if (!default_all_in && !explicit_fixed) return false ;
20442046 const double mark = default_all_in ? order.sizing_mark
20452047 : order.affordability_signal_price ;
0 commit comments