input lookback = 50;
input ema1_len = 8;
input ema2_len = 21;
input averageType = AverageType.EXPONENTIAL;
def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);
def bull_cross = ema1 crosses above ema2;
def bear_cross = ema1 crosses below ema2;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);
def ma = MovAvgExponential(close,200);
def s = ma*0.003;
plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.Black else if signal == 1 then Color.Black else Color.BLACK);
AssignBackgroundCOlor(if signal == 2 then Color.Black else if signal == 1 then Color.Black else Color.BLACK);
def myvariable = absvalue(close - ma) is less than or equal to s;
AssignBackgroundColor(if myvariable and bull_cross or myvariable and bear_cross then Color.RED else Color.Black);