Episode 29 · AI Trading · Building a Complete Trading System

TradingView indicator or strategy: signal, order and fill are not one

Write an SMA-cross indicator, check a few candles by hand in the Data Window, then build a strategy with costs, SL and TP, and compare its trade list.

On TradingView, when is an indicator that draws signals enough, and when do you need a strategy that simulates orders?

This post writes an SMA-cross indicator, checks a few candles by hand in the Data Window, and only then builds a strategy with costs, SL and TP. The easy mistake: an arrow on the chart isn’t a filled order. The signal, the order and the fill are three separate events.

Terms used below:

  • Indicator: a Pine script that draws values and markers; it creates no trades.
  • Strategy: a Pine script that places simulated orders; the Strategy Tester lists the resulting trades.
  • Broker emulator: TradingView’s order-fill simulator, with its own assumptions about when and at what price orders fill.
  • Data Window: the panel that shows each plotted value for the candle under the cursor.
  • Repaint: a signal that changes or moves after the fact, so history looks better than what you’d have seen live.

Start with an indicator, then a strategy

Open the Pine Editor, create a new script for the current Pine version (v6), and start with an indicator that draws the condition and when it happens. Compile it and check a few signals in the Data Window. Only the strategy version adds entry and exit, commission and slippage, and risk. If you use a composite price, say so, and never confuse a plot or an alert with a filled order.

Indicator Strategy Neither
Does Draws the condition and signal markers; checked with the Data Window Needs orders, fills, capital, costs and exits; produces the List of Trades in the Strategy Tester through TradingView’s simulator Sends orders to a broker; proves an order really filled
Creates trades No Simulated, on history

From the SMA cross to a simulated order

The first version uses closed candles only and no other timeframe. Draw the fast and slow SMA and mark the cross. Pick one candle, work out the SMAs before and after by hand to confirm the cross, and only then build a strategy with a declared fill method, SL, TP and costs. Compare the List of Trades with the markers, and note the gap between when an order is created and when it fills.

When Event What happens
Close of the marked candle Signal The indicator draws the SMA-cross marker
Set by the strategy’s timing settings Simulated order The strategy declares how it fills, its SL, TP and costs
The earliest moment after the signal Simulated fill TradingView’s broker emulator, by the fill assumptions you wrote
After running the Tester Comparison List of Trades against the markers; note the creation-to-fill gap

A Buy marker at the close doesn’t mean the strategy filled on that candle. Read the creation and fill settings, then compare with the List of Trades.

The indicator:

//@version=6
indicator("SMA cross on closed candles", overlay = true)
fastLen = input.int(10, "Fast SMA length", minval = 1)
slowLen = input.int(30, "Slow SMA length", minval = 2)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
// a cross only counts once the candle has closed; on history every candle is closed
crossUp = ta.crossover(fast, slow) and barstate.isconfirmed
crossDown = ta.crossunder(fast, slow) and barstate.isconfirmed
plot(fast, "Fast SMA", color = color.teal)
plot(slow, "Slow SMA", color = color.orange)
plot(crossUp ? 1 : 0, "Cross up (1 = yes)", display = display.data_window)
plotshape(crossUp, title = "Cross up", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.small)
plotshape(crossDown, title = "Cross down", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.small)
alertcondition(crossUp, "Cross up", "Fast SMA crossed above slow SMA on a closed candle")

The strategy, using the same signal logic. With process_orders_on_close = false, an order created at the signal candle’s close fills at the next candle’s open; slippage is in ticks and commission is a fixed cash amount per order, both assumptions you should set to your own numbers:

//@version=6
// Practice only: simulates trades on history in the Strategy Tester; sends nothing to a broker.
strategy("SMA cross strategy (practice)", overlay = true, initial_capital = 10000,
     default_qty_type = strategy.fixed, default_qty_value = 1,
     commission_type = strategy.commission.cash_per_order, commission_value = 3,
     slippage = 2, process_orders_on_close = false, calc_on_every_tick = false)
fastLen = input.int(10, "Fast SMA length", minval = 1)
slowLen = input.int(30, "Slow SMA length", minval = 2)
slDist = input.float(5.0, "Stop loss distance (price units)", minval = 0.01)
tpDist = input.float(10.0, "Take profit distance (price units)", minval = 0.01)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
crossUp = ta.crossover(fast, slow)
// the order is created at the signal candle's close and, with process_orders_on_close = false, fills at the next open
if crossUp and strategy.position_size == 0
    strategy.entry("Long", strategy.long)
if strategy.position_size > 0
    strategy.exit("Exit", from_entry = "Long", stop = strategy.position_avg_price - slDist,
         limit = strategy.position_avg_price + tpDist)
plot(fast, "Fast SMA", color = color.teal)
plot(slow, "Slow SMA", color = color.orange)

To add them: Pine Editor tab at the bottom of the chart → the arrow next to the script name → Create new → Indicator (or Strategy); paste the code, Save (Ctrl+S), Add to chart, and read every compile error in the panel under the editor. For the strategy, open the Strategy Tester and set the date range, the simulated capital, and assumed costs and slippage in Properties.

Worked example: the same arrow, two different jobs

An indicator can draw a marker on the cross candle; a strategy also needs assumptions about orders, fills, capital, costs and exits to produce a trade list. An arrow on the chart doesn’t prove an order filled, or that the Strategy Tester tested the idea you meant.

First ask the AI for an indicator that explains the condition at the close. Check ten candles with and without a signal in the Data Window. Only then ask for the strategy version, using the same signal logic and stating the assumption that it fills at the earliest moment after the signal; check Properties and every flag that changes the simulation.

