Waiting for the trade flow to disengage


for example, forex

An example of how the GlobalVariableSetOnCondition() function is used

In this article I would like to consider an example of its practical usage.

//+----------------------------------------------------------------------------------------+
//| int StartTrading()                                                               |
//|                                                                                        |
//| The function occupies the trading flow                                      |
//| The function returns:                                                         |
//|   0 – if trade is allowed                                                 |
//|   1 – of the Expert Advisor was stopped                       |
//|   2 – if expert advisors aren’t allowed to trade at the level |
//|       of the client terminal’s settings                                                |
//+----------------------------------------------------------------------------------------+  
#define GLOB_VAR_NAME "TradeIsAllowed"
int StartTrading() { // if the expert advisor works in a testing mode we’ll simply exit if (IsTesting()) return(0);
int LastError;
while (!IsStopped()) { // If expert advisors aren’t allowed to trade at the level // of the client terminal’s settings we’ll exit and return 2 if (!IsExpertEnabled()) return(2);
// we’ll check whether a global variable exists if (GlobalVariableCheck(GLOB_VAR_NAME)) break;
// in case of an error at checking of the global variable, // we report of it in logs and wait for 0.1 second LastError = GetLastError(); if (LastError!=0) { Print("StartTrading(): error ", LastError, " at checking if a global variable exists ", GLOB_VAR_NAME); Sleep(100); continue; }
// global variable doesn’t exist – we’ll create it if (GlobalVariableSet(GLOB_VAR_NAME, 0)>0) break;
// an error occurred at creating a variable – we’ll record in the log LastError = GetLastError(); Print("StartTrading(): error ", LastError, " at creating a global variable ", GLOB_VAR_NAME); Sleep(100); }
// a global variable exists or the expert advisor was stopped
// We check in the loop how the situation has changed while (!IsStopped()) { // If expert advisors aren’t allowed to trade at the level // of the client terminal’s settings we’ll exit and return 2 if (!IsExpertEnabled()) return(2);
// If we managed to change the value of the global variable, we’ll refresh the data // of the current rates and return 0 if (GlobalVariableSetOnCondition(GLOB_VAR_NAME, 1, 0)) { 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); }
//+----------------------------------------------------------------------------------------+ //| int StopTrading() | //| | //| The function allows the following expert to trade | //| The function returns: | //| 0 – if another expert is allowed to trade | //| 1 – if the expert advisor was stopped | //| 2 – if expert advisors aren’t allowed to trade at the level| //| of the client terminal’s settings | //+----------------------------------------------------------------------------------------+ #define GLOB_VAR_NAME "TradeIsAllowed"
int StopTrading() { // if the expert advisor works in a testing mode we’ll simply exit if (IsTesting()) return(0);
int LastError;
while (!IsStopped()) { // If expert advisors aren’t allowed to trade at the level // of the client terminal’s settings we’ll exit and return 2 if (!IsExpertEnabled()) return(2);
// If we managed to change the value of the global variable, we’ll refresh the data // of the current rates and return 0 if (GlobalVariableSet(GLOB_VAR_NAME, 0)>0) { return(0); }
// an error occurred at resetting the value of the global variable LastError = GetLastError(); Print("StopTrading(): error ", LastError, " at resetting the global variable value ", GLOB_VAR_NAME);
// we’ll 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); }

Next article: "Critical section: prevention of concurrent access to a shared resource"

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