how to loop and declare zones; And How do i also detect lines that are too close to each other

 

Does any one know how i can loop around all object that are hLine find the nearest line to it and declare it a zone . Like this example first picture below:


And On the second picture shows how would i detect lines that are too close to each other like this example:

Purpose being so i can average out all the lines and plot a line on average position.

 

"nearest line to it" What is it? Until you can state what you want, it can not be coded.

"detect lines that are too close" To close to what?

 
William Roeder #:

"nearest line to it" What is it? Until you can state what you want, it can not be coded.

"detect lines that are too close" To close to what?

I am talking about nearest lines to each other, meaning if a HLine is near each other by 100pip then declare as a zone for both lines.

By "detecting lines that are to close", meaning that i want to be able to see what Hline are near each other so i can add the lines position up and divide it by how much lines are near each other.

For example the second picture has a lot of Hline that are all near each other i want to detect when there is a group of lines that are too close to each other so i can remove them and plot a line that was the average of the lines that was crowded.

 

.

Files:
GetZones.mq5  6 kb
 
entrepreneur vision:

Does any one know how i can loop around all object that are hLine find the nearest line to it and declare it a zone . Like this example first picture below:


And On the second picture shows how would i detect lines that are too close to each other like this example:

Purpose being so i can average out all the lines and plot a line on average position.

You need to include the graphical objects library and declare an array of type CChartObjectHLine and a second array of a structure containing the values necessary for the construction of all the lines in the array of graphical objects.

#include <ChartObjects/ChartObjectsLines.mqh>

CChartObjectHLine HLine[];

struct HLineStruct
  {
   long    chart_id; 
   string  name;
   long    window;   
   double  price;    
  };

HLineStruct       HS[];

You can add array fields with ArrayResize. Then first calculate the data you and store them in the structures. Secondly create the object with data from the corresponding structure and the field index it should belong to. Like so:

h_line[i].Create(HS[i].chart_id, HS[i].name, HS[i].window, HS[i].price); //here you use the values from the corresponding structure directly.

Then you can loop through the HLines by index and check the values by accessing the structure. Of course the structure and object indexes must be corresponding, else it doesn't work. So at all times the structure and object arrays need to hold the same number of fields and you need to perform the same changes to them. You could probably use just one structure to create all objects with different values and then the object class method Price() to work with the values. The structure approach is clunkier but it seemed less abstract to me when I used it. That was some months ago with rectangles.

Then when you have an Expert that creates all your objects you need it to compare and delete those that are too close together. So the others must be passed to the empty places and fill the gaps.

It is not as elegant as the CArrayObj approach above and it will probably need some extensive debugging until it is finished. How familiar are you with debugger?