来源:雪球App,作者: 小智投,(https://xueqiu.com/6664855288/314616157)
TradingView 指标系统详解
一、内置指标使用
1. 添加指标的三种方法
点击工具栏中的指标按钮
在图表空白处右键选择"指标"
2. 常用指标设置
主图指标
移动平均线(MA)
Plain Text//@version=5study("Moving Average", overlay=true)length = input(20)src = input(close, title="Source")plot(ta.sma(src, length))
布林带(Bollinger Bands)
Plain Text//@version=5study("Bollinger Bands", overlay=true)length = input(20)mult = input(2.0)basis = ta.sma(close, length)dev = mult * ta.stdev(close, length)upper = basis + devlower = basis - dev
副图指标
RSI(相对强弱指标)
Plain Text//@version=5study("RSI")len = input(14)src = input(close)rsi = ta.rsi(src, len)plot(rsi, "RSI", color.blue)
MACD(移动平均趋同散离)
Plain Text//@version=5study("MACD")fast_length = input(12)slow_length = input(26)signal_length = input(9)[macdLine, signalLine, histLine] = ta.macd(close, fast_length, slow_length, signal_length)
3. 指标参数优化
周期设置
价格来源选择
计算方法调整
显示样式定制
二、Pine Script 基础
1. 语言特点
专为技术分析设计
简单易学
性能优化
安全性保障
2. 基础语法
Plain Text//@version=5indicator("My Script")// 变量声明var float myVar = 0.0// 条件语句if conditionmyVar := 1.0// 循环for i = 1 to 10// 执行操作// 函数定义myFunction() =>result = close > openresult
3. 常用内置函数
技术指标函数
Plain Textta.sma(source, length) // 简单移动平均ta.ema(source, length) // 指数移动平均ta.rsi(source, length) // RSI计算ta.crossover(a, b) // 穿越判断
绘图函数
Plain Textplot(series, title, color, linewidth, style)plotshape(series, title, style, location, color, size)plotarrow(series, title, colorup, colordown, offset)
三、构建自定义指标
1. 基本架构
Plain Textindicator("指标名称", overlay=true/false)// 1. 输入参数定义length = input(14, "周期")src = input(close, "来源")// 2. 计算逻辑myCalc = ta.sma(src, length)// 3. 绘制结果plot(myCalc)
2. 实例:自定义动量指标
Plain Textindicator("动量指标", overlay=false)// 参数length = input(14, "周期")src = input(close, "价格源")// 计算动量momentum = src - src[length]// 绘制plot(momentum, "动量", color = momentum >= 0 ? color.green : color.red)
3. 复杂指标开发技巧
模块化设计
性能优化
错误处理
可视化优化
四、指标组合应用
1. 多指标协同
趋势确认
背离识别
交叉信号
组合过滤
2. 实战示例:趋势跟踪系统
Plain Textstrategy("趋势跟踪系统", overlay=true)// 参数fastLength = input(10)slowLength = input(20)rsiLength = input(14)// 计算指标fastMA = ta.sma(close, fastLength)slowMA = ta.sma(close, slowLength)rsi = ta.rsi(close, rsiLength)// 交易信号longCondition = ta.crossover(fastMA, slowMA) and rsi > 50shortCondition = ta.crossunder(fastMA, slowMA) and rsi < 50// 绘制plot(fastMA, "快线", color.blue)plot(slowMA, "慢线", color.red)
五、调试与优化
1. 常见问题排查
语法错误检查
逻辑错误定位
性能问题诊断
绘图异常处理
2. 性能优化方法
减少重复计算
使用内置函数
优化循环结构
管理变量作用域
3. 回测验证
设置测试周期
分析指标表现
参数敏感度测试
对比基准指标