显示一致颜色的多个时间框架移动平均线

Multiple time frame moving average showing consistent colors

目标是根据以下标准对同一 MA 周期的不同时间范围进行着色。例如,无论发生什么情况,我都希望 5 分钟图表中的 MA 以正确的颜色绘制

问题是脚本将一个更高的时间框架分割成我当前的时间框架。 5 分钟分为五个 1 分钟小节。这些 1 分钟条形图的颜色在绘制时与 5 分钟 TF 颜色无关。请参阅所附图片,带圆圈的方块间隔均匀,但中间有灰色方块,我想将其涂成绿色或理想情况下将其去除。我在 1 分钟的时间范围内,圆圈是 5 分钟的 TF enter image description here

这是 1 分钟的代码,更高的 TF 在需要的地方用 '5' 替换 '1'。

//overlay is false
timeframe1          = input(1, "Timeframe 1", minval=1)

calculateBg(timeframe) =>
    sma = security(syminfo.tickerid, tostring(timeframe), sma(close, 20))
    uptrend = sma > sma[1] and close > sma
    downtrend =   sma < sma[1] and close < sma
    downtrend ? color.red : uptrend ? color.green : color.new(#B2B5BE, 50)

plotshape(1.00, "1", shape.square, location.absolute, color=calculateBg(timeframe1), size=size.tiny, transp=0)

您的计算必须在 security() 的 HTF 上下文中完成。默认 HTF TF 已更改为 5 分钟:

//@version=4
study("")
timeframe1          = input(5, "Timeframe 1", minval=1)

calculateBg(timeframe) =>
    sma = sma(close, 20)
    uptrend = sma > sma[1] and close > sma
    downtrend =   sma < sma[1] and close < sma
    [up, dn] = security(syminfo.tickerid, tostring(timeframe), [uptrend, downtrend])
    dn ? color.red : up ? color.green : color.new(#B2B5BE, 50)

plotshape(1.00, "1", shape.square, location.absolute, color=calculateBg(timeframe1), size=size.tiny, transp=0)