返回顶部
p

pine-script-developerPine脚本开发

专业Pine Script开发技能,专门用于TradingView指标和策略的编写、调试和优化。包含Pine Script v5语法、最佳实践、指标开发、策略回测、性能优化等。

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
239
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

pine-script-developer

Pine Script开发技能

专门用于TradingView Pine Script代码的编写、修改、调试和优化。

🎯 适用场景

使用此技能当您需要:

  1. 1. 编写新的TradingView指标
  2. 修改现有的Pine Script代码
  3. 调试Pine Script错误
  4. 优化指标性能
  5. 创建交易策略
  6. 添加警报功能
  7. 实现复杂的图表绘制
  8. 进行策略回测

📚 Pine Script核心知识

版本信息

  • - Pine Script v5:最新版本,推荐使用
  • Pine Script v4:兼容旧版本
  • 关键区别:v5有更严格的类型检查,更好的性能

基本结构

pinescript //@version=5 indicator(My Indicator, overlay=true)

// 输入参数
length = input.int(14, Length, minval=1)

// 计算
sma = ta.sma(close, length)

// 绘制
plot(sma, color=color.blue, linewidth=2)

常用函数

  • - ta.sma() - 简单移动平均
  • ta.ema() - 指数移动平均
  • ta.rsi() - RSI指标
  • ta.macd() - MACD指标
  • ta.stoch() - 随机指标
  • ta.bb() - 布林带
  • ta.atr() - 平均真实波幅

🔧 开发工作流

1. 需求分析

  • - 明确指标功能
  • 确定输入参数
  • 选择计算逻辑
  • 设计输出显示

2. 代码编写

  • - 使用正确的版本声明
  • 添加详细的注释
  • 实现错误处理
  • 优化性能

3. 测试调试

  • - 在TradingView中测试
  • 检查边界条件
  • 验证计算结果
  • 优化代码性能

4. 部署优化

  • - 添加用户友好的参数
  • 优化图表显示
  • 添加警报功能
  • 编写使用说明

💡 最佳实践

代码结构

pinescript //@version=5 indicator(专业指标, overlay=true, shorttitle=ProInd)

// === 输入参数 ===
length = input.int(20, 计算周期, minval=1, group=基本参数)
source = input.source(close, 数据源, group=基本参数)

// === 计算逻辑 ===
smaValue = ta.sma(source, length)
emaValue = ta.ema(source, length)

// === 条件判断 ===
bullish = close > smaValue
bearish = close < smaValue

// === 绘制 ===
plot(smaValue, SMA, color=color.blue, linewidth=2)
plot(emaValue, EMA, color=color.red, linewidth=2)

// === 背景色 ===
bgcolor(bullish ? color.new(color.green, 90) : bearish ? color.new(color.red, 90) : na)

// === 警报条件 ===
alertcondition(bullish, 看涨信号, 价格上穿SMA)
alertcondition(bearish, 看跌信号, 价格下穿SMA)

性能优化

  1. 1. 避免重复计算:缓存常用计算结果
  2. 使用内置函数:优先使用TradingView内置函数
  3. 减少绘图数量:过多的绘图会影响性能
  4. 优化循环:避免在每根K线上进行复杂循环

错误处理

pinescript // 检查数据有效性 if not na(close) and not na(volume) // 安全计算 volumeAvg = ta.sma(volume, 20) else volumeAvg := na

🎨 图表绘制技巧

线条绘制

pinescript // 基本线条 plot(series, title, color, linewidth, style)

// 样式选项
plot.style_line // 实线
plot.style_linebr // 虚线
plot.style_circles // 圆点
plot.style_cross // 十字
plot.style_area // 面积图

形状标记

pinescript // 标记点 plotshape(condition, title, location, color, style, size)

// 位置选项
location.abovebar // K线上方
location.belowbar // K线下方
location.top // 图表顶部
location.bottom // 图表底部

背景色

