Secrets of proper trade


for example, forex

I will tell you about three functions: IsExpertEnabled(), IsTradeContextBusy() и IsTradeAllowed().

The IsExpertEnabled() function

   bool IsExpertEnabled()

The IsExpertEnabled() function returns true, if expert advisors are allowed to perform transactions and manage orders. The point is that at the level of client terminal settings it is possible to prohibit ALL expert advisors to trade. You can do it for example with the help of the main menu command: Service -> Settings. In the appeared window we select the tab «Expert Advisors», where we put or remove a mark opposite «Enable expert advisors …».

A simpler way to switch on/off expert advisors is a button.

If at the level of the terminal’s settings expert advisors aren’t allowed to work the IsExpertEnabled() function will return false.

The IsTradeContextBusy() function

At each moment of time only one expert advisor can trade. If at this moment another expert advisor tries to trade the trading function will return the following error ERR_TRADE_CONTEXT_BUSY (146).

   bool IsTradeContextBusy()

The IsTradeContextBusy() function will allow you to reveal beforehand a situation when another expert advisor is already trying to trade (in this case it will return the value true), and to avoid an ineffective call of the trading function (which anyway will complete with an error). Usually in such cases it is better to wait a little bit until the moment when the other expert terminates trading (then the IsTradeContextBusy() function will return the value false) and only after that to call Your trading function.

The IsTradeAllowed() function

The IsTradeAllowed() function combines the both previous functions. It will return true only in case if expert advisors are allowed to trade and trade context is available for trading, i.e. no other expert advisor is performing trading operations at this moment.

   bool IsTradeAllowed()

As it was mentioned at each moment of time only one expert advisor can trade. If at this moment another expert advisor tries to perform a trading operation the trading function will return the following error ERR_TRADE_CONTEXT_BUSY (146).

So what to do if several expert advisors are trading at the same time?

We’ll write a function which returns:

  • 0, if trade context is available;
  • 1, if the work of an expert advisor was stopped (the reasons are given below);
  • 2, if expert advisors are not allowed to trade at the level of settings of the client terminal.

If at the moment of calling the function the trade context is busy the function waits until it becomes available. After that it updates the data of the current quotes with the help of the RefreshRates() function and returns 0.

The possible reasons why the work of an expert advisor is stopped:

  • MetaTrader 4 terminates working,
  • the chart is closed,
  • expert advisor is deleted from the chart,
  • the instrument or the period of the chart are changed,
  • a successful recompilation of the program in MetaEditor,
  • modification of parameters of the expert advisor,
  • switching over to another account.

The source code of the function which resolves the set task:

   //+-------------------------------------------------------------------+
   //| The WaitUntilTradingIsAllowed function returns:|
   //|   0 – if trade context is available                |
   //|   1 – if the work of the expert advisor is stopped |
   //|   2 – if expert advisora are not allowed to trade at the level|
   //|        of settings of the client terminal  |
   //+-------------------------------------------------------------------+  
   int WaitUntilTradingIsAllowed()
     {
       // If trade is allowed we’ll return 0 and exit
       if (IsTradeAllowed()) return(0);
// In the loop we check how the situation has changed while (!IsStopped()) { // If trade isn’t allowed at the level of settings // of the client terminal we’ll exit and return 2 if (!IsExpertEnabled()) return(2);
// If trade context has become available we’ll refresh the data // of the current rates and return 0 if (!IsTradeContextBusy()) { RefreshRates(); return(0); }
// If we have reached the moment, the situation hasn’t changed // That’s why we make a 0.1 second pause Sleep(100); }
// As we exited the loop the work of the expert advisor was stopped // We’ll return 1 return(1); }

The IsStopped() function.

   bool IsStopped()

The function returns true, if the expert advisor was «stopped», and it returns false — в противном случае.

An expert advisor gets a command to stop in the following cases:

  • client terminal terminates working,
  • the chart to which the expert advisor is attached is closed,
  • the expert advisor is deleted from the chart,
  • the instrument or the period of the chart are changed,
  • the expert advisor is recompiled in MetaEditor,
  • the parameters of the expert advisor are modified,
  • at switching over to another trading account.

In order not to try to perform a transaction at old quotes and not to receive a message of an error we refresh the information of the current rates with the help of the RefreshRates() functio.

   bool RefreshRates()

The function returns true, the information has been refreshed and it returns false, if data haven’t changed and there is no need in refreshment. If the data have been refreshed the call of the RefreshRates() function will also refresh the time of the last quote which is returned by the CurTime() function.

It is considered a good form to call the RefreshRates() function before calling a trading function if the expert advisor was recalculating something before that. Or if the expert advisor was waiting for another expert advisor to finish trading and the trade context to become available.

As You most probably will use several expert advisors at the same time or one and the same expert advisor but at different time periods and instruments Your expert advisors will wait from time to time for the trade context to become available and You will use the RefreshRates() function practically always.


Next article: "Making a pause between transactions"

+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?