How do I identify the highest range over the past 10 bars?


0
0

Can someone help me  identify the highest range over the past 10 bars?

I need someone to help fix Think or swim code I have written. The purpose is to give an alert when the current bars range is highest range in previous 10 bars. I am sure this is something simple I am missing below is the code I have written.

input TimeStart = 0950; def time = SecondsFromTime(TimeStart) >= 0; def TRI = TrueRange(high, close, low); def ATR = average(TRI,1); def highatr = atr > Highest(tri, 10);

def b = (highatr && time); plot a = b; a.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); a.SetLineWeight(5); a.SetDefaultColor(Color.YELLOW); Alert(a, “Trend is up!”, Alert.ONCE, Sound.Chimes);

 

Thanks in advance Lindsay

RESOLVED
Marked as spam
Posted by (Questions: 7, Answers: 11)
Asked on May 12, 2018 4:16 pm
132 views
0
Private answer

Yes, we have a very simple change to make to get this working. And since you have posted your own code I will take the time to explain how we get this working.

First, Some Cleanup Work

I have two lines of code that I am removing, by placing a “#” mark at the start of these two lines:

def ATR = average(TRI,1);
def b = (highatr && time);

We do not need the “def ATR” because that is the one period moving average of the TRI variable. A one period moving average of any value is that value. So we don’t need it. This change does nothing to fix the issue. It’s just a matter of cleaning up the code to it minimal required components.

Same thing for the “def b” statement. That assignment can be done directly within the “plot a” statement. So we don’t need this extra step either. Again, this does nothing to fix the issue. It’s just cleaning things up.

Now to Fix the Problem

The mistake is found in this line of code:

def highatr = TRI > Highest(TRI, 10);

But wait. That code is not exactly the same. If you noticed that, good job. You see when I cleaned the code and removed the “def ATR” line I was able to replace ATR in this line of code with TRI. Remember that TRI is exactly the same value as the one period moving average of TRI.

Now that we have it stated in it’s cleanest form, something should jump out at you. We are checking if the current bar’s TRI value is greater than the Highest TRI value of the previous 10 bars (including the current bar). This can never evaluate to true, because you are including the current bar in the “Highest(TRI, 10)”. It’s like saying, the current bar is higher than the current bar. No it’s not, that’s impossible.

The Fix

So we need to use the previous value of TRI within the test for highest:

def highatr = TRI > Highest(TRI[1], 10);

We do this by placing ‘[1]’ at the end of the TRI that is within the test for highest. This excludes the current bar from the test, enabling us to properly compare the highest in 10 to the current bar. Now study this carefully because you may find that you need to reduce that from 10 to 9. Just depends whether your intention was to include the current bar or not, in the test for highest.

The Final Code

input TimeStart = 0950;
def time = SecondsFromTime(TimeStart) >= 0;
def TRI = TrueRange(high, close, low);
#def ATR = average(TRI,1);
def highatr = TRI > Highest(TRI[1], 10);
#def b = (highatr && time);
plot a = (highatr && time);
a.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
a.SetLineWeight(5);
a.SetDefaultColor(Color.YELLOW);
Alert(a, “Trend is up!”, Alert.ONCE, Sound.Chimes);

There it is, all cleaned up and fixed. I left those original lines there but commented out, just in case you decide you need them.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 12, 2018 8:24 pm
0

Thank you so much. Your site is great! Thanks Lindsay

( at May 13, 2018 7:04 am)