Making a pause between transactions


for example, forex

In the client terminal MetaTrader 4, in contrast to MetaTrader 3, there is no set limit for a pause between trading operations, performed by expert advisors. That’s why theoretically it is possible to perform tens of transactions per second. But it may lead to locking of Your trading account.

It will happen either because the server will think that such a number of requests per second only a malefactor whose aim is to disable the trading server can perform . Or for the reason that by hundreds of requests per second You will make angry a dealer who has to process Your requests.

It is considered a rule of good form not to perform trading operations more often than one operation in 5-10 seconds. Of course in any rule there can be exceptions and sometimes You may need to perform operations with a smaller time lag, but try not to misuse this option.

Try to make a pause of minimum 5 seconds. My function WaitBeforeTransaction() will help You in this.

//+----------------------------------------------------------------------------------------+
//| The WaitBeforeTransaction function make a pause of  Secs seconds between trading operations of the expert advisor (5 seconds on default)
//|                                                                                
//| Returns:                                                            
//|   1 – if pause is made without errors         
//|   0 – if the expert advisor is stopped            
//|  -1 – if an error occurs   
//+----------------------------------------------------------------------------------------+
int WaitBeforeTransaction(int Secs = 5)
  {
    // if it is testing mode it isn’t necessary to wait
    if (IsTesting()) return(1);
// if a global variable LastTradeTime doesn’t exist, // create it if (!GlobalVariableCheck("LastTradeTime")) { // if an error occurred at calling the function // GlobalVariableCheck(), we’ll exit with an error if (GetLastError()!=0) { Print("WaitBeforeTransaction(): error ",GetLastError(), " at checking a global variable LastTradeTime"); return(-1); }
// if a global variable doesn’t exist we’ll create it if (GlobalVariableSet("LastTradeTime", 1)==0) { // an error occurred at creation of the global variable Print("WaitBeforeTransaction(): error",GetLastError(), " at creation of the global variable LastTradeTime"); return(-1); }
// the global variable is created successfully Print("WaitBeforeTransaction(): global variable ", "LastTradeTime is created"); }
// we’ll get the time of the last operation datetime LastTradeTime; LastTradeTime = GlobalVariableGet("LastTradeTime");
// if an error occurred (equals to zero), we exit with an error if (LastTradeTime==0) { Print("WaitBeforeTransaction(): error ",GetLastError(), " at reading the global variable LastTradeTime"); return(-1); }
// wait for Secs seconds while(true) { // if the expert advisor is stopped we’ll exit with value 0 if (IsStopped()) { Print("WaitBeforeTransaction(): expert advisor is stopped. Exit..."); return(0); }
// if less than Secs seconds passed we wait if ((LocalTime()-LastTradeTime) // as more than Secs seconds passed we try to change the value of // the global variable LastTradeTime for the current time
// we use the GlobalVariableSetOnCondition() function to reveal // the error as during the time of waiting another expert advisor managed to perform // a transaction and changed the value of the global value if (GlobalVariableSetOnCondition("LastTradeTime", LocalTime(), LastTradeTime)) { // during the time of waiting the global variable didn’t change, that’s why // we managed to set its new value // the pause is held, we’ll refresh rates and exit without an error RefreshRates(); return(1); } else { // we failed to change the value of the global variable, as another expert advisor // had performed a transaction earlier and set a new value of the variable // that’s why we’ll receive the current value of the global variable and continue to wait LastTradeTime = GlobalVariableGet("LastTradeTime");
// if an error occurs (equals to zero), we exit with an error if (LastTradeTime==0) { Print("WaitBeforeTransaction(): error ",GetLastError(), " at reading of global variable LastTradeTime"); return(-1); } } } }

Determination of the mode of testing on history with the help of the IsTesting() function

The source code of the function has enough comments and I’m sure that the logic of this function realization will become absolutely clear for You after You consider the following functions:

  • IsTesting()
  • GlobalVariableCheck()
  • GlobalVariableSet()
  • GlobalVariableGet()
  • LocalTime()

Let’s start with the IsTesting() function.

   bool IsTesting()

The IsTesting() function returns true, if the expert advisor works in the mode of testing on history data, and false — if it works on a demo or live account.

The point is that it isn’t necessary to make the expert advisor trade on a demo or a live account at once. It is much more effective at first to test the expert advisor on history data which are available in the client terminal. After that we’ll know the limits of the expert advisor’s abilities .

The main advantage of testing on history data is quickness. You don’t need to wait for months or years to test your expert advisor on real rates. You simply «run» your expert advisor on history. It takes you only several minutes. But if each time you make a 10 second pause between trading operation you will loose this advantage. That’s why in the code of our function there is the following line:

   if (IsTesting()) return(1);

It means that we exit the function if the expert advisor works in the mode of testing on history data.


Next article: "Global variables"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?