fbpx
Tickerly Trading bot service logo

BLOG

How to Reverse a Position on a Signal in TradingView Bots

by

To reverse a position on a signal, your system has to close the open trade and open an equal-sized trade in the opposite direction, ideally as one clean sequence. In TradingView you can get there three ways: let Pine Script’s strategy.entry reverse for you, send separate close and open alerts with explicit messages, or let your trading bot handle the flip. Each works. Each fails in a different way if you automate it carelessly.

This guide compares the three, shows where reversals go wrong in live trading, and helps you pick the setup that matches your strategy.

Tickerly
Automate TradingView Reversals
Tickerly automates TradingView strategies into trading bots, helping execute reversal signals across connected exchange platforms.

Visit Tickerly

TL;DR

  • A reversal is two actions in one: close the current position and open the opposite one. If you are long 2 contracts, the order flow sells 4.
  • Pine Script reverses automatically when strategy.entry fires against an open opposite position. That is a feature in backtests and a trap in live automation.
  • The most reliable setup is an alert that says whether each order opens or closes a position: its own alert_message per order, or on Tickerly the prev_position field, so your bot never has to guess whether “sell” means exit or flip.
  • Reversals cost more than plain entries because you pay spread, fees and slippage twice. Test that cost before you go live.
  • Tickerly turns your TradingView alerts into live orders across crypto, forex, stocks and futures, with a 30-day free trial.

Table of Contents

What “reverse position” actually means

TradingView’s own documentation describes the Reverse Position feature as instantly closing your current position and opening an opposite one of the same size. Its example: long 2 contracts, reverse, and the system sells 4. Two close the long, two open the short.

That arithmetic matters when you automate. A bot that receives “sell 2” while you are long 2 will simply go flat. A bot that receives “sell 4” will flip. The signal itself carries the whole difference, so the signal has to be precise.

Sell quantity outcomes when reversing a long

There is also a distinction that trips up many new bot traders. A reversal is not a special order type on most exchanges. When you use exchange APIs directly, a reverse is a logical sequence rather than a single atomic command: close one position, then open the next. Flags such as reduceOnly exist on some exchanges to make the closing half safer. The same reference notes that beginners often ignore the latency between the two steps, which can lead to double-positioning or margin problems. Professional bots tend to check position state before sending the second half.

Why strategies and exchanges do not talk to each other

A common surprise: your Pine Script strategy never touches your exchange. The strategy’s broker emulator tracks simulated performance only. Real execution is a separate step that depends on alert messages being interpreted by a third-party integration, which is where a platform like Tickerly sits.

So when your backtest shows a clean flip from long to short, the live version depends on three links holding together:

  1. Your script fires an alert with the right message.
  2. TradingView delivers it through a webhook.
  3. Your bot translates the message into the correct exchange orders in the correct order.

If any link is vague, the reversal is vague. If you want the full pipeline, our walkthrough on going from TradingView alert to live order covers it step by step.

Three ways to reverse a position on a signal

Method How it works Strength Main risk
1. Native strategy.entry reversal Opposite-direction entry closes and flips in one call Least code, matches the backtest exactly Accidental flips when you only wanted to exit
2. Explicit close and open alerts Separate strategy.close and strategy.entry with custom alert_message strings Unambiguous, easy to debug More alerts to manage, a small gap between legs
3. Bot-side reverse logic Your bot receives one signal and performs close-then-open itself One alert per flip, state checks handled for you You depend on how your bot defines reverse

Method 1: Let Pine Script reverse for you

The Pine Script documentation explains that strategy.entry() can reverse an open market position without additional function calls. If you are long and the script issues a short entry, the long closes and the short opens.

This is the shortest path for always-in-the-market systems: trend followers that are long or short and never flat, moving average crossovers, supertrend flips. The code stays small, and the backtest and the alert come from the same event.

The weakness appears when you want a trade to end without a new one beginning. The same documentation points to strategy.close for that case. If you use an entry call where you meant a close, you get a reversal you never designed.

Use this method when:

  • Your strategy is genuinely stop-and-reverse by design.
  • You have no separate stop-loss or take-profit orders that also fire alerts.
  • Your bot can read the order size in the alert and act on it.

Method 2: Explicit close and open messages

The default {{strategy.order.action}} placeholder returns only “buy” or “sell”. That produces the same string for exiting a long and entering a short, which is exactly where bots misread intent. A TradingView community script by Peter_O, published in March 2024, names reliance on default order actions as the most common pitfall. The fix it describes is to pass custom strings, such as “closebuy” and “closesell”, through the alert_message parameter of every order function.

