如何读取Fib Retracement线价格值?求固定斐波那契实现方案
固定斐波那契回撤位绘制与价格对比实现(Pine Script)
完全可以实现固定的斐波那契回撤位,不会随新价格波动自动更新。下面是基于TradingView Pine Script的实现代码,支持手动指定高低点位置,绘制固定水平位并实时对比当前价格:
//@version=5 indicator("固定斐波那契回撤位", overlay=true) // 手动指定高低点的Bar索引(在图表上找到对应K线的Bar号填入) high_bar_index = input.int(title="高点Bar索引", defval=0, tooltip="找到目标高点K线,输入其Bar索引") low_bar_index = input.int(title="低点Bar索引", defval=0, tooltip="找到目标低点K线,输入其Bar索引") // 获取指定Bar的高低价 high_price = ta.valuewhen(bar_index == high_bar_index, high, 0) low_price = ta.valuewhen(bar_index == low_bar_index, low, 0) // 定义常用斐波那契回撤比例 fib_levels = [0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0] fib_prices = array.new_float() // 计算每个比例对应的价格(从高点到低点的回撤计算逻辑) for level in fib_levels price = low_price + (high_price - low_price) * (1 - level) array.push(fib_prices, price) // 绘制各斐波那契水平位 for i = 0 to array.size(fib_levels) - 1 level = array.get(fib_levels, i) price = array.get(fib_prices, i) plot(price, title=str.tostring(level * 100) + "%", color=color.new(color.blue, 0), linewidth=1, style=plot.style_linebr) // 实时对比当前价格与斐波那契位 current_price = close var label comparison_label = label.new(bar_index, high, "", color=color.white, textcolor=color.black, style=label.style_label_down) // 定位当前价格所在的回撤区间 above_level = 0.0 below_level = 1.0 for i = 0 to array.size(fib_prices) - 1 price = array.get(fib_prices, i) if current_price > price above_level = array.get(fib_levels, i) else below_level = array.get(fib_levels, i) break // 更新对比标签内容 label.set_xy(comparison_label, bar_index, high) label.set_text(comparison_label, str.format("当前价: {0}\n位于 {1}% - {2}% 区间", current_price, above_level*100, below_level*100))
使用说明:
- 在TradingView编辑器新建Pine Script,粘贴上述代码
- 在指标设置面板填入目标高低点的Bar索引:
- 鼠标悬停在目标K线上,TradingView左上角会显示该K线的Bar编号(格式如
Bar 1234)
- 鼠标悬停在目标K线上,TradingView左上角会显示该K线的Bar编号(格式如
- 图表将自动绘制固定斐波那契水平位,右上角标签实时展示当前价格所处的回撤区间
关键逻辑说明:
- 通过
input.int手动锁定高低点位置,确保水平位不会随新价格变动 - 基于指定高低价计算各斐波那契比例对应的价格
- 用
plot绘制水平线,label实时展示价格与回撤位的对比关系
内容的提问来源于stack exchange,提问作者anze87
相关产品推荐
相关产品推荐

