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

时间:2021-01-10 09:00:38

标签: pine-script algorithmic-trading

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

这是我当前的代码

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")

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用 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

这部分代码将使触摸的蜡烛不着色。