Why hedging, inverse trading, position flips, and partial take-profits can turn a perfectly reasonable TradingView strategy into something its developer never intended.
A trading strategy developer enjoys one considerable advantage over the person running the strategy: the developer knows exactly what the code is supposed to do. At least until that code starts placing real orders.
Consider an ordinary TradingView strategy. It opens a long position, sets a stop-loss, and distributes the take-profit across three price levels. The first target closes 30% of the position, the second another 30%, and the third closes whatever remains. When an opposite signal appears, the strategy reverses direction.
Nothing particularly adventurous here. Pine Script provides strategy.entry(), strategy.exit(), and strategy.close() for precisely this kind of work. The developer tests the strategy, adjusts the conditions, examines the trades, and eventually arrives at a result worth publishing.
Then someone connects it to MetaTrader 5 through a webhook – a common TradingView to MT5 automation setup.
The signals arrive, the orders begin flowing, and everything appears to be working. Until the user discovers that the strategy has opened both a long and a short position on the same instrument.
The developer checks the Pine Script. There is no instruction to maintain opposing positions. TradingView's Strategy Tester shows exactly the expected behavior.
So where did the second position come from?
A Sell Order That Wasn't Supposed to Open a Short
Let's start with a long position of 10 units and three partial profit targets.
In Pine Script v6, the essential logic can be demonstrated with a small, self-contained example:
//@version=6
strategy("Three Partial Exits", overlay = true)
if bar_index % 100 == 0 and strategy.position_size == 0
strategy.entry("Long", strategy.long, qty = 10, alert_message = "OPEN_LONG")
log.info("Submitted long entry for 10 units")
if strategy.position_size > 0 and strategy.position_size[1] <= 0
entry = strategy.position_avg_price
strategy.exit("TP1", "Long", limit = entry * 1.01, qty_percent = 30, alert_message = "CLOSE_LONG_TP1")
strategy.exit("TP2", "Long", limit = entry * 1.02, qty_percent = 30, alert_message = "CLOSE_LONG_TP2")
strategy.exit("TP3", "Long", limit = entry * 1.03, qty_percent = 40, alert_message = "CLOSE_LONG_TP3")
log.info("Submitted three partial take-profit orders")
The example deliberately uses a simple entry condition and percentage-based price targets. It demonstrates order behavior, not a profitable trading strategy. The alert messages are descriptive labels, not complete broker execution instructions.
When TP1 is filled, TradingView executes a sell transaction for 3 units. Seven remain long. TP2 closes another 3 units, and TP3 handles the remaining 4, assuming the orders fill as intended.
Inside TradingView's broker emulator, this is perfectly straightforward.
But suppose an external execution system receives the first sell transaction without knowing that it represents a partial exit.
On an MT5 netting account, an opposite transaction generally reduces the existing position. Sell 3 against Long 10 leaves Long 7.
On an MT5 hedging account, however, a regular Sell 3 instruction can open a separate short position rather than close part of the long.
Now the broker holds Long 10 and Short 3.
The net exposure is still Long 7, which may give the comforting impression that everything is fine. It isn't. Two separate positions can have different entry prices, transaction costs, protective orders, and margin treatment.
TradingView believes 30% of its original trade has been closed. The broker is holding two trades.
The difference is not academic. It affects subsequent exits, position accounting, and potentially every decision the strategy makes afterward.
The correct operation on a hedging MT5 account is to close the appropriate volume of the existing position. MetaTrader's MQL5 API explicitly supports partial position closing, including operations targeting a particular position ticket.
The original sell transaction was not wrong. The execution system simply needed to understand what that sell meant.
Unfortunately, matters become more entertaining when someone decides to trade the strategy backward.
The Trader Who Wanted to Do the Opposite
Inverse trading appeals to a particular school of market philosophy: if a strategy consistently chooses the wrong direction, perhaps reversing every signal will produce better results.
The temptation is understandable. Buy becomes Sell. Sell becomes Buy. Two words exchanged, and an entirely new trading philosophy is born.
Except that an exit is not an entry.
Suppose our TradingView strategy opens Long 10, while the user has enabled inverse execution. The broker therefore opens Short 10.
TradingView reaches TP1 and produces a Sell 3 transaction to reduce its long position.
A naïve inverse system changes Sell 3 into Buy 3.
Directionally, that transformation is correct. Operationally, it may be disastrous.
On a netting account, Buy 3 generally reduces Short 10 to Short 7. On a hedging account, Buy 3 may create a new long position alongside Short 10.
We are back where we started, except that the directions have exchanged places.
Reversing an order's direction is not the same thing as preserving the purpose of the order.
A long entry should become a short entry. A partial long exit should become a partial short exit. A complete exit should remain a complete exit.
And then there are the protective orders.
A take-profit price above a long entry cannot automatically serve as the take-profit for an inverted short. Simply swapping the stop-loss and take-profit levels may not work either, particularly when the strategy calculates them using volatility, market structure, or independent conditions.
A TradingView trade copier can reverse trading direction without recreating the entire strategy's risk model.
Those are two very different operations, and developers who offer inverse trading should make that distinction clear.
When a Flip Meets an Unfinished Trade
The word flip is used rather generously in trading software. Sometimes it means reversing every incoming signal. Sometimes it means immediately closing an existing position and opening another in the opposite direction. Elsewhere, it describes a manual change of direction while the original strategy continues running.
Those distinctions become important when partial exits are involved.
Imagine that TP1 has closed 3 units of our original Long 10. TradingView now holds Long 7.
A fresh short entry signal arrives, requesting a new position of Short 10.
TradingView's strategy.entry() has a behavior that surprises developers unfamiliar with its reversal logic: by default, it automatically includes the size of the existing opposing position in the transaction.
Here is a separate Pine Script example:
//@version=6
strategy("Reversal Size Example", overlay = true)
if bar_index == 20
strategy.entry("Long", strategy.long, qty = 7)
log.info("Requested long position: 7 units")
if bar_index == 40
strategy.entry("Short", strategy.short, qty = 10)
log.info("Requested short position: 10 units")
Provided the first position is still open, the second entry produces a sell transaction of 17 units, not 10.
Seven units close the previous long. Ten establish the new short.
The resulting position is Short 10, but the transaction quantity is 17.
That distinction is clearly documented by TradingView, although it can be remarkably easy to overlook when constructing webhook messages.
If a receiving system interprets those 17 units as the desired size of a new short, rather than the total reversal transaction, the broker's resulting exposure can be wrong.
If the receiver also has its own automatic reversal logic, the same reversal may effectively be accounted for twice.
Now consider a third possibility. An older partial-exit instruction is still waiting in an external queue, or a broker-side pending order associated with the previous trade has not been canceled.
TradingView has already moved to Short 10. The delayed instruction still concerns the previous Long 7.
Should it be executed?
Certainly not without checking which position it belongs to.
Otherwise, an instruction intended to close part of an old trade can reduce, enlarge, or hedge a new one.
TradingView normally manages its own simulated exit orders when positions change. The stale-command scenario arises when execution state has escaped into external queues, pending orders, or separate trading systems that do not share the emulator's state.
This is where an apparently simple webhook integration becomes a state-management problem.
The Missing Information Between a Signal and a Trade
A basic TradingView webhook alert often contains three things: a symbol, a direction, and a quantity.
For opening a simple market position, that may be enough.
For a strategy involving pyramiding, multiple take-profits, inverse execution, and reversals, it is not.
Consider the word sell.
Does it mean opening a short position? Closing a long position? Reducing an existing long? Reversing from long to short? Or executing an inverted version of a different order?
TradingView exposes several useful strategy alert placeholders, but they describe different aspects of the same event.
{{strategy.order.action}} identifies the transaction direction.
{{strategy.order.contracts}} describes its quantity.
{{strategy.market_position}} describes the resulting strategy position state.
None of these values, taken alone, fully describes the developer's intention.
After a partial long exit, for example, the order action is Sell while the remaining market position is still Long. Building an automation command entirely from either value can produce unexpected results.
Pine Script provides the alert_message parameter on order-generating functions. It allows developers to associate distinct messages with different types of execution events.
For Pine Script strategy alerts, {{strategy.order.alert_message}} can then expose that information to the receiving system.
This makes it possible to distinguish a new short entry from a partial long exit before either instruction reaches the broker. The message still needs the relevant quantity, instrument, and sufficient position context; a descriptive label alone is not an execution protocol.
For a concrete implementation reference, the AlgoWay guide to TradingView order actions and alert placeholders explains why a partial exit should be represented as an explicit closing operation rather than inferred from Buy or Sell.
The important distinction is between describing a transaction and describing the operation that transaction is intended to perform.
A reliable executor also needs to reconcile its own view of the trade with the broker's actual state.
A position might have been closed manually. A stop-loss could have triggered before the next webhook arrived. The broker may have rejected an order because of its minimum volume, quantity step, unavailable symbol, or trading restrictions.
A successful HTTP response proves that a message reached its destination. It does not prove that the broker accepted an order, much less that the expected position now exists.
Even duplicate protection requires thought. Two identical webhooks may be accidental retries, while two separate legitimate partial exits may have the same symbol, direction, and quantity. Deduplicating solely on those three fields risks suppressing a valid operation.
The execution system must have enough information to distinguish one from the other.
The Strategy Tester Wasn't Necessarily Wrong
When a trader reports that a strategy has opened opposing positions, reversed twice, or closed the wrong quantity, the first instinct is often to inspect the Pine Script for bugs.
Sometimes that is exactly where the problem lies.
But sometimes the strategy did precisely what it was designed to do. Its simulated orders were converted into messages, those messages were interpreted under a different position-accounting model, and the broker executed something the developer never intended.
The Strategy Tester works with TradingView's simulated execution rules. A live brokerage account operates under its own constraints, fills, and position accounting.
The bridge between them must preserve the intention of each operation while continuously checking what actually happened.
That is particularly important for strategy creators distributing scripts to users whose brokers, account modes, and preferred execution settings they cannot control.
A strategy author may never have intended to support hedging, inverse trading, or manual flips. Nevertheless, a user may enable all three and then wonder why the third take-profit has started behaving like an entry signal.
At which point the developer may discover that the most complicated part of automated trading was not calculating the signal.
It was explaining to the broker what the signal meant.
