Detected Country () : English
EA AutoClose By Equity - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

EA AutoClose By Equity

Samples [2018-03-30 20:46:44] - by hillzx
Featured Image

EA can close all transactions if it has reached a certain Equity (Equity is capital + profit). The trader can ask the robot to automatically stop the transaction if the trader already feels the profit earned is enough. How to make it pretty easy.

Prepare input variables to set targets:

int Corner=1;
int YPos=20;
int XPos=20;
int Slippage=3;
extern double AutoCloseEquity=0; // Tulis Equity disini, apabila 0 artinya robot akan diam
// Apabila target Equity $2000 maka set AutoCloseEquity=2000
double totalMoney;

 

Next write modules Init, Deinit, and DeleteObjects to clean:

void DeleteObjects()
{
for (int i = ObjectsTotal() - 1; i > -1; i--)
{
if (StringFind(ObjectName(i), "HiLLzXAutoCloseEquity") >= 0) ObjectDelete(ObjectName(i));
}
}

int OnInit()
{
DeleteObjects();
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
DeleteObjects();
}



Next OnTick module :

void OnTick()
{
   CheckEquity();
}

 

Module CheckEquity for validation :

void CheckEquity(){
   WriteEquity();
   
   if( (GetEquity()>AutoCloseEquity) && (AutoCloseEquity!=0) ){
      CloseAll();
   }
}

 

GetEquity Module to calculate + swap + commission + profit + capital from all transactions:

double GetEquity(){

double profit;
double commission;
double swap;
int total=OrdersTotal();
totalMoney=AccountBalance();

for(int pos=total-1;pos>=0;pos--)
{
      OrderSelect(pos, SELECT_BY_POS, MODE_TRADES);
      
   profit = OrderProfit();
   commission=OrderCommission();
   swap=OrderSwap();

    int type   = OrderType();
    switch(type)
    {
      case OP_BUY       : 
      totalMoney+=profit + commission + swap;
      break;

      case OP_SELL      : 
      totalMoney+=profit + commission + swap;
      break;
   
    }

}

return totalMoney;

}



WriteEquity module for writing on screen :

void WriteEquity(){
   ObjectCreate("HiLLzXAutoCloseEquity", OBJ_LABEL, 0, 0, 0);// Creating obj.
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_CORNER, Corner);    // Reference Corner
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_XDISTANCE, XPos);// X coordinate
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_YDISTANCE, YPos);// Y coordinate
   
   if (AutoCloseEquity==0){
      ObjectSetText("HiLLzXAutoCloseEquity","No target equity",12,"Verdana",Silver);
   }else{
      ObjectSetText("HiLLzXAutoCloseEquity","Target Equity: $" + AutoCloseEquity + " Current: $" + DoubleToString(totalMoney,2) ,12,"Verdana",White);
   }
}

 

Last formula CloseAll to close all transactions if Equity reaches the target :

void CloseAll()
{
   int total= OrdersTotal();
   for(int i=total-1;i>=0;i--)
     {
      bool tempselect=OrderSelect(i,SELECT_BY_POS);
      int type=OrderType();

      bool result=false;

      switch(type)
        {
         //Close opened long positions
         case OP_BUY       : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Blue);
         break;

         //Close opened short positions
         case OP_SELL      : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Red);
        }
      }
}

 

If you need full working code, please copy and paste complete source code below :

//+------------------------------------------------------------------+
//|                                            AutoCloseByEquity.mq4 |
//|                                                 hillzx@gmail.com |
//|                                           http://codingforex.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "http://codingforex.com"
#property version   "1.00"
#property strict
extern int Corner=1;
extern int YPos=20;
extern int XPos=20;
extern int Slippage=3;
extern double AutoCloseEquity=0;
double totalMoney;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void DeleteObjects()
{
for (int i = ObjectsTotal() - 1; i > -1; i--)
{
if (StringFind(ObjectName(i), "HiLLzXAutoCloseEquity") >= 0) ObjectDelete(ObjectName(i));
}
}

int OnInit()
  {
//---
DeleteObjects();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteObjects();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckEquity();
  }
//+------------------------------------------------------------------+
void CheckEquity(){
   WriteEquity();
   
   if( (GetEquity()>AutoCloseEquity) && (AutoCloseEquity!=0) ){
      CloseAll();
   }
}

double GetEquity(){

double profit;
double commission;
double swap;
int total=OrdersTotal();
totalMoney=AccountBalance();

for(int pos=total-1;pos>=0;pos--)
{
      OrderSelect(pos, SELECT_BY_POS, MODE_TRADES);
      
   profit = OrderProfit();
   commission=OrderCommission();
   swap=OrderSwap();

    int type   = OrderType();
    switch(type)
    {
      case OP_BUY       : 
      totalMoney+=profit + commission + swap;
      break;

      case OP_SELL      : 
      totalMoney+=profit + commission + swap;
      break;
   
    }

}

return totalMoney;

}


void WriteEquity(){
   ObjectCreate("HiLLzXAutoCloseEquity", OBJ_LABEL, 0, 0, 0);// Creating obj.
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_CORNER, Corner);    // Reference Corner
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_XDISTANCE, XPos);// X coordinate
   ObjectSet("HiLLzXAutoCloseEquity", OBJPROP_YDISTANCE, YPos);// Y coordinate
   
   if (AutoCloseEquity==0){
      ObjectSetText("HiLLzXAutoCloseEquity","No target equity",12,"Verdana",Silver);
   }else{
      ObjectSetText("HiLLzXAutoCloseEquity","Target Equity: $" + AutoCloseEquity + " Current: $" + DoubleToString(totalMoney,2) ,12,"Verdana",White);
   }
}

void CloseAll()
{

   int total= OrdersTotal();
   for(int i=total-1;i>=0;i--)
     {
      bool tempselect=OrderSelect(i,SELECT_BY_POS);
      int type=OrderType();

      bool result=false;

      switch(type)
        {
         //Close opened long positions
         case OP_BUY       : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Blue);
         break;

         //Close opened short positions
         case OP_SELL      : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Red);
        }
      }
}


Let's try please share and follow our facebook and our youtube channel, happy trading!






- Provided by HiLLzSoft Studio -