Scan to find FIRST N bar high/low


Category:
0
0

I have the code for a study/scan that will plot/find N bar highs/lows. It works well for plotting/finding instruments that have a N bar high/low in the last N bars. I can’t figure out how to modify the code to find the FIRST N bar high/low (in the last N bars). This would be useful for finding trend changes. At a high level I think the code would plot/find a N bar high/low (N bar high/low is true) in the last N bars and then look back several more bars (adjustable number?) to ensure there were no preceding N bar highs/lows.

input nbar = 20;
def na = double.nan;
input HidePriceChannels={“No”, default “Yes”};
def h=hidepricechannels;
#Start the Code for High
def htest = if Highest(close, nbar) == close then 1 else 0;
plot NBarHigh = if htest then Highest(close, nbar) else na;
plot highchannel=highest(close, nbar);
#Format the plot
NBarHigh.SetDefaultColor(color.blue);
NBarHigh.SetStyle(curve.points);
NBarHigh.SetLineWeight(5);
#Start the Code for Low
def ltest = if Lowest(close, nbar) == close then 1 else 0;
plot nbarlow = if ltest then Lowest(close, nbar) else na;
plot lowchannel=Lowest(close, nbar);
#Format the plots
nbarlow.SetDefaultColor(color.red);
nbarlow.SetStyle(curve.points);
nbarlow.SetLineWeight(5);
highchannel.setDefaultColor(color.blue);
highchannel.setstyle(curve.short_dash);
highchannel.sethiding(h);
lowchannel.setDefaultColor(color.red);
lowchannel.setstyle(curve.short_dash);
lowchannel.sethiding(h);

RESOLVED
Marked as spam
Posted by (Questions: 5, Answers: 4)
Asked on March 12, 2018 9:35 pm
610 views
1
Private answer

The code you provided does not do this. Let me see if I have this correct. For n bars == 20. You want to find the highest bar in the previous 20, but you need there to be no “highest bar in 20”, within the previous 20 bars?

If that is correct, the change you will make to the plot scan statement is as follows:

plot scan = highestCloseInXBars and Highest(highestCloseInXBars[1], xBars) < 1;

I will explain. We signal on a new highest close in x bars. We add to that a test to see if there has been any highest close in x bars in the previous x bars (excluding the current bar).

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 13, 2018 9:55 am
0
This is exactly what I need! Thank you, Pete! It works as expected to find the highest bar in the previous 20, but no “highest bar in 20”, within the previous 20 bars. It does not work as expected to find the lowest bar in the previous 20, but no “lowest bar in 20”, within the previous 20 bars when using “plot scan = lowestCloseInXBars and Lowest(lowestCloseInXBars[1], xBars) < 1;"
( at March 13, 2018 10:17 am)
0

Yeah, I can see how you might think that is correct. But this shows that you really don’t understand what is taking place for that second test. If you care to understand, I’ll try to provide more detail. If you only want the lowest scan to work, then use this:

plot scan = lowestCloseInXBars and Highest(lowestCloseInXBars[1], xBars) < 1;

Tip: the variable, lowestCloseInXBars is either true or false. In thinkscript, true == 1 and false == 0. So for every true/false statement we always evaluate to either a 1 or a 0. So we just check to see if that variable has remained below 1 (true) for the length of xBars. Stated another way. The highest true/false value in xBars is less than 1 (true).

( at March 13, 2018 10:57 am)
0

“plot scan = lowestCloseInXBars and Highest(lowestCloseInXBars[1], xBars) < 1;" works great. Thank you for explaining!

( at March 13, 2018 11:24 am)
0
Private answer

If you want to run a scan to find stocks that have the highest close in x number of bars the solution is very simple. I won’t bother trying to figure out how to get your code to do it. However I will mention that your code looks for highest close in x bars and lowest close in x bars. It does not look for highest high or lowest low.

input xBars = 10;
def highestCloseInXBars = close > Highest(close[1], xBars);
def lowestCloseInXBars = close < Lowest(close[1], xBars);
plot scan = highestCloseInXBars;
# alternate scan for lowest close
#plot can = lowestCloseInXBars;

You should know this is very similar to a Donchian Channel.

You may also find similar material posted here: https://www.hahn-tech.com/ans/donchian-long-entry-alert/

and here: https://www.hahn-tech.com/ans/dong-chian-channel-scan/

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 13, 2018 8:01 am