MetaTrader 4 MT4 Libraries Capture MouseEvents on Chart – library for MetaTrader 4

Capture MouseEvents on Chart – library for MetaTrader 4

Author:

Russell

The some functions in the cfunctions.dll ( attached ) are wrappers for two user32.dll functions. With those in combination with the extension of WinUser32.mqh will enable you to read the mouseevents on the chart. After which it becomes easy the attach some action to it. In the example I created a button on chart, which will print “right away” ones pressed. It can take some time before the message prints for two reasons.

  • Bid has changed -> Strategy take highest “interupt”.
  • Polling is at low rate; polling rate becomes smaller after a each period of mouse inactivity.

In the example I mainly focussed on not slowing down the trading strategy (on price action).

Code:

//+------------------------------------------------------------------+
//|                                                    mousetest.mq4 |
//|                                Copyright © 2008, Berkers Trading |
//|                                         |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Berkers Trading"
#property link      ""

#include <WinUser32.mqh> //adjusted!!
#include <cfunctions_v1.0.1.mqh>

int init(){
   Print(MT4_cfunctions_version());
   Comment("+-----------------+\n",
           " | Mail Report  |\n",
           "+-----------------+");
   
   return(0);
}

int deinit(){
   return(0);
}

int start(){
   eventPoller();
}

int startStrategy(){
   Print("Run Strat");
   return(0);
}

bool eventPoller(){
   
   int iCoords[2], iCoordsPrevious[2],hWin,liKS;
   hWin = WindowHandle( Symbol(), Period()); 
   static double ldBid, ldPollTime;
   int liTC, liCompareTime;
   
   Print("Start Polling Mouse");
   while(1==1){
      RefreshRates();
      if (Bid != ldBid){
         startStrategy();
      }
      ldBid = Bid;
      liTC = TimeCurrent();
      
      if (liTC > liCompareTime){
          
         MT4_ScreenToClient(hWin, iCoords);        
         if (iCoords[0] > 0 && iCoords[0] < 170 && iCoords[1] > 0 && iCoords[1] < 50){
            liKS = GetAsyncKeyState(VK_LBUTTON);
           // Print(iCoords[0]," ",iCoords[1]," ",hWin," ",liKS);
            if (liKS != 0){
               Print("right away!");
            }
         }
         //mouse inactive
         if (iCoordsPrevious[0] == iCoords[0] && iCoordsPrevious[1] == iCoords[1]){ 
            if(ldPollTime < 10){
               ldPollTime+= 0.01;
            }
         } else {
            ldPollTime = 0;
         }
         liCompareTime = liTC+MathRound(ldPollTime);
         iCoordsPrevious[0] = iCoords[0];
         iCoordsPrevious[1] = iCoords[1];
      } else {
         Sleep(90);
      }
      Sleep(10);     
   }
}

Heads-up:

Alternative:   HML Rainbow - indicator for MetaTrader 4
  • WinUser32 had some adjustments.
  • eventPoller() is by no means developed; it and example, it works for me
  • polling is frequention is lowered if the mouse is inactive ( can take up to 10 secs) before it comes responsive
  • c++ source + dll avaible in zip ( cfunctions.dll -> libraries )
📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks