How to use OnChartEvent buttons to work in Tester?

Hi All,

I am creating a a simple manual trader with buy and sell buttons but cannot get the buttons to work in the tester. I’m sure there is a secret to this. Anyone that can help?

1 Like

For simple control you can read the OBJPROP_STATE via ObjectGetInteger() function).

#include<Controls/Button.mqh>

CButton buybutton;
CButton sellbutton;

extern int        magic          = 1234;            //Magic Number
extern double     Lots           = 0.1;             //Lot Size
extern double     Take_Profit    = 15;              //Take Profit
extern double     Stop_Loss      = 4.5;             //Stop Loss
extern int        BuyX           = 0;               //Buy Button Height Start
extern int        BuyY           = 500;             //Buy Button Width Start
extern int        BuyX1          = 0;               //Buy Button Height Finish
extern int        BuyY1          = 590;             //Buy Button Width Finish
extern int        SellX           = 0;              //Sell Button Height Start
extern int        SellY           = 600;            //Sell Button Width Start
extern int        SellX1          = 0;              //Sell Button Height Finish
extern int        SellY1          = 690;            //Sell Button Width Finish

double ticket1, ticket2;

   int P = 1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits == 5 || Digits == 3 || Digits == 1)P = 10;else P = 1; // To account for 5 digit brokers


   
   buybutton.Create(0,"buybutton",BuyX,BuyY,BuyX1,BuyY1,25);
   buybutton.Text("Buy");
   buybutton.FontSize(12);
   buybutton.Color(clrWhite);
   buybutton.ColorBackground(clrGreen);
   
   sellbutton.Create(0,"sellbutton",SellX,SellY,SellX1,SellY1,25);
   sellbutton.Text("Sell");
   sellbutton.FontSize(12);
   sellbutton.Color(clrWhite);
   sellbutton.ColorBackground(clrRed);
   

   return(0);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void start()
   {
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(Ask>OrderOpenPrice())
                     if(OrderStopLoss()<Bid-(Stop_Loss*Point*P))
                        ticket1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
                        
                          
                        
          
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
                  if(Bid<OrderOpenPrice())
                     if(OrderStopLoss()>Ask+(Stop_Loss*Point*P))   
                        ticket2=OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
   }    
   }             
            

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="buybutton")
   {
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid - (Stop_Loss*Point*P),Bid + (Take_Profit*Point*P),NULL,magic,0,clrBlue)==-1)
         Print("did not place buy order");
      buybutton.Pressed(false);
   }
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="sellbutton")
   {
      if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+(Stop_Loss*Point*P),Ask-(Take_Profit*Point*P),NULL,magic,0,clrRed)==-1)
         Print("did not place buy order");
      buybutton.Pressed(false);
   }   
  }

Sorry, Maybe I was not clear. I wrote a simple manual EA which works as expected in live trading but the buttons do not work in the Tester (MT4). Can anyone help?

Thanks.

Buttons do not work in the tester unless you use a work around.

You can check the state of the button by calling ObjectGetInteger() and property OBJPROP_STATE.

3x.

And if you have any trouble implementing that feel free to post a job here: https://www.mql5.com/en/job

Actually he was right. Since OnChartEvent doesn’t work in tester, you can only get button’s state through ObjectGetInteger and work with it.

Here is the fix if anyone who wants to know.

Sections highlighted in yellow is the workaround to get the buttons to work in the strategy tester.

Enjoy

void OnTick()
   {
//---
   if( IsVisualMode() )
   {
      long   lparam = 0;
      double dparam = 0.0;
      string sparam = "";
//---
      sparam = "buybutton";
      if( bool( ObjectGetInteger( 0, sparam, OBJPROP_STATE ) ) )
        OnChartEvent( CHARTEVENT_OBJECT_CLICK, lparam, dparam, sparam );
//---
      sparam = "sellbutton";
      if( bool( ObjectGetInteger( 0, sparam, OBJPROP_STATE ) ) )
        OnChartEvent( CHARTEVENT_OBJECT_CLICK, lparam, dparam, sparam );
   }
//---
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(Ask>OrderOpenPrice())
                     if(OrderStopLoss()<Bid-(Stop_Loss*Point*P))
                        ticket1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
                        
                          
                        
          
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
                  if(Bid<OrderOpenPrice())
                     if(OrderStopLoss()>Ask+(Stop_Loss*Point*P))   
                        ticket2=OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
   }    
   }             
            

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="buybutton")
   {
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid - (Stop_Loss*Point*P),Bid + (Take_Profit*Point*P),NULL,magic,0,clrBlue)==-1)
         Print("did not place buy order");
      buybutton.Pressed(false);
   }
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="sellbutton")
   {
      if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+(Stop_Loss*Point*P),Ask-(Take_Profit*Point*P),NULL,magic,0,clrRed)==-1)
         Print("did not place buy order");
      buybutton.Pressed(false);
   }   
  }
1 Like

Thanks @lbrodbinw just what I’m looking for.

Just a little bug fix:

if(id==CHARTEVENT_OBJECT_CLICK && sparam=="sellbutton")
   {
      if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+(Stop_Loss*Point*P),Ask-(Take_Profit*Point*P),NULL,magic,0,clrRed)==-1)
         Print("did not place buy order");
      sellbutton.Pressed(false);//must call sellbutton.Pressed instead of buybutton.Pressed
   } 
1 Like

I can’t get status of CButton any way. Because CButton that are added to CappDialiog never get mouse action like click to handle it status.
Am I right?