如何在Pine Script v5中确定鼠标选中收盘价的X周高点?
Pine Script v5 判断选中收盘价对应的X周高点
实现思路
要判断某根K线的收盘价是否为20/40/52周高点,核心逻辑是:定位目标K线后,对比该收盘价与目标K线往前对应周期内的收盘价最高值是否相等。
代码实现
//@version=5 indicator("X周高点判断工具", overlay=true) // 定义要判断的周周期及对应K线数量(日线图按每周5交易日计算,周线图直接用周数即可) weekCycles = [20, 40, 52] barCounts = [20*5, 40*5, 52*5] // 选择目标K线的日期 targetDate = input.time(title="选择目标日期", timestamp=timestamp("2023-01-01")) // 定位目标K线索引 targetBarIdx = ta.barssince(time == targetDate) targetBarIdx := na(targetBarIdx) ? 0 : targetBarIdx // 获取目标K线收盘价 targetClose = close[targetBarIdx] // 判断是否为对应周期高点 is20WeekHigh = targetClose == ta.highest(close, barCounts[0])[targetBarIdx] is40WeekHigh = targetClose == ta.highest(close, barCounts[1])[targetBarIdx] is52WeekHigh = targetClose == ta.highest(close, barCounts[2])[targetBarIdx] // 在图表显示结果 var label resultLabel = label.new(na, na, "", color=color.white, textcolor=color.black, style=label.style_label_down) if barstate.islast resultText = str.format("目标收盘价: {0}\n{1}\n{2}\n{3}", str.tostring(targetClose, "#.##"), is20WeekHigh ? "✅ 是20周高点" : "❌ 不是20周高点", is40WeekHigh ? "✅ 是40周高点" : "❌ 不是40周高点", is52WeekHigh ? "✅ 是52周高点" : "❌ 不是52周高点") label.set_xy(resultLabel, bar_index, high) label.set_text(resultLabel, resultText)
注意事项
- 若使用周线图,直接将
barCounts替换为weekCycles即可,无需转换交易日数量 - 浮点数精度问题可通过
ta.abs(targetClose - ta.highest(close, barCounts[0])[targetBarIdx]) < 1e-6替代==判断 - 若需要鼠标点击选K线,可结合
mouse_click事件扩展逻辑,替换input.time的定位方式
内容的提问来源于stack exchange,提问作者user3848207
相关产品推荐
相关产品推荐

