Pine Script Mastery
What this skill does: Activates elite-level Pine Script capability.
Not just code generation — complete trading system architecture.
Strategy logic, indicator design, risk management, backtesting integrity,
publication-ready structure, and Pine Script v5/v6 mastery in one skill.
Reference Architecture
| File | Contents | When to read |
|---|
| INLINECODE0 | Full v5/v6 syntax, built-ins, 2025 updates, common errors | Every coding task |
| INLINECODE1 |
Strategy architecture, signal logic, entry/exit systems | Strategy builds |
|
references/risk-management.md | Position sizing, SL/TP systems, drawdown control, Kelly | Risk components |
|
references/indicators-library.md | 30+ indicator implementations with explanations | Indicator builds |
|
references/quality-standards.md | Code structure, documentation, anti-patterns, publication checklist | All outputs |
Standard task: Read language-mastery.md + relevant reference.
Full strategy build: Read all five files.
Execution Protocol
STEP 1 — Classify the Request
CODEBLOCK0
STEP 2 — Pre-Code Intelligence Gathering
Before writing a single line, extract:
CODEBLOCK1
If information is missing for a strategy build → ask before coding.
If request is clear → proceed immediately.
STEP 3 — Architecture Design (Strategy Builds)
Read references/strategy-design.md and design before coding:
CODEBLOCK2
STEP 4 — Write Production-Grade Code
Read references/language-mastery.md before writing.
Read references/quality-standards.md to apply all standards.
Every script must have:
CODEBLOCK3
Code structure order:
- 1. Version declaration + header comment
- INLINECODE9 or
strategy() declaration with all parameters - Input groups (organized, labeled, with tooltips)
- Core calculations
- Signal logic
- Risk management
- Strategy entries/exits (if strategy)
- Plots and visuals
- Alerts
- Dashboard table (if needed)
STEP 5 — Validate Before Delivering
Run this checklist on every output:
CODEBLOCK4
STEP 6 — Deliver With Context
Every code delivery includes:
1. What it does — plain English, 2-3 sentences
2. How to use it — specific settings, what to look for
3. Optimization hints — which inputs to tune first
4. Known limitations — honest about what it doesn't handle
5. Risk warning — always include for strategies:
⚠️ *Backtest results do not guarantee future performance.
Always validate on out-of-sample data and demo trade before going live.*
Strategy Design Principles
These apply to every strategy output regardless of complexity:
The Signal Quality Hierarchy
Tier 1 — Price action (most reliable, no lag)
Tier 2 — Volume confirmation (validates moves)
Tier 3 — Momentum oscillators (RSI, MACD — confirm, don't lead)
Tier 4 — Moving averages (trend context only — laggy for entry)
Never use Tier 4 alone for entries. Always combine tiers.
The Repainting Rule
Scripts must never repaint without explicit disclosure.
Common repainting causes:
- -
request.security() without INLINECODE12 - Using
high[0] or low[0] in real-time bar calculations - INLINECODE15 /
ta.pivotlow() — these inherently repaint
Always use barstate.isconfirmed for alert conditions.
The Overfitting Warning
If a strategy has >5 optimizable inputs and shows >70% win rate in backtest:
flag this explicitly. Recommend walk-forward validation.
Real edges produce 52-60% win rates with good R:R, not 80%+.
Pine Script v6 Key Differences (vs v5)
Pine Script v6 was released December 2024. Future updates apply exclusively to v6.
Key changes from v5:
CODEBLOCK6
2025 updates: Scope limit removed (was 550, now unlimited). String length increased 10x to 40,960 characters. Pine Editor moved to side panel with word wrap support.
When to use v6: new scripts that need dynamic request.*() calls,
or when scope/string limits were previously a constraint.
When to stay on v5: maximum community compatibility needed.
Quick Reference — Most Used Patterns
Multi-Timeframe Analysis
CODEBLOCK7
ATR-Based Dynamic Levels
CODEBLOCK8
Strategy Entry with Risk Management
CODEBLOCK9
Dashboard Table
CODEBLOCK10
Confirmed Alerts (No Repainting)
alertcondition(long_signal and barstate.isconfirmed,
title="Long Signal",
message="{{ticker}} — Long signal on {{interval}}")
Teaching Mode
When the user asks to learn or understand something:
- 1. Explain the concept — plain English first, no jargon
- Show minimal example — smallest possible code that demonstrates it
- Show real-world application — how it's actually used in a strategy
- Common mistakes — what trips people up
- Next step — what to learn after this
Never dump the full Pine Script manual. Teach the specific concept needed,
with just enough context to make it click.
Pine Script 精通
此技能的作用:激活精英级 Pine Script 能力。
不仅仅是代码生成——完整的交易系统架构。
策略逻辑、指标设计、风险管理、回测完整性、
可发布结构,以及 Pine Script v5/v6 精通,集于一身。
参考架构
| 文件 | 内容 | 何时阅读 |
|---|
| references/language-mastery.md | 完整的 v5/v6 语法、内置函数、2025 年更新、常见错误 | 每次编码任务 |
| references/strategy-design.md |
策略架构、信号逻辑、入场/出场系统 | 策略构建 |
| references/risk-management.md | 仓位管理、止损/止盈系统、回撤控制、凯利公式 | 风险组件 |
| references/indicators-library.md | 30+ 个指标实现及说明 | 指标构建 |
| references/quality-standards.md | 代码结构、文档、反模式、发布检查清单 | 所有输出 |
标准任务:阅读 language-mastery.md + 相关参考文件。
完整策略构建:阅读全部五个文件。
执行协议
第一步 — 分类请求
类型:
[ ] 仅指标 — 可视化数据,不进行交易
[ ] 策略 — 生成买入/卖出信号并包含回测
[ ] 库 — 供其他脚本使用的可复用函数
[ ] 调试/修复 — 现有代码存在错误
[ ] 优化 — 改进现有脚本的性能或逻辑
[ ] 解释 — 教授某功能的工作原理
[ ] 转换 — 从其他语言移植或 v4→v5/v6
版本目标:
[ ] v5 (兼容性最强,社区庞大)
[ ] v6 (最新版本 — 动态请求、严格布尔值、新功能)
默认:v5,除非用户指定 v6 或需要 v6 专属功能
复杂度:
[ ] 简单 — 单一概念,<50 行
[ ] 中等 — 多组件,50-200 行
[ ] 高级 — 完整系统,200+ 行,多个交互系统
第二步 — 编码前信息收集
在编写一行代码之前,提取以下信息:
资产类别 : 外汇 / 加密货币 / 股票 / 期货 / 指数
时间周期 : 剥头皮 (1-5m) / 日内 (15m-4h) / 波段 (D) / 头寸 (W+)
市场类型 : 趋势 / 震荡 / 波动 — 策略针对哪种市场?
入场逻辑 : 什么信号触发入场?(动量 / 反转 / 突破 / 均值回归)
出场逻辑 : 固定止损/止盈 / 追踪 / 基于指标 / 基于时间?
风险方法 : 固定百分比 / 基于ATR / 凯利公式 / 固定手数?
过滤器 : 趋势过滤器?成交量过滤器?交易时段过滤器?
可视化需求 : 图表上应显示什么?信号?水平线?仪表板?
如果策略构建缺少信息 → 在编码前询问。
如果请求清晰 → 立即继续。
第三步 — 架构设计(策略构建)
阅读 references/strategy-design.md 并在编码前进行设计:
信号堆栈
主要信号 : [主要入场触发条件]
确认信号 1 : [第一个过滤器 — 减少虚假信号]
确认信号 2 : [第二个过滤器 — 可选但推荐]
趋势过滤器 : [宏观方向背景]
成交量过滤器 : [参与确认 — 可选]
风险堆栈
止损 : [方法 + 计算]
止盈 1 : [第一个目标 — 部分出场]
止盈 2 : [第二个目标 — 全部出场]
追踪止损 : [激活水平 + 追踪距离]
最大回撤 : [紧急停止条件]
仓位管理
方法 : [固定百分比 / 基于ATR / 凯利公式]
每笔风险 : [权益百分比]
最大持仓数量 : [并发仓位限制]
第四步 — 编写生产级代码
在编写代码前阅读 references/language-mastery.md。
阅读 references/quality-standards.md 以应用所有标准。
每个脚本必须包含:
pine
// ============================================================
// [脚本名称] — [一行描述]
// 版本 : v1.0
// Pine Script : v5 (或 v6)
// 作者 : [姓名]
// 描述 : [2-3 句说明其功能、
// 有何用途、如何使用]
// ============================================================
代码结构顺序:
- 1. 版本声明 + 头部注释
- indicator() 或 strategy() 声明及所有参数
- 输入组(有组织、带标签、含工具提示)
- 核心计算
- 信号逻辑
- 风险管理
- 策略入场/出场(如果是策略)
- 绘图和可视化
- 警报
- 仪表板表格(如果需要)
第五步 — 交付前验证
对每个输出执行此检查清单:
语法
[ ] 正确的版本声明
[ ] 无已弃用的 v4 函数(study()、security() 等)
[ ] 所有变量在使用前已声明
[ ] 无重绘逻辑,除非有意为之并已披露
[ ] request.security() 使用正确(barmerge 设置)
逻辑
[ ] 入场和出场条件相互一致
[ ] 无除零风险
[ ] 如果使用数组,已检查数组边界
[ ] 已处理 na 值(必要时使用 na() 检查)
[ ] 策略中无前瞻偏差
质量
[ ] 所有输入都有工具提示
[ ] 所有绘图都有标题和颜色
[ ] 已定义警报条件
[ ] 策略使用佣金和滑点参数
[ ] 代码可读 — 无魔法数字,使用命名常量
性能
[ ] 循环内无不必要的计算
[ ] 繁重计算适当使用 var 或 varip
[ ] 最小化 request.security() 调用
第六步 — 附带上下文交付
每个代码交付包含:
1. 功能说明 — 通俗英语,2-3 句
2. 使用方法 — 具体设置,关注什么
3. 优化提示 — 首先调整哪些输入
4. 已知限制 — 诚实地说明未处理的内容
5. 风险警告 — 策略始终包含:
⚠️ *回测结果不保证未来表现。
始终在样本外数据上验证,并在实盘前进行模拟交易。*
策略设计原则
这些原则适用于每个策略输出,无论复杂度如何:
信号质量层级
第一层 — 价格行为(最可靠,无滞后)
第二层 — 成交量确认(验证走势)
第三层 — 动量振荡器(RSI、MACD — 确认,不领先)
第四层 — 移动平均线(仅作为趋势背景 — 入场时滞后)
切勿单独使用第四层进行入场。始终结合多个层级。
重绘规则
脚本不得在未明确披露的情况下重绘。
常见重绘原因:
- - request.security() 未使用 lookahead=barmerge.lookahead_off
- 在实时K线计算中使用 high[0] 或 low[0]
- ta.pivothigh() / ta.pivotlow() — 这些本质上会重绘
始终使用 barstate.isconfirmed 作为警报条件。
过拟合警告
如果策略有 >5 个可优化输入,且回测胜率 >70%:
明确标记此问题。推荐使用向前验证。
真实优势产生 52-60% 的胜率并配合良好的盈亏比,而非 80%+。
Pine Script v6 主要差异(与 v5 对比)
Pine Script v6 于 2024 年 12 月发布。未来更新将仅适用于 v6。
与 v5 的关键变化:
动态请求 : request.*() 现在接受序列字符串参数
可以在历史K线上动态更改数据源
可以在循环和条件结构内调用
严格布尔值 : bool 类型现在严格为 true 或 false,绝不会是 na
消除了整类静默逻辑错误
更好的策略 : 改进了复杂订单逻辑的处理
2025 年更新:范围限制已移除(原为 550,现无限制)。字符串长度增加 10 倍至 40,960 个字符。Pine 编辑器移至侧面板,支持自动换行。
何时使用 v6:需要动态 request.*() 调用的新脚本,
或之前受范围/字符串限制约束时。
何时保留 v5:需要最大社区兼容性时。
快速