Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Days Since Last Peak Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
Sunbird
Posted : Tuesday, December 7, 2004 8:06:50 PM
Registered User
Joined: 10/7/2004
Posts: 45
Many indicators use a recent peak/top or bottom in price to mease % decline, Fibs, etc.

Is there any way (except visually) to automatically mark the last peak and count the number of days since?
Craig_S
Posted : Wednesday, December 8, 2004 9:35:34 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
The formula MAXH21 would return the value of the highest high in the last 21 days. It won't note if it is really a "peak" (the peak could be 22 days ago) or tell you the number of days since. Visually will be the best method for this.

- Craig
Here to Help!
Tanstaafl
Posted : Thursday, December 9, 2004 7:14:19 PM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
You can do this, albeit awkwardly, using the current PCF language. If your search window is fairly short, and the function or price is a simple one, this solution works fine. Here goes:

Solution:
To find how many days ago the MaxH5 value actually occured, you need to check each of the past five days to see if it equals that value, and output the daysago number for that day.

Important trick:
Any boolean (True/false) expression in PCF-eze can be enclosed in parentheses and used as a numeric value in a formula. "True" changes into a minus one, and "false" changes into a zero. This is a very, very useful technique.

For each day in our search, we check if the high for that day is equal to the MaxH5, and if so, we output the day number (we will call today day #1, yesterday day #2 and so on. For the current day, with High value "H0":
(H0=MaxH5)*(-1)
will output +1 if today's high hit the max, or 0 if not

Expand this to five days:
(H0=MaxH5)*(-1)
- (H1=MaxH5)*2
- (H2=MaxH5)*3
- (H3=MaxH5)*4
- (H4=MaxH5)*5


The minus signs are used to convert the -1 "True" multiplier to a +1 multiplier.

At first glance (once you digest this info), you might presume that the possible answers are 1,2,3,4,5. Not quite.

What happens if you hit the same MaxH more than once? For example, if H2=MaxH5 and H3=MaxH5, but the others don't, the answer will be 3+4 = 7. Not very useful ... particularly since you can get a result of 5 in three different ways (figger it out!)

So, here's another method ... reserve one power-of-ten for each of the five days. Check this out:

(H0=MaxH5)*(-1)
- (H1=MaxH5)*20
- (H2=MaxH5)*300
- (H3=MaxH5)*4000
- (H4=MaxH5)*50000

Our prior example, with hits on H2 and H3, will yield an answer of 4300.

OK ... that's pretty clear ... but what if you have to check 20 days? There aren't enough digits available in the pcf display (or internal calc variable) to hold them?

In that case, you need to decide ahead of time, in the case that you have multiple hits, whether the most recent one should be reported, or the oldest one.

If you want to know the most recent of five hits (I'm sticking with five to keep the example short, but you can expanding this out till your fingers get tired if you want):

(H0=MaxH5)*(-1) + (H0<>MaxH5)* (
(H1=MaxH5)*(-2) + (H1<>MaxH5)* (
(H2=MaxH5)*(-3) + (H2<>MaxH5)* (
(H3=MaxH5)*(-4) + (H3<>MaxH5)* (
(H4=MaxH5)*(-5) ))))

This will print out a 2 if the most recent hit was on the H1 day, even if all three prior days were pinned at the MaxH5.

The tricks are:
1) the "Hn<>MaxH5" terms evaluate to the OPPOSITE of the "Hn=MaxH5" terms, and
2) the progressive days are "nested" in parens, so the more recent days override any earlier ones

Just remember: boolean True becomes -1, and boolean False becomes 0, as long as you wrap the entire boolean expression in paren's.

For those of you who care ... this *IS* an "If-Then-Else" clause for TC.

Too bad their is no shortcut to looping! :~)



alatar
Posted : Thursday, December 9, 2004 9:48:46 PM
Registered User
Joined: 10/7/2004
Posts: 4
Location: Suwanee, GA
Jim Dean's logic is, as always, beautiful and irrefutable. But you can improve processing speed by reducing the number of nested parentheses with the following formula to determine the number of days since the high was observed.

