Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2016.01.13 10:24
Experts

Close all orders once the drawdown reaches a specific percentage - expert for MetaTrader 4

| English Español Português 日本語 Deutsch Русский

Views:
5599
Rating:
votes: 16
\MQL4\Include\\MQL4\Experts\

This code has some functions to close all opened orders once the drawdown reaches a specific percentage of the account balance.

You will just enter the magic number of the orders (enter 0 for control all orders), and the maximum percentage of drawdown that will allow the code logic to close the order once it is reached.

Only call the function DD_close to use this code.

Attached is the header file of this code with the EA that is built on this code.

// To use this option, just you need to call the function : ( DD_close ) 
//+------------------------------------------------------------------+
//|                          Global scope                            |
//+------------------------------------------------------------------+
bool Close_All_V;
//+------------------------------------------------------------------+
//|                         Main function                            |
//+------------------------------------------------------------------+
// DD:               Here it is the DD percentage, 100 means never close any order .
// Magic_Number:     Your EA magic number, enter 0 to control the all orders .
void DD_close(int DD,int Magic_Number)
  {
   if(DD(Magic_Number)>=DD)
      Close_All_V=true;
   if(Close_All_V)
      Close_All(Magic_Number);
  }
//+------------------------------------------------------------------+
//|                          Check close                             |
//+------------------------------------------------------------------+
void Check_Close(int Check_Number) // check close order
  {
   if(Check_Number<0) Print("OrderClose failed with error: ",ErrorDescription(GetLastError()));
   else Close_All_V=false;
  }
//+------------------------------------------------------------------+
//|                          Close all                               |
//+------------------------------------------------------------------+
void Close_All(int M_N)
  {
   int Loop=0;
   for(int i=0; Loop<OrdersTotal(); i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         Loop++;
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==M_N || OrderMagicNumber()==0)
              {
               if(OrderType()==OP_BUY)
                  Check_Close(OrderClose(OrderTicket(),OrderLots(),Bid,100,clrNONE));
               if(OrderType()==OP_SELL)
                  Check_Close(OrderClose(OrderTicket(),OrderLots(),Ask,100,clrNONE));
              }
        }
  }
//+------------------------------------------------------------------+
//|                          Calculate loss                          |
//+------------------------------------------------------------------+
double Loss(int M_N)
  {
   double re=0;
   int Loop=0;
   for(int i=0; Loop<OrdersTotal(); i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         Loop++;
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==M_N || OrderMagicNumber()==0)
               re=re+OrderProfit();
        }
   return re * -1;
  }
//+------------------------------------------------------------------+
//|                  Calculate drawdown percentage                   |
//+------------------------------------------------------------------+
double DD(int M_N)
  {
   return ( 100 / AccountBalance ( ) ) * Loss ( M_N );
  }
//+------------------------------------------------------------------+
SBVolumeAvg SBVolumeAvg

SBVolumeAvg is a Forex Indicator (MetaTrader 4) for calculating Average Volume on the Swing Bars by a ZigZag trend line that is bounded by two vertical lines.

Plot MQL signal Plot MQL signal

This script plots MQL signal history on a MetaTrader 4 chart.

Pivot Only Pivot Only

This is a simple indicator showing pivot only for H1, H4, daily, weekly and monthly.

PricePercentRange PricePercentRange

Price(%)Range is the indicator for the MetaTrader 4, which calculates the price movement based on percentage High (Highest) and Low (Lowest) price on 100 bars.