Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Realcode to automate trend line drawing through high/low Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
kamisyed
Posted : Wednesday, February 20, 2013 5:17:15 AM
Registered User
Joined: 10/12/2011
Posts: 24

Hi Bruce,

I was hoping you could help me with a real code for plotting trend lines through highs & lows. My criteria for the plot is:

1) High Trend Line: Plots last 3 highs. A high is classified when the following condition is true:

    Price.High(1) > Price.High (2) And Price.High(1) > Price.High

    I want a trend line drawn from Price.High(1) each of the last 3 times the condition was met,.

2) LowTrend Line: Plots last 3 lows. A low is classified when the following condition is true:

    Price.low(1) > Price.low(2) And Price.low(1) > Price.low

    I want a trend line drawn from Price.low(1) each of the last 3 times the condition was met,.

 

kamisyed
Posted : Wednesday, February 20, 2013 5:18:28 AM
Registered User
Joined: 10/12/2011
Posts: 24

Sorry..low condition is:

Price.low(1) < Price.low(2) And Price.low(1) < Price.low

Bruce_L
Posted : Wednesday, February 20, 2013 10:25:00 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

We can create a RealCode Indicator to connect all of the highs which meet your requirements:

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com 
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Highs
'|******************************************************************
If Price.High > Price.High(1) AndAlso _
	Price.High > Price.High(-1) Then
	Plot = Price.High
Else
	Plot = Single.NaN
End If

And another RealCode Indicator to connect all of the lows which meet your requirements:

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com 
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Lows
'|******************************************************************
If Price.Low < Price.Low(1) AndAlso _
	Price.Low < Price.Low(-1) Then
	Plot = Price.Low
Else
	Plot = Single.NaN
End If

But there is no obvious way to extend these connections to create trendlines.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Bruce_L
Posted : Wednesday, February 20, 2013 10:57:41 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

To clarify when I say that there is no obvious way to extend these trendlines, we can't extend all three of the possible trendlines created by connecting the different combinations of the three most recent highs in the same indicator.

We can connect the two outside highs when the central high would be at or below the line created by doing so (you can replace everything below the Inherits line in the Class tab of a RealCode Indicator with the following):

	Sub New
		AutoLoop = False
	End Sub
	Public Overrides Function Plot() As System.Single
		If Price.Bar.Count >= 7 Then
			Dim Highs(2) As Integer
			Dim Position As Integer = 2
			For i As Integer = Price.Bar.Count - 2 To 1 Step -1
				If Price.Bar.HighValue(i) > Price.Bar.HighValue(i + 1) AndAlso _
					Price.Bar.HighValue(i) > Price.Bar.HighValue(i - 1) Then
					Highs(Position) = i
					Position -= 1
				End If
				If Position = -1 Then
					Dim Slope As Single = (Price.Bar.HighValue(Highs(2)) - _
						Price.Bar.HighValue(Highs(0))) / _
						(Highs(2) - Highs(0))
					If Price.Bar.HighValue(Highs(1)) <= _
						Price.Bar.HighValue(Highs(0)) + _
						(Highs(1) - Highs(0)) * Slope Then
						Dim trendline As Single = Price.Bar.HighValue(Highs(0))
						For j As Integer = Highs(0) To Price.Bar.Count - 1
							AddToOutput(Price.Bar.DateValue(j), trendline)
							trendline += Slope
						Next
					End If
					Exit For
				End If
			Next
		End If
	End Function
End Class

But which two line segments should we connect if the central high would be above the line created by connecting the two outside highs?

It should probably be noted that the above is for an arithmetic scale. The RealCode would need to be different for a logarithmic scale because a straight line in one of these scales is not a straight line in the other scale.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kamisyed
Posted : Thursday, February 21, 2013 8:22:15 AM
Registered User
Joined: 10/12/2011
Posts: 24

Thanks Bruce!

The codes above serve the purpose!

As always, much appreciated!

filme_bune
Posted : Wednesday, February 19, 2014 3:23:18 PM
Registered User
Joined: 12/12/2011
Posts: 28

I put this code in stock finder and give errors .

