Custom Indicator Scripting: Pine and ThinkScript Starter Patterns
Building custom indicators in Pine Script and ThinkScript follows repeatable patterns; five starter templates for alerts, multi-timeframe reads, confluence scoring, and visual overlays get you from blank editor to working tool.
Интерактивные инструменты могут не работать в переведённом виде.
Custom Indicator Scripting: Pine and ThinkScript Starter Patterns
Off-the-shelf indicators force your analysis into someone else's logic. A working knowledge of five scripting patterns lets you build the exact read your strategy needs, in TradingView's Pine or ThinkorSwim's ThinkScript.
Pine and ThinkScript share most logic. The five templates below cover 80% of what a discretionary trader needs to script.
Pattern 1: the alert-on-condition template
Fire an alert when a condition is met. Pine Script:
//@version=5
indicator("VSA Stopping Volume Alert", overlay=true)
v30 = ta.sma(volume, 30)
spread = high - low
closePos = (close - low) / (high - low)
stoppingVol = spread > ta.atr(20) * 1.4 and volume > v30 * 1.75 and closePos > 0.6
plotshape(stoppingVol, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
alertcondition(stoppingVol, title="Stopping Volume", message="Stopping volume detected")
ThinkScript equivalent: define each component with def, combine into a boolean, then AddChartBubble(stoppingVol, low, "SV", Color.GREEN, no). The pattern — define components as variables, combine into a boolean, plot a shape, attach an alert — is reusable for any condition-based signal.
Pattern 2: the multi-timeframe read
Pull a higher-timeframe value into the current chart. Pine:
//@version=5
indicator("HTF EMA Filter", overlay=true)
htfEma = request.security(syminfo.tickerid, "240", ta.ema(close, 20))
plot(htfEma, color=color.blue, linewidth=2)
uptrend = close > htfEma
bgcolor(uptrend ? color.new(color.green, 90) : color.new(color.red, 90))
request.security fetches the 4-hour EMA onto any lower timeframe. Default to lookahead_off for signals to avoid repainting.
Pattern 3: the confluence score
Combine multiple conditions into a single number. Pine:
//@version=5
indicator("Confluence Score", overlay=false)
score = 0
score := score + (close > ta.ema(close, 20) ? 1 : 0)
score := score + (ta.rsi(close, 14) > 50 ? 1 : 0)
score := score + (volume > ta.sma(volume, 30) ? 1 : 0)
score := score + (close > ta.vwap ? 1 : 0)
plot(score, style=plot.style_columns, color=score >= 3 ? color.green : color.gray)
hline(3, "Tradeable", color=color.green)
Threshold the score (≥ 3 tradeable) and the chart shows a single column for the confluence — the scriptable version of the manual scoring systems used in VSA and Fibonacci analysis.
Pattern 4: the visual overlay (zones and levels)
Draw supply/demand zones or harmonic PRZs on the chart. Pine:
//@version=5
indicator("Demand Zone Overlay", overlay=true)
var float zoneLow = na
var float zoneHigh = na
if low < low[1] and close > open and (high - low) > ta.atr(14) * 3
zoneLow := low
zoneHigh := close
bgcolor(not na(zoneLow) and close < zoneHigh and close > zoneLow ? color.new(color.green, 85) : na)
plot(zoneHigh, style=plot.style_linebr, color=color.green)
plot(zoneLow, style=plot.style_linebr, color=color.green)
var persists the zone across bars; plot.style_linebr draws it only while valid.
Pattern 5: the backtest stub
A minimal strategy stub to test an idea before committing. Pine:
//@version=5
strategy("Pullback Entry Test", overlay=true)
ema20 = ta.ema(close, 20)
pullback = low < ema20 and close > ema20 and close > open
if pullback and strategy.position_size == 0
strategy.entry("Long", strategy.long)
strategy.exit("Exit", "Long", stop=low - ta.atr(14) * 0.5, limit=close + ta.atr(14) * 2)
Run in the Strategy Tester for win rate, profit factor, and drawdown before discretionary-trading the idea.
Platform notes
Pine: use //@version=5; request.security for MTF. ThinkScript: declare lower for oscillators, AddChartBubble for labels, AddVerticalLine for time markers; use ThinkBack for backtests.
The workflow
- Define the condition in plain English first.
- Translate each clause to a variable.
- Combine into a boolean.
- Plot a shape and attach an alert.
- Test on 50 bars of historical data before trusting live.
The five patterns compose: a confluence score that fires an alert inside a visual zone, validated by a backtest stub, takes a discretionary idea from guess to system.
Live Chart
Open full chart →Related market data, powered by TradingView.