You're reading...
EA development, Money Management, MQL4

Drawdown Percent Close EA

This is a very simple trade management EA that will close all your Open trades (or Open AND Pending Trades) when your drawdown percentage exceeds a set limit.

E.g. Balance = $10,000, if “DrawdownPercent” is set at 20.00, if Equity falls below $8,000, all Open Orders will be closed


I wrote it because a myfxbook member “carlosforex” was asking how to use another trade management EA “CloseAllAtEquityLevel”, which did not serve his purpose, and I advised him to use Steve Hopwood’s excellent “Multi Purpose Trade Management EA”.
http://www.myfxbook.com/community/programming/closeallatequitylevel/318979,1

With him starting 2 more threads and not getting any answers,
http://www.myfxbook.com/community/programming/multi-purpose-trade-manager/324498,1
http://www.myfxbook.com/community/programming/multi-purpose-trade-manager-ea/324497,1
I took a look again at MPTM and see now that it does not have this function either. I trawled the net (see what I did there? ;)) and was surprised not to find it, hence here it is.
(It’s simple enough to plug it into MPTM but it’s not my call)


//+------------------------------------------------------------------+
//|                                       Drawdown Percent Close.mq4 |
//|                            Copyright © 2012, www.raidenworks.com |
//|                                          contact@raidenworks.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.raidenworks.com"
#property link      "contact@raidenworks.com"
#define  NL "\n"

// Rev 1.00 (31st May 2012)
//    1. GENERAL COMMENTS: This simple EA closes all Open Orders(or Open AND Pending orders) when the Drawdown Percentage against the Balance exceeds a set amount.
//                         E.g. Balance = $10,000, if DrawdownPercent = 20.0, if Equity drops below $8,000, all Open Orders will be closed.

extern string _1              = "Settings";
extern double DrawdownPercent = 20.00;     //e.g. for 20% drawdown
extern int    MaxSlippage     = 3;         //maximum pips slippage allowed (auto-adjusts for 4-digit or 5-digit brokers)
extern bool   ClosePendingToo = False;     //set to True to close Pending Orders too
extern string _2              = "Display";
extern int    TopPadding      = 0;         //to shift Display X number of lines down from topleft corner
extern int    LeftPadding     = 0;         //to shift Display X number of spaces right from topleft corner

int mt;

int init(){
   if(Digits==3||Digits==5)mt=10;
   else mt=1;
}

int start(){
   if((1-AccountEquity()/AccountBalance())*100>NormalizeDouble(DrawdownPercent,2)){
      if(ClosePendingToo){
         CloseOpenOrders();
         ClosePendingOrders();
      }
      else CloseOpenOrders();
   }
   Display();
   return(0);
}

void CloseOpenOrders(){
   bool result=false;
   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_BUY||OP_SELL)result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MaxSlippage*mt,Blue);
      if(result==false){
         Print("CloseOpenOrders failed with error#",GetLastError());
         continue;
      }
   }
}

void ClosePendingOrders(){
   bool result=false;
   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_BUYSTOP||OP_BUYLIMIT||OP_SELLSTOP||OP_SELLLIMIT)result=OrderDelete(OrderTicket());
      if(result==false){
         Print("ClosePendingOrders failed with error#",GetLastError());
         continue;
      }
   }
}

void Display(){
   string TOP="";
   for(int line=0;line<TopPadding;line++)TOP=TOP+NL;
   string LEFT="";
   for(int space=0;space<LeftPadding;space++)LEFT=LEFT+" ";
   string A1=StringConcatenate(TOP,NL);
   string A2=StringConcatenate(LEFT,"Balance: $",DoubleToStr(AccountBalance(),2),NL);
   string A3=StringConcatenate(LEFT,"Equity: $",DoubleToStr(AccountEquity(),2),NL);
   string A4=StringConcatenate(LEFT,"DD%Close: ",DoubleToStr(DrawdownPercent,2),NL);
   string A5=StringConcatenate(LEFT,"DD%Current: ",DoubleToStr((1-AccountEquity()/AccountBalance())*100,2),NL);
   string Display=StringConcatenate(A1,A2,A3,A4,A5);
   Comment(Display);
   return (0);
}

 


A simple text display is included for some visual feedback.

Basic notes: It only needs to be attached to one chart to function. Ideally attach to the same currency pair as the one you trade on. If trading on multiple pairs, attach it to the one with the most frequent ticks.

Advanced notes: I’ve played around in the past with a constant regular refresh, i.e. regardless of whether ticks come in or not, the code still runs a pass every X milliseconds; however, too low a resolution and the EA will not capture a fast market properly, too high and it sucks memory and is redundant for quiet markets. Hence, I’ve not opted for it.

Download Drawdown Percent Close EA mq4 file

Advertisements

Discussion

One thought on “Drawdown Percent Close EA

  1. Thanks for making this available, it’s very helpful!

    Posted by Ben Anderson | 2017-01-27, 6:22 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

Visitors

Flag Counter
%d bloggers like this: