如何在1分钟策略中调用15分钟周期的RSI Divergence指标值
在1分钟策略中调用15分钟周期RSI Divergence指标
需求说明
需要将15分钟周期的RSI Divergence指标值引入1分钟周期策略,并用divergence > 0作为策略触发条件。
原RSI Divergence指标代码
study(title="RSI Divergence", shorttitle="RSI Divergence") src_fast = close, len_fast = input(5, minval=1, title="Length Fast RSI") src_slow = close, len_slow = input(14,minval=1, title="Length Slow RSI") up_fast = rma(max(change(src_fast), 0), len_fast) down_fast = rma(-min(change(src_fast), 0), len_fast) rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast)) up_slow = rma(max(change(src_slow), 0), len_slow) down_slow = rma(-min(change(src_slow), 0), len_slow) rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow)) divergence = rsi_fast - rsi_slow plotdiv = plot(divergence, color = divergence > 0 ? lime:red, linewidth = 2) band = hline(0)
初步写法的修正与正确实现
你提到的security函数方向正确,但需要注意:不能直接引用当前周期的divergence变量,必须把计算逻辑封装后传入security,确保在15分钟周期环境下完成计算。
方式1:封装计算逻辑直接调用
// 1分钟策略环境 strategy(title="1min Strategy with 15min RSI Divergence", overlay=true) // 封装RSI Divergence计算逻辑 get_divergence() => src_fast = close, len_fast = 5 src_slow = close, len_slow = 14 up_fast = rma(max(change(src_fast), 0), len_fast) down_fast = rma(-min(change(src_fast), 0), len_fast) rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast)) up_slow = rma(max(change(src_slow), 0), len_slow) down_slow = rma(-min(change(src_slow), 0), len_slow) rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow)) rsi_fast - rsi_slow // 获取15分钟周期的divergence值 divergence15 = security(syminfo.tickerid, "15", get_divergence()) // 策略触发条件:15分钟divergence大于0 longCondition = divergence15 > 0 if (longCondition) strategy.entry("Long", strategy.long)
方式2:保留参数可配置性(推荐)
如果需要保留原指标的参数可调性,可以把指标封装为带参数的函数:
// 1分钟策略环境 strategy(title="1min Strategy with 15min RSI Divergence", overlay=true) // 封装带参数的RSI Divergence函数 rsi_divergence(len_fast=5, len_slow=14) => src_fast = close src_slow = close up_fast = rma(max(change(src_fast), 0), len_fast) down_fast = rma(-min(change(src_fast), 0), len_fast) rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast)) up_slow = rma(max(change(src_slow), 0), len_slow) down_slow = rma(-min(change(src_slow), 0), len_slow) rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow)) rsi_fast - rsi_slow // 保留原指标的输入参数配置 len_fast = input(5, minval=1, title="Length Fast RSI") len_slow = input(14, minval=1, title="Length Slow RSI") // 获取15分钟周期的divergence值 divergence15 = security(syminfo.tickerid, "15", rsi_divergence(len_fast, len_slow)) // 策略触发逻辑 longCondition = divergence15 > 0 if (longCondition) strategy.entry("Long", strategy.long)
关键注意点
security的第三个参数必须是在目标周期(15分钟)下运行的计算逻辑,直接引用当前周期变量会导致数据错误。- 若需要实时数据同步,可添加合并参数:
security(syminfo.tickerid, "15", get_divergence(), barmerge.gaps_on, barmerge.lookahead_on),但需注意未来函数的潜在风险。
内容的提问来源于stack exchange,提问作者John Smith
相关产品推荐
相关产品推荐

