Pine Script Drawdown Tracker
Watches peak strategy equity and blocks new entries once trailing drawdown from that peak closes in on your firm's limit.
Pine Script
//@version=6
strategy("Target Filled - Drawdown Tracker Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
ddCap = input.float(2000.0, "Trailing Drawdown Cap ($)", step=100,
tooltip="Block new entries once drawdown from peak equity reaches this. Keep it inside your firm's hard limit.")
// --- Peak equity tracking -----------------------------------------
var float peakEquity = strategy.equity
peakEquity := math.max(peakEquity, strategy.equity)
currentDD = peakEquity - strategy.equity
blocked = currentDD >= ddCap
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
canLong = ta.crossover(fastMA, slowMA) and not blocked
canShort = ta.crossunder(fastMA, slowMA) and not blocked
if canLong
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if canShort
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visuals
bgcolor(blocked ? color.new(color.red, 88) : na, title="Drawdown Lock Active")
plot(currentDD, "Current Drawdown ($)", display=display.data_window) Settings
| Input | Default | Purpose |
|---|---|---|
| ddCap | $2,000 | Trailing drawdown in dollars that locks out new entries. Set 10-20% inside the firm's hard limit — an Apex 50k trails $2,500. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
strategy.equityreflects the backtest/paper account, not a live prop account balance — treat this as a guardrail, not the firm's authoritative number.- Red background shading means the lock is active and no new entries will fire.
- Current drawdown is plotted to the data window so it can be inspected bar by bar.
Frequently Asked Questions
How do I track drawdown in Pine Script?
Keep a persistent var that holds the highest strategy.equity ever seen, refresh it every bar with math.max, and subtract the current equity from that peak. The result is trailing drawdown in dollars — compare it to a limit input to block new entries, or feed it into an alert once it gets close.
What is trailing drawdown on a prop firm account?
It's a loss limit that follows the account's equity peak upward instead of sitting fixed at the starting balance. On an Apex 50k, for example, the floor trails $2,500 behind the highest equity reached, until it eventually locks once the account clears its profit target. Because the floor keeps climbing with every new high, a strong run followed immediately by a losing stretch can breach it faster than the starting-balance math would suggest — check a firm's own published numbers before relying on any of this in your script.