Pine Script求助:绘制最新K线高点及近两根K线最低低点水平线
Pine Script 实现方案
以下是满足你需求的代码,基于Pine Script v5编写:
//@version=5 indicator("Latest High & Lowest Low Lines", overlay=true) // 获取所需价格数据 latest_high = high[0] // 最新K线的高点 lowest_low = math.min(low[0], low[1]) // 最新K线与前一根K线低点的最小值 // 创建并更新高点水平线及价格标签 var line high_line = na if barstate.islast high_line := line.new(bar_index, latest_high, bar_index + 100, latest_high, color=color.new(color.green, 0), width=2) label.new(bar_index + 5, latest_high, str.tostring(latest_high, "#.####"), color=color.new(color.green, 0), textcolor=color.white, style=label.style_label_left) // 创建并更新最低低点水平线及价格标签 var line low_line = na if barstate.islast low_line := line.new(bar_index, lowest_low, bar_index + 100, lowest_low, color=color.new(color.red, 0), width=2) label.new(bar_index + 5, lowest_low, str.tostring(lowest_low, "#.####"), color=color.new(color.red, 0), textcolor=color.white, style=label.style_label_left)
代码说明:
//@version=5:指定使用Pine Script第5版本,确保语法兼容性indicator(..., overlay=true):将指标叠加在主图上显示latest_high = high[0]:直接获取当前最新K线的高点价格lowest_low = math.min(low[0], low[1]):用math.min()函数取最新K线(low[0])和前一根K线(low[1])低点的最小值var line:使用var关键字确保只创建一次线条对象,避免每根K线重复生成barstate.islast:仅在最新K线时执行线条和标签的创建/更新,提升性能line.new():创建水平线,参数依次为起点X、起点Y、终点X、终点Y、颜色、宽度label.new():在线条右侧添加价格标签,str.tostring()用于格式化价格显示精度,style=label.style_label_left让标签左对齐指向线条
你可以直接将这段代码复制到TradingView的Pine编辑器中运行,根据需要调整颜色、宽度或价格显示精度。
内容的提问来源于stack exchange,提问作者blured
相关产品推荐
相关产品推荐

