Scripts
Indicators and Pine scripts
PlotStudio is where you write scripts, with an editor, a compiler, a debugger and a backtest. Scripts are compatible with Pine Script® v3 to v6, and they run in your browser on every plan.
Create a script
- Open the PlotStudio layout, or add its panels with Add panel.
- In Scripts, choose New script, its type (indicator, strategy or library) and the version of Pine.
- Write it in Editor. Problems lists errors and warnings while you type.
- Compile (Ctrl+Enter) puts it on the chart in front, and Save (Ctrl+S) keeps it. Versions lists what you saved and brings any version back.
For example, a strategy that enters on the crossing of two averages, with a stop loss at 1.5 ATR:
//@version=6
indicator("Regime Pilot", overlay = true)
// Settings
fastLen = input.int(21, "Fast EMA", minval = 2, group = "Trend ribbon")
slowLen = input.int(55, "Slow EMA", minval = 3, group = "Trend ribbon")
atrLen = input.int(14, "ATR length", minval = 1, group = "Volatility stop")
atrMult = input.float(2.5, "ATR multiple", minval = 0.5, step = 0.25, group = "Volatility stop", tooltip = "How far the stop trails price, in ATRs")
htf = input.timeframe("240", "Confirmation timeframe", group = "Higher timeframe", tooltip = "Signals fire only when price is on the same side of this timeframe's slow EMA")
showDash = input.bool(true, "Show dashboard", group = "Display")
BULL = #26A69A
BEAR = #EF5350
tone(bool up, int transp) => color.new(up ? BULL : BEAR, transp)
// A stop that ratchets with the trend and flips when a close crosses it
type Stop
float level
int dir = 1
method trail(Stop s, float band) =>
if na(s.level) or (s.dir == 1 and close < s.level) or (s.dir == -1 and close > s.level)
s.dir := na(s.level) ? 1 : -s.dir
s.level := s.dir == 1 ? close - band : close + band
else
s.level := s.dir == 1 ? math.max(s.level, close - band) : math.min(s.level, close + band)
s
fastEma = ta.ema(close, fastLen)
slowEma = ta.ema(close, slowLen)
var stop = Stop.new()
dir = stop.trail(ta.atr(atrLen) * atrMult).dir
// Higher-timeframe filter: flips against it are only marked, never signalled
htfEma = request.security(syminfo.tickerid, htf, ta.ema(close, slowLen))
htfBull = close > htfEma
flipUp = dir == 1 and dir[1] == -1
flipDn = dir == -1 and dir[1] == 1
longSignal = flipUp and htfBull
shortSignal = flipDn and close < htfEma
// Ribbon, stop and regime
ribbonUp = fastEma > slowEma
pFast = plot(fastEma, "Fast EMA", tone(ribbonUp, 0), 2)
pSlow = plot(slowEma, "Slow EMA", tone(ribbonUp, 50), 1)
fill(pFast, pSlow, tone(ribbonUp, 80), "Ribbon")
pStop = plot(dir == dir[1] ? stop.level : na, "Stop", tone(dir == 1, 0), 2, plot.style_linebr)
pMid = plot(hl2, "Mid", display = display.none)
fill(pMid, pStop, tone(dir == 1, 88), "Stop zone")
bgcolor((dir == 1) == htfBull and ribbonUp == htfBull ? tone(htfBull, 94) : na, title = "Aligned regime")
plotshape(longSignal, "Long", shape.triangleup, location.belowbar, BULL, text = "Long", textcolor = BULL, size = size.small)
plotshape(shortSignal, "Short", shape.triangledown, location.abovebar, BEAR, text = "Short", textcolor = BEAR, size = size.small)
plotshape((flipUp or flipDn) and not (longSignal or shortSignal) ? stop.level : na, "Unconfirmed flip", shape.circle, location.absolute, color.new(color.gray, 30), size = size.tiny)
alertcondition(longSignal, "Long flip", "Regime Pilot: long flip confirmed by the higher timeframe")
alertcondition(shortSignal, "Short flip", "Regime Pilot: short flip confirmed by the higher timeframe")
// Dashboard
row(table t, int r, string key, string value, color c) =>
table.cell(t, 0, r, key, text_color = color.gray, text_size = size.small, text_halign = text.align_left)
table.cell(t, 1, r, value, text_color = c, text_size = size.small, text_halign = text.align_right)
var dash = table.new(position.top_right, 2, 3, bgcolor = color.new(#131722, 20), border_width = 1)
if showDash and barstate.islast
row(dash, 0, "Trend", dir == 1 ? "Up" : "Down", tone(dir == 1, 0))
row(dash, 1, "Higher TF", na(htfEma) ? "n/a" : htfBull ? "Above EMA" : "Below EMA", tone(htfBull, 0))
row(dash, 2, "Stop dist.", str.tostring(math.abs(close - stop.level) / close * 100, "#.##") + "%", color.silver)The editor
- Suggestions as you type, or with Ctrl+Space, and the parameters of a call with Ctrl+Shift+Space.
- What a name is, under the pointer, and its definition with F12.
- Find with Ctrl+F, replace with Ctrl+H, go to a line with Ctrl+G.
- Several cursors: Ctrl+D adds the next match. Alt+↑ and Alt+↓ move lines, and Ctrl+/ comments them.
- Format (Shift+Alt+F) lays the code out the same way every time.
- Reference searches every function, variable and type of the language.
On the chart
A script runs on the bars of its chart: once on every bar that closed, and on the bar still forming at every price, which rolls back until the bar closes. Its plots, shapes, labels, lines, boxes, tables and fills draw on the chart, and Console shows what it logs.
Its inputs are in its settings, like those of an indicator. It can read other intervals and instruments with request.security.
In your browser
Scripts run in your browser on the same virtual machine as our servers, compiled to WebAssembly, so a script gives the same result in both places. Limits keep the page responsive: for example, 10 million instructions per bar and 64 MiB of memory per script.
A strategy can also trade: Run in this browser sends its orders to a paper or a real account while its tab stays open. Closing the tab stops it and leaves any position open. From the browser it sends entries and closes at market; limit and stop orders, exits with levels and partial closes are simulated in the backtest, not sent.
Libraries
A library is a script that others import. A script can import the libraries you saved, each by its name and version.
Languages
Pine Script® v3, v4, v5 and v6, each with the semantics of its version. MQL4, MQL5 and EasyLanguage® are on the way.