Skip to content

Discretionary to Automated

Automate Stop Loss and Take Profit: Breakeven, Trailing, Exit Alerts

To automate stop loss and take profit, first decide which layer owns each exit. A broker-side Stop Loss and Take Profit are stored and executed on the broker's server. EA-managed exits, such as PineConnector's automatic breakeven and trailing, request later stop changes that the broker must accept. A strategy-only exit exists inside TradingView until an alert sends a close message such as closelong.

Automating exits executes the rules you choose; it does not make them good rules. This page sits in the series guide to getting started with automated trading, beside the guide to semi-automated setups.

Illustrative cover for automating exits: a stop level stepping up behind a rising price path, with the official PineConnector logo.
Know which layer owns each exit before you automate it.

Automated exits at a glance

Scroll horizontally to read every column.

Exit type Where it lives What it depends on
Broker-side SL/TP Stored and executed on the broker's server An accepted level; not your terminal running
EA-managed stop changes PineConnector breakeven, pip trailing or ATR trailing, set in the entry message The EA requesting each change and the broker accepting it
MT5's own Trailing Stop The trading platform, not the server The platform staying on
Strategy-only exit A TradingView strategy's simulation An alert that sends a close message, and that message reaching the broker

MetaQuotes says Stop Loss and Take Profit “are stored and executed on the broker's server”. It also says an Expert Advisor running on your PC “cannot manage its positions in case of a computer failure or server connection loss”.[1] MT5's own Trailing Stop “is executed in the trading platform rather than on the server”, so only its last Stop Loss level triggers while the platform is off.[2]

Illustrative diagram of three exit layers: broker-side stop loss and take profit on the server, EA-managed breakeven and trailing that request changes, and TradingView strategy-only exits that need a close alert.
Illustrative. Each accepted EA change becomes the new broker-side stop; a strategy-only exit reaches the broker only as a close request.

How do you set a broker-side stop loss and take profit at entry?

Put the distances in the entry message. In PineConnector syntax, sl_pips sets the stop distance and tp_pips sets the target distance, both in PineConnector pips from the applicable entry.[3] On an illustrative five-decimal EURUSD quote, one pip is 0.00010.

Illustrative message, not an instruction to trade. Verify on a demo account first, and choose your own symbol, size and levels. Paste the message into the Message field of a TradingView alert that uses the PineConnector webhook.

LicenseID,buy,EURUSD,vol_lots=0.01,sl_pips=20,tp_pips=40

For a buy filled at 1.10000, the requested Stop Loss is 1.10000 − 20 × 0.00010 = 1.09800. The requested Take Profit is 1.10000 + 40 × 0.00010 = 1.10400. The broker still has to accept both levels.

Two EA settings interact with that result. The Setting input chooses whether Stop Loss, Take Profit and Risk come from the alert, the EA inputs or a mix. It governs the legacy sl=, tp= and risk= parameters; the syntax reference says explicit units such as sl_pips= “take priority over corresponding setting-dependent inputs”.[3] Shadow Targets Off, the documented default, uses normal broker targets.[4] With Shadow Targets On, the broker sees more distant targets while the EA monitors the intended ones.

To change the levels of an open position later, the syntax reference documents newsltplong for buys and newsltpshort for sells:[3]

LicenseID,newsltplong,EURUSD,sl_price=1.09800,tp_price=1.10400

How does automatic breakeven work in PineConnector?

Automatic breakeven moves the stop once, after a favourable move. Two entry-message parameters control it, both in pips:[3]

  • betrigger=: the favourable movement before breakeven activates. It must be above zero and needs beoffset= too.
  • beoffset=: where the stop goes relative to entry after the trigger. It must be smaller than betrigger=. A value of 0 requests the entry price.

Buy: trigger = entry + betrigger × pip; requested SL = entry + beoffset × pip. Sell: subtract instead of add.

LicenseID,buy,EURUSD,vol_lots=0.01,sl_pips=20,betrigger=10,beoffset=2

Worked example from the syntax reference's figures. Entry 1.10000, initial stop 1.09800. The trigger is 1.10000 + 10 × 0.00010 = 1.10100. At that point, the requested stop is 1.10000 + 2 × 0.00010 = 1.10020. Further gains leave the breakeven level at 1.10020.[3]

