Help with chart event

 

Hi there,

I am trying to understand how the OnChartEvent() function works, but cannot figure it out;

Here is a simple code to alert the word 'Hello' when button1 is pressed: but with no success !!! Please help.

#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

int OnInit()
  {
  createbutton();
  return(0);
  }
 
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0);
  }
 
void OnChartEvent(const int ID,const long lparam,const double dparam, const string sparam)
{
   if(ID==CHARTEVENT_OBJECT_CLICK)
   {
   if(sparam=="button1")
        {
         if(ObjectGetInteger(0,sparam,OBJPROP_STATE)==true)
           {
            Alert("hello");
            ObjectSetInteger(0,sparam,OBJPROP_STATE,0);  //set button to the unpressed state
           }
         ChartRedraw();
         return;
        }
    }     

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---   
  }
//+------------------------------------------------------------------+
void createbutton()
{
   ObjectCreate      (0,"button1",OBJ_BUTTON,0,0,0);
   ObjectSetString   (0,"button1",OBJPROP_TEXT,"Button1");
   ObjectSetInteger  (0,"button1",OBJPROP_SELECTABLE,false);
   ObjectSetInteger  (0,"button1",OBJPROP_XDISTANCE,930);
   ObjectSetInteger  (0,"button1",OBJPROP_YDISTANCE,100);
   ObjectSetInteger  (0,"button1",OBJPROP_XSIZE,70);
   ObjectSetInteger  (0,"button1",OBJPROP_YSIZE,25);
   ObjectSetInteger  (0,"button1",OBJPROP_COLOR,Black);
   ObjectSetInteger  (0,"button1",OBJPROP_BGCOLOR,Aqua);
}
  

Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

You have error in call of function

void OnChartEvent(const int ID,const long lparam,const double dparam, const string sparam)

Pay attention for arguments, which passed as reference. For more information see https://www.mql5.com/en/docs/basis/function/events#onchartevent

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
 
bouchepat:

Hi there,

I am trying to understand how the OnChartEvent() function works, but cannot figure it out;

Here is a simple code to alert the word 'Hello' when button1 is pressed: but with no success !!! Please help.

void createbutton()
{
   ObjectCreate      (0,"button1",OBJ_BUTTON,0,0,0);
   ObjectSetString   (0,"button1",OBJPROP_TEXT,"Button1");
   ObjectSetInteger  (0,"button1",OBJPROP_SELECTABLE,false);

   ObjectSetInteger  (0,"button1",OBJPROP_XDISTANCE,930);
   ObjectSetInteger  (0,"button1",OBJPROP_YDISTANCE,100);

   ObjectSetInteger  (0,"button1",OBJPROP_XSIZE,70);
   ObjectSetInteger  (0,"button1",OBJPROP_YSIZE,25);
   ObjectSetInteger  (0,"button1",OBJPROP_COLOR,Black);
   ObjectSetInteger  (0,"button1",OBJPROP_BGCOLOR,Aqua);
}

My solution to your problem...

////////////////////////////////////////////////////////////////////////////////
//                                Trade expert                                //
////////////////////////////////////////////////////////////////////////////////
//****************************************************************************//
//                       Expert initialization function                       //
//****************************************************************************//
int OnInit()
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

CreateButton(0,"button1","Button 1",Black,Aqua,30,30);

//----------------------------------------------------------------------------//
return(0);
//----------------------------------------------------------------------------//
}
//****************************************************************************//
//                      Expert deinitialization function                      //
//****************************************************************************//
void OnDeinit(const int reason)
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//
ObjectsDeleteAll(0);
//----------------------------------------------------------------------------//
}
//****************************************************************************//
//                           Expert Ticks function                            //
//****************************************************************************//
void OnTick()
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//  
}
//****************************************************************************//
//                           Expert Timer function                            //
//****************************************************************************//
void OnTimer()
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
}
//****************************************************************************//
//                         Expert Book Event function                         //
//****************************************************************************//
void OnBookEvent(const string& symbol)
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
}
//****************************************************************************//
//                        Expert Chart Event function                         //
//****************************************************************************//
void OnChartEvent(const int id,         // идентификатор события  
                  const long& lparam,   // параметр события типа long
                  const double& dparam, // параметр события типа double
                  const string& sparam  // параметр события типа string
                  )
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
//                Processing events "CHARTEVENT_OBJECT_CLICK"                 //
//----------------------------------------------------------------------------//
  if(id==CHARTEVENT_OBJECT_CLICK)
  {

    if(sparam=="button1")
    {

      if(ObjectGetInteger(0,sparam,OBJPROP_STATE)==true)
      {
      Alert("hello");
      ObjectSetInteger(0,sparam,OBJPROP_STATE,0);  //set button to the unpressed state
      ChartRedraw();
      }

    }

  }
