I am trying to draw a vertical line on MQL5 Ontick event. But its not printing the line.
Anyone knows what the issue in my code?
void OnTick()
{
MqlDateTime mqlDateTime;
TimeToStruct(TimeCurrent(), mqlDateTime);
// Check if it's 10:00 AM and the line hasn't been drawn yet
if(mqlDateTime.hour == 10 && mqlDateTime.min == 0 && !ObjectFind(0, "Line_10AM"))
{
// Create a vertical line at the current time
datetime targetTime = TimeCurrent();
if(!ObjectCreate(0, "Line_10AM", OBJ_VLINE, 0, targetTime, 0))
{
Print("Failed to create line: ", GetLastError());
return;
}
// Set the color and style of the line
ObjectSetInteger(0, "Line_10AM", OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, "Line_10AM", OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, "Line_10AM", OBJPROP_WIDTH, 2);
Your problem with the code as shown is the ObjectFind
Function. The return value for ObjectFind
is the number of the subwindow (0 means the main window of the chart), in which the object is found. If the object is not found, the function returns a negative number.
To correct your code, it should read
MqlDateTime mqlDateTime;
TimeToStruct(TimeCurrent(), mqlDateTime);
Comment(mqlDateTime.hour,"\r\n",mqlDateTime.min);
// Check if it's 10:00 AM and the line hasn't been drawn yet
if(mqlDateTime.hour == 10 && mqlDateTime.min == 00 && ObjectFind(0, "Line_10AM")<0)
{
// Create a vertical line at the current time
datetime targetTime = TimeCurrent();
if(!ObjectCreate(0, "Line_10AM", OBJ_VLINE, 0, targetTime, 0))
{
Print("Failed to create line: ", GetLastError());
return;
}
// Set the color and style of the line
ObjectSetInteger(0, "Line_10AM", OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, "Line_10AM", OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, "Line_10AM", OBJPROP_WIDTH, 2);
}