|
16 | 16 | #include "bar.hpp" |
17 | 17 | #include "broker_events.hpp" |
18 | 18 | #include "quantity_intent.hpp" |
| 19 | +#include "execution.hpp" |
19 | 20 | #include "market_admission.hpp" |
20 | 21 | #include "reservation_expansion.hpp" |
21 | 22 | #include "order_cancellation.hpp" |
@@ -248,10 +249,10 @@ struct PyramidEntry { |
248 | 249 | // Entry-leg commission in account currency at this slice's actual fill |
249 | 250 | // boundary. Percentage commission depends on quote->account FX, so an |
250 | 251 | // effective-time provider must not retroactively reprice this already-paid |
251 | | - // fee when a later rate becomes active. Closed-trade reporting deliberately |
252 | | - // ignores this snapshot: TV converts the complete realized trade at the |
253 | | - // exit-time FX rate. NaN is reserved for legacy/synthetic test injection; |
254 | | - // every production entry path initializes the snapshot at fill time. |
| 252 | + // fee when a later rate becomes active. Partial realization allocates this |
| 253 | + // paid cost proportionally and leaves only the unconsumed cost on the lot, |
| 254 | + // independently of the current fee schedule. NaN is reserved for legacy/ |
| 255 | + // synthetic injection; every production entry path captures a real quote. |
255 | 256 | double entry_commission_account = |
256 | 257 | std::numeric_limits<double>::quiet_NaN(); |
257 | 258 | // Monotonic per-run identity of the PendingOrder object whose broker fill |
@@ -1790,6 +1791,18 @@ class BacktestEngine { |
1790 | 1791 | // --- Per-trade extreme tracking --- |
1791 | 1792 | void update_per_trade_extremes(); |
1792 | 1793 |
|
| 1794 | + // Trusted native matching-adapter extension: settle an already resolved |
| 1795 | + // execution into this engine's one physical book. This does not place an |
| 1796 | + // order, perform admission/slippage, or provide cancellation/replay. The |
| 1797 | + // owning run must abort on an exception; failed commits are not retryable |
| 1798 | + // in place. A saved arithmetic plan is not execution authority. |
| 1799 | + execution::Result settle_resolved_execution(const execution::Action& action, |
| 1800 | + const execution::Fill& fill); |
| 1801 | + // Native account value: realized balance plus marked physical lots minus |
| 1802 | + // their remaining paid entry costs, for every fee type. No Pine sizing or |
| 1803 | + // end-of-range reporting convention participates in this value. |
| 1804 | + double marked_equity(double price) const; |
| 1805 | + |
1793 | 1806 | // --- Strategy order commands --- |
1794 | 1807 | // NOTE: prior to v0.2 the runtime accepted a leading `double market_price` |
1795 | 1808 | // positional after `is_long`. The implementation never read it; every |
@@ -2428,6 +2441,12 @@ class BacktestEngine { |
2428 | 2441 | : calc_commission(pe.price, pe.qty); |
2429 | 2442 | } |
2430 | 2443 |
|
| 2444 | + double allocated_entry_commission(const PyramidEntry& pe, double units) const { |
| 2445 | + if (units <= 0.0) return 0.0; |
| 2446 | + const double paid = open_entry_commission(pe); |
| 2447 | + return units >= pe.qty ? paid : paid * (units / pe.qty); |
| 2448 | + } |
| 2449 | + |
2431 | 2450 | void snapshot_entry_commission(PyramidEntry& pe) const { |
2432 | 2451 | pe.entry_commission_account = calc_commission(pe.price, pe.qty); |
2433 | 2452 | } |
@@ -3851,6 +3870,8 @@ class BacktestEngine { |
3851 | 3870 | uint64_t entry_incarnation); |
3852 | 3871 | void execute_market_exit(double fill_price); |
3853 | 3872 | void append_same_side_fill(PyramidEntry lot); |
| 3873 | + void append_quoted_lot(PyramidEntry lot, double total_qty, double average_price); |
| 3874 | + void open_quoted_position(PositionSide requested, PyramidEntry lot); |
3854 | 3875 | // Range-end accounting: record the rows that close a position still |
3855 | 3876 | // open after the final script bar at that bar's close, the way |
3856 | 3877 | // TradingView's deep-backtest report does (engine_orders.cpp). Called by |
@@ -4200,13 +4221,24 @@ class BacktestEngine { |
4200 | 4221 | // engine_orders.cpp). |
4201 | 4222 | void emit_close_trade(const PyramidEntry& pe, double close_qty, |
4202 | 4223 | double fill_price, bool was_long); |
| 4224 | + void record_close_trade(Trade trade); |
| 4225 | + void validate_close_trade_counters(const Trade* rows, size_t count) const; |
| 4226 | + // Quote one resolved execution's current charges. Entry costs on the |
| 4227 | + // closed rows are historical allocations. Returns close shares in FIFO |
| 4228 | + // order followed by the opening share (zero when there is no opening). |
| 4229 | + std::vector<double> quote_execution_commissions( |
| 4230 | + const std::vector<double>& closed_units, double opening_units, |
| 4231 | + const execution::Fill& fill) const; |
4203 | 4232 | // The arithmetic of emit_close_trade without its bookkeeping: the Trade |
4204 | 4233 | // row a close of ``close_qty`` of ``pe`` at ``fill_price`` on the |
4205 | 4234 | // current bar would record (pnl, pnl_pct, commission, excursions, bar |
4206 | 4235 | // indexes). emit_close_trade builds and commits; the range-end close |
4207 | 4236 | // builds only. |
4208 | 4237 | Trade build_close_trade(const PyramidEntry& pe, double close_qty, |
4209 | 4238 | double fill_price, bool was_long) const; |
| 4239 | + Trade build_close_trade_with_costs(const PyramidEntry& pe, double close_qty, |
| 4240 | + double fill_price, bool was_long, double entry_commission, |
| 4241 | + double exit_commission) const; |
4210 | 4242 | // FIFO-drain up to qty_limit from pyramid_entries_, in order, splitting the |
4211 | 4243 | // boundary entry as needed. When from_entry is non-null only entries whose |
4212 | 4244 | // entry_id == *from_entry are eligible (others are kept untouched); null |
|
0 commit comments