//----------------------------------------------------------------------------//
//                          Processing user events                            //
//----------------------------------------------------------------------------//
  if(id>CHARTEVENT_CUSTOM)
  //User event is received
  {
  }
//----------------------------------------------------------------------------//  
}
//****************************************************************************//
//                           Expert trade function                            //
//****************************************************************************//
void OnTrade()
{
//----------------------------------------------------------------------------//
//Work variables
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//  
}
//****************************************************************************//
//                           Expert tester function                           //
//****************************************************************************//
//Function OnTester
double OnTester()
{
//----------------------------------------------------------------------------//
//Work variables
double Result;
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
return(Result);
//----------------------------------------------------------------------------//
}
////////////////////////////////////////////////////////////////////////////////
//                               Implementation                               //
////////////////////////////////////////////////////////////////////////////////
//Function CreateButton
bool CreateButton(long Chart_ID,string Name,string Title ="",
                  color BorderColor = Black,color ButtonColor = Aqua,
                  int DistanceX = 30,int DistanceY = 30,
                  int Width = 70,int Height = 25)
{
//----------------------------------------------------------------------------//
//Work variables
bool Result;
//----------------------------------------------------------------------------//

ResetLastError();

//Create new button
Result = ObjectCreate(Chart_ID,Name,OBJ_BUTTON,0,0,0);

//Title for the new button
ObjectSetString(Chart_ID,Name,OBJPROP_TEXT,Title);
ObjectSetInteger(Chart_ID,Name,OBJPROP_SELECTABLE,false);
//Coordinates for button on chart
ObjectSetInteger(Chart_ID,Name,OBJPROP_XDISTANCE,DistanceX);
ObjectSetInteger(Chart_ID,Name,OBJPROP_YDISTANCE,DistanceY);
//Size for new buttons (Width and Height)
ObjectSetInteger(Chart_ID,Name,OBJPROP_XSIZE,Width);
ObjectSetInteger(Chart_ID,Name,OBJPROP_YSIZE,Height);
//Color for new button (BorderColor and ButtonColor)
ObjectSetInteger(Chart_ID,Name,OBJPROP_COLOR,BorderColor);
ObjectSetInteger(Chart_ID,Name,OBJPROP_BGCOLOR,ButtonColor);

  if(_LastError!=0)
  {
  Result = false;
  }
//----------------------------------------------------------------------------//
return(Result);
//----------------------------------------------------------------------------//
}
////////////////////////////////////////////////////////////////////////////////
 
ALozovoy:

You have error in call of function

Pay attention for arguments, which passed as reference. For more information see https://www.mql5.com/en/docs/basis/function/events#onchartevent



Thanks a lot.

I didn't know that this little thing '&'      void OnChartEvent(const int ID,const long& lparam,const double& dparam, const string& sparam) 

would make a difference. I have no background in programming; I leaned MQL4 on my own and succeded with working codes. Now I'm learning MQL5 and those rules or programming types I have never learned.

Anyway the way you answered brings me back to high-school..... (pointing the obvious but not saying it).  :)

 
hi

i just want to ask.. i am trying this code in testing mode.. but seems OnChartEvent is not working there.. please do you have an idea why?
thank you for any suggestions..
 
oki i found out that OnChartEvent is not implemented for Testing mode.. but there is solution to check Button state (true, false)...