logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Topkat  
#1 Posted : Tuesday, February 8, 2005 9:56:36 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

I was hoping someone could direct me to find how to draw an auto trendline (with code, not manually) connecting the latest two peaks (or troughs) on a daily chart. As an example, using a 3% minimum change. I realize the last leg of the peak (or trough) is dynamic, I have code to tell when it is valid and not subject to change. I have read the manual and couldn't find anything, so if there is an example in one of the manuals let me know which one (I missed it). Thank you...
Patrick  
#2 Posted : Tuesday, February 8, 2005 10:05:51 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Do you mean to only plot a line between the last 2 peaks ( or throughs ) and nothing else before and after ? If the answer is yes then let me tell you that it is not possible.
Topkat  
#3 Posted : Tuesday, February 8, 2005 10:18:11 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

<<<Do you mean to only plot a line between the last 2 peaks ( or throughs ) and nothing else before and after ? If the answer is yes then let me tell you that it is not possible.>>> If the line would extend beyond the first or second peak (to the left of the first peak or to the right of the second peak) would be okay. What I would like to be able to do is to check for a close that crosses above the trendline.
Patrick  
#4 Posted : Tuesday, February 8, 2005 10:35:54 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
I'm sorry I'm not sure I understood what you wanted but try this and let me know if I'm getting closer :) LastValue(Trough(1,C,3)); LastValue(Peak(1,C,3))
Topkat  
#5 Posted : Tuesday, February 8, 2005 11:11:28 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Sorry Patrick, I must not be clear with what I am looking for. Let's deal with peaks first. I want to draw a line connecting the last two peaks as defined by the peak function. I assume those to be: pk1:=LastValue(Peak(2,H,3); pk2:=LastValue(Peak(1,H,3); These are the two points or coordinates I want to connect with a trendline. It's okay if the line extends to the left or right on either side of these values. What else do I need to define to draw the trendline that intersects these two points? Thank you...
Patrick  
#6 Posted : Wednesday, February 9, 2005 5:24:05 AM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
That was actually quite a puzzle :D Here is the best I can come up with : {Trough} TR1:=Trough(1,L,3); {current trough Value} TR2:=Trough(2,L,3); {previous trough value} CD1:=TR1<>Ref(TR1,-1); {Trough Value change} CD2:=TR2<>Ref(TR2,-1); {Trough Value change} C1:=ValueWhen(1,CD1,Cum(1)); {Number of bars that have passed till condition } C2:=ValueWhen(2,CD2,Cum(1)); {Number of bars that have passed till condition } V1:=ValueWhen(1,CD1,L); {Low value at time of Current trough} V2:=ValueWhen(2,CD2,L); {Low value at time of Previous trough} Increment:=LastValue((V1-V2)/(C1-C2)); If(TR2<>Ref(TR2,-1),L,PREV+Increment) For the peak version just replace TR1 and TR2. There are problems with this : #1 only the last leg has an accurate value #2 I can't just display the last Leg accross the chart #3 I made it, so there might be a mistake somewhere :) What we are trying to do here is calculate how much the trough value has changed and in how many bars. Then I calculate the daily rate of change and apply it to all the bars starting from the second trough. For example My trough1 is 20 My trough2 is 10 , the value has changed $10 and let's say it was done in 10 bars. It means that each day till the most recent trough the price went up $1. So In the final of my formula I say Trough2 has occured, plot the Low value, if not Use the previous value and add the daily change On trough2 we will have 10 ( Low Value ) on the bar after we will have 11 ( Prev+Increment) next .... 12 (Prev+Increment) I hope this helps explain what I did.
Topkat  
#7 Posted : Wednesday, February 9, 2005 3:15:27 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Patrick... I like what you've done. A couple of observations... first, would it be possible to place small comments by the code to tell what your doing. I can work through the syntax to see what you did, but why you are doing it is kind of a mystery. Second, I was poking around some internet sights and found some code that looks interesting except that they draw trend lines based on a lookback period, and I'm not exactly sure if it can be changed to look at the previous two peaks or troughs. Can I e-mail you a copy of what I found? Thank you... I'm still going through your code.
Patrick  
#8 Posted : Wednesday, February 9, 2005 4:10:39 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Post the code on this thread, I'm sure others will be interrested as well.
Topkat  
#9 Posted : Wednesday, February 9, 2005 7:00:17 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Patrick... yes, that explains it... THANK YOU for taking the time. I never would have thought you would have to define the slope. lol... whatever happened to a simple two point plot?
Topkat  
#10 Posted : Monday, February 14, 2005 2:58:05 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Patrick, Here is some code someone posted on the web for drawing a trendline. It prompts for a period variable (we'll call 'x') which then looks for the last two highs that spanned the next 'x' bars. I'm more interested in its ability to draw only one leg of a line. Drop this indicator on the price chart and play with different periods. Any chance of drawing this kind of line on only the last leg of our peak/trough example? Thank you... peri:=Input("Period",2,100,5); hic:= H>Ref(HHV(H,peri),-1) AND H>=Ref(HHV(H,peri),peri); y1t:=LastValue(ValueWhen( 2, hic=1, H )); y2t:=LastValue(ValueWhen( 1, hic=1, H )); x1t:=LastValue(ValueWhen( 2, hic=1 , Cum(1) )); x2t:=LastValue(ValueWhen( 1, hic=1 , Cum(1) )); {at:=(y1t-y2t)/(x1t-x2t);bt:=y1t-at*x1t;} yt:=(y1t-y2t)/(x1t-x2t)*Cum(1)+y1t-(y1t-y2t)/(x1t-x2t)*x1t; {yt:=at*cum(1)+bt;} e1:=H-yt; ret1:=LastValue(Cum(1)-(x1t+Int((x2t-x1t)/2)-1)); seg1:=LastValue(Int((x2t-x1t)/2+Int(2*peri/5)){+peri-1}); x1:=LastValue(Cum(1)-HHVBars(Ref(e1,-ret1),seg1)-ret1); y1:=LastValue(ValueWhen(1,x1=Cum(1),H)); ret2:=LastValue(Cum(1)-(x2t+Int(4*peri/5))); seg2:=LastValue(Int((x2t-x1t)/2)+Int(4*peri/5)); x2:=LastValue(Cum(1)-HHVBars(Ref(e1,-ret2),seg2)-ret2); y2:=LastValue(ValueWhen(1,x2=Cum(1),H)); {a:=(y1-y2)/(x1-x2);b:=y1-a*x1;} a:=(y1-y2)/(x1-x2); HiTL:=If(Cum(1)>x1t-5,(y1-y2)/(x1-x2)*Cum(1)+y1-(y1-y2)/(x1-x2) *x1,BarsSince(Cum(1)>x1t-5)); Diff:=Ref(HiTL-L,-LastValue(Cum(1)-x2)); decal:=LastValue(HHV(Diff,x2-x1)); HiTL;HiTL-decal;
Patrick  
#11 Posted : Monday, February 14, 2005 4:36:23 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Alright I think we got it. {Trough} TR1:=Trough(1,L,3); TR2:=Trough(2,L,3); CD1:=TR1<>Ref(TR1,-1); CD2:=TR2<>Ref(TR2,-1); C1:=ValueWhen(1,CD1,Cum(1)); C2:=ValueWhen(2,CD2,Cum(1)); V1:=ValueWhen(1,CD1,L); V2:=ValueWhen(2,CD2,L); Div0:=If(C1-C2=0,0.0001,C1-C2); Increment:=LastValue((V1-V2)/Div0); LastLegFix:=ValueWhen(1,Cum(1)=LastValue(C2),L); If(CD1,L,If(CD2,LastlegFix,PREV+Increment)) Try it out let me know if you find any problems with this.
Topkat  
#12 Posted : Monday, February 14, 2005 7:53:27 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Yes, that is it.... I am going to check for crosses above peak trend lines (peak trend lines that are downsloping) and below trough trend lines (trough trend lines that are up sloping). Is there any way to show the whole line from Trough(2,L,3) to Trough(1,L,3) instead of just extending it to the right from Trough(1,L,3)? Showing the whole line touching both troughs allows to visually see if we have two well defined troughs. As it draws now, we cannot tell if both troughs look good. This is a subjective judgement by eye-balling the line touches. It makes it easier to tell if I'm going to play a break of the trend line by how well the troughs 'look'. This comes in handy more for smaller % troughs (4% or lower). Would you think we need to use the log function to accurately show crosses through the trend line?
Patrick  
#13 Posted : Monday, February 14, 2005 8:27:34 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
I edited the formula above to start plotting from the second trough. I try to stay away from the log function as much as possible ...
Topkat  
#14 Posted : Tuesday, February 15, 2005 1:46:34 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

