You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script多时间框架RSI买卖信号编写及日期异常排查求助

多时间框架RSI交易策略问题排查

需求说明

  • 买入信号:5分钟周期RSI高于80,且1分钟周期RSI同时高于80
  • 卖出信号:5分钟周期RSI低于20,且1分钟周期RSI同时低于20

初始V4代码问题

代码未定义rsi_5m和rsi_1m变量,直接使用会导致无信号输出:

//@version=4
strategy("how to")

rsi = rsi(close, 14)

long = rsi_5m > 80 and rsi_1m > 80
short = rsi_5m < 80 and rsi_1m < 80

start = timestamp(2023,4,1,0,0)
end = timestamp (2023,4,30,0,0)

if time >= start and time <= end
    strategy.entry("long", strategy.long, 1.0, when = long)
    strategy.entry("short", strategy.short, 1.0, when = short)

plot(rsi, title="1", color=#2600ff, linewidth = 3)

strategy.exit("long", from_entry ="long", loss=30)
strategy.exit("short", from_entry = "short", loss=30)

V5代码报错原因

报错error at 8:10 Could not find function or function reference 'rsi',因为Pine Script V5中所有技术指标函数都归在ta命名空间下,需使用ta.rsi()而非直接rsi();同时V5中跨周期数据获取需用request.security而非security:

//@version=5
strategy("RSI Strategy", overlay=true)

// RSI conditions
rsi_5m = ta.rsi(close, 14)  // 修正:添加ta前缀
rsi_1m = request.security(syminfo.tickerid, "1", ta.rsi(close, 14))  // 修正:V5使用request.security,添加ta前缀

// Buy condition
buy_signal = rsi_5m > 80 and rsi_1m > 80
// Sell condition
sell_signal = rsi_5m < 20 and rsi_1m < 20  // 修正:卖出阈值应为20而非80

// Close buy condition
close_buy_signal = sell_signal  // 修正:当卖出信号触发时平仓多单
// Close sell condition
close_sell_signal = buy_signal  // 修正:当买入信号触发时平仓空单

// Entry and exit
if buy_signal
    strategy.entry("Buy", strategy.long)
if sell_signal
    strategy.entry("Sell", strategy.short)
if close_buy_signal
    strategy.close("Buy")
if close_sell_signal
    strategy.close("Sell")

V4最终代码异常(日期过滤后仅5月初有信号)

异常原因分析

  1. 周期参数错误:代码中security函数使用"15"(15分钟)获取RSI,但需求是5分钟,导致获取的周期数据不符合要求
  2. RSI长度不合理:RSI长度设为1,指标波动极端剧烈,几乎无法稳定满足>80或<20的条件
  3. 卖出条件错误:代码中卖出条件写为rsiValue5m < 80 and rsiValue1m < 80,与需求的<20不符
  4. 日期判断逻辑问题:time变量使用UTC时间戳,若图表时区为本地时区,会导致时间范围判断偏差;同时将日期判断直接写入信号变量,可能限制信号触发时机
  5. 平仓逻辑错误:平仓多单的条件是rsiValue1m < 80,未结合5分钟RSI状态,逻辑不符合策略需求

修复后的V4代码

//@version=4
strategy("LongSell", overlay=true)

// 设置时区为交易所时区(示例为纽约时区,可根据实际修改)
tz = "America/New_York"
// 转换为对应时区的时间戳
startDate = timestamp(tz, 2023, 1, 1, 0, 0)
endDate = timestamp(tz, 2023, 5, 31, 23, 59)

// 获取5分钟和1分钟周期的RSI(长度改为默认14)
rsiValue5m = security(syminfo.tickerid, "5", rsi(close, 14))
rsiValue1m = security(syminfo.tickerid, "1", rsi(close, 14))

// 修正买入/卖出条件
buySignal = rsiValue5m > 80 and rsiValue1m > 80
sellSignal = rsiValue5m < 20 and rsiValue1m < 20

// 结合日期过滤触发交易
strategy.entry("Compra", strategy.long, when=buySignal and time >= startDate and time <= endDate)
strategy.entry("Venta", strategy.short, when=sellSignal and time >= startDate and time <= endDate)

// 修正平仓逻辑:当反向信号触发时平仓
strategy.close("Compra", when=sellSignal)
strategy.close("Venta", when=buySignal)

修复说明

  • 修正周期参数为"5",匹配需求的5分钟RSI
  • 将RSI长度改为14,指标更稳定合理
  • 修正卖出条件阈值为20,符合初始需求
  • 使用时区参数生成时间戳,避免时区偏差导致的日期判断错误
  • 调整平仓逻辑,当反向信号触发时平仓,保证策略逻辑一致性

内容的提问来源于stack exchange,提问作者DiegoR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:50:02