Draw vertical line code does not working

 

Hi Everyone! I am brand new to FOREX and MQL4. I had some limited C/C++ previously and when I realized that there is a possibility to program Expert Advisors I became very excited. So I started learning it from different materials on the net and applied for some udemy tutorials as well. I am progressing fine (I guess) but as it is always happening to new things I run into problems with one of my tasks (which I set up for myself as a learning process).

So with this little task I would like to be able to insert vertical line at a given point. As this is my first attempt I set it up to draw a vertical line every tick, but my code unfortunatelly doesnt work. It is strange because I do not have compiling problems and I do not have error message while the code is running and the code surely runs as I get the alerts.

The source code I took it from the official docs page: https://docs.mql4.com/constants/objectconstants/enum_object/obj_vline.

My reworked code looks like this:

double myMA;

bool myLine=false;


string L_name = "VLine";
int L_date = TimeCurrent();
color L_color = clrRed;
ENUM_LINE_STYLE L_style = STYLE_DASH;
int L_width = 1;
bool L_back = false;
bool L_selection = false;
bool L_hidden = true;
long L_order = 0;


bool VLine(      const long            chart_ID=0,        // chart's ID
                 const string          name="VLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 datetime              time=0,            // line time
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)
{
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);

}


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

Moreover it is strange to me the even the original code did not draw any vertical lines to my chart.

So I would like to ask three questions:

1) Why isn't the original code drawing vertical lines at every tick?

2) Why isn't my code drawing vertical lines at every tick?

3) What does the ObjectSetInteger() command do? What it is good for? (I could not find it out from the original documentation...:( )


Excuse me if I asked something stupid and thank you in advance for your kind help!

Thank you!

OBJ_VLINE - Object Types - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
OBJ_VLINE - Object Types - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
OBJ_VLINE - Object Types - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
 
hunsnowboarder:

So I would like to ask three questions:

1) Why isn't the original code drawing vertical lines at every tick?

2) Why isn't my code drawing vertical lines at every tick?

3) What does the ObjectSetInteger() command do? What it is good for? (I could not find it out from the original documentation...:( )


Excuse me if I asked something stupid and thank you in advance for your kind help!

Thank you!

All you have shown is the function. You don't show that the function is being called.

If you are calling it, are you giving it an individual name each time? An object with the same name can only be drawn once.
 
Keith Watford:
All you have shown is the function. You don't show that the function is being called.

If you are calling it, are you giving it an individual name each time? An object with the same name can only be drawn once.

Hi Keith Watford,


Thank you very much for the quick reply. Somehow I missed to copy the entire code. Ill paste it below again, now the full code:


double myMA;

bool myLine=false;


string L_name = "VLine";
int L_date = TimeCurrent();
color L_color = clrRed;
ENUM_LINE_STYLE L_style = STYLE_DASH;
int L_width = 1;
bool L_back = false;
bool L_selection = false;
bool L_hidden = true;
long L_order = 0;


bool VLine(      const long            chart_ID=0,        // chart's ID
                 const string          name="VLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 datetime              time=0,            // line time
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)
{
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);

}


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  
  }

void OnTick()
  {
;
      
      
      VLine(0,L_name,0,L_date,L_color, L_style, L_width, true, false,true,0);
      //return;
      ChartRedraw();
      Sleep(1000);
      
      Alert("code is running");
  }
 

It will only draw one line and on the next tick give an error 4200 Object already exists.

It will not draw any lines now since there are no incoming ticks, markets closed.

It should draw one line in the tester.

ObejctSetInteger sets the parameters for the object.

int L_date = TimeCurrent();

You should not ignore the compiler warnings TimeCurrent() is not an integer but a datetime datatype.

datetime L_date = TimeCurrent();
 
hunsnowboarder:

Hi Keith Watford,


Thank you very much for the quick reply. Somehow I missed to copy the entire code. Ill paste it below again, now the full code:


double myMA;

bool myLine=false;


string L_name = "VLine";
int L_date = TimeCurrent();
color L_color = clrRed;
ENUM_LINE_STYLE L_style = STYLE_DASH;
int L_width = 1;
bool L_back = false;
bool L_selection = false;
bool L_hidden = true;
long L_order = 0;


bool VLine(      const long            chart_ID=0,        // chart's ID
                 const string          name="VLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 datetime              time=0,            // line time
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)
{
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);

}


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  
  }

void OnTick()
  {
;
      
      
      VLine(0,L_name,0,L_date,L_color, L_style, L_width, true, false,true,0);
      //return;
      ChartRedraw();
      Sleep(1000);
      
      Alert("code is running");
  }
And move your "datetime L_date = TimeCurrent();" within OnTick() body
 

Hi Everyone! Thank you very much for your help and comments!

I changed everything as you proposed and the code still DOES NOT WORK! I get no red vertical line at all!

Everything seems fine (I get the right alert messages) still there is no vertical red line displayed on the chart.


What on earth am I doing wrong?

Thank you.

//+------------------------------------------------------------------+
//|                                                   2MA_X_over.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

double myMA;

bool myLine=false;


string L_name = "VLine";
datetime L_date = TimeCurrent();
color L_color = clrRed;
ENUM_LINE_STYLE L_style = STYLE_DASH;
int L_width = 1;
bool L_back = false;
bool L_selection = false;
bool L_hidden = true;
long L_order = 0;

bool myFlag = true;

bool VLine(      const long            chart_ID=0,        // chart's ID
                 const string          name="VLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 datetime              time=0,            // line time
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)
{
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);

}


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  
  }

void OnTick()
  {
;
      datetime L_date = TimeCurrent();
      
      if (myFlag){
      
         VLine(0,L_name,0,L_date,L_color, L_style, L_width, true, false,true,0);
         //return;
         ChartRedraw();
         Sleep(1000);
         myFlag = false;
        
         Alert("myFlag TRUE");
         }
      else
      {
         Alert("myFlag false");
      }
  }

 
Unless I am just missing seeing it - where do you create the object?
 
hunsnowboarder:

I changed everything as you proposed and the code still DOES NOT WORK! I get no red vertical line at all! 

Your code does not contain ObjectCreate() anywhere in it.

Edit - sorry for cross-post Keith, same thought. 

Edit again - try to get into the habit of checking function return values. ObjectSetInteger() returns a true/false. If you checked the return value, you'd almost certainly get false and GetLastError() would be 4202 (ERR_OBJECT_DOES_NOT_EXIST) e.g.

if(!ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr))
   printf("ObjectSetInteger failed. Error Code: %i",GetLastError());


 

 

Yes! Thank you all for your help! It is working! The problem as mentioned by Keith and honest_knave was that I did not create the object so ObjectCreate() was missing!

Thank you very much for your help again! This is a great community, happy to be here! :)

 
You should also consider to only create an object and set its properties once. There is no point in re-setting its properties every tick.

If you need to move your Vline, just use

ObjectSetInteger(chart_ID,name,OBJPROP_TIME,Time[0]);
when required.
Reason: