Pine Script如何引用历史K线数据并在Ichimoku指标对应位置绘制标记
问题解答&代码实现
1. 历史K线数值获取说明
- Pine Script的历史引用运算符
[]默认以当前K线为索引0,填入正整数n即可获取往前第n根K线的对应数值,不需要写负数- 获取往前30根K线最高价:
h = high[30] - 获取往前30根K线的自定义指标值:
hist_TKk = TKk[30]、hist_KJk = KJk[30]、hist_CSk = CSk[30]
- 获取往前30根K线最高价:
2. 指定位置绘制标记实现
用plotshape函数可以实现你要的绿色小点绘制需求:
- 绘制在目标K线下方:设置
location = location.belowbar - 绘制在TKk对应价位:直接将
y参数设置为对应的历史TKk值即可
3. 三者位置关系判断
直接对三个历史值做大小比较即可,可根据比较结果输出不同的标记颜色或者标签内容:
// 判断上下关系示例 rank_condition = hist_TKk > hist_KJk and hist_KJk > hist_CSk ? 1 : hist_CSk > hist_KJk and hist_KJk > hist_TKk ? -1 : 0
完整修改后的一目均衡表代码
//@version=5 indicator("Ichimoku 历史值标记", overlay=true) res = input.timeframe(title='Time Frame', defval='15') TKlength = input.int(10, 'Tenkansen Length', minval=1) KJlength = input.int(30, 'Kijunsen Length', minval=1) CSHSlength = input.int(30, 'Chikouspan Length/Horizontal Shift', minval=1) SBlength = input.int(60, 'SenkouspanB Length', minval=1) SAlength = input.int(30, 'SenkouspanA Length', minval=1) // 新增输入项:可自定义要查询的历史K线偏移量 hist_offset = input.int(30, "历史K线偏移量", minval=1) TK = math.avg(ta.lowest(TKlength), ta.highest(TKlength)) KJ = math.avg(ta.lowest(KJlength), ta.highest(KJlength)) CS = close SB = math.avg(ta.lowest(SBlength), ta.highest(SBlength)) SA = math.avg(TK, KJ) TKk = request.security(syminfo.tickerid, res, TK) KJk = request.security(syminfo.tickerid, res, KJ) CSk = request.security(syminfo.tickerid, res, CS) SBk = request.security(syminfo.tickerid, res, SB) SAk = request.security(syminfo.tickerid, res, SA) // 原指标绘制逻辑 plot(TKk, linewidth=2, color=color.new(color.blue, 0), title='TenkanSen') plot(KJk, linewidth=2, color=color.new(color.red, 0), title='KijunSen') plot(CSk, offset=-CSHSlength, linewidth=2, color=color.new(#DDA0DD, 0), title='ChikouSpan') SenkouA = plot(SAk, offset=CSHSlength, linewidth=1, color=color.new(color.green, 0), title='SenkouSpanA') SenkouB = plot(SBk, offset=CSHSlength, linewidth=1, color=color.new(color.purple, 0), title='SenkouSpanB') fill(SenkouA, SenkouB, color=SAk > SBk ? color.green : color.red, transp=90) // ========== 新增历史值处理逻辑 ========== // 获取指定偏移量的历史值 hist_TKk = TKk[hist_offset] hist_KJk = KJk[hist_offset] hist_CSk = CSk[hist_offset] // 绘制目标K线下方的绿色小点 plotshape(barstate.islast, location=location.belowbar, offset=-hist_offset, style=shape.circle, color=color.green, size=size.small) // 绘制对应位置的TKk/KJk/CSk标记点,颜色和原指标保持一致 plot(hist_TKk, offset=-hist_offset, style=plot.style_circles, color=color.blue, size=size.tiny, title="历史TKk") plot(hist_KJk, offset=-hist_offset, style=plot.style_circles, color=color.red, size=size.tiny, title="历史KJk") plot(hist_CSk, offset=-hist_offset, style=plot.style_circles, color=#DDA0DD, size=size.tiny, title="历史CSk") // 判断三者上下位置关系并显示标签 var label hist_label = na if barstate.islast // 排序得到从高到低的顺序 val_arr = array.new_float() array.push(val_arr, hist_TKk) array.push(val_arr, hist_KJk) array.push(val_arr, hist_CSk) array.sort(val_arr, order.descending) rank_text = str.format("前%s根K线数值排序(高→低):\n%.2f\n%.2f\n%.2f", str.tostring(hist_offset), array.get(val_arr, 0), array.get(val_arr, 1), array.get(val_arr, 2)) hist_label := label.new(bar_index - hist_offset, high[hist_offset], rank_text, color=color.white, textcolor=color.black, style=label.style_label_down)
内容的提问来源于stack exchange,提问作者Babakartal
相关产品推荐
相关产品推荐

