You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

日内自动Fibonacci回撤指标异常:调用前日数据及显示范围求助

问题解决方案

1. 解决获取前一日数据的问题

原代码用request.security(syminfo.tickerid, 'D', high)取日线高低点,默认返回已闭合的前一日周期数据,而非当前交易日的实时高低点。可通过以下方式获取当日实时高低:

实现逻辑

利用ta.barssince(ta.change(time('D')))统计当前交易日以来的K线数量,再用ta.highest/ta.lowest计算该范围内的极值:

// 获取当前交易日的实时高点和低点
current_day_high = ta.highest(ta.barssince(ta.change(time('D'))) + 1)
current_day_low = ta.lowest(ta.barssince(ta.change(time('D'))) + 1)

替换原代码中h和l的定义,后续所有基于高低点的计算改用上述变量即可。

2. 实现仅在当前交易时段显示指标

要隐藏历史时段的Fibonacci线条,只需在绘制时添加条件判断:仅当K线属于当前交易日时才渲染线条,否则返回na(不绘制)。

实现步骤

  • 添加交易日判断条件:
    // 判断当前K线是否属于当前交易日
    is_current_session = time('D') == ta.time('D', bar_index=0)
    
  • 所有plot函数修改为带条件的形式,示例:
    plot(is_current_session ? fib0plot : na, color=color.new(color.gray, 0), linewidth=3, style=plot.style_line)
    

修改后的完整代码

//@version=5
indicator('Auto Fib Track', overlay=true)

// 获取当前交易日的实时高点和低点
current_day_high = ta.highest(ta.barssince(ta.change(time('D'))) + 1)
current_day_low = ta.lowest(ta.barssince(ta.change(time('D'))) + 1)

fibext = input(1)
fib1 = input(0.2360)
fib2 = input(0.382)
fib3 = input(0.50)
fib4 = input(0.618)
fib5 = input(0.786)
range_1 = current_day_high - current_day_low

// 计算Fibonacci水平
fib0plot = current_day_high - range_1 * fibext
fib1plot = range_1 * fib1 + current_day_low
fib2plot = range_1 * fib2 + current_day_low
fib3plot = range_1 * fib3 + current_day_low
fib4plot = range_1 * fib4 + current_day_low
fib5plot = range_1 * fib5 + current_day_low
fib6plot = range_1 * fibext + current_day_low

// 判断是否为当前交易日,仅显示当前时段的线条
is_current_session = time('D') == ta.time('D', bar_index=0)

plot(is_current_session ? fib0plot : na, color=color.new(color.gray, 0), linewidth=3, style=plot.style_line)
plot(is_current_session ? fib1plot : na, color=color.new(color.gray, 0), linewidth=2, style=plot.style_line)
plot(is_current_session ? fib2plot : na, color=color.new(color.gray, 0), linewidth=2, style=plot.style_line)
plot(is_current_session ? fib3plot : na, color=color.new(color.orange, 0), linewidth=2, style=plot.style_line)
plot(is_current_session ? fib4plot : na, color=color.new(color.orange, 0), linewidth=2, style=plot.style_line)
plot(is_current_session ? fib5plot : na, color=color.new(color.gray, 0), linewidth=2, style=plot.style_line)
plot(is_current_session ? fib6plot : na, color=color.new(color.gray, 0), linewidth=3, style=plot.style_line)

额外说明

  • 如果交易时段不是自然日线(如美股盘前/盘后),可将time('D')替换为对应时段的时间框架,例如time("D", session="0930-1600")匹配特定交易时段。
  • 实时高低点会随当前交易日的K线更新动态调整,适配日内交易需求。

内容的提问来源于stack exchange,提问作者Philo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 07:53:08