money protection

 

Hello all!

I need help for my mq4 code, I want that the ea close all trade when total trade running+closed trade reach 5%(different pairs running)

I have got a code for that but the ea close trade when closed trade reach 5%, however all running trade are in loss,so it's nothing

This is the part of the code(I put it in my ea code)

Thanks in advance for your help



extern bool RiskManagement=false; //money management

extern double RiskPercent=10; //risk in percentage

extern bool   DoNotCloseOrders=true;//OK

extern double ProfitTarget = 10;//Percent

double ProfitRunning;//Running profit

extern bool   UseAlerts        = false;//Message not prompt



int Start{

//Target Profit

double ProfitTargets=AccountBalance()*ProfitTarget/100;              

ProfitRunning=AccountProfit();

if(ProfitRunning >= ProfitTargets) CloseAll(); 

Comment("Capital: ",AccountBalance(),"\n Marge disponible: ",AccountEquity(),"\n Profits en cours: ",ProfitRunning);

}

//CloseAllFunction

void CloseAll()

{

   int i;

   bool result = false;



   while(OrdersTotal()>0)

   {

      // Close open positions first to lock in profit/loss

      for(i=OrdersTotal()-1;i>=0;i--)

      {

         if(OrderSelect(i, SELECT_BY_POS)==false) continue;



         result = false;

         if ( OrderType() == OP_BUY)  if (!DoNotCloseOrders) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 15, Red );

         if ( OrderType() == OP_SELL) if (!DoNotCloseOrders) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 15, Red );

         if (UseAlerts) PlaySound("alert.wav");

      }

      for(i=OrdersTotal()-1;i>=0;i--)

      {

         if(OrderSelect(i, SELECT_BY_POS)==false) continue;



         result = false;

         if ( OrderType()== OP_BUYSTOP)  result = OrderDelete( OrderTicket() );

         if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );

         if ( OrderType()== OP_BUYLIMIT)  result = OrderDelete( OrderTicket() );

         if ( OrderType()== OP_SELLLIMIT)  result = OrderDelete( OrderTicket() );

         if (UseAlerts) PlaySound("alert.wav");

      }

      Sleep(7200000);

   }

   

      Alert ("Account Profit Reached. All Open Trades Have Been Closed");

}
 

Please edit your post and

use the code button (Alt+S) when pasting code


topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 
tabadany19:

Hello all!

I need help for my mq4 code, I want that the ea close all trade when total trade running+closed trade reach 5%(different pairs running)

I have got a code for that but the ea close trade when closed trade reach 5%, however all running trade are in loss,so it's nothing

This is the part of the code(I put it in my ea code)

Thanks in advance for your help



Try this...

int Start{

//Target Profit

static double ProfitTargets=(AccountBalance()*ProfitTarget/100)+AccountBalance();              

ProfitRunning=AccountEquity();

if(ProfitRunning >= ProfitTargets) 
{
        CloseAll(); 
        ProfitTargets=(AccountBalance()*ProfitTarget/100)+AccountBalance();
}
Comment("Capital: ",AccountBalance(),"\n Marge disponible: ",AccountEquity(),"\n Profits en cours: ",ProfitRunning);

}

Or use the script in codebase... https://www.mql5.com/en/code/31812

Master Exit Plan
Master Exit Plan
  • www.mql5.com
Master Exit Plan is collection of simple strategies that would help traders manage on how to exit open trades and pending orders. NOTE: This EA doesn't open a trade for you. It only manages your open trades and pending orders.  Here are the list of exit strategies... Equity Target - this part of the program helps you protect your account equity...
Reason: