Pine Script开发求助:每日15:20基于1分钟K线生成买卖信号
解决方案:1分钟K线图15:20定时买卖信号实现
核心思路
要实现每日15:20的1分钟K线信号,关键是精准匹配目标时间的K线,再与前一根(15:19)K线的收盘价做对比。之前使用time_open/time_close未成功,大概率是因为未准确锁定时间节点,或忽略了时区匹配问题。
Pine Script 实现代码
//@version=5 indicator("15:20 买卖信号", overlay=true) // 定义目标时间:15点20分,可根据时区调整 target_hour = 15 target_minute = 20 // 判断当前K线是否为15:20的1分钟K线 is_target_bar = hour == target_hour and minute == target_minute // 对比当前收盘价与前一根K线收盘价 buy_condition = is_target_bar and close > close[1] sell_condition = is_target_bar and close < close[1] // 绘制买卖信号 plotshape(buy_condition, title="BUY", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", textcolor=color.white, size=size.small) plotshape(sell_condition, title="SELL", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", textcolor=color.white, size=size.small) // 可选:指定时区(比如北京时间GMT+8),避免时区错位 // is_target_bar = hour(time(timeframe.period, "GMT+8")) == target_hour and minute(time(timeframe.period, "GMT+8")) == target_minute
关键细节说明
- 时间匹配:直接用
hour和minute函数判断当前K线的时间,确保只在15:20的1分钟K线触发信号。如果图表时区和本地时区不一致,打开代码中注释的时区指定语句,替换为你需要的时区字符串。 - 收盘价对比:
close[1]代表前一根K线(即15:19的1分钟K线)的收盘价,直接与当前K线收盘价做大小判断即可。 - 信号显示:用
plotshape绘制标签式信号,分别在K线下方(BUY)和上方(SELL)显示,清晰直观。
常见问题排查
- 若信号未触发:检查图表的时区设置是否与代码中时间匹配,比如你所在时区是GMT+8,而图表默认是UTC,就会导致时间错位。
- 若信号重复出现:确保图表周期是1分钟,代码中
indicator的overlay=true已开启,避免在其他周期下误触发。
内容的提问来源于stack exchange,提问作者Ovgu Sayan
相关产品推荐
相关产品推荐

