Skip to content
NyriaDocs

Flip between long and short

Set up a strategy that trades both directions, and read the bot log when an alert reverses a position.

You need an Active strategy with a paper bot on it. If you have neither, start with strategies and bots.

When an alert reverses an open trade

Nyria cancels the entry if it is still resting, or closes it if it filled, then opens the new side. It works from this trade's own entry order, never the broker's total position in the ticker, which can include holdings from another bot or an earlier session.

Why "go short" during an open long works

Under Long behavior, set On a long entry alert to Enter position. Under Short behavior, set On a short entry alert to Enter position.

With both sides opening positions, an alert that reverses the position closes the long, then opens the short. Nyria reads the reversal from the position the alert reports: TradingView's Order fills only alert with prevMarketPosition long and marketPosition short, or its default text alert, which ends New strategy position is -1. Nyria then handles the long side:

  • A resting long entry order is canceled, and the short opens on a flat account.
  • A filled long position is closed (sized from the fill, not the broker's total), then the short opens.
  • A long that was already canceled or rejected needs no cancel, and the short opens.

If any bot still holds the long after the close, no bot gets the short, so no account ends up holding both sides.

A plain SPY short entry reports no position, so it doesn't reverse on its own. With Close on exit alerts on for the long, Nyria holds it for a few seconds for the long's exit alert and opens the short once that exit has run. If no exit arrives, the short is rejected with held alert rejected: the long SPY position is still open because its exit alert never arrived, so the short entry wasn't placed. With the long's exit orders off, it isn't held and opens beside the long.

This only works when both directions open positions. If your strategy is long-only (Short = Do nothing), a short alert during an open long will not close the long.

Steps

Open your strategy's behavior settings

Open the strategy's ⋯ menu, select Edit, and go to the What it trades step. The Long behavior and Short behavior boxes are there.

Set both directions to Enter position

The Long behavior and Short behavior panels: entry and exit action, order type and duration for each direction.

Under On a long entry alert, pick Enter position. Do the same under On a short entry alert. Long and short alerts now each open a position in their own direction.

An alert that reverses an open position cancels or closes the old side first, then opens the new direction.

If your alert source sends only one direction's alerts, set the other side to Exit long position (or Exit short position) instead. That side's alerts then only close, never open. That makes a long-only or short-only strategy, not a reversal one.

Turn on exit orders for both directions (optional)

You can also turn on Close on exit alerts. Some alert sources send both a "long exit" when the indicator goes flat and a "short entry" when it flips. With exits on, the exit alert closes the position without waiting for the opposite entry.

Choose order types

Step 2 of the strategy builder: Long behavior with its order type and duration, the choices that decide how an entry is placed.

For reversal trading, market orders on exits are the safest default. A limit exit in a fast reversal can sit unfilled while the underlying keeps moving against you.

  • Entries: a limit at bid, ask or mid is fine. If a reversal arrives before the limit fills, Nyria cancels it.
  • Exits: use market unless you have a clear reason for a limit.

Set this in the Order type dropdowns for each direction's entry and exit.

Turn off multiple entries

On the What it trades step, leave Allow multiple entries off. Then each trade runs strictly entry → exit → entry, and each reversal has one entry order to cancel or close.

Turn on multiple entries after the basic reversal works end to end. Nyria still cancels or closes every open entry for the trade, but the log has more rows to match up.

Check the alert format

Check the strategy's alert format as usual, with one example for each enabled trade type. Nyria reads each example the way it reads a live alert and places no orders.

If a reversal example fails with "this message doesn't show the position reversing", the message is missing the position fields. Keep marketPosition and prevMarketPosition in it, as in the Order fills only alert.

Test on paper first

Deploy a bot on this strategy to a broker's paper account (Tradier sandbox or Alpaca paper). Nyria Paper fills or refuses an entry at once, so it never shows a cancel. Send a long entry alert, then an alert reversing to short (prevMarketPosition long, marketPosition short) a few seconds later, and check the bot's log.

It shows:

  • A long entry order placed
  • The short entry alert running an exit
  • A cancel of the long entry (if it was still resting) or a close of the long (if it filled before the short arrived)
  • The short entry placed once the long is gone

Read the logs

Open the bot's log on its detail page. A reversal shows as one line on the symbol you reversed:

What you seeWhat happened
Entry order <id> cancelled (no position to close)The long entry was still resting, so it was canceled and there was nothing to close.
Failed: Could not cancel pending entry order(s) and no position visibleThe entry had already filled or already died at the broker. Check the position on the bot before sending another alert.
No position foundEvery entry for this trade was already rejected or canceled, so there was nothing to exit.

Go live

When paper shows the right log lines for a fast reversal, deploy a bot on your live account and watch the first few real reversals. Reversals work the same on paper and live, but some rejections, such as too little buying power or a halted ticker, only happen on a funded account.

Sample alerts

The smallest JSON payloads for a TradingView or custom webhook reversal source. The webhook URL carries your strategy's token. See the webhook reference for every field.

Long entry

{
  "ticker": "SPY",
  "action": "buy"
}

Short entry (also exits an open long)

{
  "ticker": "SPY",
  "action": "sell"
}

Long exit (optional, if your source sends flat alerts)

{
  "ticker": "SPY",
  "action": "exit_long"
}

Short exit

{
  "ticker": "SPY",
  "action": "exit_short"
}

The two-field minimum (ticker and action) is enough. Nyria takes the rest, such as direction and sizing, from your strategy and bot.

TradingView Pine snippet

If your alerts come from a Pine strategy, the simplest safe pattern calls alert() once per direction change, next to strategy.entry:

//@version=5
strategy("Reversal demo", overlay=true)

longCondition = ta.crossover(ta.sma(close, 9), ta.sma(close, 21))
shortCondition = ta.crossunder(ta.sma(close, 9), ta.sma(close, 21))

if longCondition
    strategy.entry("Long", strategy.long)
    alert('{"ticker":"' + syminfo.ticker + '","action":"buy"}', alert.freq_once_per_bar_close)

if shortCondition
    strategy.entry("Short", strategy.short)
    alert('{"ticker":"' + syminfo.ticker + '","action":"sell"}', alert.freq_once_per_bar_close)

alert.freq_once_per_bar_close is the safe default. It stops one bar from firing repeated alerts as the price moves inside the bar. For faster reactions, use alert.freq_once_per_bar, but expect more alerts and more reversals to cancel or close.

Do not use alert.freq_all for a reversal strategy. It fires on every tick while the condition is true, and floods Nyria's webhook with duplicate entries, which Nyria drops.

Common questions

What happens if the broker is unreachable when the reverse alert arrives?

The cancel fails and the close fails too, so the trade stays as it was, and the bot log shows an error. Close the position from Close positions in the strategy's ⋯ menu, or at your broker. See closing a position.

Does this work with options spreads?

Yes. On a multi-leg entry, Nyria cancels the parent broker order, which covers every leg, and treats the entry as one unit. If only some legs of a spread filled, the close refuses to leave naked legs.

Does this work on Schwab single-leg options?

Yes, the same way. See Schwab for its sign-in and the order types it takes.

Does this work on crypto?

Yes, for spot crypto, the same way. Perpetual futures currently trade on Nyria Paper only, where the new direction's entry flips the position.

  • Strategies: every strategy setting.
  • Trading behavior: Enter position, Exit long / Exit short position, Do nothing.
  • Bots: deploying a bot on this strategy.
  • Webhook reference: every field an alert can carry, and what Nyria takes from your strategy when a field is left out.
  • Schwab: sign-in and order types.
  • Alpaca: set up an Alpaca paper account to test this guide end to end.

Your edge, on autopilot.

Trading carries risk. Read the risk disclosure.

© 2026 Nyria LLC. All rights reserved.