5
+ 5*(H0=maxH5)
- 4*(H1=maxH5)*(H1>H0)
- 3*(H2=maxH5)*(H2>maxH2)
- 2*(H3=maxH5)*(H3>maxH3)
- 1*(H4=maxH5)*(H4>maxH4)

Ken Powers
aka "alatar"

StockGuy
Posted : Thursday, December 9, 2004 10:25:01 PM

Administration

Joined: 9/30/2004
Posts: 9,187
QUOTE (alatar)
Jim Dean's logic is, as always, beautiful and irrefutable. But you can improve processing speed by reducing the number of nested parentheses with the following formula to determine the number of days since the high was observed.

5
+ 5*(H0=maxH5)
- 4*(H1=maxH5)*(H1&gt;H0)
- 3*(H2=maxH5)*(H2&gt;maxH2)
- 2*(H3=maxH5)*(H3&gt;maxH3)
- 1*(H4=maxH5)*(H4&gt;maxH4)

Ken Powers
aka &quot;alatar&quot;


Nice work, Ken. I like the values this formula returns as well. A 4 means the peak occurred '4 days ago', 3 = '3 days ago', etc. Simple.

PS - the forum does something weird with the formula when you click Quote, so if you want to use this formula reference alatar's post above.
usertm
Posted : Sunday, December 12, 2004 3:36:50 PM
Registered User
Joined: 11/13/2004
Posts: 141
QUOTE (alatar)
Jim Dean's logic is, as always, beautiful and irrefutable. But you can improve processing speed by reducing the number of nested parentheses with the following formula to determine the number of days since the high was observed.

5
+ 5*(H0=maxH5)
- 4*(H1=maxH5)*(H1>H0)
- 3*(H2=maxH5)*(H2>maxH2)
- 2*(H3=maxH5)*(H3>maxH3)
- 1*(H4=maxH5)*(H4>maxH4)

Ken Powers
aka alatar


This Formula is just what I have been looking for for a while now and I owe a big thanks to Jim Dean for getting this thread rolling as well as to Ken for refining the formula, however I still need a little help here.

So far I have this formula working for finding the max high X number of days ago, but when I tried to change the formula from max high to max low, I get incorrrect results.

Is the formula below correct for finding the max low X number of days ago? or am I doing something wrong.

I am stretching this to 25 days
This one works to find the max high

25
+ 25*(H0=maxH25)
- 24*(H1=maxH25)*(H1>H0)
- 23*(H2=maxH25)*(H2>maxH2)
- 22*(H3=maxH25)*(H3>maxH3)
- 21*(H4=maxH25)*(H4>maxH4)
- 20*(H5=maxH25)*(H5>maxH5)
- 19*(H6=maxH25)*(H6>maxH6)
- 18*(H7=maxH25)*(H7>maxH7)
- 17*(H8=maxH25)*(H8>maxH8)
- 16*(H9=maxH25)*(H9>maxH9)
- 15*(H10=maxH25)*(H10>maxH10)
- 14*(H11=maxH25)*(H11>maxH11)
- 13*(H12=maxH25)*(H12>maxH12)
- 12*(H13=maxH25)*(H13>maxH13)
- 11*(H14=maxH25)*(H14>maxH14)
- 10*(H15=maxH25)*(H15>maxH15)
- 9*(H16=maxH25)*(H16>maxH16)
- 8*(H17=maxH25)*(H17>maxH17)
- 7*(H18=maxH25)*(H18>maxH18)
- 6*(H19=maxH25)*(H19>maxH19)
- 5*(H20=maxH25)*(H20>maxH20)
- 4*(H21=maxH25)*(H21>maxH21)
- 3*(H22=maxH25)*(H22>maxH22)
- 2*(H23=maxH25)*(H23>maxH23)
- 1*(H24=maxH25)*(H24>maxH24)

but when I try to apply this formula to the max low X number of days ago, I get incorrect results.
Example CHRW returns a value of 24, which is actually the max high, rather than the max low. Can some body please help me figure out what I am doing wrong (see below)