In practice that means mapping every event in your strategy to its own message:

  1. Long entry sends an “open long” message.
  2. Short entry sends an “open short” message.
  3. Stop loss and take profit each send their own “close” message.
  4. A signal-based flip sends a “close” message followed by an “open” message, or one explicit “reverse” message if your bot supports it.

This is more typing, but it removes guesswork for a bot that reads the action alone. Since stop losses and take profits are standard now, a bare buy or sell no longer covers what a strategy does. Every entry and exit needs either a distinct label or the strategy’s previous position alongside it, so an exit never becomes an accidental reversal.

Tickerly uses the previous position, so you don’t write custom strings, and you shouldn’t: the alert’s action field accepts only buy or sell. Instead, every Tickerly alert also carries prev_position, filled by {{strategy.prev_market_position}}, which tells Tickerly whether a sell closes a long or opens a short. Closing orders go out reduce-only, so a stop-loss or take-profit fill can only flatten the position, never open a new one the other way. For strategies that flip between long and short, Tickerly has a dedicated Swing alert type, covered in the advanced alert signals guide.

The tradeoff is timing. Two alerts mean two webhooks and two orders. If the close fills and the open is delayed, you are briefly flat, and in a fast market the price can move before your second leg arrives. That is where execution speed stops being a marketing line and becomes a profit-and-loss number.

Method 3: Let the bot own the reversal

Here your alert says one thing, “flip to short”, and the bot does the sequencing. Some bots read the current position, close it, confirm it is flat, then open the opposite side at the intended size, a flatten-and-check pattern meant to avoid double positions.

This approach shrinks your Pine code and moves the sequencing next to the exchange connection. It also makes you dependent on exact bot behavior, so test it with small size before you scale. Ask these questions of any bot you evaluate:

  • How does it tell a close from an open, and can an oversized close flip you by accident?
  • What happens if the close fails or partially fills?
  • Does the reverse use market orders, and what slippage will you accept?
  • Can you see every step in a log?

That last point is why we built an alert log: when a flip behaves oddly, you want to see what arrived, when, and what was sent to the exchange.

On Tickerly the other answers are just as concrete. Each alert carries the previous position, closing orders go out reduce-only so an oversized close can only flatten, and a rejected order shows its reason in the alert log and your notifications. Tickerly doesn’t compare your strategy’s position with the exchange’s, so start the strategy and the account flat and keep them in step.

The hidden cost of reversing

Reversals are the most expensive thing a strategy does. You cross the spread twice, pay fees on both legs and take slippage on a position twice the normal size. A strategy that looks great in the tester can lose its edge when every signal flips the book.

Academic work on trading with predictive signals makes the same point. A study from the National Bureau of Economic Research finds that when trading is costly, the optimal approach is to “aim in front of the target” and trade partially toward it rather than jumping all the way there. Signals that decay faster deserve more weight in that aim. For a retail bot, the practical takeaway is not to copy a portfolio model, but to question whether every signal deserves a full-size flip.

Some practical ways to apply that:

  • Add a filter. Require a second condition, such as trend direction on a higher timeframe, before the flip is allowed.
  • Scale the flip. Close the full position on the signal, but open the opposite side at half size and add if the move confirms.
  • Mind thin markets. Market flips on thin pairs can slip badly. A Tickerly alert has no limit-price field, so where slippage worries you, flip on liquid pairs or at a smaller size.
  • Model costs in the tester. Set commission and slippage in your strategy properties so the backtest reflects what you will actually pay. Our piece on Pine Script strategy tester limitations lists what the tester will and will not show you.

Five mistakes that cause unwanted reversals

  1. Sending a bare buy or sell. One “sell” string covers both an exit and a short entry. Pair the action with the previous position, as Tickerly’s alert does with prev_position, or give each order its own message for a bot that reads the action alone.
  2. Calling strategy.entry when you meant strategy.close. The script flips when you only wanted to go flat.
  3. Forgetting that stops and targets send alerts too. A stop-loss fill that arrives as a bare “sell” can be read as a new short. With prev_position in the alert, Tickerly reads it as a close and sends it reduce-only.
  4. Ignoring the gap between legs. If your close and open are separate alerts, the second may arrive after price has moved or after a margin check fails.
  5. Not testing position state. If your bot assumes it is long when it is already flat, the “close” half can open a position instead. Tickerly sends closing orders reduce-only, so a stray close can only flatten, but start the strategy and the account flat so they stay in step.

The cure for all five is the same: alerts that say whether each order opens or closes a position (distinct messages, or Tickerly’s prev_position), a small live test, and logs you can read.

