日内斐波那契指标var line设置:如何让线在最新K线处截止并随新K更新
问题
我制作了一个日内斐波那契(intraday Fibonacci)指标,但由于是首次使用var line(此前一直使用plot),遇到了设置难题。目前设置extend.right时,指标线会向右无限延伸;关闭extend.right后,线又无法覆盖至最新K线。我希望实现指标线自动生成并在最后一根蜡烛处截止,且在新蜡烛形成时自动更新延伸的效果,请问该如何调整以下代码?
//@version=5 indicator(title='Auto Intraday Fibonnaci', overlay=true) line_width = input.int(title="Line Width", defval=2, minval=1, maxval=100) Fhigh = request.security(syminfo.tickerid, 'D', high) Flow = request.security(syminfo.tickerid, 'D', low) F0 = Flow F236 = (Fhigh - Flow) * 0.236 + Flow F382 = (Fhigh - Flow) * 0.382 + Flow F500 = (Fhigh - Flow) * 0.500 + Flow F618 = (Fhigh - Flow) * 0.618 + Flow F786 = (Fhigh - Flow) * 0.786 + Flow F1000 = (Fhigh - Flow) * 1.000 + Flow f_newLine(_color) => line.new(na, na, na, na, xloc.bar_time, extend.right, color=_color, width=line_width) f_moveLine(_line, _x, _y) => line.set_xy1(_line, _x, _y) line.set_xy2(_line, _x+1, _y) var line line_0 = f_newLine(color.gray) var line line_236 = f_newLine(color.gray) var line line_382 = f_newLine(color.gray) var line line_500 = f_newLine(color.orange) var line line_618 = f_newLine(color.orange) var line line_786 = f_newLine(color.gray) var line line_1000 = f_newLine(color.gray) [t0,t236,t382,t500,t618,t786,t1000,dt] = request.security(syminfo.tickerid,"D", [F0,F236,F382,F500,F618,F786,F1000,time], lookahead=barmerge.lookahead_on) if barstate.islast f_moveLine(line_0, dt, t0) f_moveLine(line_236, dt, t236) f_moveLine(line_382, dt, t382) f_moveLine(line_500, dt, t500) f_moveLine(line_618, dt, t618) f_moveLine(line_786, dt, t786) f_moveLine(line_1000, dt, t1000) plotshape(F0 ? F0 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.000', offset=5) plotshape(F236 ? F236 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.236', offset=5) plotshape(F382 ? F382 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.382', offset=5) plotshape(F500 ? F500 : na, style=shape.circle, location=location.absolute, color=color.rgb(255, 235, 59, 100), textcolor=color.new(color.orange, 0), show_last=1, text='0.500', offset=5) plotshape(F618 ? F618 : na, style=shape.circle, location=location.absolute, color=color.rgb(255, 235, 59, 100), textcolor=color.new(color.orange, 0), show_last=1, text='0.618', offset=5) plotshape(F786 ? F786 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.786', offset=5) plotshape(F1000 ? F1000 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='1.000', offset=5)
解决方案
核心思路是关闭extend.right,并将线的终点动态绑定到当前最新K线的时间,这样线会刚好覆盖到最后一根蜡烛,且新K线形成时自动更新终点位置。具体修改如下:
- 修改
f_newLine函数,将extend.right改为extend.none,关闭无限延伸 - 修改
f_moveLine函数,把线的终点从固定的_x+1改为当前K线的time - 移除
barstate.islast限制,让每根K线都更新线的终点,确保实时跟随最新行情
以下是完整修改后的代码:
//@version=5 indicator(title='Auto Intraday Fibonnaci', overlay=true) line_width = input.int(title="Line Width", defval=2, minval=1, maxval=100) Fhigh = request.security(syminfo.tickerid, 'D', high) Flow = request.security(syminfo.tickerid, 'D', low) F0 = Flow F236 = (Fhigh - Flow) * 0.236 + Flow F382 = (Fhigh - Flow) * 0.382 + Flow F500 = (Fhigh - Flow) * 0.500 + Flow F618 = (Fhigh - Flow) * 0.618 + Flow F786 = (Fhigh - Flow) * 0.786 + Flow F1000 = (Fhigh - Flow) * 1.000 + Flow // 修改:关闭无限延伸,使用extend.none f_newLine(_color) => line.new(na, na, na, na, xloc.bar_time, extend.none, color=_color, width=line_width) // 修改:将线的终点设置为当前最新K线的时间time f_moveLine(_line, _startX, _y) => line.set_xy1(_line, _startX, _y) line.set_xy2(_line, time, _y) var line line_0 = f_newLine(color.gray) var line line_236 = f_newLine(color.gray) var line line_382 = f_newLine(color.gray) var line line_500 = f_newLine(color.orange) var line line_618 = f_newLine(color.orange) var line line_786 = f_newLine(color.gray) var line line_1000 = f_newLine(color.gray) [t0,t236,t382,t500,t618,t786,t1000,dt] = request.security(syminfo.tickerid,"D", [F0,F236,F382,F500,F618,F786,F1000,time], lookahead=barmerge.lookahead_on) // 实时更新线的终点,确保跟随最新K线 f_moveLine(line_0, dt, t0) f_moveLine(line_236, dt, t236) f_moveLine(line_382, dt, t382) f_moveLine(line_500, dt, t500) f_moveLine(line_618, dt, t618) f_moveLine(line_786, dt, t786) f_moveLine(line_1000, dt, t1000) plotshape(F0 ? F0 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.000', offset=5) plotshape(F236 ? F236 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.236', offset=5) plotshape(F382 ? F382 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.382', offset=5) plotshape(F500 ? F500 : na, style=shape.circle, location=location.absolute, color=color.rgb(255, 235, 59, 100), textcolor=color.new(color.orange, 0), show_last=1, text='0.500', offset=5) plotshape(F618 ? F618 : na, style=shape.circle, location=location.absolute, color=color.rgb(255, 235, 59, 100), textcolor=color.new(color.orange, 0), show_last=1, text='0.618', offset=5) plotshape(F786 ? F786 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='0.786', offset=5) plotshape(F1000 ? F1000 : na, style=shape.circle, location=location.absolute, color=color.rgb(120, 123, 134, 100), textcolor=color.new(color.gray, 0), show_last=1, text='1.000', offset=5)
关键修改说明
- 关闭无限延伸:
extend.none让线仅显示在起点和终点之间,不会无限向右延伸。 - 动态终点绑定:将线的终点设置为当前K线的
time,每根新K线形成时,终点会自动更新到最新位置,刚好覆盖到最后一根蜡烛。 - 实时更新:移除
barstate.islast判断后,线会在每根K线都更新,确保始终跟随最新行情变化。
内容的提问来源于stack exchange,提问作者Philo
相关产品推荐
相关产品推荐

