MQL4 - automated forex trading   /  

Articles

Articles  Features  Breakpoints in Tester: It's Possible! To post a new article, please log in or register


This article is about
MetaTrader 4
Download MT 4 - 5.5 Mb

Mobile trading!
Buy a license and be mobile in your trading!

Breakpoints in Tester: It's Possible! [ ru ]


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);
   
   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)
{
   if (!IsVisualMode() || (!Condition)) return(0);
   //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.


Translated from Russian by MetaQuotes Software Corp.
Created: 2007.08.15  Author: Christo Tsvetanov
Warning: All rights to these materials are reserved by MetaQuotes Software Corp. Copying or reprinting of these materials in whole or in part is prohibited.
MQL4 Language for Newbies. Technical Indicators and Built-In Functions
MQL4 Language for Newbies. Technical Indicators and Built-In Functions

This is the third article from the series "MQL4 Language for Newbies". Now we will learn to use built-in functions and functions for working with technical indicators. The latter ones will be essential in the future development of your own Expert Advisors and indicators. Besides we will see on a simple example, how we can trace trading signals for entering the market, for you to understand, how to use indicators correctly. And at the end of the article you will learn something new and interesting about the language itself.

Technical Analysis: Make the Impossible Possible!
Technical Analysis: Make the Impossible Possible!

The article answers the question: Why can the impossible become possible where much suggests otherwise? Technical analysis reasoning.

4 comments  To add comments, please, log in or register

Hey this worked great in correcting the speed problem of Visual Mode where the last speed 32 is way too fast and speed 31 is way too slow. But the only problem was that at speed 32 it's moving so fast that by the time the keyboard event pauses the test it may be 20 to 50 bars past the even you wanted to pause at. So I added some number crunching as a sleep, since the Sleep() function doesn't work in testing.


void pause() // pauses visual mode during testing needs #include <WinUser32.mqh>
{
if(IsVisualMode())
{
keybd_event(19,0,0,0);
keybd_event(19,0,2,0);
//----crunch some numbers as sleep
int a=199999999;
int b=1;
while(a>b)
b++;
}

}



2009.10.27 18:44 bunnychopper
Here is the Pausebreak.mg
Attached files:
  Pausebreak.mq4 (2.49 KB)
2009.08.27 11:07 lsee8868

Further to my last comment, I have created an advisor which allows for pauses during programming. (Pausebreak.mq attached) Cannot get it to work if dates go passed a weekend but no doubt someone will know how.

I have also inserted the code in an amended Vhands (vHandsTradepause.mq) but cannot get it to work (vHandsTradepause.mq4)

Would really be great to have the vHands working with breaks so we can check at certain intervals and when and do other stuff at the same time.

Attached files:
  vHandsTradepause.mq4 (73.24 KB)
2009.08.27 11:06 lsee8868
Very interesting article. Unfortunately the current strategy tester only allows one to skip forward to a new date which is limited I guess to the period seperator time. What would be helpful is the have a script that allows for stopping at programmed invervals: i.e Set keyboard event to pause at a predetermined time. Or engage pause in so many minutes. Any ideas welcome.
2009.08.23 14:12 lsee8868
4 comments