Line coordinates and prices?

 

Greeings,


  I am new to MQL5 and this is my first post...!  From the research I have been conducting I am unsure if this is possible...

  I have a need to enable detection of 'intersecting' lines on a chart.  I believe it can be determined if lines intersect by obtaining the (X, Y) coordinates of the start of the line and the end of the line (where it goes off the edge of the chart).  If these coordinate values could be calculated it should be feasible to compare these coordinates of lines and confirm 'intersection' with the coordinates of other lines.  I think I know a way to obtain the start coordinate...however I am at a complete loss to figure the coordinate just before the line exits off the chart edge.

  In addition, it would be required to determine the coordinates where the intersection took place, which I am confused about as well.  For this I was thinking comparing arrays of price information along the intersecting lines.  For example if I use the 'Object' functions to create a 'Trendline'...when I hover my cursor over the line on the chart a small dialog box appears with the prices at each and every point along that line.  Is it somehow possible to obtain these prices and store them programmatically?  If this is possible it would just be a matter of comparing the arrays of information for the 2 intersecting lines and finding the price that exists in both arrays...which should be the point of intersection, I believe.

  However this thought is probably too cumbersome even if it were possible to store the prices along a drawn line...as the arrays containing such information would be massive.

  I am hoping there is some other methodology that might be useful in solving these problems.  Any help would be greatly appreciated, I thank you in advance for any suggestions.


Regards



   

 
pangit68:

Greeings,


  I am new to MQL5 and this is my first post...!  From the research I have been conducting I am unsure if this is possible...

  I have a need to enable detection of 'intersecting' lines on a chart.  I believe it can be determined if lines intersect by obtaining the (X, Y) coordinates of the start of the line and the end of the line (where it goes off the edge of the chart).  If these coordinate values could be calculated it should be feasible to compare these coordinates of lines and confirm 'intersection' with the coordinates of other lines.  I think I know a way to obtain the start coordinate...however I am at a complete loss to figure the coordinate just before the line exits off the chart edge.

  In addition, it would be required to determine the coordinates where the intersection took place, which I am confused about as well.  For this I was thinking comparing arrays of price information along the intersecting lines.  For example if I use the 'Object' functions to create a 'Trendline'...when I hover my cursor over the line on the chart a small dialog box appears with the prices at each and every point along that line.  Is it somehow possible to obtain these prices and store them programmatically?  If this is possible it would just be a matter of comparing the arrays of information for the 2 intersecting lines and finding the price that exists in both arrays...which should be the point of intersection, I believe.

  However this thought is probably too cumbersome even if it were possible to store the prices along a drawn line...as the arrays containing such information would be massive.

  I am hoping there is some other methodology that might be useful in solving these problems.  Any help would be greatly appreciated, I thank you in advance for any suggestions.


Regards



   

This code example will help.

I have made it compatible with MT5.

Draw two intersecting trend-lines manually and name them "LineA","LIneB" and then run the script

void OnStart()
  {
   // Linear Equation:
   //
   // y = m x + b
   //
   //             y1 - y2
   // y - y1 = ( --------- ) ( x - x1 )
   //             x1 - x2
   //
   // y = mA (x - xA1) + yA1
   // y = mB (x - xB1) + yB1
   //
  
   // Lines object name
   string lineNameA = "LineA";
   string lineNameB = "LineB";
  
   // Line 1, Point 1
   double xA1 = (double)ObjectGetInteger(0,lineNameA, OBJPROP_TIME,0);
   double yA1 = ObjectGetDouble(0,lineNameA, OBJPROP_PRICE,0);

   // Line 1, Point 2
   double xA2 = (double)ObjectGetInteger(0,lineNameA, OBJPROP_TIME,1);
   double yA2 = ObjectGetDouble(0,lineNameA, OBJPROP_PRICE,1);

   // Line 2, Point 1
   double xB1 = (double)ObjectGetInteger(0,lineNameB, OBJPROP_TIME,0);
   double yB1 = ObjectGetDouble(0,lineNameB, OBJPROP_PRICE,0);

   // Line 2, Point 2
   double xB2 = (double)ObjectGetInteger(0,lineNameB, OBJPROP_TIME,1);
   double yB2 = ObjectGetDouble(0,lineNameB, OBJPROP_PRICE,1);

   // Line slope
   double mA = (yA1 - yA2) / (xA1 - xA2);
   double mB = (yB1 - yB2) / (xB1 - xB2);

   // Calculate intersection points
   double x = (-mB*xB1 + yB1 + mA*xA1 - yA1) / (mA - mB);
   double y = mA * (x - xA1) + yA1;

   // Show results
   Print("Intersections Points: X=", (datetime)x, " , Y=", (double)y);

  }
The intersections of two lines
The intersections of two lines
  • www.mql5.com
In MetaTrader 4, we intend to calculate the intersection coordinates of two straight line objects with different slopes using MQL4.
Files:
Untitled.png  24 kb
 

Thank you for the response, this looks like it could be helpful.  I will take a look at it more closely when I have the chance.


Regards

 

Hello Navdeep,

   One quick question if I may...you are defining 2 points for each line, as here:

   // Line 1, Point 1
   double xA1 = (double)ObjectGetInteger(0,lineNameA, OBJPROP_TIME,0);
   double yA1 = ObjectGetDouble(0,lineNameA, OBJPROP_PRICE,0);

   // Line 1, Point 2
   double xA2 = (double)ObjectGetInteger(0,lineNameA, OBJPROP_TIME,1);
   double yA2 = ObjectGetDouble(0,lineNameA, OBJPROP_PRICE,1);

   // Line 2, Point 1
   double xB1 = (double)ObjectGetInteger(0,lineNameB, OBJPROP_TIME,0);
   double yB1 = ObjectGetDouble(0,lineNameB, OBJPROP_PRICE,0);

   // Line 2, Point 2
   double xB2 = (double)ObjectGetInteger(0,lineNameB, OBJPROP_TIME,1);
   double yB2 = ObjectGetDouble(0,lineNameB, OBJPROP_PRICE,1);

   Are the 2 points for each line the 'start' (the coordinate where the line is drawn from) and 'end' position (where the line extends off the chart boundary)....OR the positions from the 'first' plotted coordinate to the 'second' plotted coordinate?  I thank you again for your time.


Regards