Get started
Examples
Copy one and change it.
The pictures are each script run on sample prices.
Indicators
RSI
RSI in a pane with overbought / oversold lines
//@version=2
// RSI in its own pane with overbought / oversold lines.
indicator({ name: 'RSI', overlay: false, range: [0, 100] })
const len = input.int('Length', 14, { min: 2, max: 100 })
const src = input.source('Source', 'close')
plot('RSI', ta.rsi(src, len), { color: color.purple, width: 2 })
hline(70, { label: 'Overbought', color: color.down })
hline(30, { label: 'Oversold', color: color.up })Bollinger Bands
Upper, middle and lower bands over the candles
//@version=2
// Bollinger Bands over the candles.
indicator({ name: 'Bollinger Bands', overlay: true })
const len = input.int('Length', 20, { min: 2 })
const mult = input.number('StdDev multiplier', 2, { min: 0.5, max: 5, step: 0.1 })
const band = ta.bb(close, len, mult)
plot('Upper', band.upper, { color: color.blue })
plot('Basis', band.mid, { color: color.gray })
plot('Lower', band.lower, { color: color.blue })MACD
MACD and signal lines with a histogram
//@version=2
// MACD with the histogram as bars.
indicator({ name: 'MACD', overlay: false })
const m = ta.macd(close, 12, 26, 9)
plot('Histogram', m.hist, { style: 'histogram' })
plot('MACD', m.macd, { color: color.blue })
plot('Signal', m.signal, { color: color.orange })
hline(0)Stochastic
%K and %D with the 80 / 20 levels
//@version=2
// Stochastic oscillator.
indicator({ name: 'Stochastic', overlay: false, range: [0, 100] })
const s = ta.stoch(input.int('Length', 14), 3, 3)
plot('%K', s.k, { color: color.blue })
plot('%D', s.d, { color: color.orange })
hline(80)
hline(20)Ichimoku
The cloud shifted ahead, colored by which span is on top
//@version=2
// Ichimoku: conversion and base lines, the two leading spans shifted 26 bars ahead with the cloud between them
// (green while A is on top, red while B is), and the lagging span shifted 26 bars back.
indicator({ name: 'Ichimoku', overlay: true })
const conv = input.int('Conversion', 9, { min: 2 })
const base = input.int('Base', 26, { min: 2 })
const spanB = input.int('Lead B', 52, { min: 2 })
const shift = input.int('Displacement', 26, { min: 1, max: 100 })
const mid = (len) => avg(ta.highest(high, len), ta.lowest(low, len))
const tenkan = mid(conv)
const kijun = mid(base)
plot('Conversion', tenkan, { color: color.blue })
plot('Base', kijun, { color: color.red })
plot('Lead A', avg(tenkan, kijun), { color: color.green, offset: shift })
plot('Lead B', mid(spanB), { color: color.orange, offset: shift })
plot('Lagging', close, { color: color.gray, offset: -shift })
fill('Lead A', 'Lead B', { color: color.green, downColor: color.red })Volume spike
How much more than usual is trading
//@version=2
// Volume against its average: bars above 1 trade more than usual.
indicator({ name: 'Volume ratio', overlay: false })
const len = input.int('Average length', 20, { min: 2 })
const ratio = div(volume, ta.sma(volume, len))
plot('Volume / average', ratio, { style: 'histogram' })
hline(2, { label: 'Spike' })ATR trailing stop
A stop line that never moves down
//@version=2
// A trailing stop line: highest close minus a multiple of ATR, never moving down while in a trend.
indicator({ name: 'ATR trailing stop', overlay: true })
const mult = input.number('ATR multiplier', 3, { min: 0.5, step: 0.5 })
const atr = ta.atr(14)
let stop = NaN
const line = map(i => {
const level = close[i] - mult * atr[i]
if (Number.isNaN(level)) return NaN
stop = Number.isNaN(stop) || close[i] < stop ? level : Math.max(stop, level)
return stop
})
plot('Trailing stop', line, { color: color.down, width: 2 })Strategies
EMA cross
Buy when the fast line crosses above, sell when it crosses back
//@version=2
// EMA cross strategy: buy when the fast EMA crosses above the slow one, sell when it crosses back.
indicator({ name: 'EMA cross strategy', overlay: true })
const fastLen = input.int('Fast length', 12, { min: 2, max: 100 })
const slowLen = input.int('Slow length', 26, { min: 3, max: 300 })
const fast = ta.ema(close, fastLen)
const slow = ta.ema(close, slowLen)
plot('Fast EMA', fast, { color: color.orange })
plot('Slow EMA', slow, { color: color.purple })
strategy({ long: ta.crossover(fast, slow), longExit: ta.crossunder(fast, slow) })SuperTrend
Long while the trend is up
//@version=2
// SuperTrend as a strategy: long while the trend is up.
indicator({ name: 'SuperTrend', overlay: true })
const factor = input.number('Factor', 3, { min: 1, max: 10, step: 0.5 })
const len = input.int('ATR length', 10, { min: 2 })
const st = ta.supertrend(factor, len)
plot('SuperTrend', st.line, { color: color.orange, width: 2 })
const up = eq(st.dir, 1)
strategy({ long: and(up, not(prev(up, 1))), longExit: and(not(up), prev(up, 1)) })Donchian breakout
Buy a new 20-bar high, sell a new 10-bar low
//@version=2
// Donchian channel breakout: buy a new 20-bar high, sell a new 10-bar low.
indicator({ name: 'Donchian breakout', overlay: true })
const upper = ta.highest(high, input.int('Entry length', 20))
const lower = ta.lowest(low, input.int('Exit length', 10))
plot('Upper channel', upper, { color: color.green })
plot('Lower channel', lower, { color: color.red })
// The breakout compares with the channel up to the previous bar (no look-ahead).
strategy({ long: gt(close, prev(upper, 1)), longExit: lt(close, prev(lower, 1)) })Both ways with risk control
Long and short with stop, target and trailing stop
//@version=2
// Both ways with risk control: long on a golden cross above the 200 EMA, short on a dead cross below it.
// Stop 1.5 ATR away (a price per bar), target 3%, a 2% trailing stop, 50% of the account per trade.
indicator({ name: 'EMA cross with stops', overlay: true })
const fast = ta.ema(close, input.int('Fast', 12, { min: 2 }))
const slow = ta.ema(close, input.int('Slow', 26, { min: 3 }))
const trend = ta.ema(close, 200)
const atr = ta.atr(14)
plot('Fast', fast, { color: color.orange })
plot('Slow', slow, { color: color.purple })
plot('Trend', trend, { color: color.gray })
const up = gt(close, trend)
strategy({
long: and(ta.crossover(fast, slow), up),
short: and(ta.crossunder(fast, slow), not(up)),
stopLoss: map((i) => (close[i] > trend[i] ? close[i] - 1.5 * atr[i] : close[i] + 1.5 * atr[i])),
takeProfit: 3,
trailingStop: 2,
size: 50,
})Another timeframe and data
Trade the hourly chart with the daily trend
//@version=2
// Another timeframe and market data: trade the hourly chart only with the daily trend (the last
// closed daily bar above its 20-day EMA, worked out on the daily bars), and skip longs when
// funding runs hot.
indicator({ name: 'Daily trend filter', overlay: true })
const daily = request.bars({ timeframe: '1D' })
const dailyEma = daily.compute((b) => b.ta.ema(b.close, 20))
const funding = request.data('funding')
const fast = ta.ema(close, 20)
plot('EMA 20', fast, { color: color.orange })
plot('Daily EMA 20', dailyEma, { style: 'step', color: color.gray })
const dailyUp = gt(daily.close, dailyEma)
bgcolor(dailyUp, { color: color.green })
const calm = map((i) => Number.isNaN(funding[i]) || funding[i] < 0.03)
strategy({ long: and(and(ta.crossover(close, fast), dailyUp), calm), longExit: ta.crossunder(close, fast), stopLoss: 2 })Signals & logic
Signals
Shade the uptrend and mark buys and sells
//@version=2
// Signals: shade the uptrend (close above the 200 EMA); inside it mark ▲ Buy when RSI crosses above 30,
// and ▼ Sell when it falls below 70. The latest bar gets the RSI now.
indicator({ name: 'RSI signals', overlay: true })
const len = input.int('RSI length', 14, { min: 2 })
const trendLen = input.int('Trend EMA', 200, { min: 10 })
const rsi = ta.rsi(close, len)
const trend = ta.ema(close, trendLen)
const upTrend = gt(close, trend)
plot('Trend EMA', trend, { color: color.purple, width: 2 })
bgcolor(upTrend, { color: '#12B88614' })
plotshape(and(upTrend, ta.crossover(rsi, 30)), { shape: 'triangleup', text: 'Buy' })
plotshape(ta.crossunder(rsi, 70), { shape: 'triangledown', text: 'Sell' })
label(-1, `RSI ${last(rsi).toFixed(1)}`, { location: 'above' })Rising streak
Counts consecutive rising closes
//@version=2
// Consecutive rising closes, counted bar by bar with map and an outer variable.
indicator({ name: 'Up streak', overlay: false })
let streak = 0
const count = map(i => {
streak = i > 0 && close[i] > close[i - 1] ? streak + 1 : 0
return streak
})
plot('Bars up in a row', count, { style: 'histogram', color: color.up })