# near_above_avg2
#https://usethinkscript.com/threads/how-to-scan-for-a-range-of-prices.17901/
#I want to scan for if prices are near an SMA, like if prices are up to 10% above the SMA. Is there any way to do that?
#close is equal to SimpleMovingAvg("length" = 10)."SMA" OR
# close*1.10 + close is equal to SimpleMovingAvg("length" = 10)
declare lower;
def na = double.nan;
def bn = barnumber();
def data = close;
input near_percent = 10.0;
#input avg1_type = AverageType.exponential;
input avg1_type = AverageType.Simple;
input avg1_length = 10;
def avg1 = MovingAverage(avg1_type, data, avg1_length );
input include_negative_diff = no;
def diff = if include_negative_diff then absvalue(close - avg1) else (close - avg1);
def near_per = 100*diff/avg1;
def isnear = near_per > 0 and near_per <= near_percent;
plot znear = isnear;
input test_bub = no;
addchartbubble(test_bub and !isnan(close), -0.5,
close + " cls\n" +
avg1 + " avg\n" +
diff + " diff\n" +
near_per + " %"
, (if isnear then color.yellow else color.gray), no);
#plot z = avg1;
#