Download MetaTrader 5
To add comments, please log in or register
How to launch your trading signal and become a provider
Konstantin Yankov
119
Konstantin Yankov 2014.08.15 12:42 

Hello, I was experimenting with an EA but I got stuck when I tried to make it close all orders when 20% drawdown is reached. The code is extremely simple, in fact it is randomly opening orders, I just wanted to see if I have basic coding skills.

So, up to the question - I tried to limit the maximum drawdown in a very simple way (which didn't work and I don't know why). There are the few lines that somehow I got wrong:

- there is a variable (in my case DDpercent) which I set to 20.

- the logic is simple - i just inserted IF function - if ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100 { CloseAll; } which sends the EA to the void function which has to close all open and pending orders. 

 The void function is working pretty well when it has to take profit but I think that there is something wrong with the way I do the math for balance and equity. I would be very thankful if someone is willing to help me.

Keith Watford
Moderator
7906
Keith Watford 2014.08.15 13:07  
ksofty:

- the logic is simple - i just inserted IF function - if ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100 { CloseAll; } which sends the EA to the void function which has to close all open and pending orders. 

I don't see how that can compile
 if ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100 { CloseAll; }
 if ( accountbalance() - accountequity() )
   / accountbalance() >= DDpercent / 100    //What does this do?
 CloseAll;                             //Is this supposed to be a function call?
      
Hilton Global LLC
683
Sean Hilton 2014.08.15 13:19  
GumRai:
I don't see how that can compile

Try this.

if( ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100) { CloseAll(); }
Keith Watford
Moderator
7906
Keith Watford 2014.08.15 13:39  
Hiltos:

Try this.

 

if( ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100) { CloseAll(); }

 


Yes, that will compile, but that is not the code that was posted by the OP.

I would think that everyone who posts code (that is not working as expected) would simply copy and paste from their actual code, so there should be no typos. 

Hilton Global LLC
683
Sean Hilton 2014.08.15 14:26  
GumRai:


Yes, that will compile, but that is not the code that was posted by the OP.

I would think that everyone who posts code (that is not working as expected) would simply copy and paste from their actual code, so there should be no typos. 

Yes. I was just thinking that he might have been missing some brackets by mistake. You're right, it wouldn't have compiled the way that it was.
Konstantin Yankov
119
Konstantin Yankov 2014.08.16 10:26  
It compiled the way I did it and I found out why it wasn't working - I just moved the if function in void(start) so it will check it every time. And yes, CloseAll is a function call as I wrote in the first post if you read it carefully. Thank you for your assistance!
Keith Watford
Moderator
7906
Keith Watford 2014.08.16 11:22  
ksofty:
It compiled the way I did it and I found out why it wasn't working - I just moved the if function in void(start) so it will check it every time. And yes, CloseAll is a function call as I wrote in the first post if you read it carefully. Thank you for your assistance!


There is no way that the code you wrote in your first post compiled.

Hiltos corrected your code. As follows-- 

if( ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100) { CloseAll(); }
/
To add comments, please log in or register