I have a custom indicator that draws rays from highs and lows and I would like to have another indicator that shows me when three rays intersect perfectly. Sometimes it looks like they do visually but they don't so an indicator would be helpful. I wrote an excel program to do this and used the slopes and y-intercepts to calculate the intersections and then checked all the three line combinations for a match. I assume I'll have to approach this problem differently as a ninjatrader custom indicator?
First: Do you think this is possible to achieve?
Second: Any ideas on how to approach this?
I was thinking of an "if the y values of rays are equal at certain bars" but the intersections hardly ever occur at exact bar x values. Hmmmm....
If you have two lines that intersect (i'll get to 3 in a minute) then you would compute the intersection as the simultaneous solution of two linear equations: So y=mx+B and z=nx+C .
An example with a downward sloping line from Peak A and an upward sloping line from trough B:
Peak A= 1342 at 17:11 June 10, 2012 and the ray from peak A is downward sloping at -0.02 per minute.
So m=-0.02 and B=1342
Setting midnight of 10 June 2012 = minute zero, then the start time of your ray is 17*60+11 or minute 1031.
So Ray1 draws downward from 1342 at a slope of -0.02, starting from a peak at minute 1031 then it would be:
y=-0.02 * (x-1031) +1342. ; where x is in minutes and y in points.
So at 8:30am on June 11, 2012 minute is 1440+8*60+30 or 1950:
y=-0.02 * (1950-1031) +1342
y= -0.02 * 919 +1342
y= -18.38 + 1342
Trough B = 1299.75 at 6:43 June 11, 2012 (or minute 1440+6*60+43 or 1843) and the ray is ascending at +0.01 points per minute. so n=+0.01 and C=1299.75. It's equation is :
z=+0.01 * (x-1843) + 1299.75
We are solving for x such the y=x ie the point level (price is the same)
y=-0.02 * (x-1031) + 1342.00 (eq1)
z=+0.01 * (x-1843) + 1299.75 (eq2)
------ so at this point you would go back to (eq4) and write the generalized form of the solution and that would give you the time a value where your two rays crossed = intersection1
So you would plot a marker at 1308.42 at 21:10 on June 11 ( and perhaps text as well? "lines cross at 1308.42 at 21:10 June 11, 2012"
------------------re 3 points---------
A straight line can only cross another at one point and one point only.
Now you can see that odds are unlikely that Ray3 actually crosses Ray1 and Ray 2 at exactly intersection1, it might look like it but it is probably only close. This would be a perfect triangulation. The slope of Ray3 HAS TO BE an exact slope and only one slope for the starting point of Ray3 for Ray3 to cross at intersection 1. Ray3 is redundant - that is Ray3, (or Ray4 Ray5 Ray6 for that matter) do not change the intersection point of Ray1 and Ray2.
Using the same approach as above you can compute where Ray3 meets Ray1 = Intersection2. Then you can repeat and find out where Ray3 meets Ray2 = Intersection3.
Probably you will get three intersection points - perhaps close forming a tiny triangle.
So if you are using this for trading then you could probably average the three points or since all the points have to be very close for you to see them as crossing at the same point - OR just ignore Ray3 and Intersection2 and interection3. It isn't changing Intersection Point1.
I hope this helps
Good trading to everyone.
Last edited by aquarian1; June 17th, 2012 at 12:35 AM.
IF you only want to know the intersection after it happens you can just test for a reversal. So in the example above the decending ray is greater than the ascending ray until they cross and then it is less.