如何在Pine Script中仅绘制已收盘的10分钟K线?
解决Pine Script仅绘制已收盘10分钟K线的问题
要实现只显示已收盘的10分钟K线,核心是判断10分钟周期的K线是否已确认收盘,并仅在确认后输出K线数据。你之前尝试的lookahead.off用于规避未来数据,但无法过滤未收盘的实时K线,需要结合目标周期的barstate.isconfirmed来实现需求。
修改后的代码
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © n6504 //@version=6 indicator("Finalized 10 min Candles", overlay = true) //INPUTS Show10MinCandles = input.bool(true, title = "Show 10 Minute Candles") // 获取10分钟周期的OHLC以及K线确认状态 [o, h, l, c, isConfirmed] = request.security( syminfo.tickerid, '10', [open, high, low, close, barstate.isconfirmed], barmerge.gaps_on, lookahead=barmerge.lookahead_off ) // 仅当10分钟K线已收盘且开启显示时,输出K线数据,否则返回na plotcandle( Show10MinCandles and isConfirmed ? o : na, Show10MinCandles and isConfirmed ? h : na, Show10MinCandles and isConfirmed ? l : na, Show10MinCandles and isConfirmed ? c : na, title = "10 Minute Candles" )
关键说明
- 在
request.security的返回参数中加入barstate.isconfirmed:这个变量是10分钟周期自身的K线确认状态,当10分钟K线收盘后返回true,实时未收盘时返回false。 - 在
plotcandle中增加isConfirmed判断:只有当10分钟K线已确认收盘(isConfirmed为true)且开启显示开关时,才绘制K线,否则用na跳过绘制。 - 保留
lookahead=barmerge.lookahead_off:确保不会引入未来数据,保证指标的精准性。
内容的提问来源于stack exchange,提问作者Blake Zuidema
相关产品推荐
相关产品推荐

