Convert a TradingView indicator to a strategy
Turn an indicator() Pine script into a strategy() so one alert covers every entry and exit, instead of one alert per condition per ticker.
An indicator traded on 4 sides across 5 tickers means 20 alerts to create and keep in step. As a strategy, the same script needs 5 alerts, one per ticker, and each fires on every order the strategy places.
Indicator vs. strategy alerts
indicator() | strategy() | |
|---|---|---|
| How it trades | plotshape, alertcondition, alert() | strategy.entry / strategy.exit / strategy.close orders |
| Alerts you create | One per condition, per ticker | One "Order fills only" alert, per ticker |
| Message placeholders | You write the action and direction into each alert | {{strategy.order.action}}, {{strategy.market_position}} and the rest are filled in by TradingView |
| Backtestable | No | Yes, in the Strategy Tester |
What changes
The math that decides your entries (EMAs, RSI, crossovers) stays as it is. You change the declaration and turn each arrow or alert into an order call.
Swap indicator(...) for strategy(...). Add process_orders_on_close=true so orders fill on the confirmed bar close, which matches the Once per bar close alert timing Nyria recommends. Add calc_on_every_tick=false so the script doesn't repaint inside the bar.
Replace each arrow or alert with an order call. Wherever your indicator drew an arrow or declared an alertcondition, call strategy.entry() to open a position. To exit, call strategy.close() or an opposite-side strategy.entry().
Delete the old alertcondition() and alert() calls. The single order-fills alert replaces all of them.
Before: an indicator
//@version=5
indicator("EMA Cross", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
long = ta.crossover(fast, slow)
short = ta.crossunder(fast, slow)
plotshape(long, title="Long", style=shape.triangleup, location=location.belowbar, color=color.green)
plotshape(short, title="Short", style=shape.triangledown, location=location.abovebar, color=color.red)
// Each of these becomes its OWN TradingView alert, per ticker:
alertcondition(long, title="Long entry", message="open long")
alertcondition(short, title="Short entry", message="open short")After: the same logic as a strategy
//@version=5
strategy("EMA Cross", overlay=true, process_orders_on_close=true, calc_on_every_tick=false)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
long = ta.crossover(fast, slow)
short = ta.crossunder(fast, slow)
if long
strategy.entry("Long", strategy.long)
if short
strategy.entry("Short", strategy.short) // entering short auto-closes the longThe crossover logic is unchanged. Only the outputs changed: the drawings and alertcondition() calls became orders. The strategy now places an order on every entry and exit, and one alert covers them all.
Pick an exit pattern
- Long-only: open with
strategy.entry("Long", strategy.long)and exit withstrategy.close("Long"). Nyria sends a buy, then a sell-to-close. - Long and short (flip): open each side with
strategy.entry. Entering one side closes the other, so one alert both exits and reverses. Set both sides to Enter position in Nyria. See flip long and short. - Price-level exits in Pine:
strategy.exit("x", from_entry="Long", stop=..., limit=...)closes the position inside your script. When it fills, TradingView sends one order-fill alert and Nyria treats it as an exit. Nyria places no stop or target order at your broker.
Nyria sets the size. It reads the action and direction from the alert
and sizes the order from your bot's budget per entry. Any qty of 1 or
more works in the script, including the default of 1.
Create the one alert
You now need one alert per ticker. In the Nyria alert wizard, pick Strategy and copy the default message, or paste this into TradingView's Message box.
{
"action": "{{strategy.order.action}}",
"instrument": "{{ticker}}",
"marketPosition": "{{strategy.market_position}}",
"prevMarketPosition": "{{strategy.prev_market_position}}",
"contracts": "{{strategy.order.contracts}}",
"price": {{close}}
}TradingView fills in each placeholder when the alert fires.
| Field | TradingView placeholder | What Nyria does with it |
|---|---|---|
action | {{strategy.order.action}} | buy or sell, mapped to entry or exit by your strategy's settings. |
marketPosition | {{strategy.market_position}} | long, short, or flat after this fill. Lets Nyria spot flips. |
prevMarketPosition | {{strategy.prev_market_position}} | Position before the fill. Tells a close apart from a reverse. |
contracts | {{strategy.order.contracts}} | The strategy's order size. Reference only; Nyria sizes from your bot. |
price | {{close}} | Confirmed bar close, used for limit pricing and sizing math. |
The alert message builder has a placeholders switch to try these before you paste the message into TradingView.
Then create the TradingView alert with Condition set to your strategy and Order fills only chosen in the second dropdown under it. Create the alert covers each field.
Converting doesn't fix repainting. Keep process_orders_on_close=true
and read higher-timeframe data as close[1]. See
the repainting warning.
Next steps
- Connect TradingView alerts: the full setup, including Once per bar close and common pitfalls.
- Webhook reference: every field Nyria reads from the alert message.
- Strategies: how Nyria turns
{{strategy.order.action}}and your settings into a sized order.
Connect TradingView alerts to Nyria
Set up a TradingView webhook alert for a Nyria strategy: add the script, create the alert, check the format, send a test, and fix common mistakes.
TradingView alert messages
Copy-ready TradingView alert messages for a strategy or an indicator, every placeholder you can use, and the mistake that stops an alert from trading.