Skip to content

Get the gross loss of a TradingView strategy in Pine Script

Get the gross loss of a TradingView strategy in Pine Script

Section titled “Get the gross loss of a TradingView strategy in Pine Script”

TL;DR
Track strategy.grossloss to understand how much capital closed losing trades have consumed and when to stop trading.

DifficultyBeginner
Time to implement10-15 min
CategoryStrategy Basics
//@version=5
strategy("Gross loss tracker", overlay=false)
if strategy.grossloss > 5_000
strategy.close_all("Pause trading – loss limit hit")
plot(strategy.grossloss, "Gross loss", color=color.red)
Tip. `strategy.grossloss` is the sum of all closed losing trades. It ignores current open positions—use `strategy.openprofit` for unrealised P/L.

Knowing how much your strategy has already lost helps enforce risk rules: stop trading after a drawdown, adjust position sizing, or alert the operator. Since the variable is updated on every bar, you can check thresholds mid-backtest or intrabar with calc_on_every_tick.

  • Accessing strategy.grossloss on the current bar and historical bars
  • Setting loss limits or visualising cumulative drawdown
  • Combining gross loss with other metrics (average loss, lowest drawdown)
  • Avoiding false assumptions about open position losses
CallPurpose
strategy.grosslossSeries representing cumulative currency loss from closed losers.
strategy.close_all()Stops trading when the loss limit is breached.
ta.lowest(series, length)Finds the worst (maximum) gross loss over a period.
ta.sma(series, length)Calculates smoothed drawdown averages.
plot(series)Displays loss metrics directly on the chart.
  1. Inspect gross loss on each bar
    Because strategy.grossloss is a series, you can reference the current value or prior bars using the history operator.

    currentLoss = strategy.grossloss
    lossOneDayAgo = strategy.grossloss[1]
  2. Trigger actions when loss thresholds are breached
    Use conditional logic to stop trading, reduce size, or send alerts.

    lossLimit = input.float(5_000, "Stop after loss", step=100)
    if currentLoss > lossLimit
    strategy.close_all("Loss limit reached")
    alert("Gross loss exceeded " + str.tostring(lossLimit))
  3. Visualise and analyse loss history
    Plot raw, lowest, and averaged losses to understand drawdown behaviour.

    length = input.int(50, "Lookback")
    lowestLoss = ta.lowest(strategy.grossloss, length)
    avgLoss = ta.sma(strategy.grossloss, length)
    plot(strategy.grossloss, "Gross loss", color=color.red)
    plot(lowestLoss, "Lowest loss", color=color.new(color.red, 50))
    plot(avgLoss, "Average loss", color=color.orange)
//@version=5
strategy("Gross loss monitor", overlay=true, pyramiding=1)
fastLen = input.int(20)
slowLen = input.int(50)
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
plot(fast, "Fast EMA", color=color.green)
plot(slow, "Slow EMA", color=color.orange)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
lossCap = input.float(3_000, "Global loss cap", step=100)
lookback = input.int(100, "Loss lookback")
lowestLoss = ta.lowest(strategy.grossloss, lookback)
plot(strategy.grossloss, "Gross loss", color=color.new(color.red, 0))
plot(lowestLoss, "Lowest loss", color=color.new(color.red, 60), style=plot.style_cross)
if strategy.grossloss > lossCap
strategy.close_all("Stop after drawdown")
alert("Loss cap reached: " + str.tostring(strategy.grossloss))
Why this works.
  • Gross loss is cumulative—comparing it with a threshold implements a simple drawdown governor.
  • The lowest-loss plot shows the deepest drawdown over the chosen window.
  • Alerts and `strategy.close_all` prevent further trades once the cap is breached.
  • strategy.grossloss resets only when the backtest starts over (or in live trading when you reset the strategy). Store the starting value if you need session-level loss caps.
  • Combine with strategy.closedtrades.profit(trade_num) to inspect individual losing trades.
  • Use strategy.openprofit alongside strategy.grossloss for a complete picture of realised vs unrealised drawdown.
  • Remember that gross loss ignores winning trades. For net performance, check strategy.netprofit.

The strategy might not have any closed losing trades yet. Gross loss increases only after a losing trade is closed.

No; it’s a read-only series. Use custom variables to track session-based loss caps if needed.

  • strategy.grossloss reports cumulative realised losses from closed trades.
  • Leverage it to enforce drawdown limits, notify operators, or inform analytics.
  • Pair with plotting or averages to visualise drawdown behaviour over time.
  • Always complement gross loss with other equity metrics for a full risk view.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.