Download MetaTrader 5

Coding question

To add comments, please log in or register
Share your program via Code Base. Gain access to millions of traders!
John McClark
553
John McClark 2016.01.01 20:52 

Can someone help me with a code that closes all orders when orders hit certain % of DD

 

thanks 

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.01.01 20:55  
John McClark:

Can someone help me with a code that closes all orders when orders hit certain % of DD

 

thanks 

Post the code if you want coding help.
Mohammad Soubra
25019
Mohammad Soubra 2016.01.01 21:17  
Alain Verleyen:
Post the code if you want coding help.
Or apply for a new job
John McClark
553
John McClark 2016.01.02 11:11  
Mohammad Soubra:
Or apply for a new job
I just need the if sentence...what is it?
Marco vd Heijden
Moderator
4707
Marco vd Heijden 2016.01.02 11:19  
if(AccountFreeMargin()<X)
 {

 }
 

Rodrigo da Silva Boa
7107
Rodrigo da Silva Boa 2016.01.03 02:38  
There is already some free market or code base.
Khaled Mohamed
2389
Khaled Mohamed 2016.01.04 21:27  

Hello sir,

I programmed it,

Thank you .


//------------------------ DD Close orders ------------------------------//

// Header files for the errors description:

#include <stderror.mqh>
#include <stdlib.mqh>

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 ;
}

//-------

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 ( OrderMagicNumber ( ) == M_N && OrderSymbol ( ) == Symbol ( ) )
         {
            if ( OrderType ( ) == OP_BUY )
            
               Check_Close ( OrderClose ( OrderTicket ( ) , OrderLots ( ) , Bid , 100 , clrNONE ) ) ;
               
               
            if ( OrderType ( ) == OP_SELL )
            
               Check_Close ( OrderClose ( OrderTicket ( ) , OrderLots ( ) , Ask , 100 , clrNONE ) ) ;
         }
      }
}

//-------

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 ( OrderMagicNumber ( ) == M_N && OrderSymbol ( ) == Symbol ( ) )
         
            re = re + OrderProfit ( ) ;
            
      }
      
   return re * -1 ;
}

//-------

double DD ( int M_N )
{
   return ( 100 / AccountBalance ( ) ) * Loss ( M_N ) ;
}

//-------

bool Close_All_V ;

void OnInit ( )
{
   Close_All_V = false ;
}

//-------

void DD_close ( int DD , int Magic_Number )
{
        // DD:           Here it is the DD persantge, 100 means never close any order .
   
       // Magic_Number:  Your EA magic number .


   if ( DD ( Magic_Number ) >= DD )
   
      Close_All_V = true ;
      
   
   
   if ( Close_All_V )
   
      Close_All ( Magic_Number ) ;
}

// To use this option, just you need to call the function : DD_close

//------------------------ DD Close orders end ------------------------------//
To add comments, please log in or register