Skip to content

Types of Chart Events

Types of Chart Events

There are 9 types of events that can be processed using the predefined function OnChartEvent(). For custom events 65535 identifiers are provided in the range of CHARTEVENT_CUSTOM to CHARTEVENT_CUSTOM_LAST inclusive. To generate a custom event, the EventChartCustom() function should be used.

ENUM_CHART_EVENT

IDDescription
CHARTEVENT_KEYDOWNKeystrokes
CHARTEVENT_MOUSE_MOVEMouse move, mouse clicks (if CHART_EVENT_MOUSE_MOVE=true is set for the chart)
CHARTEVENT_OBJECT_CREATEGraphical object created (if CHART_EVENT_OBJECT_CREATE=true is set for the chart)
CHARTEVENT_OBJECT_CHANGEGraphical object property changed via the properties dialog
CHARTEVENT_OBJECT_DELETEGraphical object deleted (if CHART_EVENT_OBJECT_DELETE=true is set for the chart)
CHARTEVENT_CLICKClicking on a chart
CHARTEVENT_OBJECT_CLICKClicking on a graphical object
CHARTEVENT_OBJECT_DRAGDrag and drop of a graphical object
CHARTEVENT_OBJECT_ENDEDITEnd of text editing in the graphical object Edit
CHARTEVENT_CHART_CHANGEChange of the chart size or modification of chart properties through the Properties dialog
CHARTEVENT_CUSTOMInitial number of an event from a range of custom events
CHARTEVENT_CUSTOM_LASTThe final number of an event from a range of custom events

For each type of event, the input parameters of the OnChartEvent() function have definite values that are required for the processing of this event. The events and values passed through this parameters are listed in the below table.

EventValue of the id parameterValue of the lparam parameterValue of the dparam parameterValue of the sparam parameter
Event of a keystrokeCHARTEVENT_KEYDOWNcode of a pressed keyRepeat count (the number of times the keystroke is repeated as a result of the user holding down the key)The string value of a bit mask describing the status of keyboard buttons
Mouse events (if CHART_EVENT_MOUSE_MOVE=true is set for the chart)CHARTEVENT_MOUSE_MOVEthe X coordinatethe Y coordinateThe string value of a bit mask describing the status of mouse buttons
event of graphical object creation (if CHART_EVENT_OBJECT_CREATE=true is set for the chart)CHARTEVENT_OBJECT_CREATEName of the created graphical object
Event of change of an object property via the properties dialogCHARTEVENT_OBJECT_CHANGEName of the modified graphical object
Event of graphical object deletion (if CHART_EVENT_OBJECT_DELETE=true is set for the chart)CHARTEVENT_OBJECT_DELETEName of the deleted graphical object
Event of a mouse click on the chartCHARTEVENT_CLICKthe X coordinatethe Y coordinate
Event of a mouse click in a graphical object belonging to the chartCHARTEVENT_OBJECT_CLICKthe X coordinatethe Y coordinateName of the graphical object, on which the event occurred
Event of a graphical object dragging using the mouseCHARTEVENT_OBJECT_DRAGName of the moved graphical object
Event of the finished text editing in the entry box of the LabelEdit graphical objectCHARTEVENT_OBJECT_ENDEDITName of the LabelEdit graphical object, in which text editing has completed
Event of change of the chart size or modification of chart properties through the Properties dialogCHARTEVENT_CHART_CHANGE
ID of the user event under the N numberCHARTEVENT_CUSTOM+NValue set by the EventChartCustom() functionValue set by the EventChartCustom() functionValue set by the EventChartCustom() function

Example:

#define KEY_NUMPAD_5       12
#define KEY_LEFT           37
#define KEY_UP             38
#define KEY_RIGHT          39
#define KEY_DOWN           40
#define KEY_NUMLOCK_DOWN   98
#define KEY_NUMLOCK_LEFT  100
#define KEY_NUMLOCK_5     101
#define KEY_NUMLOCK_RIGHT 102
#define KEY_NUMLOCK_UP    104
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print("The Expert Advisor with name ",MQLInfoString(MQL_PROGRAM_NAME)," is running");
//--- enable object create events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
//--- the left mouse button has been pressed on the chart
   if(id==CHARTEVENT_CLICK)
     {
      Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
     }
