Breakpoints in Tester: It's Possible!

Christo Tsvetanov | 15 August, 2007

Introduction

The only thing I miss in MQL4 is a normal debugger for Expert Advisors. All human beings have human feelings, so we make mistakes. At normal programming, we set breakpoints, launch the program, and then, when execution reaches a breakpoint, it will be stopped. So we can have a look into the contents of variables exercising us.

Displaying of debugging data is now possible due to such functions as Print, Comment, etc. But we sometimes may want to stop the program temporarily in certain places at a certain moment in order to review the situation. There are some finer points here. The program is usually launched to trade on a demo account or on a real account. This means that we will only be able to see the results in several months… Thus, the debug mode is reasonable only in the test mode of Expert Advisors (in Tester).


How It Works

Since a "Visual Test Mode" appeared in the Tester, it has become possible to track our EA's responses during quick passing of the program in Tester. If we want to stop execution temporarily, we can press "Pause" on the keyboard or click with the mouse button on the button of the same name in Tester toolbar. The developers of the terminal provide a library named WinUser32.mqh that contains some very interesting functions. One of them is keybd_event. It allows us to press any keys we want.

The idea was tossed up then – we can write a function that would press pause programmatically and print the necessary debugging information. Since our Expert Advisor will use DLL, we should first enable it for the EA. Press Ctrl+O and select/deselect check boxes:

Then we have to declare the using of WinUser32 somewhere at the beginning of the code:

#include <WinUser32.mqh>

This action is followed by declaration of function BreakPoint itself. There are some finer points here, but the simplest realization assumes no passed/returned parameters:

void BreakPoint()

The function must trigger only in the visual testing mode, so we will insert a check: If the Tester is not in the visual testing mode, we leave it:

if (!IsVisualMode()) return(0);

Then we will visualize some data. To my opinion, the most descriptive would be to use Comment(). Suppose we need only Bid and Ask.

string Comm="";
Comm=Comm+"Bid="+Bid+"\n";
Comm=Comm+"Ask="+Ask+"\n";
   
Comment(Comm);

The “\n” here means that data following after will be shown in the next string. Finally, let's press Pause.

keybd_event(19,0,0,0);
Sleep(10);
keybd_event(19,0,2,0);

The first string presses the key whereas the last one releases it. The Sleep button is necessary, too, since a too quick pressing/release may remain unprocessed. 19 is a virtual code of pause, 2 in the last string shows that release must be emulated.

All we have to do now is to substitute the break point in the Expert Advisor's code, say immediately after the long position the example from article Expert Advisor Sample has been opened.

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
BreakPoint();

Below is the entire code to be inserted:

//We will use a function, described in header file
#include 
 
//Breakpoint neither receive nor send back any parameters
void BreakPoint()
{
   //It is expecting, that this function should work
   //only in tester
   if (!IsVisualMode()) return(0);
   
   //Preparing a data for printing
   //Comment() function is used as 
   //it give quite clear visualisation
   string Comm="";
   Comm=Comm+"Bid="+Bid+"\n";
   Comm=Comm+"Ask="+Ask+"\n";
   
   Comment(Comm);
   
   //Press/release Pause button
   //19 is a Virtual Key code of "Pause" button
   //Sleep() is needed, because of the probability
   //to misprocess too quick pressing/releasing
   //of the button
   keybd_event(19,0,0,0);
   Sleep(10);
   keybd_event(19,0,2,0);
}


What Shall We Do to Watch Local Variables?

The problem is in "invisibility" of such variables outside their declarations. In this case, the data should be passed. Suppose we want to watch variable MacdCurrent from the same article. For this, we will modify the function as follows:

void BreakPoint(double MacdCurrent)
{
   if (!IsVisualMode()) return(0);
nbsp;  
   Comment("MacdCurrent = ",MacdCurrent);


Optional Break Points

We sometimes may wish that the program does not always stop when it reaches a specific string, but only when some additional requirements are met. This usually happens in loops. For example, we want to break execution when the counter has reached a certain predefined value. For this, we have to pass an additional requirement:

void BreakPoint(double MacdCurrent, bool Condition)
{
nbsp;  if (!IsVisualMode() || (!Condition)) return(0);
nbsp;  //Or - which is the same:
   //if (!(IsVisualMode()&&Condition)) return(0);
   
   Comment("MacdCurrent = ",MacdCurrent);

We will call it as follows:

for(cnt=0;cnt<total;cnt++)     
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      BreakPoint(MacdCurrent, cnt==1);


Conclusion

So, why don't we just create a library to connect to the program and then use? The matter is that there are many variations, for which it would be better to modify function BreakPoint. Indeed, all written above is just a general idea. It can be implemented in many different ways.

Concluding, I would like to acknowledge Klot who was the first to guess how to realize pause pressing programmatically.