lines to rectangles

 

Hello everyone, I have a problem. in my for loop, the bot draws lines at the top and bottom anchor points of the red rectangles that are on the chart. unfortunately lines are set uninterrupted and i only need one above and one below per rectangle. unfortunately I don't know how to end the loop, so that logically only one line is set at the top and one at the bottom per rectangle.

for (int i = obj_index; i>=0; i--){
    
      string object_name = ObjectName(ChartID(), i);
          
      if (ObjectGetInteger(ChartID(), object_name, OBJPROP_TYPE) != OBJ_RECTANGLE) continue;
 
      string high_name =  "Line-High"+string(i);
      string low_name  =  "Line-Low"+string(i);       

      if(ObjectGetInteger(ChartID(), object_name,OBJPROP_COLOR)==clrRed){
        
      if(!ObjectCreate(_Symbol,high_name,OBJ_HLINE,0,0,ObjectGetDouble(ChartID(), object_name,OBJPROP_PRICE,1)))PrintFormat("Error object %s not created!",high_name);
      else PrintFormat("object %s created successfully", high_name);
      
      if(!ObjectCreate(_Symbol,low_name ,OBJ_HLINE,0,0,ObjectGetDouble(ChartID(), object_name,OBJPROP_PRICE,0)))PrintFormat("Error object %s not created!",low_name);
      else PrintFormat("object %s created successfully", low_name);
      
      
}
    }
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yango:

Hello everyone, I have a problem. in my for loop, the bot draws lines at the top and bottom anchor points of the red rectangles that are on the chart. unfortunately lines are set uninterrupted and i only need one above and one below per rectangle. unfortunately I don't know how to end the loop, so that logically only one line is set at the top and one at the bottom per rectangle.

It was uninterrupted because you are creating a horizontal line. Use OBJ_TREND instead. Check your refrence guide on how to create such.
 
Yango:

Hello everyone, I have a problem. in my for loop, the bot draws lines at the top and bottom anchor points of the red rectangles that are on the chart. unfortunately lines are set uninterrupted and i only need one above and one below per rectangle. unfortunately I don't know how to end the loop, so that logically only one line is set at the top and one at the bottom per rectangle.

Try this, only checking the objects once. This will probably have issues if objects are deleted from the chart, to resolve that you would need to store the rectangle names in an array and do a full loop through the objects checking against the array by name before drawing the lines.

// Public var out of function
int checkedObjects = 0;

// Loop within function
void DrawLines()
{
   for (int i = checkedObjects; i<ObjectsTotal(); i++)
   {
    
      string object_name = ObjectName(ChartID(), i);
          
      if (ObjectGetInteger(ChartID(), object_name, OBJPROP_TYPE) != OBJ_RECTANGLE) continue;
 
         string high_name =  "Line-High"+string(i);
         string low_name  =  "Line-Low"+string(i);       
   
         if(ObjectGetInteger(ChartID(), object_name,OBJPROP_COLOR)==clrRed){
           
         if(!ObjectCreate(_Symbol,high_name,OBJ_HLINE,0,0,ObjectGetDouble(ChartID(), object_name,OBJPROP_PRICE,1)))PrintFormat("Error object %s not created!",high_name);
         else PrintFormat("object %s created successfully", high_name);
         
         if(!ObjectCreate(_Symbol,low_name ,OBJ_HLINE,0,0,ObjectGetDouble(ChartID(), object_name,OBJPROP_PRICE,0)))PrintFormat("Error object %s not created!",low_name);
         else PrintFormat("object %s created successfully", low_name);
      
      }
      checkedObjects = i+1;
    }
}
 
Thank-god Avwerosuoghene Odukudu #:
It was uninterrupted because you are creating a horizontal line. Use OBJ_TREND instead. Check your refrence guide on how to create such.

Thank you for your reply. But why would a trendline change the problem? The loop has to be broken somehow.
 
Mark Penzer #:

Try this, only checking the objects once. This will probably have issues if objects are deleted from the chart, to resolve that you would need to store the rectangle names in an array and do a full loop through the objects checking against the array by name before drawing the lines.

Thanks for your answer, Mark!
when the rectangle disappears, the lines should also disappear. but I can't do that until I've ticked off the lines

with your code it only checks a rectangle and then doesn't continue. I'm trying to optimize it, that helped me

 
Yango :

Hello everyone, I have a problem. in my for loop, the bot draws lines at the top and bottom anchor points of the red rectangles that are on the chart. unfortunately lines are set uninterrupted and i only need one above and one below per rectangle. unfortunately I don't know how to end the loop, so that logically only one line is set at the top and one at the bottom per rectangle.

Tell me, who initially draws these rectangles? Does the user draw them by hand?

 
Vladimir Karputov #:

Tell me, who initially draws these rectangles? Does the user draw them by hand?

an indicator plots those on the chart. I've also thought about doing this using the indicator, but that's too complicated because it numbers the rectangles consecutively and always names them differently (because of different colors) and it's therefore difficult to come up with a name.

 
Yango # :

an indicator plots those on the chart. I've also thought about doing this using the indicator, but that's too complicated because it numbers the rectangles consecutively and always names them differently (because of different colors) and it's therefore difficult to come up with a name.

Good. So as soon as a new rectangle appears, it is necessary to draw two lines: along the upper border and along the lower border of the rectangle?

 
Vladimir Karputov #:

Good. So as soon as a new rectangle appears, it is necessary to draw two lines: along the upper border and along the lower border of the rectangle?

Yes, exactly!=)

 
Yango # :

Yes, exactly!=)

Is this an MQL5 indicator?

 
Vladimir Karputov #:

Is this an MQL5 indicator?

yes