Pine Script Strategy Builder
by @shailendartigga16-collab
Write, fix, test, and backtest Pine Script v5 strategies and indicators for TradingView. Use when asked to create trading strategies, indicators, Pine Script...
clawhub install pine-script-strategyπ About This Skill
name: pine-script-strategy description: Write, fix, test, and backtest Pine Script v5 strategies and indicators for TradingView. Use when asked to create trading strategies, indicators, Pine Script code, fix compilation errors, backtest ideas, optimize strategies, write SMC/scalping/trend-following systems, or anything related to TradingView Pine Script. Triggers on phrases like "pine script", "tradingview strategy", "tradingview indicator", "backtest this", "write a strategy", "fix my pine script", "pine script error", "create indicator", "trading strategy".
Pine Script Strategy Builder
Write production-ready Pine Script v5 strategies and indicators for TradingView.
Workflow
1. Understand the request β What instrument, timeframe, style (scalping/intraday/swing)?
2. Choose type β strategy() for backtesting + signals, indicator() for visual-only
3. Follow architecture β Data β Signal β Score β Filter β Risk β Execute β Visual
4. Apply non-repaint rules β MANDATORY, no exceptions
5. Run pre-flight checklist β Before finalizing ANY script
6. Test mentally β Walk through edge cases, verify logic on paper
7. Deliver β Complete script + explanation + suggested backtest settings
Non-Negotiable Rules
Non-Repaint (ALWAYS)
barstate.isconfirmedstrategy() must have calc_on_every_tick=false, process_orders_on_close=falserequest.security() β use barmerge.gaps_off, barmerge.lookahead_on with [1] offsetRisk Management (ALWAYS)
Quality Gates (ALWAYS)
Strategy Architecture
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
Pre-Flight Checklist
Before delivering ANY strategy, verify:
barstate.isconfirmed)?Templates
Strategy Template
//@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)
Indicator Template
//@version=5
indicator("Name", overlay=true, max_lines_count=500, max_labels_count=500)
Key Patterns
Non-Repaint Entry
var bool longSignal = false
if barstate.isconfirmed
longSignal :=
else
longSignal := falseif longSignal and strategy.position_size == 0 and barstate.isconfirmed
strategy.entry("Long", strategy.long)
Multi-Timeframe Non-Repaint
htfClose = request.security(syminfo.tickerid, "60", close[1], barmerge.gaps_off, barmerge.lookahead_on)
ATR-Based SL/TP
atrVal = ta.atr(14)
slLong = close - atrVal * 1.5
tpLong = close + atrVal * 1.5 * rrRatio
Score System (0-100)
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)
Cool-off After Loss
var int barsSinceLoss = 999
if strategy.closedtrades > 0
if strategy.closedtrades.profit(strategy.closedtrades - 1) < 0
barsSinceLoss := 0
barsSinceLoss += 1
coolOffOK = barsSinceLoss > 5
Common Errors & Fixes
| Error | Fix |
|-------|-----|
| Cannot call ta.dmi().adx | [diPlus, diMinus, adxVal] = ta.dmi(len, len) |
| Undeclared identifier | Declare with var before use |
| Cannot call operator [] on bool | Store in var variable first |
| Script could not be translated | Check commas, parentheses, v5 syntax |
| Too many plot calls | Max 64 plots β use tables/labels |
| Loop takes too long | Max 500K iterations β reduce loop |
| Variable type mismatch | Use float() or int() casting |
Detailed References
Output Format
When delivering a Pine Script: 1. Complete, copy-paste-ready code 2. Brief explanation of what it does 3. Suggested TradingView backtest settings (timeframe, dates, instrument) 4. Known limitations or things to watch for 5. Ideas for improvement
Fixing Errors
When fixing Pine Script compilation errors: 1. Read the error message carefully 2. Match to known error patterns above 3. Fix the specific issue β don't rewrite the whole script 4. Verify the fix doesn't break other logic 5. Re-check non-repaint compliance