feat(orders): TradingView margin-call liquidation#47
Merged
Conversation
Model TradingView's broker-emulator forced liquidation, which the engine previously did not (margin_liquidation_price() was stubbed to na). Closes a proven parity gap on strategies that ride leveraged/short positions into a margin call: 24 scraped strategies have TV "Margin call" rows the engine never produced (it held the position instead). - margin_liquidation_price(): the validated TV formula liq = ((init_cap + net_profit)/(pointvalue*|size|) - dir*entry) / (margin/100 - dir), mintick-rounded (ceil short / floor long); na when flat or for a long at 100% margin (denominator 0 -> can never be liquidated). - per-bar process_margin_call(): when equity at the bar's adverse extreme (high for shorts, low for longs) drops below required margin, force-close 4x the minimum needed to restore margin (TV's documented over-liquidation), filled at the adverse extreme, tagged "Margin call". Default-on to match TV, with set_margin_call_enabled() opt-out. Validated: P2 probe (100%-equity short) 0 -> 48 margin-call exits, first fills 1798.26 / 1819.00 / 1845.78 / 1885.00 byte-for-byte vs TradingView. Scraped strict match: abhivish 16->64%, pridarasx 44->80%. ctest 76/76 (new test_margin_call, teeth-verified). Corpus verify_corpus --all = 251 excellent / 1 anomaly: a true no-op (equity-mirror is long-only -> na). Residual vs TV's 68 calls is per-instrument liquidation-lot quantization, a separable follow-up. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Models TradingView's broker-emulator forced liquidation (margin call), which the engine previously didn't —
margin_liquidation_price()was stubbed tonaand positions were never liquidated. Proven parity gap: 24 scraped strategies have TVMargin callrows the engine never produced (it held the position).Implementation
margin_liquidation_price()— validated TV formula:liq = ((init_cap + net_profit)/(pointvalue·|size|) − dir·entry) / (margin/100 − dir), mintick-rounded (ceil short / floor long);nawhen flat or for a long at 100% margin (denominator 0 → can never be liquidated → corpus no-op).process_margin_call()— when equity at the bar's adverse extreme (high for shorts, low for longs) drops below required margin, force-close 4× the minimum needed to restore margin (TV's documented over-liquidation), filled at the adverse extreme, tagged"Margin call". Default-on to match TV, withset_margin_call_enabled()opt-out.The formula + trigger + fill price were reverse-engineered and validated against a TradingView ground-truth probe (TV margin-call docs).
Gates
test_margin_call(teeth-verified: neutralizing the liquidation → 9 failures).verify_corpus --all= 251 excellent / 1 anomaly — a true no-op (equity-mirror is long-only →na); zero tier regressions.PF_API/C-ABI export added (CI ABI gate unaffected).Known residual (separable follow-up): TV floors each liquidation to the instrument's qty-step, so its position survives longer (68 calls vs our 48). Doesn't affect corpus parity.
🤖 Generated with Claude Code