//--- the mouse has been clicked on the graphic object
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print("The mouse has been clicked on the object with name '"+sparam+"'");
     }
//--- the key has been pressed
   if(id==CHARTEVENT_KEYDOWN)
     {
      switch(int(lparam))
        {
         case KEY_NUMLOCK_LEFT:  Print("The KEY_NUMLOCK_LEFT has been pressed");   break;
         case KEY_LEFT:          Print("The KEY_LEFT has been pressed");           break;
         case KEY_NUMLOCK_UP:    Print("The KEY_NUMLOCK_UP has been pressed");     break;
         case KEY_UP:            Print("The KEY_UP has been pressed");             break;
         case KEY_NUMLOCK_RIGHT: Print("The KEY_NUMLOCK_RIGHT has been pressed");  break;
         case KEY_RIGHT:         Print("The KEY_RIGHT has been pressed");          break;
         case KEY_NUMLOCK_DOWN:  Print("The KEY_NUMLOCK_DOWN has been pressed");   break;
         case KEY_DOWN:          Print("The KEY_DOWN has been pressed");           break;
         case KEY_NUMPAD_5:      Print("The KEY_NUMPAD_5 has been pressed");       break;
         case KEY_NUMLOCK_5:     Print("The KEY_NUMLOCK_5 has been pressed");      break;
         default:                Print("Some not listed key has been pressed");
        }
      ChartRedraw();
     }
//--- the object has been deleted
   if(id==CHARTEVENT_OBJECT_DELETE)
     {
      Print("The object with name ",sparam," has been deleted");
     }
//--- the object has been created
   if(id==CHARTEVENT_OBJECT_CREATE)
     {
      Print("The object with name ",sparam," has been created");
     }
//--- the object has been moved or its anchor point coordinates has been changed
   if(id==CHARTEVENT_OBJECT_DRAG)
     {
      Print("The anchor point coordinates of the object with name ",sparam," has been changed");
     }
//--- the text in the Edit of object has been changed
   if(id==CHARTEVENT_OBJECT_ENDEDIT)
     {
      Print("The text in the Edit field of the object with name ",sparam," has been changed");
     }
  }

For CHARTEVENT_MOUSE_MOVE event the sparam string parameter contains information about state of the keyboard and mouse buttons:

BitDescription
1State of the left mouse button
2State of the right mouse button
3State of the SHIFT button
4State of the CTRL button
5State of the middle mouse button
6State of the first extra mouse button
7State of the second extra mouse button

Example:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- enable CHART_EVENT_MOUSE_MOVE messages
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,1);
  }
//+------------------------------------------------------------------+
//| MouseState                                                       |
//+------------------------------------------------------------------+
string MouseState(uint state)
  {
   string res;
   res+="\nML: "   +(((state& 1)== 1)?"DN":"UP");   // mouse left
   res+="\nMR: "   +(((state& 2)== 2)?"DN":"UP");   // mouse right
   res+="\nMM: "   +(((state&16)==16)?"DN":"UP");   // mouse middle
   res+="\nMX: "   +(((state&32)==32)?"DN":"UP");   // mouse first X key
   res+="\nMY: "   +(((state&64)==64)?"DN":"UP");   // mouse second X key
   res+="\nSHIFT: "+(((state& 4)== 4)?"DN":"UP");   // shift key
   res+="\nCTRL: " +(((state& 8)== 8)?"DN":"UP");   // control key
   return(res);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
      Comment("POINT: ",(int)lparam,",",(int)dparam,"\n",MouseState((uint)sparam));
  }

See also

Event Handling Functions, Working with events

Last updated on