Check if price is near an average

halcyonguy

Expert
VIP
Lifetime
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;


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
HInyg3t.jpg



some part of the candle crossed into the range
yellow squares on the average line
S1gB2Ug.jpg



close is within the range , within the past 4 bars
yellow squares on the close
lAHkshx.jpg
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
583 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top