25
+ 25*(L0=maxL25)
- 24*(L1=maxL25)*(L1>L0)
- 23*(L2=maxL25)*(L2>maxL2)
- 22*(L3=maxL25)*(L3>maxL3)
- 21*(L4=maxL25)*(L4>maxL4)
- 20*(L5=maxL25)*(L5>maxL5)
- 19*(L6=maxL25)*(L6>maxL6)
- 18*(L7=maxL25)*(L7>maxL7)
- 17*(L8=maxL25)*(L8>maxL8)
- 16*(L9=maxL25)*(L9>maxL9)
- 15*(L10=maxL25)*(L10>maxL10)
- 14*(L11=maxL25)*(L11>maxL11)
- 13*(L12=maxL25)*(L12>maxL12)
- 12*(L13=maxL25)*(L13>maxL13)
- 11*(L14=maxL25)*(L14>maxL14)
- 10*(L15=maxL25)*(L15>maxL15)
- 9*(L16=maxL25)*(L16>MaxL16)
- 8*(L17=maxL25)*(L17>maxL17)
- 7*(L18=maxL25)*(L18>maxL18)
- 6*(L19=maxL25)*(L19>maxL19)
- 5*(L20=maxL25)*(L20>maxL20)
- 4*(L21=maxL25)*(L21>maxL21)
- 3*(L22=maxL25)*(L22>maxL22)
- 2*(L23=maxL25)*(L23>maxL23)
- 1*(L24=maxL25)*(L24>maxL24)

Thank You
UserTM
alatar
Posted : Monday, December 13, 2004 1:24:54 AM
Registered User
Joined: 10/7/2004
Posts: 4
Location: Suwanee, GA
The formula you're using returns the value I would expect. When I look at CHRW's daily chart, the MAX low over the last 25 trading days was 24 trading days ago at $54.44 on November 5th. Incidentally, the MAX high over the last 25 trading days was also 24 trading days ago at $55.37 on November 5th.

I suspect that what you were actually looking for was the MIN low over the last 25 trading days, which was 17 trading days ago at $51.25 on November 16th. Incidentally, the MIN high over the last 25 trading days was 17 trading days ago at $52.11 on November 16th.

Now, here's the puzzling thing. Jim Dean created a separate thread (which has since disappeared) in which he replicated this topic and to which I replied in greater detail (with the general formulas for determining both the highest value and the lowest value over any number of bars). It probably would have been helpful. Let me see if I can recreate it.

The general formula for determining the highest value (the High in this case) over the last [n] bars is:

[n]
+ [n]*(H0=maxH[n])
- [n-1]*(H1=maxH[n])*(H1>H0)
- [n-2]*(H2=maxH[n])*(H2>maxH2)
- ...
- 1*(H[n-1]=maxH[n])*(H[n-1]>maxH[n-1])

The general formula for determining the lowest value (the Low in this case) over the last [n] bars is:

[n]
+ [n]*(L0=minL[n])
- [n-1]*(L1=minL[n])*(L1<L0)
- [n-2]*(L2=minL[n])*(L2<minL2)
- ...
- 1*(L[n-1]=minL[n])*(L[n-1]<minL[n-1])

You can expand either formula to as many terms as you like, but I have found that my computer begins to slow when it exceeds 100 bars. Of course, that is dependent on how fast your processor is, how much RAM you have and how many other PCFs you have. And, if I put too many in custom indicators (to avoid long PCF calc times), I can slow my computer to a virtual crawl. So, forewarned is forearmed. Happy computing and good trading/investing!


usertm
Posted : Monday, December 13, 2004 11:27:29 AM
Registered User
Joined: 11/13/2004
Posts: 141
Ken
I am away from home at the moment, so cannot test right now, but from the looks of it I would say your are correct. I was using the "Max low" value, rather than the "Min low" I should have thought of that.

I will try it when I get home later today

Thank You
UserTM
usertm
Posted : Monday, December 13, 2004 8:54:16 PM
Registered User
Joined: 11/13/2004
Posts: 141
Ken
Thanks again for your help
My Aroon indicator formula is now complete and working.

UserTM
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.