CloseOnTime

Download script
Author: ��� ����� �. aka KimIV
CloseOnTime

This script is designed for traders who need to automatically exit all their open market positions at a specific time of day. It acts as an automated "hard stop" to ensure that no trades remain active past a user-defined deadline, which is particularly useful for managing risk during high volatility or when closing out daily positions.

How the Logic Works

1. Custom Scheduling The core functionality relies on a pre-set time. You can define exactly which hour and minute the script should trigger the closing action. Once the platform's clock reaches that specific moment, the script activates its execution sequence.

2. Security and Filtering Options The script includes several safety layers to control how it behaves across different accounts and assets:

  • Account Lock: You can set the script to only run on a specific account number. If it detects it is running on a different account, it will display a warning message and refuse to execute any trades.
  • Symbol Selection: You can choose whether the script should close every open trade across all markets or limit its actions to only the specific currency pair or asset you currently have open.

3. Automated Execution Process When the designated time is reached, the script performs a comprehensive sweep of all active trades:

  • It identifies every open position in your portfolio.
  • If the filters (Account and Symbol) are met, it automatically detects whether the trade is a "Buy" or a "Sell."
  • It then calculates the current market price for those specific positions and executes a market order to close them immediately.

4. Visual Feedback & Reliability To ensure smooth execution even in fast-moving markets, the script includes a "Slippage" setting. This allows the trade to be closed even if the price fluctuates slightly during the split second it takes to process the command. Additionally, it provides visual confirmation by assigning specific colors (Blue for Buy closures and Red for Sell closures) to the closing actions in your trade history.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
13 Views
0 Downloads
0 Favorites
CloseOnTime
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                  CloseOnTime.mq4 |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//| Ñîâåòíèê â íóæíîå âðåìÿ çàêðûâàåò âñå ïîçèöèè.                   |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link      "http://www.kimiv.ru"

//---- input parameters
extern int   CloseHour     = 7;      // Âðåìÿ çàêðûòèÿ, ÷àñû
extern int   CloseMinute   = 0;      // Âðåìÿ çàêðûòèÿ, ìèíóòû
extern bool  UseCurrSymbol = False;  // Èñïîëüçîâàòü òîëüêî îäèí èíñòðóìåíò
extern bool  UseOneAccount = False;  // Èñïîëüçîâàòü òîëüêî îäèí ñ÷¸ò
extern int   NumberAccount = 11111;  // Íîìåð òîðãîâîãî ñ÷¸òà
extern int   Slippage      = 3;      // Ïðîñêàëüçûâàíèå öåíû
extern color clCloseBuy    = Blue;   // Öâåò çàêðûòèÿ ïîêóïêè
extern color clCloseSell   = Red;    // Öâåò çàêðûòèÿ ïðîäàæè

void start() {
  double pBid, pAsk;

  if (UseOneAccount && AccountNumber()!=NumberAccount) {
    Comment("Ðàáîòà íà ñ÷¸òå: "+AccountNumber()+" ÇÀÏÐÅÙÅÍÀ!");
    return;
  } else Comment("");

  if (Hour()==CloseHour && Minute()>=CloseMinute) {
    for (int i=OrdersTotal()-1; i>=0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
        if (!UseCurrSymbol || OrderSymbol()==Symbol()) {
          if (OrderType()==OP_BUY) {
            pBid=MarketInfo(OrderSymbol(), MODE_BID);
            OrderClose(OrderTicket(), OrderLots(), pBid, Slippage, clCloseBuy);
          }
          if (OrderType()==OP_SELL) {
            pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
            OrderClose(OrderTicket(), OrderLots(), pAsk, Slippage, clCloseSell);
          }
        }
      }
    }
  }
}
//+------------------------------------------------------------------+

Comments