Patrick, Thank you for all your help.
Patrick  
#15 Posted : Tuesday, February 15, 2005 3:48:34 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Anytime :) This is actually quite an interresting indicator, I'm curious to see how it backtest and how many different way you could turn this into a system. Anyway this might be for later though ...
Topkat  
#16 Posted : Tuesday, February 15, 2005 10:42:28 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

It should be fun. There are a few more edits I need to apply. BTW... if they are looking for any enhancement requests for future versions... :D any chance upgrades will increase the number of variables that can be defined? Also, an easier interface to load individual stocks rather than having the user navigate through folders to find them... that could be done behind the scenes, hidden from the user regardless of where they reside. Again, thank you for all you've done!
fiorenzo  
#17 Posted : Monday, February 21, 2005 2:58:42 PM(UTC)
fiorenzo

Forum Rank:: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 2/21/2005(UTC)
Posts: 44
Location: Genoa (Italy)

Thanks: 2 times
Was thanked: 3 time(s) in 3 post(s)
Try editing this new formula. It might be of use: { Trend-lines } pv:= Input("VeryLong=4,Long=3,Med=2,Short=1",1,4,3); pp:= LastValue(If(pv=4,4,If(pv=3,2,If(pv=2,1,0.5)))); r0:= If(MP()>=Ref(MP(),-1),ATR(1),0); r1:= Log(If(r0>0,ATR(1),HIGH)/HIGH); r2:= Cum(r1)/Max(1,Cum(If(r0>0,1,0))); r3:= LastValue(100*Exp(LastValue(r2)))*pp; r4:= LastValue(PeakBars(2,H,r3))-LastValue(PeakBars(1,H,r3)); r5:= (LastValue(Peak(1,H,r3))-LastValue(Peak(2, H,r3)))/r4; {slope} r6:= LastValue(Peak(2, H,r3))+(r5*(Cum(1)-LastValue(Cum(1)- PeakBars(2,H,r3)))); r7:= If(Cum(1) <LastValue(Cum(1)-PeakBars(2,H,r3)),BarsSince(Cum(1)>=LastValue(Cum(1)-PeakBars( 2,H,r3))),r6); s0:= If(MP()<=Ref(MP(),-1),ATR(1),0); s1:= Log(If(s0>0,ATR(1),LOW)/LOW); s2:= Cum(s1)/Max(1,Cum(If(s0>0,1,0))); s3:= LastValue(100*Exp(LastValue(s2)))*pp; s4:= LastValue(TroughBars(2,L,s3))-LastValue(TroughBars(1,L,s3)); s5:= (LastValue(Trough(1,L,s3))-LastValue(Trough(2,L,s3)))/s4; {slope} s6:= LastValue(Trough(2,L,s3))+(s5*(Cum(1)-LastValue(Cum(1)-TroughBars(2,L,s3)))); s7:= If(Cum(1)<LastValue(Cum(1)-TroughBars(2,L,s3)),BarsSince(Cum(1)>=LastValue(Cum(1 )-TroughBars(2, L,s3))),s6); r7;s7;
Patrick  
#18 Posted : Monday, February 21, 2005 5:12:23 PM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Wow that's pretty good, where were you last week :D Have you ran into any problems with this Formula?
Topkat  
#19 Posted : Monday, February 21, 2005 5:57:49 PM(UTC)
Topkat

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/10/2004(UTC)
Posts: 31

fiorenzo, Wow... that is incredible. Thank you very much. Being new at writing these things I am going through it line by line to see what you are doing. However, applying my edits to the code itself looks fairly easy. After this, it's off to try and create a good divergence recognition indicator... :x
Jose  
#20 Posted : Sunday, October 23, 2005 7:38:10 PM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
A very late reply, but better late than never...
Topkat wrote:
... whatever happened to a simple two point plot?
You can find "2-point plot - coding example of 2-point, x/y line plot - useful for creating automatic chart trendlines" here.
Topkat wrote:
After this, it's off to try and create a good divergence recognition indicator...
Look for "Divergence v3 - class "A" (strongest bullish/bearish patterns) signals, price vs 4-indicator divergence" in the "System/signal indicators" section of the above link. The MACDH Divergence kit may also be of interest to you. jose '-)
Users browsing this topic
Guest (Hidden)
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.