#HINT: This is a custom watchlist column looking for xx% percent above or below moving average line. \n It provides a visual alert with the percent from MA line where you expect price to revert to the mean by reversing to return to the MA line.
## because this is a watchlist column there is no Edit box. therefore def could have been used instead of input. To change type of price and/or type of moving average, click on the word "Inspector" top right of this box and click on the word close or the word AverageType.SIMPLE and the Inspector will show you your other choices.
input price = close;
input length = 20;
input Average_Type = AverageType.SIMPLE;
input percent_from_MA = 1;
## these def or "define a new word to ThinkScript" did not have to be used, but it saved a lot of time and typing later in the code by providing ability to use the new "words" instead of typing the math formula multiple times in different lines later in the code
def Avg = MovingAverage(average_Type, price, length);
def MA_below_price = price > Avg;
def MA_above_price = price < Avg;
def percent_away = if MA_below_price or MA_above_price then (close / avg) - 1 else 0;
def oversold = percent_away * 100 <= percent_from_MA * (-1);
def overbought = percent_away * 100 >= (Percent_from_MA);
### AddLabel needs (1) condition to trigger it to be visible or the word yes if always visible, (2) text to appear in quotes + data to appear in the label, (3) color for the label which can be static color or can be dynamic using if __ then __ else ___
AddLabel(yes, if oversold then " below " + AsPercent(percent_away) else if overbought then " above " + AsPercent(percent_away) else AsPercent(percent_away), color.BLACK);
AssignBackgroundColor(if oversold then color.DARK_ORANGE else if overbought then color.GREEN else color.LIGHT_GRAY);
# end code ------------------