Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

huangapple go评论53阅读模式
英文:

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

问题

我正在尝试计算4小时内的close - ta.sma(close, 5)并在1小时图上显示它。我有以下代码:

//@version=5
indicator("close - SMA(5)", overlay=true)
[momentumClose, momentumClose4] = request.security(syminfo.tickerid, "240", [close - ta.sma(close, 5), close[4] - ta.sma(close[4], 5)], barmerge.gaps_on)
plot(momentumClose)

hline(0, "Zero Line", color=color.black, linestyle=hline.style_solid, linewidth=2)
bgcolor(momentumClose - momentumClose4 > 0 ? color.green : color.red)

我得到了下面的图像。这是可以理解的,因为在当前时间框架的每个条上,momentumClosemomentumClose4 可能没有任何数据,因为它包含每4个(1小时)条的数据。

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

如果我使用 barmerge.gaps_off,我得到了下面的图像。虽然背景现在是正确的,但我不想在较低时间框架的每个条上重新计算这个图表。

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

是否可以使这个图表仅在每4小时而不是每1小时在1小时时间框架上计算?我应该处理 barmerge.gaps_off 还是在较高时间框架上绘图呢?

英文:

I'm trying to calculate the close - ta.sma(close, 5) of 4H and display it on 1H. I have the following code:

//@version=5
indicator("close - SMA(5)")
[momentumClose, momentumClose4] = request.security(syminfo.tickerid, "240", [close - ta.sma(close, 5), close[4] - ta.sma(close[4], 5)], barmerge.gaps_on)
plot(momentumClose)

hline(0, "Zero Line", color=color.black, linestyle=hline.style_solid, linewidth=2)
bgcolor(momentumClose - momentumClose4 > 0 ? color.green : color.red)

I get the below image. This is understandable as for every bar on current timeframe's momentumClose, momentumClose4 may not have any data as it contains data every 4 (1H) bars.

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

If I use barmerge.gaps_off instead, I get the below image. While the background is now correct, I don't want to be recalculating this chart on every bar of the lower timeframe.

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

Is it possible to make this plot only calculate every 4H rather than every 1H on a 1H timeframe? Should I deal with barmerge.gaps_off or plotting on the higher timeframe instead?

答案1

得分: 0

我决定采用以下内容:

indicator("动量 4小时")
momentumClose = request.security(syminfo.tickerid, "240", ta.sma(close, 20), barmerge.gaps_on)

calculate_difference_htf_current_vs_bars_ago(indicator, htf_bars_ago) =>
    float _current_val = na
    float _previous_val = na
    int _count = 0
    for i = 0 to 100  // 100是占位符
        if not na(indicator[i])
            _count += 1
            if _count == 1
                _current_val := indicator[i]
            else if _count == htf_bars_ago
                _previous_val := indicator[i]
                break
    _current_val - _previous_val

bgcolor(calculate_difference_htf_current_vs_bars_ago(momentumClose, 5) > 0 ? color.green : color.red)
英文:

I decided to go with the following:

indicator("Momentum 4H")
momentumClose = request.security(syminfo.tickerid, "240", ta.sma(close, 20), barmerge.gaps_on)

calculate_difference_htf_current_vs_bars_ago(indicator, htf_bars_ago) =>
    float _current_val = na
    float _previous_val = na
    int _count = 0
    for i = 0 to 100  // 100 is placeholder
        if not na(indicator[i])
            _count += 1
            if _count == 1
                _current_val := indicator[i]
            else if _count == htf_bars_ago
                _previous_val := indicator[i]
                break
    _current_val - _previous_val

bgcolor(calculate_difference_htf_current_vs_bars_ago(momentumClose, 5) > 0 ? color.green : color.red)

Draw chart of indicator on every higher timeframe bar instead of current timeframe bar

It will fetch the value of the latest value from the HTF, then traverse backwards until it gets the value of the HTF htf_bars_ago then return the difference.

If anyone has any better ideas than this, I'd be keen to know.

huangapple
  • 本文由 发表于 2023年5月28日 06:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定