Write production-ready Pine Script v5 strategies and indicators for TradingView.
strategy() for backtesting + signals, indicator() for visual-onlybarstate.isconfirmedstrategy() must have calc_on_every_tick=false, process_orders_on_close=falserequest.security() → use barmerge.gaps_off, barmerge.lookahead_on with [1] offsettext
Layer 1: Data — Collect indicators (EMA, RSI, ATR, VWAP, ADX, Volume, BB, Pivots)
Layer 2: Signal — Generate signals (trend direction, breakouts, pullbacks, sweeps)
Layer 3: Score/Filter — Score + filter (multi-factor score, chop filter, session, cool-off)
Layer 4: Risk — SL/TP calculation (ATR/swing-based, partial exits, trailing)
Layer 5: Execute — Entry/exit logic (NON-REPAINT, barstate.isconfirmed)
Layer 6: Visual — Labels, boxes, tables, alerts, backgrounds
Before delivering ANY strategy, verify:
barstate.isconfirmed)?pine
//@version=5
strategy("Name", overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=10,
commission_type=strategy.commission.percent,
commission_value=0.05,
slippage=1,
pyramiding=0,
process_orders_on_close=false,
calc_on_every_tick=false)
pine
//@version=5
indicator("Name", overlay=true, max_lines_count=500, max_labels_count=500)
pine
var bool longSignal = false
if barstate.isconfirmed
longSignal := <conditions>
else
longSignal := false
if longSignal and strategy.position_size == 0 and barstate.isconfirmed
strategy.entry("Long", strategy.long)
pine
htfClose = request.security(syminfo.tickerid, "60", close[1], barmerge.gaps_off, barmerge.lookahead_on)
pine
atrVal = ta.atr(14)
slLong = close - atrVal * 1.5
tpLong = close + atrVal * 1.5 * rrRatio
pine
score = 0
if close > ema50 and close > ema200 // Trend: 0-25
score += 25
if rsi > 60 // Momentum: 0-20
score += 20
if volume > ta.sma(volume, 10) * 1.2 // Volume: 0-15
score += 15
if close > high[1] and close > open // Breakout: 0-20
score += 20
if ta.atr(14) > ta.atr(14)[1] // Volatility: 0-10
score += 10
if close > ta.vwap(hlc3) // VWAP: 0-10
score += 10
totalScore = math.min(score, 100)
pine
var int barsSinceLoss = 999
if strategy.closedtrades > 0
if strategy.closedtrades.profit(strategy.closedtrades - 1) < 0
barsSinceLoss := 0
barsSinceLoss += 1
coolOffOK = barsSinceLoss > 5
ErrorFixCannot call ta.dmi().adx``[diPlus, diMinus, adxVal] = ta.dmi(len, len)``Undeclared identifierDeclare with var before useCannot call operator [] on boolStore in var variable firstScript could not be translatedCheck commas, parentheses, v5 syntaxToo many plot callsMax 64 plots — use tables/labelsLoop takes too longMax 500K iterations — reduce loopVariable type mismatchUse float() or int() casting
When delivering a Pine Script:
When fixing Pine Script compilation errors: