Why is no OBJ_HLINE showing on my chart?

 

Morning, 

So I'm plotting a horizontal line on my chart, except I'm not.

My code is the following: 

double   sma   =  iMA(NULL,0,10,0,MODE_SMA,PRICE_OPEN,0);

ObjectCreate(0,"Horiz_Line",OBJ_HLINE,0,0,sma);
   
      ObjectSetInteger(0,"Horiz_Line",OBJPROP_WIDTH,3);
         
            ObjectSetInteger(0,"Horiz_Line",OBJPROP_COLOR,clrGreen);

For demonstration purposes I have simply plotted the line to be at the current SMA price. The place I actually want to plot the line requires significant coding and so wanted to keep it simple for here, as per Forum guidelines. 

I don't see what the problem is here. 

All the best, 

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
 
double   sma   =  iMA(NULL,0,n,0,MODE_SMA,PRICE_OPEN,0);

ObjectCreate(0,"Horiz_Line",OBJ_HLINE,0,0,sma);
   
      ObjectSetInteger(0,"Horiz_Line",OBJPROP_WIDTH,3);
         
            ObjectSetInteger(0,"Horiz_Line",OBJPROP_COLOR,clrGreen);

What is the value of n?

Make sure that the object "Horiz_Line" has not already been created as it may be elsewhere and off screen.

 
Keith Watford #:

What is the value of n?

Make sure that the object "Horiz_Line" has not already been created as it may be elsewhere and off screen.

Sorry, Keith, I've changed the value of n to 10. 

It hasn't already been created, upon OnInit() I set it to clear all objects on the chart to ensure that no "residual" objects remain, so it should'nt be offscreen either :) 

 
TheHonestPrussian #:

Sorry, Keith, I've changed the value of n to 10. 

It hasn't already been created, upon OnInit() I set it to clear all objects on the chart to ensure that no "residual" objects remain, so it should'nt be offscreen either :) 

Then GetLastError() after the code to create the object (If it fails).

 
Keith Watford #:

Then GetLastError() after the code to create the object (If it fails).

Code updated:

double   sma    =  iMA(NULL,0,10,0,MODE_SMA,PRICE_OPEN,0);

if(!ObjectCreate(0,"Horiz_Line",OBJ_HLINE,0,0,sma)){
   
      PrintFormat("Draw Line Error %d",_LastError);
   
   } else {
   
      Print("Your line has been drawn, you just can't see the blighter!");
      
         ObjectSetInteger(0,"Horiz_Line",OBJPROP_WIDTH,3);
            
               ObjectSetInteger(0,"Horiz_Line",OBJPROP_COLOR,clrGreen);
   }

The print result is the following:

2022.06.09 10:50:48.633 xxx EURCHF,H4: Your line has been drawn, you just can't see the blighter!

So no ObjectAlreadyExists error is returning, which says to me it's either plotting somewhere else off of the chart, or it's not picking up with width or the colour. 

Odd. :) 

 
TheHonestPrussian #:


Hello,

I didn't find the problem in your code.

   double   sma   =  iMA(NULL,0,10,0,MODE_SMA,PRICE_OPEN,0);
   Print("sma: ",sma);

   ObjectCreate(0,"Horiz_Line",OBJ_HLINE,0,0,sma);
   ObjectSetInteger(0,"Horiz_Line",OBJPROP_WIDTH,3);
   ObjectSetInteger(0,"Horiz_Line",OBJPROP_COLOR,clrGreen);


On my chart, I found your green horizontal line :)

or you can try to give another color for that horizontal line.
Who knows that way you can see it.

 
Yohana Parmi #:


Hello,

I didn't find the problem in your code.


On my chart, I found your green horizontal line :)

or you can try to give another color for that horizontal line.
Who knows that way you can see it.

Fixed it, it was my fault. I'm too embarrassed to say what it was :P  

 
TheHonestPrussian #: Fixed it, it was my fault. I'm too embarrassed to say what it was :P  

Don't do that. Someone searching might find this thread and still be clueless. What was the problem? What solved what?

How To Ask Questions The Smart Way. (2004)
     When You Ask.
          Follow up with a brief note on the solution.

 
William Roeder #:

Don't do that. Someone searching might find this thread and still be clueless. What was the problem? What solved what?

How To Ask Questions The Smart Way. (2004)
     When You Ask.
          Follow up with a brief note on the solution.

Fair point.

I initiated an ObjectDeleteAll() on OnInit after the ObjectCreate() function, meaning that as the programme reads down the page, it created the plotted lines then immediately deleted them, as it was programmed to do. 

I did place an ObjectDeleteAll() to remove any objects from the chart which may have been drawn/plotted during the previous session, so the EA initialises on a fresh chart. This is also in case there were errors on the code which resulted in the EA not deleting any objects from previous sessions, as a time-saving function.

It was just a simple solution of placing the ObjectDelete() function BEFORE the ObjectCreate() function so the EA initialises with just the lines plotted after any other object is deleted. It deletes any objects on the chart, and then it created the plotted lines.