If a community script places its marker back on a pivot, ask when the marker appeared, not just which candle it sits on. Use Replay to capture the chart while the pivot is still forming, then again after it’s confirmed. A marker drawn neatly into the past isn’t an opportunity you could have taken in real time.

Optional: run Pine outside TradingView and compare numbers

Outside this post’s main path. Pine an AI writes can usually only be run and checked inside TradingView; to compare its numbers with a Python version on your own machine you need a second Pine runner. PineTS is an open-source JavaScript library that runs Pine code on Node.js. It’s dual-licensed: AGPL-3.0 (free for personal use, research and internal tools; distributing software with it or running it as a network service means publishing your code under the AGPL) and a commercial licence for closed-source software. Running native Pine v6 is still marked experimental. Use it only on your own machine.

PineTS is a new tool, so fill in a review form, docs/vetting/pinets.md, as in the vetting post before installing. Facts checked on 2026-11-23, as evidence for the form rather than a substitute: version 0.13.1 (released 2026-10-27), AGPL-3.0 plus a commercial licence, 3 dependencies (acorn, acorn-walk, astring, all MIT), no install scripts, no key needed when you supply the candles yourself. It needs Node.js as in the setup post. The package’s node_modules folder is about 19 MB in 500 files, so keep it out of git before installing, or git add -A will put it all in your history.

In Windows PowerShell, from C:\trading-agent-lab:

Add-Content -Path .gitignore -Value node_modules/
mkdir pinets
cd pinets
npm.cmd install --ignore-scripts --save-exact [email protected]
cd ..
git check-ignore -v pinets/node_modules

The last line must print a line containing node_modules/. On macOS (~/trading-agent-lab/pinets) use echo node_modules/ >> .gitignore and npm instead of npm.cmd. Commit package-lock.json: it pins all 3 dependencies, and another machine reinstalls with npm ci --ignore-scripts.

Then ask the agent for two scripts:

  1. A Node script in pinets/ that reads the MT5 demo candle CSV from the MT5-reading post into an array of candles, each with open, high, low, close, volume and openTime (milliseconds, UTC); passes the array to new PineTS(candles); and writes each candle’s values to a CSV. Don’t use the package’s built-in data sources (its examples pull candles from a crypto exchange). To be sure the data never leaves the machine, run it offline (Wi-Fi off, cable out), or have the script override globalThis.fetch and the socket connect function of the net module at the top so every network call throws and is counted, then load PineTS with a dynamic import. The script must print the number of network calls, and it must be 0.
  2. A Python script that compares each column with TA-Lib on the same CSV, skips the warm-up at the start, and reports the largest difference.
Step Who What
1 You Fill in docs/vetting/pinets.md before installing
2 You Add node_modules/ to .gitignore before installing
3 You Install [email protected] with --ignore-scripts; git check-ignore; commit package-lock.json
4 Agent The Node script reading the MT5 demo CSV; offline or network blocked; network calls must be 0
5 Agent Compare each column with TA-Lib, skipping the warm-up, reporting the largest difference
6 You Compare 3–5 candles with TradingView’s Data Window, including the first candles, before trusting it

On a 10,831-candle XAUUSD H1 sample (Windows 11, Node.js 24.15, Python 3.14.3, 2026-11-23; 0 network calls):

Function Result against TA-Lib
EMA 20, RSI 14 Match on every candle where both have a value (EMA from candle 20, RSI from candle 15); largest difference under 0.0000000001, just rounding
ATR 14 PineTS takes the first candle’s true range as high − low (no previous close yet), so it has ATR from candle 14; TA-Lib skips the first candle and starts at candle 15. Largest difference 0.065 USD at the start, shrinking to under 0.000001 from candle 165 on

So Pine run outside TradingView has to be checked function by function against an independent answer, with a stated tolerance, skipping the warm-up, all written into the spec. One function matching doesn’t prove another does. And matching TA-Lib doesn’t prove matching TradingView: open the Data Window, copy the values of 3–5 candles (including the first ones) and compare them with PineTS before trusting it.

Try it: an indicator or strategy within what Pine can do

Write Pine Script v6 for an [INDICATOR/STRATEGY] from the spec below. Before coding, check TradingView's official documentation on Pine v6, Strategies, Alerts and Repainting; say clearly which conditions can't be programmed with the supported API. Use standard candles, no future data or lookahead, and don't paint signals back into the past. Signals are only confirmed on [CLOSED CANDLE/DEFINED CONDITION]. Separate the indicator and the strategy if needed. Add clearly named inputs, comments for a beginner, and an explanation of each variable. Write a test checklist: compile, a few given true and false cases, chart refresh, replay, costs, and historical versus realtime differences. Don't claim any profit. Spec: [PASTE THE LOCKED TABLE].

Fill the [brackets] with your own details. Never paste passwords, API keys or tokens into a prompt, and check whatever the AI returns on the demo account before relying on it.

The order to work in:

  1. Pine Editor tab → Create new → Indicator; paste, Save (Ctrl+S), Add to chart; read every compile error.
  2. When the rule is clear, create a separate strategy; open the Strategy Tester and set the dates, simulated capital, and assumed costs and slippage.
  3. Compare closed candles, realtime signals and history after a reload to catch repainting.

Key takeaways

  • The first version uses closed candles only, no other timeframe; work out the SMAs of the candles before and after by hand to confirm the cross.
  • The Strategy Tester only simulates fills on history and sends nothing to a broker; compare the List of Trades with every signal marker.
  • To check for repainting, use Replay or reload the chart, and compare the markers while the candle is forming with the markers on history.

Comments