Skip to content

0.7.0 — Margin simulation (margin_long/margin_short)

Choose a tag to compare

@heyphat heyphat released this 08 Jul 14:17
· 9 commits to main since this release
46be87f

Added

  • Margin simulation (margin_long / margin_short), modeled from
    TradingView's public margin documentation (design record:
    dev-docs/margin-plan.md; broker doc: docs/strategy-broker.md §9):
    • Order gating — an exposure-increasing fill whose resulting position
      would require more equity than is available at the fill price is rejected
      outright (never reduced), per the v6 migration guide. Closes, exit
      brackets, and the reducing leg of a netting order are never gated.
    • Margin calls — walking the bar's assumed intrabar path, the broker
      force-closes 4× the quantity needed to cover a margin deficit (TV's rule
      for "preventing constant Margin Call events"), capped at the whole
      position, booked through the normal FIFO close path. Not a halt: pending
      orders and exit brackets survive.
    • strategy.margin_liquidation_price now reports the live liquidation
      level (was a hardcoded na): na while flat, when the side's margin is 0,
      or for a fully-funded long.
    • StrategyReport.marginCalls — TradingView's "Margin Calls" Performance
      Summary count.

Changed

  • BREAKING (behavior): margin defaults are now the Pine v6 100/100
    (was: no margin model, i.e. the v5 0/0). Strategies that open positions
    larger than their equity — or shorts that lose enough — now get rejected
    entries and margin-call liquidations, exactly as on TradingView v6. To keep
    the old unlimited-buying-power behavior, declare
    strategy(..., margin_long = 0, margin_short = 0) (the migration guide's
    own escape hatch).