Daily Target

Download script
Author: Copyright © 2019, Vladimir Karputov

This script is designed as an automated management tool for MetaTrader 5, specifically built to monitor your account's profit and handle position closures based on a daily goal. Here is a professional breakdown of how the logic works:

Core Functionality

The primary purpose of this script is to oversee your trading activity and automatically clear all open positions once a specific monetary profit target has been met within a new day. It acts as a "safety net" or an automated "take-profit" for your entire account rather than just a single trade.

Key Logical Steps

  • Target Monitoring: You can define a specific profit amount (in currency) that the script should look for. The system continuously calculates the difference between your current Account Equity (the total value of your trading account) and your Account Balance (your actual funds).
  • Daily Cycle Awareness: The script is programmed to recognize new days. It tracks the time of the last "closing event." This ensures that it only triggers the profit-taking logic once per day or when a new daily candle opens, preventing it from repeatedly trying to close positions in the same timeframe.
  • Automated Position Liquidation: Once the current floating profit meets or exceeds your predefined target, the script triggers an automated "Close All" command. It scans every active position currently held in your account and closes them immediately to secure the gains.
  • Smart Execution:
    • It automatically detects the correct settings for the specific financial instrument (symbol) you are trading to ensure smooth execution.
    • It includes a specialized "Tester Mode" logic; if you are using this in a simulation (backtesting), it will automatically open a small trade to ensure there is something to monitor and close.

Summary for the User

In simple terms, this script works like an automated "Profit Lock." You tell it how much money you want to make today. As soon as your combined open trades reach that profit level, the script "shuts down" all active trades to lock in those profits. It resets its logic daily, allowing you to manage your goals consistently without having to manually watch the charts every minute.

13 Views
0 Downloads
0 Favorites
Daily Target
ÿþ//+------------------------------------------------------------------+

//|                                                 Daily Target.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\AccountInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

CAccountInfo   m_account;                    // object of CAccountInfo class

//--- input parameters

input double InpTargetProfit  =  50;   // Target profit, in money

//---

bool     m_need_close_all     = false; // close all positions

datetime m_last_closing       = 0;     // "0" -> D'1970.01.01 00:00';

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      return(INIT_FAILED);

     }

//---

   m_trade.SetExpertMagicNumber(0);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(10);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      else

         m_need_close_all=false;

     }

//---

   datetime D1_0=iTime(m_symbol.Name(),PERIOD_D1,0);

   if(D1_0>m_last_closing)

      if(m_account.Equity()-m_account.Balance()>=InpTargetProfit)

        {

         m_last_closing=D1_0;

         m_need_close_all=true;

        }

   /*

   //--- when testing

   if(MQLInfoInteger(MQL_TESTER))

   if(PositionsTotal()==0)

    m_trade.Sell(0.5);

    */

  }

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

//| Is position exists                                               |

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

bool IsPositionExists(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         return(true);

//---

   return(false);

  }

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

//| Close all positions                                              |

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

void CloseAllPositions(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         m_trade.PositionClose(m_position.Ticket()); // close a position by the specified m_symbol

  }

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

Comments