20 day high/low lines

premiumseller

New member
Hello -

I want to draw two lines that mark the most recent 20 day high and low.

I found the following code that does that for the high:

Code:
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,period) else Double.NaN;
plot HighLine = if BarRem <= period then HighestAll(High65) else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HighLine.SetDefaultColor(Color.GREEN);

My question: How can I draw the line starting at the bar with the actual high (or low) instead of drawing the line across the last 20 days?

Thanks
 
Solution
Hello -

I want to draw two lines that mark the most recent 20 day high and low.

I found the following code that does that for the high:

Code:
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,period) else Double.NaN;
plot HighLine = if BarRem <= period then HighestAll(High65) else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HighLine.SetDefaultColor(Color.GREEN);

My question: How can I draw the line starting at the bar with the actual high (or low) instead of drawing the line across the last 20 days?

Thanks

the original code used highestall and was complex, so i started over from scratch.

pick a...
Hello -

I want to draw two lines that mark the most recent 20 day high and low.

I found the following code that does that for the high:

Code:
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,period) else Double.NaN;
plot HighLine = if BarRem <= period then HighestAll(High65) else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
HighLine.SetDefaultColor(Color.GREEN);

My question: How can I draw the line starting at the bar with the actual high (or low) instead of drawing the line across the last 20 days?

Thanks

the original code used highestall and was complex, so i started over from scratch.

pick a period number, for how many bars back to look for the highest/lowest.
can pick to show all of the lines or start them from the highest/lowest bars.


Code:
#hilo_past20bars_startat_0

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

def lastbar = (!isnan(close) and isnan(close[-1]));
input period = 20;
input start_lines_at_hilo = yes;

def hihi;
def lolo;
if bn == 1 or isnan(close) then {
 hihi = 0;
 lolo = 0;
} else if lastbar[-(period-1)] then {
 hihi = highest(high[-(period-1)], period);
 lolo = lowest(low[-(period-1)], period);
} else {
 hihi = hihi[1];
 lolo = lolo[1];
}

def hihi2 = if !start_lines_at_hilo then hihi
 else if hihi == high then hihi
 else hihi2[1];

def lolo2 = if !start_lines_at_hilo then lolo
 else if lolo == low then lolo
 else lolo2[1];

#plot zhihi = if hihi > 0 then hihi else na;
plot zhihi = if hihi2 > 0 then hihi2 else na;
zhihi.SetPaintingStrategy(PaintingStrategy.DASHES);
zhihi.SetDefaultColor(Color.GREEN);

plot zlolo = if lolo2 > 0 then lolo2 else na;
zlolo.SetPaintingStrategy(PaintingStrategy.DASHES);
zlolo.SetDefaultColor(Color.red);
#

nvYAbpu.jpg
 
Solution
the original code used highestall and was complex, so i started over from scratch.

pick a period number, for how many bars back to look for the highest/lowest.
can pick to show all of the lines or start them from the highest/lowest bars.


Code:
#hilo_past20bars_startat_0

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

def lastbar = (!isnan(close) and isnan(close[-1]));
input period = 20;
input start_lines_at_hilo = yes;

def hihi;
def lolo;
if bn == 1 or isnan(close) then {
 hihi = 0;
 lolo = 0;
} else if lastbar[-(period-1)] then {
 hihi = highest(high[-(period-1)], period);
 lolo = lowest(low[-(period-1)], period);
} else {
 hihi = hihi[1];
 lolo = lolo[1];
}

def hihi2 = if !start_lines_at_hilo then hihi
 else if hihi == high then hihi
 else hihi2[1];

def lolo2 = if !start_lines_at_hilo then lolo
 else if lolo == low then lolo
 else lolo2[1];

#plot zhihi = if hihi > 0 then hihi else na;
plot zhihi = if hihi2 > 0 then hihi2 else na;
zhihi.SetPaintingStrategy(PaintingStrategy.DASHES);
zhihi.SetDefaultColor(Color.GREEN);

plot zlolo = if lolo2 > 0 then lolo2 else na;
zlolo.SetPaintingStrategy(PaintingStrategy.DASHES);
zlolo.SetDefaultColor(Color.red);
#

nvYAbpu.jpg

Thank you!!
 

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