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?

 
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.

 
Marco vd Heijden:
For simple control you can read the OBJPROP_STATE via ObjectGetInteger() function.

This (again)

 
Marco vd Heijden:

This (again)

Dude, obviously your explanation was not helpful and left me looking at a less than detailed explanation. 

Obviously I posted a second time because what was posted so far was not helpful. 

Thanks for trying to help but best to leave this topic alone if you can't do anything more than what you posted. 
 

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
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
Once a strategy, and currencies are set, the EA will start looking to open new positions based on the config of each strategy and will Open a new position when all/or the conditions are meet, which is. - Tip to understand how it will work: You have 4 containers in which you can add points to one is open sell the other is open buy. The others...
 
Actually the dude was right. Since OnChartEvent doesn't work in tester, you can only get button's state through ObjectGetInteger and work with it.
 
Gary Heardbut the buttons do not work in the Tester (MT4). 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. That's correct, the never have. See In backtest OnTimer() not performs (M. Ali) - MQL4 and MetaTrader 4 - MQL4 programming forum and work around Chart Event For MT4 Backtester (Migel) - MQL4 and MetaTrader 4 - MQL4 programming forum
 
@ Marco, thanks. A little bit of detail goes a long way.

@ William have a little tolerance. Noobs like me haven't gotten the feel for how the community works yet. 
 
Gary Heard: @ William have a little tolerance.

Tolerance of what? I gave you a correction and then answered your question. Would you rather no one answer you because of your faux pas? That would be intolerant.