Which method fits your strategy

  • Always-in-the-market trend system: Method 1 for the code, paired with a bot built to flip. On Tickerly, that is the Swing alert type.
  • Strategy with stops and take profits: Method 2, or on Tickerly the standard alert, whose prev_position field marks stop and target fills as exits so they can’t be treated as flips.
  • Multi-strategy account: Keep each strategy’s positions apart so one strategy’s flip can’t close another’s trade. On Tickerly, two strategies on the same symbol need hedge mode (Binance USD-M and COIN-M, Bybit, Bitget, Oanda, MetaTrader 4/5) or a separate sub-account connection each.
  • Futures or prop firm accounts with strict rules: Method 2 or 3 with small-size testing first, since a double position can breach limits quickly.

If you are still building the script itself, our guide to what Pine Script is and how to use it for automation is a good starting point. To keep live results close to your backtest, see the five steps to turn Pine Script into a trading bot.

Our perspective on reversals

We see reversals as the moment a bot setup is proven or exposed. Entering a trade is forgiving: if the alert arrives a moment late, you get a slightly worse fill. A reversal asks the system to do two things in the right order while the market moves, and mistakes show up as double positions or missed exits.

Our perspective on reversals — overview diagram

Our view is simple. Make your signals explicit, because a bot should never have to infer intent from a bare “sell”. Keep the path from alert to order as short as you can, because latency during a flip costs more than latency on a plain entry. This is why we focus on placing each order within a fraction of a second of the alert, with no coding required and no VPS for direct exchange connections, and why every Tickerly alert carries the strategy’s previous position, so an exit is never mistaken for a new entry.

We also encourage a sober look at cost. Not every signal deserves a full-size reversal, and a good automation setup lets you shape that decision in your script rather than fight it afterward. Backtest with realistic costs, go live small, read your logs, and scale when the behavior matches the design.

— Jay

Running reversals with Tickerly

Tickerly connects your TradingView strategy alerts to exchanges and brokers, including crypto, forex, stocks, futures, prop firm markets and MetaTrader 4/5, with a no-code setup. You build the strategy and paste the ready-made alert message, and we handle the execution. Our published plans on the plans page run from Level 2 at 19 USD per month, through Level 3 at 29 USD per month and Level 4 at 39 USD per month (which adds MetaTrader 4/5), to Level 4 Ultra for 2,500 alerts a day, and every subscription starts with a 30-day free trial. That is enough time to test your close-and-open logic with small size before you commit.

Pick the alert type that matches your strategy: the standard alert for one that enters and exits, or the Swing alert for one that flips between long and short. The advanced alert signals page shows each format.

To get started, follow the guide on how to set up automated trading from TradingView, or read how to automate trading on TradingView using Tickerly. If you trade forex through MetaTrader, see TradingView to MT5.

A safe testing routine before you flip live

Whatever method you choose, run the same checklist:

  1. Paper or tiny size first. Use the smallest allowed contract size on the exact instrument you will trade.
  2. Force a flip on purpose. Trigger a long, then a short, and confirm that the exchange shows a closed long and an open short, not two shorts or a long plus a short.
  3. Force an exit without a flip. Confirm that a stop loss or take profit leaves you flat.
  4. Check order sizes. If the flip should be 2 closing and 2 opening, verify the total matches your intent.
  5. Read the log. Compare what TradingView sent with what reached the exchange.
  6. Repeat on a volatile moment. Reversals fail at speed, so test when the market moves, not in a quiet hour.

Only after all six pass should you raise size.

FAQ

What does it mean to reverse a position?

It means closing your open position and immediately opening an equal-sized position the other way. If you are long 2 contracts, the reversal sells 4: two to close the long and two to open the short. Many strategies use it when they are designed to always hold a long or a short.

How do I stop my TradingView bot from reversing when I only want to close?

Use strategy.close for exits instead of an opposite strategy.entry, and make sure the alert tells exits from entries. For a bot that reads the action alone, give every order its own alert_message: distinct strings like “closebuy” and “closesell” prevent it from reading an exit as a new entry. On Tickerly, the prev_position field does that job and exits go out reduce-only. Then test an exit on its own with small size.

Is a reversal one order or two?

On exchange APIs it is generally a sequence: close the current position, then open the new one. TradingView’s emulator treats it as one combined order, which is why a backtest can look cleaner than live trading. Your bot has to reproduce the sequence safely.

Are reversals more expensive than normal entries?

Yes, because you pay spread, fees and slippage on both the close and the new entry, on a larger combined order. Research on trading with signals suggests that, when costs matter, trading partially toward a target can beat jumping to it. Set realistic commission and slippage in your backtest to see the effect.

Sources

This article was produced with Al assistance and reviewed for accuracy. It is provided for general information only and is not professional or financial advice.

Tags :

Latest Post