Getting Started with Pine Script
This guide walks you through creating your first Pine Script indicator, applying it to a replay chart, tweaking its inputs, and saving it — then doing the same for a simple strategy and reading the Strategy Tester.
Prerequisites
- A TestMax account with an active Pro subscription
- An active replay session — see Market Replay if you have not created one yet
Your first indicator
-
Open the Pine editor
In your replay session, click the ƒx Indicators button in the chart toolbar. An editor drawer opens at the bottom of the screen with:
- A saved-scripts sidebar on the left, with starter templates for a new indicator and a new strategy (hover a saved script to delete it)
- A code editor (Monaco, the same editor as VS Code) with Pine syntax highlighting and autocompletion for around 55 common functions
- A name field, a kind badge that auto-detects whether the script is an indicator or a strategy, and Save and Apply to chart buttons
- An error console below the editor for compile and runtime errors
-
Create a new indicator
Click New Indicator in the sidebar. It loads a minimal indicator template into the editor.
-
Write an SMA cross indicator
Replace the template with the following script. It plots a fast and a slow simple moving average and marks the bars where they cross:
//@version=5indicator("SMA Cross", overlay=true)fastLen = input.int(10, "Fast length", minval=1)slowLen = input.int(30, "Slow length", minval=1)fastMa = ta.sma(close, fastLen)slowMa = ta.sma(close, slowLen)plot(fastMa, "Fast SMA", color=color.blue)plot(slowMa, "Slow SMA", color=color.orange)plotshape(ta.crossover(fastMa, slowMa), style=shape.triangleup,location=location.belowbar, color=color.green)plotshape(ta.crossunder(fastMa, slowMa), style=shape.triangledown,location=location.abovebar, color=color.red)Because
overlay=true, the plots draw directly on the price pane. Theplotshape()calls render as markers below and above the bars where the averages cross. -
Apply it to the chart
Click Apply to chart. The script compiles, runs over the chart history, and the two moving averages appear on the price pane with cross markers. A legend row for “SMA Cross” appears on the chart with a status dot and controls to hide, configure, or remove it.
If the script has a syntax problem, the error console lists the compile errors instead — click one to jump to the offending line.
-
Tweak the inputs
Click the gear icon on the indicator’s legend row. An inputs dialog opens with Fast length and Slow length fields, generated automatically from the
input.int()declarations. Change a value and the chart previews it live. Click OK to keep the new values or Cancel to revert. -
Save the script
Give the script a name in the name field (it defaults from the
indicator()title) and click Save. It now appears in the saved-scripts sidebar, and it is stored on your account so it is available from any device.As the replay plays, the moving averages and cross markers keep updating in real time — including on the still-forming candle.
Your first strategy
Strategies use the same editor and the same workflow — the difference is that they place simulated orders and unlock the Strategy Tester tab.
-
Create a new strategy
Click New Strategy in the sidebar and replace the template with this minimal SMA cross strategy. It goes long when the fast average crosses above the slow one and reverses to short on the opposite cross:
//@version=5strategy("SMA Cross Strategy", overlay=true,initial_capital=100000, commission_type=strategy.commission.fixed,commission_value=2.5, slippage=1)fastLen = input.int(10, "Fast length", minval=1)slowLen = input.int(30, "Slow length", minval=1)fastMa = ta.sma(close, fastLen)slowMa = ta.sma(close, slowLen)plot(fastMa, "Fast SMA", color=color.blue)plot(slowMa, "Slow SMA", color=color.orange)if ta.crossover(fastMa, slowMa)strategy.entry("Long", strategy.long)if ta.crossunder(fastMa, slowMa)strategy.entry("Short", strategy.short)Note the kind badge next to the name field now reads Strategy — it is detected automatically from the
strategy()declaration. -
Apply it to the chart
Click Apply to chart. The strategy backtests over the chart history and then keeps trading live as the replay streams. Entry and exit markers appear on the chart at each simulated fill.
-
Open the Strategy Tester
Switch to the Strategy Tester tab in the drawer. You will see:
- Metrics row — Net P&L, total trades, win rate, max drawdown, final balance, and the current open position
- Equity curve — Updates live as the strategy trades
- Trade log — Every simulated trade with entry and exit details
-
Experiment
Open the inputs dialog from the legend row and try different fast/slow lengths — the strategy re-runs and the tester updates. When you find a variant you like, Save it.