Repository navigation
calc_on_order_fills: the script runs again after the bar points with fills - #433
Merged
Merged
Conversation
…fills strategy(calc_on_order_fills = true) was accepted and ignored. On TradingView's historical bars the broker walks the bar point by point (open, nearer extreme, farther extreme, close; a fill between two points belongs to the later one) and the script runs again after each point with fills, other than the close (whose run is the bar's normal run): - that run sees the complete bar, with the broker state at that point; - its market orders fill at that point's price, its limit / stop / exit orders work on the rest of the path; - var is rolled back between the runs of a bar; plots come from the last. Fills at the close (process_orders_on_close) and margin calls do not trigger a run. For such strategies each path segment is given to the broker as a bar of its own. Strategies without the setting take the unchanged path. Also: - strategy.order with the id of an order pending from an earlier bar modifies it instead of adding another, and snaps its limit / stop to the tick grid away from the market (as strategy.entry does); in a run after a fill the market is the fill point's price; - strategy.exit's persistent-call detection and the ta.cross* call-id tracking compare with the previous bar, not an earlier run of the same bar. Co-authored-by: Cursor <cursoragent@cursor.com>
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 397aa898-1346-4036-b5a9-5a530c79dd5a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
strategy(calc_on_order_fills = true)was accepted and ignored. This implements it for historical bars, from seven TradingView probes that log every execution (BINANCE:BTCUSDT 1h, Pine v6) plus controlled case 25.What TradingView does
barstate.isconfirmedtrue), with the broker state at that point.varis rolled back between the runs of a bar; plots come from the last run.process_orders_on_close) and margin calls.Changes
PineTS._processBarWithOrderFills): for such strategies, each path segment is given to the broker as a bar of its own, opening at the segment's start. After a segment with fills, the script runs again. Its orders are kept and work from that point on. Its plots, drawings and variable changes are discarded. Strategies without the setting take the unchanged path (the broker phases moved intoprocessBarOrders).strategy.order: an order with the id of one still pending from an earlier bar modifies it instead of adding another. Limit / stop levels snap to the tick grid away from the market, asstrategy.entrydoes. In a run after a fill, "the market" is the fill point's price.strategy.exit's persistent-call detection compares with the previous bar;ta.cross*call-id tracking (barState) no longer treats a second run of a bar as a call id shared by two call sites.Known limit:
varipis still treated asvar, so it is rolled back too. Arrays, maps and matrices mutated in an extra run are not rolled back.Tests
tests/namespaces/strategy/calc-on-order-fills.test.ts(6 tests):varis rolled back and plots are not duplicated;strategy.ordermodifies a pending order with the same id;strategy.orderlimit levels are tick-rounded.4 fail on
dev; the other two guard existing behavior.Results against TradingView (compared with
dev)devdev103). Its first difference is an unrelated commission rounding.Note
High Risk
Touches core strategy bar execution, order filling, var/plot rollback, and TA state across multiple re-runs per bar; incorrect ordering could change backtest results broadly.
Overview
Implements
strategy(calc_on_order_fills = true)for historical bars so the broker walks each bar along TradingView’s open → nearer extreme → farther extreme → close path, and re-runs the script after intra-bar fills (except at the close). Mid-bar runs see full OHLC with broker state at the fill point; market orders fill at that price, limit/stop work on the remaining path, and plots, drawings, andvarchanges from those runs are discarded (final bar run wins).The bar loop adds
_processBarWithOrderFills; default strategies still use the same broker phases via newprocessBarOrders.riskAfterFillincrements_fillCountto detect fill points;barPathis exported for segment simulation.strategy.entry/strategy.ordersnap limit/stop using_fillPointPriceduring COF runs.strategy.orderalso replaces same-id pending orders from earlier bars and applies mintick rounding like entry.strategy.exitpersistent-call detection ignores same-bar re-executions.tana-aware state keys off_execTickso a second COF run on one bar is not treated as a duplicate call site.Adds
calc-on-order-fills.test.tscovering same-bar fills after entries, segment closes, pending limits, var/plot rollback, and order id replacement.Reviewed by Cursor Bugbot for commit 1dcf470. Bugbot is set up for automated code reviews on this repo. Configure here.