The reference warns that a breakeven price does not ensure zero net loss once costs and execution differences are counted, and that broker rules can reject the modification.[3]

Manual breakeven is a separate command for a position already open. Use sl=0, not sl_pips=0. The position must be in profit and far enough from entry for the broker to allow the new stop. A rejected change can leave the old stop in place.[3]

LicenseID,newsltplong,EURUSD,sl=0

How do pip trailing and ATR trailing work?

Pip trailing tightens the stop in steps once a trigger is reached. PineConnector's converter asks for all three as whole-pip values, and the syntax reference gives the step's units as pips, “regardless of the EA's Target Type”:[5][3]

  • trailtrig=: movement before trailing activates. Negative values can request immediate activation, and zero can depend on spread coverage; confirm both on your EA.
  • traildist=: distance from current price to the trailing stop.
  • trailstep=: further favourable movement needed for each adjustment.

Buy: trailing stop = current price − traildist × pip. Sell: current price + traildist × pip.

LicenseID,buy,EURUSD,vol_lots=0.01,sl_pips=20,trailtrig=12,traildist=8,trailstep=3

Worked example from the syntax reference's figures. Entry 1.10000, initial stop 1.09800 (−20 pips).

  1. At +8 pips, the move is below the 12-pip trigger. The stop stays at 1.09800.
  2. At +12 pips, trailing activates: 12 − 8 = +4 pips, so the requested stop is 1.10040.
  3. At +14 pips, only 2 pips have passed since the last adjustment. The stop stays at 1.10040.
  4. At +15 pips, the 3-pip step is reached: 15 − 8 = +7 pips, so the requested stop is 1.10070.
  5. A pullback to +13 pips leaves the stop at 1.10070. It does not loosen.
Illustrative comparison of automatic breakeven with betrigger 10 and beoffset 2, which fixes the stop at 1.10020, and pip trailing with trigger 12, distance 8 and step 3, which moves the stop to 1.10040 then 1.10070.
Illustrative figures from PineConnector's syntax reference. Requested changes are assumed accepted; spread and costs are omitted.

ATR trailing sets the distance from Average True Range on the broker's MetaTrader data, updated at each bar close of the chosen timeframe. atrtimeframe= (in minutes) and atrperiod= are required. atrmultiplier= defaults to 1, atrshift= to 0, and atrtrigger= to 0.[3] The reference lists 1, 5, 15, 30, 60, 240 and 1440 minutes as the MT4 timeframe choices and asks you to confirm other MT5 choices on your EA.

LicenseID,buy,EURUSD,vol_lots=0.01,sl_pips=20,atrtimeframe=60,atrperiod=14,atrmultiplier=2

PineConnector's converter says to choose pip trailing or ATR trailing, not both.[5] The EA reference flags Shadow Targets as incompatible with ATR trailing, pip trailing and breakeven.[4] Test any combination of stop features on demo before relying on it.

How do exit alerts from TradingView close a trade?

A strategy-only exit reaches the broker through a separate close message. The documented close commands are:[3]

  • closelong: close buys for the symbol.
  • closeshort: close sells for the symbol.
  • closelongshort: close both directions for the symbol.
  • closelongpct and closeshortpct: close the EA's Partial Close Percentage. Adding sl=0 can request breakeven on the remainder.
LicenseID,closelong,EURUSD
LicenseID,closelong,EURUSD,comment=strategy 1

Scope decides which positions a close request can affect. Every account on the License ID receives it.[5] Magic Restriction limits it to the EA's own positions, and a matching comment= narrows it to one strategy's positions.[3][6] Comment filtering is documented from MT5 EA v2.095.

Keep the stages separate. The strategy's exit condition becomes true, the alert triggers, the webhook delivers the message, PineConnector processes it, the EA sends a close request, and the broker executes a deal. Check the final position, not only the alert log.

What happens if TradingView and the broker disagree?

TradingView and MetaTrader can use different data feeds, bid and ask quotes, spreads and timestamps.[7] A broker-side stop triggers on the broker's prices; a TradingView strategy exit follows the chart's data. PineConnector's converter states the consequence directly: broker stops, targets, breakeven and trailing “can close before TradingView exits, and the adapter does not resynchronise their positions.”[5]

