WhatsAppFast quote
Pine Script strategy.exit

Pine Script strategy.exit Mistakes: Stop Loss, Take Profit, and Trailing Logic

Most Pine Script stop loss and take profit problems come from unclear entry IDs, wrong price anchors, overlapping exits, or trailing logic that the trader cannot explain.

Pine Script Guide May 4, 2026 12 min read Updated May 4, 2026
Written byJayadev Rana
FocusReal trader mistakes
UpdatedMay 4, 2026
Pine Script strategy.exit Mistakes: Stop Loss, Take Profit, and Trailing Logic cover image
Quick summary

Most Pine Script stop loss and take profit problems come from unclear entry IDs, wrong price anchors, overlapping exits, or trailing logic that the trader cannot explain.

TopicPine Script strategy.e
LevelDeveloper guide
CTAWhatsApp review
About the author

Jayadev Rana has been building Pine Script systems since 2017. These guides are written from the point of view of someone who cares about live behavior, clean alerts, maintainable code, and broker-ready logic instead of surface-level chart tricks.

Pine Script strategy.exit

Good trading code should be explainable when the market is calm and still readable when something fails live.

Need help applying this to your own build?

If you already know the logic you want and the hard part is implementation, testing, or automation structure, send the setup on WhatsApp. I can usually tell pretty quickly whether it needs a clean indicator, a strategy rewrite, or a smaller audit.

Most Pine Script stop loss and take profit problems come from unclear entry IDs, wrong price anchors, overlapping exits, or trailing logic that the trader cannot explain.

Another search pattern I see constantly is: stop loss not triggering, take profit not executing, trailing stop mismatch, or strategy.exit not working. Most of these are not TradingView bugs. They are usually design mistakes in how the exit was modeled.

A strategy exit has to answer a few questions cleanly. Which entry does it belong to? Is the stop a price or a tick distance? Is the target based on the signal bar or the actual average entry price? Does trailing start immediately or only after price moves in favor? If those answers are mixed together, the backtest becomes confusing very quickly.

Pine Script v6 stop, target, and trailing structure
//@version=6
strategy("Clean exit structure", overlay = true, pyramiding = 0)

fast = ta.ema(close, 20)
slow = ta.ema(close, 50)
atr = ta.atr(14)

longSignal = ta.crossover(fast, slow) and barstate.isconfirmed
if longSignal
    strategy.entry("Long", strategy.long)

entry = strategy.position_avg_price
stopPrice = entry - atr * 1.5
targetPrice = entry + atr * 3.0
trailOffset = atr * 1.2

if strategy.position_size > 0
    strategy.exit("Long exit", "Long", stop = stopPrice, limit = targetPrice, trail_offset = trailOffset)

plot(strategy.position_size > 0 ? stopPrice : na, "Stop", color = color.red)
plot(strategy.position_size > 0 ? targetPrice : na, "Target", color = color.green)
The important part is not the exact ATR value. It is that the exit uses the actual position average price and keeps one clear exit command tied to the entry ID.

The mistake traders make

Many traders calculate stop and target from the candle close that triggered the signal. That can be acceptable for a rough idea, but it is not always the same as the strategy entry fill. In a more realistic script, I prefer anchoring exits to strategy.position_avg_price once the position exists. That makes the logic easier to explain and easier to compare with the Strategy Tester.

The next mistake is using several exit commands with overlapping IDs and expecting Pine to know the trader's intention. Pine Script follows the order model you wrote, not the trade plan in your head. If one exit command replaces or competes with another, the result may look like the stop or target was ignored.

How I debug exit problems

  • I plot the stop, target, and trailing reference line on the chart.
  • I check whether the exit ID matches the entry ID.
  • I inspect whether the script uses price values or tick distances correctly.
  • I compare the signal candle, entry fill, and exit calculation separately.
  • I check commission, slippage, pyramiding, and realtime recalculation settings.

For automation, I also check the alert message. If the strategy exits because of a stop, the broker bridge should not receive a generic close message with no context. A useful alert says whether it was a stop, target, trailing exit, or manual session close.

Professional rule

The strongest exit logic is usually less clever than people expect. It is clear. It is documented. It uses one entry model, one exit model, and enough plotted evidence that a trader can inspect the result candle by candle.

If your strategy is profitable only because the exit is confusing, it is not ready. If you can explain every exit in the trade list, the script is moving in the right direction.

When a client wants the strategy connected to alerts, I write the alert message around the exit path. A stop exit, target exit, trailing exit, and session close are not the same event. If the broker bridge receives all of them as one generic close command, live execution becomes harder to audit. Clear exits create clearer automation.

Another important habit is naming. Entry IDs and exit IDs should read like a small system. A future developer, or the same trader six months later, should not have to decode random names like Long1, Exit2, or CloseX. Clean naming makes debugging easier and reduces accidental replacement of orders inside the strategy engine.

I also test the script with deliberately simple settings. For example, I may turn off the trailing part and test only the fixed stop and target. Then I add the trailing behavior back. This isolates the problem. If everything is mixed together from the beginning, a trader may think the stop is broken when the real problem is that the trailing rule is taking control earlier than expected.

Before I deliver a strategy with stop loss, take profit, or trailing logic, I inspect the trade list instead of only looking at the equity curve. Equity curves can hide bad assumptions. The trade list shows whether the strategy actually exited for the reason the trader expected. If the stop line is visible on the chart but the trade closed somewhere else, that mismatch has to be explained.

The delivery checklist I use for exit logic

Send your Pine Script exit issue for a fixed-price review

Want a second pair of eyes on your setup?

Send the chart idea, market, timeframe, and goal on WhatsApp. I can usually tell you quickly whether the next step is a custom Pine Script build, a strategy audit, or a broker-ready automation layer.


Frequently asked questions

Can Jayadev Rana fix my existing script or EA?

Yes. Send the code, screenshot, error message, and what you expected the system to do.

Will the solution be explained?

Yes. The goal is not only to patch code, but to make the trading logic easier to maintain.

How do I request a quote?

Send the script or EA requirement on WhatsApp with the market, timeframe, and broker details.

If you want this built properly

I take on Pine Script indicators, TradingView automation layers, strategy audits, and broker-aware execution workflows when the goal is clear and the live behavior actually matters.