Repository navigation
Commit 7a1f01c
Pine adapter: the same-pass position view after a close fill, the close's owed follow-up, open-marketable exits first, and same-side global exits, pinned by TradingView controls (#347)
* Pine adapter: a re-issued exit filled at its call keeps the pass's position view
Under process_orders_on_close, exit() fills a re-issued strategy.exit of a
one-lot short whose stop or limit is already through the bar's close at the
call (its current-close path, execute_current). Unlike an ordinary close_all
(KI-64), it did not freeze the script's position view, so the rest of the
pass read strategy.position_size == 0 and a reset strategy.position_avg_price.
A late observer, strategy.position_size[1] < 0 and strategy.position_size ==
0, then fired one bar early, and an exit cooldown blocked the next bar's
entry. The scraped job-2947 probe on BINANCE:ETHUSDT.P 15 lost its
2026-01-08 23:30 UTC short and every later trade this way.
TradingView fills that exit at the close, after the pass: the pass keeps the
pre-fill size and average. The adapter now freezes the script's position view
before it executes the exit, as close_all does. Once the fill has left the
book flat, the generated strategy.position_avg_price read
(position_entry_price_ behind a non-zero signed_position_size()) keeps the
pre-fill average until the pass ends. PineStrategyHost::
hold_script_average_price writes it, and release_script_average_price
restores the flat book's 0 right after on_source_bar, before the orders the
pass placed settle. No codegen change.
Each part has its own switch, PoocCloseFillViewSwitches::
current_exit_keeps_position_view and current_exit_keeps_average_price (on;
tests clear them). Both act only at that call site. close_all's own average
read and the intraday-cap close at the call are unchanged.
Pinned by 15 synthetic lab tv --no-note controls on BINANCE:ETHUSDT.P 15m,
2026-01-08..10. Each was exported twice, the two exports are byte-identical,
rangeProof is covered on both, and every prediction was written before its
export: 0 TradingView counterexamples. tests/fixtures/pooc_close_fill_view
holds 11 of them with their frozen generated strategies (codegen c1df8a6):
- Nine equal TradingView row for row, entry ids included. These are the
re-issued stop exit's size read, its cooldown observer, its ATR-stop
cooldown at 100 % of equity, its average read and its limit leg, plus the
long mirror, strategy.close, close_all and a first-issue exit, which never
moved.
- Two are known divergences the rule leaves open. close_all's average read
is AVGPRE on TradingView and AVGOTHER in the engine. After the re-issued
exit, TradingView keeps the pre-fill strategy.opentrades, closedtrades,
openprofit, netprofit and equity; the engine reads them after the fill.
test_pooc_close_fill_view_tapes clears each part and requires the departures
recorded in switch_off_departures.json. The average part moves 2 tapes; the
view part moves the 5 re-issued short tapes. It also requires every tape's
stream to equal its backtest with both parts on and off. Last, it records
every tape's per-bar broker-state hash with the average part on and off:
position_entry_price_ is hashed, so the hashes must be equal wherever the
trades are, and otherwise first differ after the exit bar's fold. A hold
that outlived its pass fails it: with the release_script_average_price()
call removed, the trades stay the same and the hashes of the re-issued exit
tapes differ from the exit bar's fold on. The four cooldown controls of
tests/fixtures/margin_call_rules stay equal.
Fail-before: the test module does not compile against the parent tree
(pooc_close_fill_view_switches is not a member of detail). With both parts
cleared, the five re-issued short tapes depart from TradingView, as on the
parent.
Whole-suite per-run trade dumps (%.17g, every field) against the parent: the
dumps cover 670 processes and 56,558 runs. Every pre-existing run-pass keeps
its trades with both parts on and with both off. The only differences are
the new test, the repetition counts of timing-calibrated loops and the run
order of a test that runs two hosts on two threads; their distinct runs are
identical. No witness re-pin.
The transient flag is waived in broker_state_hash_waivers.txt: it is false
at every fold. Documentation anchors into the four edited files are carried
by an exact line map from the parent (245 moved, 0 stuck).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
* Pine adapter: a POOC close's owed follow-up booked after the script, frozen close sizes and their over-fill lot, and the open's lot-by-lot follow-up
R-A (MarginScheduleSwitches::close_follow_up_after_script): a carried
process_orders_on_close short's lagged follow-up owed to its bar's close is
held through the close's own check (rule 7 still drops it where the close is
short) and booked after the script, ahead of the close's fills, at the same
print. A strategy.close(id) of the whole one-lot book (flush_pending_closes)
or a close_all() placed there keeps the size the script saw: a transaction
that crosses zero and opens the excess as a lot of its own, under the close
order's label. That lot is credited to the close ledger and the pyramiding
records ahead of the entry beside it, so the next open's nameless margin
calls take it first in first out, as the kernel does from the book, and a
later strategy.close of that entry closes all of the entry's units. A
reversal entry's excess joins its own lot (apply_frozen_reversal_close).
Partial closes, closes of several lots and the twin outside
process_orders_on_close keep today's sizing (margin_schedule_rules
frozen/close-id stays a known divergence). No tape runs pyramiding above 1,
so the pyramiding-record credit is unexercised.
Beside that lot only a market entry filling at the close it was placed at is
exempt: from the pyramiding-0 refusal, and a default-sized one from the
fill-time affordability check too (a scraped OANDA:EURUSD 15 probe, job-2550,
2026-04-09 01:45 UTC: TradingView fills a default-sized "LONG" of 18624.97
beside the 0.56 over-fill lot). Priced entries and later fills keep every
gate; no tape decides those shapes. One of them changes against the parent
tree, which opens no over-fill lot: an explicit stop entry placed at that
close at pyramiding 0 fills on the next bar there, and is refused here as a
same-side add beside the lot.
R-B (MarginOpeningSwitches::lot_by_lot_open_follow_up): on a fractional lot
grid, the close-sized call at the next open takes a book of several lots the
close's fills opened, first in first out, and its follow-up is checked lot by
lot (open_print_lot_follow_up_units). One-lot books keep
open_print_follow_up_units, and whole-share books of several lots keep the
previous rules. On a fractional book of several lots a short's booked
re-check now runs as well (the parent tree never entered this schedule for
several lots); nothing follows it, since no tape decides that follow-up.
Tests: tests/fixtures/margin_open_rules lots/ holds 10 synthetic TradingView
controls (62 trades), each exported twice with identical output and
predicted before export. All 10 match row for row and stream to the
backtest's trades; the parent tree matches none of the first 8. Two of them
close the entry by id after the open's calls (strategy.close("L"), with an
explicit and with a default-sized L). Off alone, close_follow_up_after_script
loses 7 tapes and lot_by_lot_open_follow_up 9. margin_open_rules: 53 tapes,
all match. The job-2550 probe's trades now equal TradingView's 3761 rows.
Whole-suite per-run trade dumps (%.17g, every field) with both switches on
and off: only the lots/ tapes move. Documentation: both switches' rows, the
counts, and the anchor digits carried by an exact line map.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
* Pine adapter: a market entry's opening check at its open fill follows the exit legs marketable there
TradingView, pinned on 25 synthetic controls (each exported twice,
byte-identical, every prediction written before its export): at a market
entry that opens the book from flat at a bar's open, the exit legs it
released that are already marketable at that open -- a short's stop at or
below the open's tick or its limit at or above it, a long's mirror -- fill
there first, and the opening margin check then runs on the book they leave
(nothing after a whole exit). A leg the bar reaches only later on its path,
one tick after the open included, never preempts: the check's call books at
the fill first. Both sides. Four more controls make the strategy.exit global
(no from_entry) and place it with the entry: TradingView books each exactly
as its bound control.
ab9714b's rule -- a 1x short skips the opening trim when an exit leg touches
anywhere on the entry bar -- is refuted by TradingView on its own shape
(test_margin_call_1x_long_entry_fill scenario E's settings and bar: 0.008 ETH
called at the open, then the stop closes 3.492).
For a market entry opening the book from flat at the open (one lot, no add,
no book of several entries, neither process_orders_on_close nor
calc_on_order_fills) the adapter asks the legs that fill released whether
they are marketable at the open (priced_exit_precedes_opening). The released
legs (opening_released_leg) are those bound to the entry's request, those
bound to its id and materialized at the fill on the book's side, and a
global leg placed while the book was flat whose request follows the entry's.
Every other leg (a strategy.exit without from_entry placed before the entry
or while a position was held, a leg carried from an earlier bar or lot) is
still asked ab9714b's question, and every other fill (stop and limit
entries, reversals, adds, books of several entries,
process_orders_on_close, calc_on_order_fills) keeps that rule. The scope is
narrower than the pin's wording because the wider form (every market fill at
the open, every live leg) moved the state hashes of two synthetic witnesses,
test_adapter_quiet_bar (18 runs) and test_publication_witness (10 runs):
hash-only, every trade field equal in a whole-suite dump, through legs the
kernel never fills at that open, whose owed check then lingered. Which
exclusion each of the last 8 runs needed was not bisected.
Known divergences of the kept rule:
- Two are committed as tapes the test lists in kKnownDivergences:
TradingView calls a short reversal (out of a 0.001 long) and a short stop
entry gapped at the open (a sell stop above the close) before their stop
on the path, as it does the market entry; the engine books the stop 3.5
with no call.
- A global strategy.exit placed before the entry, while the book is flat,
has no tape. TradingView voids an exit called while no position is held
and no entry order is working (its exit-binding pins), so, by inference,
it calls such a short at the open print. The engine keeps a leg there
that never fills, at the open or later, and still asks ab9714b's
question of it: the short's opening call is skipped whenever the bar
touches the leg's stop, and the short is called at the path's extreme
instead, the same with the switch off. The leg stays out of the released
legs for an engine-only reason: counted, it defers the check to a fill
that never comes, which drops the check, a long's too (in
test_adapter_quiet_bar Random32 that moved only the state hash).
- Adds, books of several entries, limit entries, process_orders_on_close
and calc_on_order_fills stay on that rule without a tape.
A partial exit at the open owes the check after its own fill
(open_exit_first_bar_: 0.0032 called after a 0.0004 stop, as TradingView
books it); the record is cleared at the next bar open if no released leg
fills. The opening check's body moves unchanged into run_opening_checkpoint
so both points run the same branch.
Switch MarginOpeningSwitches::open_marketable_exit_first, default on; off
restores the previous rule (every deterministic run-pass of the suite books
the parent tree's trades, field for field).
Tests: the 31 controls are tapes in tests/fixtures/margin_open_rules
exit-order/ (53 trades). margin_open_rules now holds 84 tapes and 244
trades: 82 match, the 2 listed divergences still differ, forward ==
backtest on all 84, and the switch off loses 17 (15 of the 25 pinned
controls, which the parent tree misses, and the two global controls that
tell the rules apart). Scenario E of test_margin_call_1x_long_entry_fill
(twin and base copy) books TradingView's shape, 0.1996 @100 "Margin call"
then 99.7504 @105; its twin-parity rewrite entry is extended. Docs: the
switch row and counts, and every anchor into the moved files re-anchored by
an exact line map.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
* Pine adapter: a global exit called while a position is held binds to it beside a same-side add too
A strategy.exit with no from_entry, called while a position is held beside an
entry order of the HELD side (an add resting at a limit or a stop, placed
earlier in the same calculation, or queued by a fill recalculation), still
took that pending add as its parent (observe_staged_parent in exit()): #345
skipped only an entry order of the other side. With the add's price basis, a
profit or trail leg rested at the add's limit plus its ticks and filled at the
next open, so a script booked thousands of one-bar trades; a loss leg, and any
leg beside a stop add, waited for the add's fill, which never came, and the
position ran to the end of the range. A market add at the pyramiding cap never
became a pending parent.
TradingView binds such an exit to the held position whatever the side of the
order beside it. In 36 pairs of synthetic `lab tv --no-note` controls on
NYSE:F 15m (a resting limit add, a resting stop add, or a market add at the
cap; profit, loss, profit and loss, and trail legs; long and short;
calc_on_order_fills off and on; every prediction written before its export),
each add script's List of Trades is byte-identical to its control's: 0
counterexamples. The skip now drops its side term, so the exit keeps the held
position's side and entry price.
Under calc_on_order_fills one more path departed: the profit-and-loss bracket
the exit stages in the entry's fill recalculation (the chart-tick scope)
counted the resting add as a competing order of the source book, which took
the bracket's chart-tick reach (its threshold moved half a tick beyond a level
the on-grid test misreads by one ULP), so a limit the fill bar's high reaches
exactly filled one bar late. flush_pending_bracket_legs now leaves the entry
orders of the held side out of the census of a held global exit's leg.
Two switches (ExitBindingRuleSwitches, on; tests clear them):
global_exit_binds_held_position (extended, sizeof unchanged) and the new
held_exit_bracket_ignores_same_side_entries (sizeof 5 -> 6). Kernel untouched;
no stream gate. Not in scope: an add that fills while the exit works
(TradingView gives each entry its own exit levels and books FIFO; the engine
books one exit at the average price) is a separate rule.
Tests: tests/fixtures/same_side_exit holds the 36 add tapes and their 20
controls (exact Pine bytes, TradingView's List of Trades, provenance, frozen
generated C++ of codegen c1df8a69); test_same_side_exit_tapes requires 38
tapes equal to TradingView row for row and the other 18 to depart exactly at
their recorded row (the same row in a control and its add tapes: the trailing
leg and calc_on_order_fills re-entries, another rule's); with
global_exit_binds_held_position off exactly the 32 resting-add tapes move,
with held_exit_bracket_ignores_same_side_entries off exactly the 4
calc_on_order_fills profit-and-loss add tapes; no control and no cap tape
moves. The calc_on_order_fills add tapes are refused by a forward stream;
every other add tape streams (1 and 30 bars of history, every run of the feed
between two nights) to the backtest's trades. test_exit_binding_tapes sets
the new switch in every_part() and lists it.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>1 parent a9544d3 commit 7a1f01c
565 files changed
Lines changed: 100569 additions & 399 deletions
File tree
- docs
- adr
- design
- pages
- include/pineforge/source
- scripts
- src/source
- tests
- fixtures
- exit_binding
- margin_open_rules
- bars
- exit-order
- eth-short-stop-on-path-0623
- f-short-refused
- f-short-stop-on-path
- global-long-stop-at-open
- global-long-stop-on-path
- global-short-stop-at-open
- global-short-stop-on-path
- long-limit-at-open
- long-stop-at-open
- long-stop-on-path
- long-stop-one-tick-below-open
- reversal-short-stop-on-path
- short-equity-sized-stop-at-open
- short-equity-sized-stop-on-path
- short-fee-stop-on-path-open-at-low
- short-fee-stop-on-path
- short-limit-at-open
- short-limit-on-path
- short-partial-stop-at-open
- short-partial-stop-on-path
- short-stop-at-open-margin50
- short-stop-at-open-noslip
- short-stop-at-open
- short-stop-equal-open
- short-stop-on-path-margin50
- short-stop-on-path-no-deficit
- short-stop-on-path-noslip
- short-stop-on-path-one-unit
- short-stop-on-path
- short-stop-one-tick-above-open
- stop-entry-short-stop-on-path
- lots
- close-all-overfill-gap
- close-id-overfill-0409
- close-id-overfill-gap
- close-id-overfill-weekend
- overfill-default-then-close-entry
- overfill-then-close-entry
- reversal-entry-joins-lot
- three-longs-last-short
- two-longs-one-close
- two-shorts-one-close
- pooc_close_fill_view
- atr-exit-cooldown-pyramiding-0
- bars
- long-reissued-exit-size
- short-close-all-average
- short-close-all-size
- short-close-size
- short-first-exit-size
- short-reissued-exit-average
- short-reissued-exit-cooldown
- short-reissued-exit-reads
- short-reissued-exit-size
- short-reissued-limit-exit
- same_side_exit
- bars
- l-l-ctl-cf
- l-l-ctl-nc
- l-l-lim-cf
- l-l-lim-nc
- l-l-stp-cf
- l-l-stp-nc
- l-p-cap-nc
- l-p-ctl-cf
- l-p-ctl-nc
- l-p-ctl1-nc
- l-p-lim-cf
- l-p-lim-nc
- l-p-stp-cf
- l-p-stp-nc
- l-pl-cap-nc
- l-pl-ctl-cf
- l-pl-ctl-nc
- l-pl-ctl1-nc
- l-pl-lim-cf
- l-pl-lim-nc
- l-pl-stp-cf
- l-pl-stp-nc
- l-t-ctl-cf
- l-t-ctl-nc
- l-t-lim-cf
- l-t-lim-nc
- l-t-stp-cf
- l-t-stp-nc
- s-l-ctl-cf
- s-l-ctl-nc
- s-l-lim-cf
- s-l-lim-nc
- s-l-stp-cf
- s-l-stp-nc
- s-p-cap-nc
- s-p-ctl-cf
- s-p-ctl-nc
- s-p-ctl1-nc
- s-p-lim-cf
- s-p-lim-nc
- s-p-stp-cf
- s-p-stp-nc
- s-pl-cap-nc
- s-pl-ctl-cf
- s-pl-ctl-nc
- s-pl-ctl1-nc
- s-pl-lim-cf
- s-pl-lim-nc
- s-pl-stp-cf
- s-pl-stp-nc
- s-t-ctl-cf
- s-t-ctl-nc
- s-t-lim-cf
- s-t-lim-nc
- s-t-stp-cf
- s-t-stp-nc
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
186 | | - | |
| 186 | + | |
187 | 187 | | |
188 | 188 | | |
189 | 189 | | |
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
| 202 | + | |
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
| 213 | + | |
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
| 244 | + | |
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1496 | 1496 | | |
1497 | 1497 | | |
1498 | 1498 | | |
1499 | | - | |
| 1499 | + | |
1500 | 1500 | | |
1501 | 1501 | | |
1502 | 1502 | | |
| |||
0 commit comments