How to close open positions depending on the value of equity ???

 

Hi every one.

I need to close a group of open positions automatically when the the value of equity reaches a certain limit.

Is this feature exist in metatrader 5 ? and if not. Is this feature can be generated by a certain code ?

Please help. thank you.

 
So you are talking about drawdown control I guess. Closing positions when equity drops past a threshold is controlling the drawdown. I made a function for that which works fine, but I'm not sure if you mean when the equity reaches a certain positive limit (profit) or negative limit (loss).
 
Conor Mcnamara #:
So you are talking about drawdown control I guess. Closing positions when equity drops past a threshold is controlling the drawdown. I made a function for that which works fine, but I'm not sure if you mean when the equity reaches a certain positive limit (profit) or negative limit (loss).

I mean a positive limit (profit).

I can control the loss by using a stop loss function for each order individually. I mean closing group of orders some of them with profit and the others with loss to achieve a net profit which can be determined by controlling the value of the equity. 

 
asser hussien #:

I mean a positive limit (profit).

I can control the loss by using a stop loss function for each order individually. I mean closing group of orders some of them with profit and the others with loss to achieve a net profit which can be determined by controlling the value of the equity. 

so when your account reaches a certain equity it should close all positions?  

#include <Trade\Trade.mqh> 

input bool save_the_funds = true;  // today we are saving the funds and not blowing the account
input double target = 100;

double current_equity;
double balance;

CTrade trade;


void OnTick()
  {

   balance = AccountInfoDouble(ACCOUNT_BALANCE);
   current_equity = AccountInfoDouble(ACCOUNT_EQUITY);

   bool closePositions = false;

   if(save_the_funds)
     {
      if(current_equity >= (balance + target))
        {
         closePositions = true;
        }
     }

   if(closePositions)
     {
      for(int i = PositionsTotal() - 1; i >= 0; i--)
        {
         ulong posTicket = PositionGetTicket(i);

         if(posTicket > 0)
           {
            trade.PositionClose(posTicket);
           }
        }
     }
     
  }
 
asser hussien:

Hi every one.

I need to close a group of open positions automatically when the the value of equity reaches a certain limit.

Is this feature exist in metatrader 5 ? and if not. Is this feature can be generated by a certain code ?

Please help. thank you.

Hello,

This code can serve as a guide for you.

//+------------------------------------------------------------------+
//|                                                      Example.mq5 |
//|                        Copyright 2024, Antonio Simón Del Vecchio |
//|             https://www.mql5.com/en/users/simondelvecchio/seller |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
//+------------------------------------------------------------------+
//| Objetos                                                          |
//+------------------------------------------------------------------+
CTrade Trade;
CPositionInfo Position;
CAccountInfo Account;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
input double MinimumEquity = 1234;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   if(Account.Equity() < MinimumEquity && PositionsTotal() > 0)
      ClosePositions();
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ClosePositions()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if(Position.SelectByIndex(i))
        {
         Trade.PositionClose(Position.Ticket());
        }
     }
  }
//+------------------------------------------------------------------+


Regards, 

 
Conor Mcnamara #:

so when your account reaches a certain equity it should close all positions?  

Exactly, when my account reaches a certain equity it should close all positions.
 
asser hussien #:
Exactly, when my account reaches a certain equity it should close all positions.
that's exactly what I coded in the example so you can take that and put it into a function like the other guy did if you want
 
Conor Mcnamara #:

so when your account reaches a certain equity it should close all positions?  

yes that is exactly what I need to do.