AgentSkillsCN

coding-pinescript

撰写、审核并调试 Pine Script v6 策略与指标。涵盖反模式防范、性能优化,以及内置函数参考。适用于在 TradingView 中实施交易系统时使用。

SKILL.md
--- frontmatter
name: coding-pinescript
description: Writes, reviews, and debugs Pine Script v6 strategies and indicators. Includes anti-pattern prevention, performance optimization, and built-in function reference. Use when implementing trading systems in TradingView.

Coding Pine Script

Triggering Contexts

  • Implementing a strategy from strategy-spec.md
  • Converting v4/v5 scripts to v6
  • Debugging script errors or unexpected behavior
  • Reviewing Pinescript code for quality
  • Optimizing script performance

Workflow Checklist

(Copy into task.md when starting)

  • Review strategy-spec.md (if available)
  • Write code in v6 syntax
  • Run code review checklist
  • Test compilation in TradingView
  • Verify backtest results match expectations

1. Version Strategy

Write new code in v6. Convert older versions before development.

VersionMarker
v4@version=4, uses study()
v5@version=5, uses indicator()
v6@version=6, current standard

Common v5 → v6 changes:

  • security()request.security()
  • input()input.int(), input.float(), input.bool()

2. Anti-Patterns & Best Practices

⚠️ Repainting Prevention (Critical!)

pinescript
// ❌ WRONG (Future leak):
dailyHigh = request.security(syminfo.tickerid, "D", high, lookahead=barmerge.lookahead_on)

// ✅ CORRECT (Offset by 1):
dailyHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)

State Preservation

pinescript
// ❌ Resets every bar:
count = 0

// ✅ Persists across bars:
var int count = 0

Performance

pinescript
// ❌ Recalculates every bar:
threshold = 50 * input.int(defval=2, title="Mult")

// ✅ Calculated once:
var float threshold = 50 * input.int(defval=2, title="Mult")

Explicit Types

Always define types explicitly: float, int, bool, string.


3. Code Review Checklist

Before delivering code, verify:

Correctness:

  • All variables defined before use
  • All functions are valid v6 functions
  • Types match expectations

Repainting:

  • request.security() uses safe lookahead
  • Historical data offset correctly ([1])

Strategy Logic:

  • Entry conditions match spec
  • Uses strategy.exit() for TP/SL

4. Essential Functions Reference

See resources/pinescript-v6-essentials.md for complete reference.

Quick lookup:

CategoryCommon Functions
Indicatorsta.sma, ta.ema, ta.rsi, ta.atr, ta.macd
Signalsta.crossover, ta.crossunder
Strategystrategy.entry, strategy.exit, strategy.close
Datarequest.security, request.financial
Inputinput.int, input.float, input.bool, input.source

5. Official Documentation Links


6. Common Patterns

See resources/common-patterns.pine for templates.


Cross-References

  • Input: Expects strategy-spec.md from planning-trading-systems
  • Optional: Use teaching-pinescript for verbose explanations