突出显示松树脚本上接近移动平均线的蜡烛

Highlight candle which is near to moving average on pine script

我正在尝试确定蜡烛是否靠近移动平均线(未触及 MA)。触及移动平均线的蜡烛运行良好。如何突出显示靠近 MA 的蜡烛?

这是我当前的代码

sma20 = sma(close, 20)
bullishEC = close > open[1] and close[1] < open[1]
maLongCondition = close > sma20 and (cross(sma20,low) or cross(sma20,close))
buy = bullishEC and maLongCondition
barcolor( buy ? color.yellow : na, title="bullish")

谢谢。

你可以用sma20值+你想要的百分比来计算你想要的面积:

colorAreaPercentage = input(1 , "Colored area precentage" , input.integer , minval = 0)
sma20 = sma(close, 20)
barColor =  close > sma20 and close < sma20 + (sma20 * colorAreaPercentage /100) ? color.fuchsia : close < sma20 and close > sma20 - (sma20 * colorAreaPercentage / 100) ? color.orange : na
if ((high >= sma20 and low <= sma20) or high == sma20 or low == sma20)
    barColor := na
barcolor(barColor)

这将在移动平均线上方 1% 和移动平均线下方 1% 处着色

编辑:

if ((high >= sma20 and low <= sma20) or high == sma20 or low == sma20)
    barColor := na

这部分代码将使触摸蜡烛保持无色。