A very simple drawdown question

 

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.

 
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?
      
 
GumRai:
I don't see how that can compile

Try this.

if( ( accountbalance() - accountequity() ) / accountbalance() >= DDpercent / 100) { CloseAll(); }
 
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. 

 
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.
 
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!
 
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(); }
Reason: