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

Pine Script技术需求:每50根K线显示最高价格(仅能显示当前高点)

Pine Script 每50根K线显示对应区间最高价问题解决

问题描述

想要实现每50根K线显示对应区间的最高价格水平,但当前代码仅能显示当前K线的高点,原代码如下:

//@version=5
indicator(title='Deneme', shorttitle='Deneme', overlay=true, timeframe='')
barLength = input.int(50,minval=1,title='Uzunluk')

counter = 0
higher = 0.0

for i = 0 to bar_index
    if counter == 49
        higher := high[50]
    counter := counter+1
    if counter == 50
        counter := 0
    
counterPlot = plot(higher != 0 ? higher : na, title = 'sayac', color = color.new(color.green,0))

问题分析

原代码存在三个核心问题:

  • 用for i = 0 to bar_index遍历所有历史K线,效率极低且逻辑冗余
  • 直接赋值high[50]仅取当前K线往前第50根的单点高点,并非50根K线区间内的最高价
  • 计数器逻辑未正确关联区间的起始与结束,无法锁定完整的50根K线范围

修正后的代码

//@version=5
indicator(title='每50根K线显示区间最高价', shorttitle='区间最高价', overlay=true, timeframe='')
barLength = input.int(50, minval=1, title='K线区间长度')

// 标记区间末尾K线
isRangeEnd = bar_index % barLength == barLength - 1
// 计算当前区间内的最高价
rangeHigh = ta.highest(high, barLength)
// 仅在区间末尾及后续区间保留该最高价,直到下一个区间更新
plot(isRangeEnd or bar_index % barLength > barLength - 1 ? rangeHigh : na, title='区间最高价', color=color.new(color.green, 0), style=plot.style_linebr)

代码说明

  • bar_index % barLength == barLength - 1精准标记每个50根K线区间的最后一根K线
  • ta.highest(high, barLength)实时计算当前区间内的最高价
  • 绘制逻辑确保区间最高价从该区间末尾开始显示,直到下一个区间的最高价生成后自动更新

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 14:35:21