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 Orders (or both Open and Pending Orders) when your drawdown against balance exceeds a set percentage limit.

E.g. Balance = $10,000 and “DrawdownPercent” is set at 2.0, if floating P&L is greater than -$200.00, all Orders will be closed.


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

extern string _1              = "Settings";
extern double DrawdownPercent = 2.0;     //e.g. for 2% 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;
   return(0);
}

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||OrderType()==OP_SELL)result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MaxSlippage*mt,Blue);
      if(!result)Print("CloseOpenOrders failed with error#",GetLastError());
   }
}

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

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.

 

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Archives

Visitors

Flag Counter
Advertisements
%d bloggers like this: