Newbie wants to draw a horizontal line in 'OnCalculate()' function

 

I found the below mentioned snippet somewhere online and am trying to use it inside `OnCaculate()` function to draw a horizontal line on, let's say the maximum value in last 5 minutes.

int i; //let's assum it has a valid value for the highest value index
if (!ObjectCreate(0, "HLine", OBJ_HLINE, 0, 0, High[i]))
{
         Print(__FUNCTION__, ": Failed to create a horizontal line! Error code =", GetLastError());
         return (false);
}

I see this error in the log window:

"OnCalculate: Failed to create a horizontal line! Error code =4200"


What is wrong in this snippet? If it is incorrect, please guide me with a correct way.

 
verynewuser:

I found the below mentioned snippet somewhere online and am trying to use it inside `OnCaculate()` function to draw a horizontal line on, let's say the maximum value in last 5 minutes.

I see this error in the log window:

"OnCalculate: Failed to create a horizontal line! Error code =4200"


What is wrong in this snippet? If it is incorrect, please guide me with a correct way.

Read this forum post
4200 Object Already Exists Error for Chart Object.
4200 Object Already Exists Error for Chart Object.
  • 2021.11.30
  • www.mql5.com
Right, really beginning to lose my patience here, why do I keep getting the error 4200 - object already exists on the " MovingText " chart label wh...
 
Rajesh Kumar Nait #:
Read this forum post
That person was using `OnInit()` function and I am using `OnCalculate()`. Also, he posted 100s of lines of code and I posted only 4. What is wrong with my code?
 

Every tick arriving causes OnCaculate to execute. The first call it creates the horizontal line but the next call there is error 4200 because the object already exists.

One solution is removing object using ObjectDelete before creating it again.

 
verynewuser #: and I posted only 4. What is wrong with my code?

Every tick, OnCalculate is called. You are trying to create the same object every tick. It exists after the first tick, thus the error 4200.

Create the line in OnInit. Delete it in OnDeinit. Move it to the new location in OnCalculate.

Reason: