Coding Help Please - easy stuff

 

Hi - could anyone tell me if there is code which calculates drawdown which could be put into an EA ? Thanks :-)

 
leeb:
Hi - could anyone tell me if there is code which calculates drawdown which could be put into an EA ? Thanks :-)

What do you mean by "drawdown"? Do you mean to stop trading when you make % loss?

 

hi mqlDev - yes that is right I would like expert to take action after % loss of account balance

 

Take a look at AccountEquity() and AccountBalance()

 

You could try something like this:

bool drawdown(double maxdrawdownpercent)

{

if(AccountEquity() < AccountBalance() - (AccountBalance() * maxdrawdownpercent))

{

return(true);

}

return(false);

}
 
Nicholishen:
You could try something like this:
bool drawdown(double maxdrawdownpercent)

{

if(AccountEquity() < AccountBalance() - (AccountBalance() * maxdrawdownpercent))

{

return(true);

}

return(false);

}

Very good function!

 

dreaded multithreading

Perhaps it's too unikely to happen, but there is a small worry in picking up AccountBalance() two times, beacuse it is possible that the balance changes in between them. The point is that the balance can be affected by other execution threads than the one processing the EA. The following would be a variation to avoid the worry:

bool drawdown(double maxdrawdownpercent)

{

if ( AccountEquity < AccountBalance * ( 1 - maxdrawdownpercent ) )

{

return( true );

}

return( false );

}

Then there is a similar thread race between reading off the balance and reading off the equity, but that race cannot be avoided. At best you could define which order to read them off in, but I'm not sure the one order is any better than the other in respect of possibly return "wrong result".

 
ralph.ronnquist:
Perhaps it's too unikely to happen, but there is a small worry in picking up AccountBalance() two times, beacuse it is possible that the balance changes in between them. The point is that the balance can be affected by other execution threads than the one processing the EA. The following would be a variation to avoid the worry:
bool drawdown(double maxdrawdownpercent)

{

if ( AccountEquity < AccountBalance * ( 1 - maxdrawdownpercent ) )

{

return( true );

}

return( false );

}

Then there is a similar thread race between reading off the balance and reading off the equity, but that race cannot be avoided. At best you could define which order to read them off in, but I'm not sure the one order is any better than the other in respect of possibly return "wrong result".

Nice catch Ralph! I didn't think about the possibilities of that. You are correct in the fact that your method is a "Best Practice".

EDIT::

I had assumed that AccountBalance() was appart of the predefined variables, but apparently it is not. Thanks for the enlightenment!

However one is able to call functions in the manner that I did with out worry because every launch of an attached expert or a custom indicator is assigned its own predefined variables; "is supported that reflect the state of the current price chart at the launching of a program".

So in the instance that script A is running and script B refreshes rates, only the predifined variables are updated in script B, not effecting the status of the predefined variables for script A.

 

Thanks for your input guys

 

Will these codes work?

-max 5% drawdown.

-stop open new trade.

Thanks B.

extern double maxdrawdownpercent = 0.05;

if ( AccountBalance() - AccountEquity() > AccountBalance() * maxdrawdownpercent )

{

return(0);

}

Reason: