Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Created May 1, 2013 10:04
Show Gist options
  • Save currencysecrets/5494562 to your computer and use it in GitHub Desktop.
Save currencysecrets/5494562 to your computer and use it in GitHub Desktop.
This script slightly modifies the _IsTradeAllowed function provided by http://articles.mql4.com/141
//* ////////////////////////////////////////////////////////////////////////////////
// int _IsTradeAllowed( int MaxWaiting_sec = 30 )
//
// the function checks the trade context status. Return codes:
// 1 - trade context is free, trade allowed
// 0 - trade context was busy, but became free. Trade is allowed only after
// the market info has been refreshed.
// -1 - trade context is busy, waiting interrupted by the user (expert was removed from
// the chart, terminal was shut down, the chart period and/or symbol was changed, etc.)
// -2 - trade context is busy, the waiting limit is reached (MaxWaiting_sec).
// Possibly, the expert is not allowed to trade (checkbox "Allow live trading"
// in the expert settings).
//
// MaxWaiting_sec - time (in seconds) within which the function will wait
// until the trade context is free (if it is busy). By default,30.
///////////////////////////////////////////////////////////////////////////////*/
int _IsTradeAllowed(int MaxWaiting_sec = 300) {
// check for whether it's the "end of the forex day"
// Pepperstone will not allow us to trade between 17:00 - 17:05 NY EST
if ( Hour() == 0 ) {
while( Minute() < 5 ) {
Sleep( 1000 ); // sleep for one second
}
}
// check whether the trade context is free
if(!IsTradeAllowed()) {
int StartWaitingTime = GetTickCount();
// infinite loop
while(true) {
// if the expert was terminated by the user, stop operation
if(IsStopped()) {
return(-1);
}
// if the waiting time exceeds the time specified in the
// MaxWaiting_sec variable, stop operation, as well
if(GetTickCount() - StartWaitingTime > MaxWaiting_sec * 1000) {
return(-2);
}
// if the trade context has become free,
if(IsTradeAllowed()) {
return(0);
}
// if no loop breaking condition has been met, "wait" for 0.1
// second and then restart checking
Sleep(1000);
}
} else {
return(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment