EquityTracker

Download script
Author: Tibor Gellert - $Revision: 234 $
EquityTracker

The EquityTracker is an automated management tool designed to help traders protect their profits by monitoring the account's equity in real-time. Its primary purpose is to act as a "safety net" that automatically closes active trades once your account balance reaches a specific financial goal you define.

How it Works

1. Goal Setting The tool functions based on a specific target value provided by the user. You define a "Target Equity" amount—the profit or total account value at which you want to stop trading for the day or for a specific session.

2. Continuous Monitoring Once activated, the script constantly watches your live account balance and the current market price of your open positions. It checks every second if your equity has reached or exceeded the target you set.

3. Automated Execution The moment your account equity hits that predefined target:

  • The script identifies all active trades associated with the current market symbol.
  • It systematically closes those positions to lock in your gains and prevent further market fluctuations from affecting your profit.
  • Once the trades are successfully closed, the script automatically switches itself to a "Disabled" state to prevent it from interfering with future trades until you reset it.

4. Visual Feedback The tool provides a real-time status update directly on your trading screen. This display informs you of:

  • Whether the tracker is currently active or disabled.
  • The specific target equity goal you have set.
  • A history of how many times the script has attempted to close orders (useful for troubleshooting if market conditions prevent a trade from closing instantly).

Key Benefits

  • Emotionless Trading: It removes the human element of "greed" or hesitation, ensuring that your exit strategy is executed the millisecond your goal is met.
  • Hands-Off Management: You can set your targets and let the software handle the technical side of closing positions while you focus on other tasks.
  • Safety First: By providing a clear visual status of the tracker's state, it ensures transparency so you always know whether your account is currently being monitored or if manual intervention is required.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
10 Views
0 Downloads
0 Favorites
EquityTracker
//+------------------------------------------------------------------+
//|                                                EquityTracker.mq4 |
//|                                                    Tibor Gellert |
//|                                          tibor.gellert@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Tibor Gellert - $Revision: 234 $"
#property link      ""

/*
  Revision control: $Id: EquityTracker.mq4 234 2008-07-30 20:44:08Z tibor $
*/  

#include <stdlib.mqh>

extern int  TargetEquity = 0;

bool  gExpertEnabled = false;
int   gCloseAttempts = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if (TargetEquity>0)
     gExpertEnabled = true;
   else
     gExpertEnabled = false;
     
   gCloseAttempts = 0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if (gExpertEnabled && TargetEquity>0 && AccountEquity()>=TargetEquity)
     {
      if (CloseAllOrders(Symbol())==0)
        {
         gExpertEnabled = false;
        }
      gCloseAttempts++;
     }
     
   ManageComment();
//----
   return(0);
  }
//+------------------------------------------------------------------+

int CloseAllOrders(string market)
{
 for(int i=OrdersTotal()-1; i>=0; i--)
   {
    if (OrderSelect(i, SELECT_BY_POS))
      {
       if (OrderSymbol()==market)
         {
          switch (OrderType())
            {
             case OP_BUY:
             case OP_SELL:
               Print("Order to delete:", OrderTicket(), " (", i, ")");
               if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, Black))
                 {
                  Print("Order closed:", OrderTicket());
                 }
               else // OrderClose failed
                 {
                  PrintError("OrderClose failed:"+OrderTicket());
                  return (1);
                 }
               break;
            } //switch
         } // if MAGIC
      } // if OrderSelect
    else // OrderSelect failed
      {
       PrintError("OrderSelect failed" );
       return (1);
      }
   } // for
 return (0);
}

//+------------------------------------------------------------------+

void PrintError( string msg )
{
 int errCode = GetLastError();
 Print(msg, " Code: ", errCode, " Desc: ", ErrorDescription(errCode));
}

//+------------------------------------------------------------------+

void ManageComment()
{
   string cmnt = "EquityTracker";
   if (TargetEquity>0)
     {
      cmnt = cmnt + "\n   Target equity:"+DoubleToStr(TargetEquity,0)+
                    "\n   Close attemps made:"+gCloseAttempts;
     }
   else
     {
      cmnt = cmnt + "\n\n Missing target equity - set TargetEquity parameter!";
     }
   
   if (gExpertEnabled)
     {
      cmnt = cmnt + "\n\n      Expert is enabled";
     }
   else
     {
      cmnt = cmnt + "\n\n      Expert is DISABLED";
     }
   Comment(cmnt);
}

Comments