distance from previous x bar's high/low

SpaciousO

New member
VIP
Hello. I am looking for a script that would calculate the distance from the close of the current candle from the highest/lowest point of the past 5 candles.
 
Solution
@halcyonguy Thank you. I would like it plotted on every candle as a bubble/text. If it can be small, that would be perfect. The above picture you sent is great! I want to be able to look at previous days and check the distance for candles of different days as well.

this plots the difference numbers on each candle

Code:
#hi_lo_diff_cls_2

#https://usethinkscript.com/threads/script-that-calculated-distance-from-previous-high-low-of-past-5-candles.19219/
#Script that Calculated distance from previous High/Low of past 5 candles

def na = double.nan;
def bn = barnumber();

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def...
Hello. I am looking for a script that would calculate the distance from the close of the current candle from the highest/lowest point of the past 5 candles.


you didn't specify what you want to see or where you want to see something.
so all this does is calculate 2 distances , on each bar, and saves them in these 2 variables,
. above_cls
. below_cls
it doesn't plot anything.

Code:
input past_bars = 5;
def hix = Highest(high[1], past_bars);
def lox = Highest(low[1], past_bars);
def rngx = hix - lox;
#
def above_cls = hix - close;
def below_cls = close - lox;


-------------------


this version draws cyan lines after the last bar, at the high and low levels.
it draws a yellow line at the last bar close.
2 bubbles display the differences, above_cls and below_cls

Code:
#hi_lo_diff_cls

def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def afterlastbar = !isnan(close[1]) and isnan(close[0]);

input past_bars = 5;
def hix = Highest(high[1], past_bars);
def lox = lowest(low[1], past_bars);
def rngx = hix - lox;

def above_cls = round(hix - close,2);
def below_cls = round(close - lox,2);


plot z1 = if afterlastbar then hix[1] else na;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

plot z2 = if afterlastbar then lox[1] else na;
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();

# close
plot z3 = if afterlastbar then close[1] else na;
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.yellow);
z3.setlineweight(2);
z3.hidebubble();


addchartbubble(afterlastbar,hix[1],
above_cls[1]
, color.yellow, yes);

addchartbubble(afterlastbar,lox[1],
below_cls[1]
, color.yellow, no);
#
 

Attachments

  • img1.JPG
    img1.JPG
    36 KB · Views: 74
@halcyonguy Thank you. I would like it plotted on every candle as a bubble/text. If it can be small, that would be perfect. The above picture you sent is great! I want to be able to look at previous days and check the distance for candles of different days as well.
 
@halcyonguy Thank you. I would like it plotted on every candle as a bubble/text. If it can be small, that would be perfect. The above picture you sent is great! I want to be able to look at previous days and check the distance for candles of different days as well.

this plots the difference numbers on each candle

Code:
#hi_lo_diff_cls_2

#https://usethinkscript.com/threads/script-that-calculated-distance-from-previous-high-low-of-past-5-candles.19219/
#Script that Calculated distance from previous High/Low of past 5 candles

def na = double.nan;
def bn = barnumber();

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def afterlastbar = !isnan(close[1]) and isnan(close[0]);

#def last_close = highestall(if lastbar then close else 0);

input show_lines_on_last_bar = no;

def lastlines = (show_lines_on_last_bar and afterlastbar);

input past_bars = 5;
def hix = Highest(high[1], past_bars);
def lox = lowest(low[1], past_bars);
def rngx = hix - lox;

def above_cls = round(hix - close,2);
def below_cls = round(close - lox,2);


plot z1 = if lastlines then hix[1] else na;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

plot z2 = if lastlines then lox[1] else na;
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();

# close
plot z3 = if lastlines then close[1] else na;
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.yellow);
z3.setlineweight(2);
z3.hidebubble();


addchartbubble(lastlines,hix[1],
above_cls[1]
, color.yellow, yes);

addchartbubble(lastlines,lox[1],
below_cls[1]
, color.yellow, no);


input diff_on_each_bar = yes;
 
plot z4 = if diff_on_each_bar then above_cls else na;
z4.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE); 
z4.SetDefaultColor(Color.white);
z4.setlineweight(2);
z4.hidebubble();

plot z5 = if diff_on_each_bar then below_cls else na;
z5.SetPaintingStrategy(PaintingStrategy.VALUES_below); 
z5.SetDefaultColor(Color.white);
z5.setlineweight(2);
z5.hidebubble();
#
 

Attachments

  • img2.JPG
    img2.JPG
    45.2 KB · Views: 62
Solution
@halcyonguy I added it to my chart and it looks good but I was wondering if this can be adjusted for Heiken Ashi close instead of bar close. It seems that it produces just bar close so I am confused as some of the numbers seem disproportionate since I am looking at Heiken Ashi candles.
 

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
559 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