Any way to have a code without errors ?

 

Bruce_L
Posted : Wednesday, February 19, 2014 3:41:02 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

I could not say what the issue might be as you did not specify either the specific RealCode being used or the error messages you are receiving.

The RealCode itself does not contain any errors which result in error messages when properly entered into StockFinder (this is not a guarantee that RealCode is bug free as I am not a programmer, just a comment that it does not result in error messages).

If you provide the details of what you are doing and the error messages you are getting, we might be able to figure out what is causing the issue.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
filme_bune
Posted : Wednesday, February 19, 2014 3:47:06 PM
Registered User
Joined: 12/12/2011
Posts: 28

I'm copying the code you post on  February 20, 2013 10:57:41 

from sub new to End Class at the end .

and I paste it in stock finder 

 

Bruce_L
Posted : Wednesday, February 19, 2014 3:50:05 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

Make sure you are pasting the that RealCode into the Class tab of the RealCode Editor for a RealCode Indicator as instructed.

Also make sure that you are only replacing everything below the Inherits line in that RealCode and not replacing all of the RealCode in the Class tab.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
filme_bune
Posted : Wednesday, February 19, 2014 4:01:53 PM
Registered User
Joined: 12/12/2011
Posts: 28

I go to Real code indicator , to class tab , I delete evrything under the inherits line , and I paste your code under the inherits line ( not touching the inherits line )

and it give errors . 

What I do wrong ?

I'm trying to make this stockfinder usefull , but to create such a code , is beyound my knowledge ,

Bruce_L
Posted : Wednesday, February 19, 2014 4:19:00 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

I do not know what might be going wrong as you appear to be describing the correct steps and I do not get any error messages when doing the same thing in my copy of StockFinder.

Make sure you aren't leaving anything out or adding anything extra to the RealCode. Even a single stray character could cause an error message, so if any of the clarifying text above and below the RealCode is getting pasted as well, it would probably cause an error.

You haven't mentioned what the exact error message or messages are. This could be useful in determining what is causing the issue.

I have attached the indicator along with a copy of price history to this post.

You should be able to Open an attached Indicator directly into a running copy of StockFinder 5 (and save it from within StockFinder 5 if desired). You could also Save it to the \My Documents\StockFinder5\(Your Username)\My Indicators\ folder and then load it like you would any other Indicator (or Copy and Paste it there from wherever it Saves if you can't specify the destination directory when Saving).

Attachments:
Connect Recent Highs Demo.sfIndRC - 10 KB, downloaded 959 time(s).



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
filme_bune
Posted : Wednesday, February 19, 2014 4:37:26 PM
Registered User
Joined: 12/12/2011
Posts: 28

I have it now without errors , I just click on the code window and errors disapeared .
I save it , I press ok and all I have is a line almost vertical , in the indicators window .  I dunno what's good  for ?

Is any chance to have this line in the price window ?  like to have a code like this that I can put it in the 
My Condition code editor to find me stocks that have a trend line under their lows ?

Bruce_L
Posted : Wednesday, February 19, 2014 4:57:26 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

You should be able to drag and drop the RealCode Indicator onto Price History and select Overlay with Price History to get the RealCode Indicator and Price History into the same pane and scale. The Working with Indicators video explores this in more detail.

Note that while kamisyed seemed to find it useful, it was really only designed as a demo. It only draws a line connected the first most recent high and the third most recent high if the second most recent high does not protrude above that line on an arithmetic chart.

This was designed to point out that it is not usually possible to connect the three most recent highs using a straight line because the central line will usually be above or below that line.

So even though we can identify all of the highs (assuming they are objectively defined as was done by kamisyed) and connect these highs with lines, it does not mean that we could logically extend any particular set of these lines to create trendlines without further refinement of the definition to specify which lines would need to be extended.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
filme_bune
Posted : Wednesday, February 19, 2014 5:07:30 PM
Registered User
Joined: 12/12/2011
Posts: 28

Is easier to just draw miself the line .  so much trouble for a line that doesnt do anything . 
it should be a way to find stocks with a line that unite 3 lows , 

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.