OnChartEvent not running until new tick

 

Hello Everyone,

I am currently in the process of translating one of my mt4 codes to mt5. I've noticed something unusual when working with my code and its that when having an event take place in OnChartEvent, the event isn't processed until after a new tick. The code below shows the isolated function. Essentially when you click the button that's created the indicator creates a line that follows your cursor. When you click your mouse it paints the line at the candle where you clicked. The problem is when you click the button to create the line, it will update the line position to your cursor only after a new tick has processed. It causes a "lag" and is very slow.

I read documentation and know that Handling events will be placed in a que in the order they are received (Reference post). Is there a way to prioritize OnChartEvent so it doesn't lag? The code you see below is exactly how I used it in MT4 and it doesn't lag (only difference is I used start() instead of oncalculate()).

Any help is greatly appreciated! Thank you guys 

#property indicator_chart_window

#include <Controls/Dialog.mqh>//includes Edit.mqh...
#include <Controls/SpinEdit.mqh>
#include <Controls/Button.mqh>
#include <Controls/Label.mqh>
CButton button;

input int  X_ButtonPanelOffset = 5;
input int Y_ButtonPanelOffset = 5;
input int ButtonRatio = 10;

int buttonwidth = 8;
int buttonheight = 3;

int charteventcount = 0;

int OnInit()
{

   CreateInterface();
   
   return(INIT_SUCCEEDED);
}
  
void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, 0, -1);
}
  
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{

return(rates_total);
}

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
   int x = lparam;
   int y = dparam;
   datetime dt =0;
   double price =0;
   int window=0;
      
   ChartXYToTimePrice(0, x, y, window, dt, price); 
              
   if(id==CHARTEVENT_CLICK && ObjectGetInteger(0,"button",OBJPROP_STATE) == true && ObjectFind(0, "lineinit") >= 0)
   {     if(charteventcount == 1)
         charteventcount++;
         
         else if(charteventcount > 1)
         {
           ObjectDelete(0, "lineinit");
           ObjectCreate(0, "historyline", OBJ_VLINE, 0, dt, 0);
           ObjectSetInteger(0,"button",OBJPROP_STATE,false);
  
         } 
   }
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button")
   {      
         charteventcount = 1;
         
         ObjectDelete(0, "lineinit");
         ObjectDelete(0, "historyline");
         ObjectCreate(0, "lineinit", OBJ_VLINE, 0, dt, 0);
         ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);
         
   }
   
   
   if(id == CHARTEVENT_MOUSE_MOVE && ObjectFind(0, "lineinit") >= 0)
   {
   ObjectSetInteger(0, "lineinit", OBJPROP_TIME, dt);
   }  
   
   
}

void CreateInterface()
{
   int fontsize = 1*ButtonRatio;

   button.Create(0,"button",0, X_ButtonPanelOffset,Y_ButtonPanelOffset,buttonwidth*ButtonRatio + X_ButtonPanelOffset,Y_ButtonPanelOffset + buttonheight*ButtonRatio);
   button.ColorBackground(clrRed);
   button.Text("Place Line");
   button.FontSize(fontsize);
}
Event handling OnTick and OnChartEvent
Event handling OnTick and OnChartEvent
  • 2016.06.10
  • www.mql5.com
Hi I am a brand new member of this forum and wondered if I could get a little shove in the right direction with event handling such as OnTick and O...
 

Ok so I tested to see if the event was running only after a tick. Turns out that the function is being passed but the line is not moving to cursor location until after a tick!

I added a print function to see if its catching the cursor moving:

if(id == CHARTEVENT_MOUSE_MOVE && ObjectFind(0, "lineinit") >= 0)
   {
   ObjectSetInteger(0, "lineinit", OBJPROP_TIME, dt);
   printf("MOVING");
   }  

So the terminal is constantly printing "MOVING" when moving the cursor, but it doesn't set the new datetime of the object until after a tick comes in. How is this possible? Its like it is skipping the ObjectSetInteger function until the new tick comes in. Does anybody have any input?

Documentation on MQL5: Object Functions / ObjectSetInteger
Documentation on MQL5: Object Functions / ObjectSetInteger
  • www.mql5.com
ObjectSetInteger - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Try ChartRedraw() after dealing with objects and see if it makes a difference.
 
Keith Watford:
Try ChartRedraw() after dealing with objects and see if it makes a difference.
Holy crap that worked. You are a saint thank you so much!
 
Keith Watford #:
ChartRedraw()

Thank you.

 
Keith Watford #:
Try ChartRedraw() after dealing with objects and see if it makes a difference.
TQ