check if price is near an average, is it within some range around an average?
a yellow square is drawn when the desired price parameter is within the range.
range can be defined by,
..dollars (0.10) default
..percent (0.4)
candle price used can be,
..close (close has to end up in the range)
..any (any time price is within the range)
can pick the average parameters and show the line. default is EMA50
-----------------------------
these are the 2 formulas for checking if price is 'near' an average. tolerance is some dollar amount.
check if candle closed within a range , average +- a tolerance.
def b = if (close > (average - tolerance) and close < (average + tolerance) then 1 else 0;
check if any part of the candle is within a range,
def b = if (high > (average - tolerance) and low < (average + tolerance) then 1 else 0;
close is within the range
yellow squares on the close
some part of the candle crossed into the range
yellow squares on the average line
close is within the range , within the past 4 bars
yellow squares on the close
a yellow square is drawn when the desired price parameter is within the range.
range can be defined by,
..dollars (0.10) default
..percent (0.4)
candle price used can be,
..close (close has to end up in the range)
..any (any time price is within the range)
can pick the average parameters and show the line. default is EMA50
-----------------------------
these are the 2 formulas for checking if price is 'near' an average. tolerance is some dollar amount.
check if candle closed within a range , average +- a tolerance.
def b = if (close > (average - tolerance) and close < (average + tolerance) then 1 else 0;
check if any part of the candle is within a range,
def b = if (high > (average - tolerance) and low < (average + tolerance) then 1 else 0;
Code:
# price_within_x_avg_ma1_00
# check if price is near an avg
def na = double.nan;
def bn = barnumber();
# last bar ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
input price_near_type = { default price , percent };
input price_within_y_dollars_of_average = 0.1;
#hint price_within_x_of_average: Enter a dollar amount, to create a range, above and below, an average line.
def y = price_within_y_dollars_of_average;
input price_within_p_percent_of_average = 0.4;
#hint price_within_p_percent_of_average: Enter a percent, to create a range, based on a percent of the average line, above and below.
def p = price_within_p_percent_of_average;
input price_came_near_within_x_bars = 1;
#hint price_came_near_within_x_bars: Enter a number for how many bars to look back for a signal. \n..1 = current bar. \n..2 = current bar and previous bar. \n..3 = current bar and 2 previous bars.
def xbars = price_came_near_within_x_bars;
input candle_within_type = { default close , any };
#hint candle_within_type: Pick which method to check if price was within a range around the average. \n..close - the candle close has to be within the range. \n..any - any part of the candle is within the range.
def price = close;
input avg1_len = 50;
input avg1_type = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg1_type, price, avg1_len);
input show_avg_line = yes;
plot z1 = if show_avg_line then avg1 else na;
z1.setdefaultcolor(getcolor(1));
z1.hidebubble();
def ma1hi;
def ma1lo;
def rng;
switch(price_near_type) {
case price:
rng = y;
ma1hi = avg1 + rng;
ma1lo = avg1 - rng;
case percent:
rng = round(avg1*(p/100),2);
ma1hi = avg1 + rng;
ma1lo = avg1 - rng;
}
input show_near_lines = yes;
#hint show_near_lines: Show the range lines, above and below the average.
plot z2 = if show_near_lines then ma1hi else na;
z2.setdefaultcolor(color.gray);
z2.hidebubble();
plot z3 = if show_near_lines then ma1lo else na;
z3.setdefaultcolor(color.gray);
z3.hidebubble();
def b;
def c;
def d;
switch(candle_within_type) {
case close:
b = if (close > ma1lo and close < ma1hi) then 1 else 0;
c = if sum(b, xbars) > 0 then 1 else 0;
d = close;
case any:
b = if (high > ma1lo and low < ma1hi) then 1 else 0;
c = if sum(b, xbars) > 0 then 1 else 0;
d = avg1;
}
input show_near_shape = yes;
#hint show_near_shape: show a yellow square on bars that are within the range lines
plot z4 = if show_near_shape and c then d else na;
z4.setpaintingstrategy(paintingstrategy.squares);
z4.setdefaultcolor(color.yellow);
z4.setlineweight(3);
z4.hidebubble();
addlabel(1, " ", color.black);
addlabel(1, "Near type: " + candle_within_type, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "Price near type: " + price_near_type, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "Near amount: " + rng, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "within " + xbars + " bars", color.yellow);
addlabel(1, " ", color.black);
#
close is within the range
yellow squares on the close
some part of the candle crossed into the range
yellow squares on the average line
close is within the range , within the past 4 bars
yellow squares on the close