pinescript // 条件背景色 bgcolor(condition ? color.new(color.green, 90) : na)

表格显示

pinescript // 创建表格 var table myTable = table.new(position.top_right, 1, 1)

// 更新表格
table.cell(myTable, 0, 0, str.tostring(close, format.volume))

📊 策略开发

策略模板

pinescript //@version=5 strategy(My Strategy, overlay=true, marginlong=100, marginshort=100)

// 输入参数
fastLength = input.int(12, 快线周期)
slowLength = input.int(26, 慢线周期)

// 计算指标
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)

// 交易条件
longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)

// 执行交易
if (longCondition)
strategy.entry(Long, strategy.long)

if (shortCondition)
strategy.entry(Short, strategy.short)

// 止损止盈
strategy.exit(Exit Long, Long, stop=close 0.95, limit=close 1.05)
strategy.exit(Exit Short, Short, stop=close 1.05, limit=close 0.95)

仓位管理

pinescript // 固定仓位 strategy.entry(Long, strategy.long, qty=1)

// 基于资金的仓位
capital = strategy.equity
riskPercent = 0.02 // 2%风险
stopLossPips = 100
positionSize = (capital * riskPercent) / stopLossPips
strategy.entry(Long, strategy.long, qty=positionSize)

回测设置

pinescript // 回测时间范围 strategy(title=My Strategy, overlay=true, initial_capital=10000, defaultqtytype=strategy.percentofequity, defaultqtyvalue=100, commission_type=strategy.commission.percent, commission_value=0.1)

🔔 警报系统

基本警报

pinescript // 简单警报 alertcondition(close > open, 阳线警报, 出现阳线)

// 复杂条件
bullishAlert = close > ta.sma(close, 20) and volume > ta.sma(volume, 20)
alertcondition(bullishAlert, 看涨放量警报, 价格上穿20日均线且放量)

策略警报

pinescript // 策略中的警报 if longCondition alert(买入信号: + syminfo.ticker, alert.freqonceper_bar)

if shortCondition
alert(卖出信号: + syminfo.ticker, alert.freqonceper_bar)

自定义消息

pinescript // 详细警报消息 alertMessage = 品种: + syminfo.ticker + \n价格: + str.tostring(close) + \n时间: + str.tostring(time, yyyy-MM-dd HH:mm) alertcondition(bullishAlert, 详细警报, alertMessage)

🛠️ 调试技巧

打印调试

pinescript // 使用表格显示调试信息 var table debugTable = table.new(position.bottom_right, 2, 5) table.cell(debugTable, 0, 0, Close: + str.tostring(close)) table.cell(debugTable, 1, 0, SMA: + str.tostring(ta.sma(close, 20)))

条件调试

pinescript // 只在特定条件下显示调试信息 debugCondition = bar_index % 100 == 0 // 每100根K线显示一次 if debugCondition label.new(bar_index, high, Close: + str.tostring(close), color=color.yellow, style=label.stylelabeldown)

性能监控

pinescript // 计算执行时间 var int startTime = na if barstate.isfirst startTime := timenow

if barstate.islast
executionTime = timenow - startTime

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 pine-script-developer-1776107362 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 pine-script-developer-1776107362 技能

通过命令行安装

skillhub install pine-script-developer-1776107362

下载

⬇ 下载 pine-script-developer v1.0.0(免费)

文件大小: 12.61 KB | 发布时间: 2026-4-14 14:44

v1.0.0 最新 2026-4-14 14:44
Initial release of pine-script-developer skill:

- 专业Pine Script开发技能,涵盖指标和策略的编写、调试与优化
- 详细介绍Pine Script v5语法、常用函数和性能优化方法
- 提供策略开发、警报系统、调试技巧和高级功能的实用模板
- 包含 TradingView 技术指标、策略回测、仓位管理与常见代码结构
- 内附常用代码片段、实用最佳实践与完整代码审查清单

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部