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
Trackstrategy.grosslossto understand how much capital closed losing trades have consumed and when to stop trading.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Strategy Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5strategy("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)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- Accessing
strategy.grosslosson 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
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
strategy.grossloss | Series 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. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Inspect gross loss on each bar
Becausestrategy.grosslossis a series, you can reference the current value or prior bars using the history operator.currentLoss = strategy.grosslosslossOneDayAgo = strategy.grossloss[1] -
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 > lossLimitstrategy.close_all("Loss limit reached")alert("Gross loss exceeded " + str.tostring(lossLimit)) -
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)
Example Playbook
Section titled “Example Playbook”//@version=5strategy("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))- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”strategy.grosslossresets 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.openprofitalongsidestrategy.grosslossfor a complete picture of realised vs unrealised drawdown. - Remember that gross loss ignores winning trades. For net performance, check
strategy.netprofit.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Why does gross loss stay at zero?
Section titled “Why does gross loss stay at zero?”The strategy might not have any closed losing trades yet. Gross loss increases only after a losing trade is closed.
Can I reset gross loss mid-test?
Section titled “Can I reset gross loss mid-test?”No; it’s a read-only series. Use custom variables to track session-based loss caps if needed.
Key Takeaways
Section titled “Key Takeaways”strategy.grosslossreports 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.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.