Illustrative event TradingView strategy Broker account
Broker stop triggers first Still shows the long open Position closed by the stop
Later exit alert sends closelong Records its own exit Already closed; other in-scope buys, if any, are closed
Breakeven change rejected No record of the rejection Old stop remains in place

Two further cases matter. On an MT5 netting account, a later order's stop levels replace the position's levels when volume is added. If that order has zero values, the position's levels are deleted.[2] And if the computer running a self-hosted EA fails, MetaQuotes notes that the EA cannot manage its positions; the last accepted stop stays on the broker's server.[1]

Reconcile each signal against the broker's history. The guide to writing automated trading rules covers stops that exist only in a backtest.

Common mistakes when automating exits

  • Treating breakeven as zero loss. Costs and execution differences still apply, and the broker can reject the change.
  • Adding breakeven to a manual trade. betrigger and the trailing parameters configure changes “when opening an entry”. For a position you opened by hand, only management commands apply, and only with Magic Restriction Off.[3][4]
  • Ignoring minimum distances. A stop too close to the broker's quote can be rejected. See stops level and freeze level in MT5.
  • Mixing legacy and explicit stops. The EA's Setting input can supply legacy sl= and tp= values from the EA instead of the alert. Explicit units such as sl_pips= take priority over those inputs.[3][4]
  • Combining stop features untested. Pip and ATR trailing together, or any of them with Shadow Targets, is not a documented combination.

The parameter values above are the syntax reference's illustrations, not recommended settings. The guide to stop-loss placement methods compares ways to choose a level.

Frequently asked questions

How do I automate stop loss and take profit on MT5?

Include the stop and target in the entry message, for example with sl_pips and tp_pips in PineConnector syntax, so the broker stores them as server-side levels. Later changes can be requested with newsltplong or newsltpshort. Explicit units such as sl_pips take priority over the EA's setting-dependent inputs, which apply to legacy sl= and tp= values.

Can PineConnector move my stop to breakeven automatically?

PineConnector's syntax documents automatic breakeven with betrigger and beoffset in the entry message. After a favourable move of betrigger pips, the EA requests a stop at entry plus beoffset pips for a buy. The broker must accept the change, and a breakeven stop does not ensure zero net loss after costs.

Does a trailing stop work if my computer is off?

MT5's own Trailing Stop runs in the trading platform, so it stops adjusting when the platform is off; only its last Stop Loss level remains on the server. MetaQuotes also says an Expert Advisor on your PC cannot manage positions after a computer failure. The last accepted stop stays on the broker's server.

How do I close a trade from a TradingView alert?

Send a close message from the alert, such as LicenseID,closelong,EURUSD for buys or closeshort for sells. The request reaches every account on the License ID and affects positions within the EA's scope, which Magic Restriction and a matching comment can narrow. Confirm the resulting deal in MT5.

Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account.

Related reading

Sources

  1. MetaQuotes – MetaTrader 5 Help: Executing Trades, accessed 25 September 2026.
  2. MetaQuotes – MetaTrader 5 Help: Basic Principles, accessed 25 September 2026.
  3. PineConnector – PineConnector Syntax: stops, breakeven, trailing and closes, accessed 25 September 2026.
  4. PineConnector – EA settings reference: Setting, Shadow Targets and Magic Restriction, accessed 25 September 2026.
  5. PineConnector – Pine Script converter, accessed 25 September 2026.
  6. PineConnector – Keep strategies separate, accessed 25 September 2026.
  7. PineConnector – FAQ: price differences, accessed 25 September 2026.

PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.


Leave a comment

Back To PiCo Blog

Ready when your strategy is

You bring the strategy.We bring the infrastructure.

Connect TradingView to MetaTrader, choose where MT5 runs and put the full PineConnector workflow through its paces from your first month.

Strategy and trading decisions remain yours. The MT5 environment can be ours.

PineConnector Edge

Run the full PineConnector workflow.

$59/mo at launch

Core plan · 1 connection · 1 hosted MT5 environment

Try Core for $7

7 days of Core for $7, then $59/month at the launch price unless you cancel.