CloseOnTime_v2
Download scriptThis script is an automated tool designed for MetaTrader 4 to manage risk by automatically closing all open trading positions at a specific time of day. It acts as a "hard stop" for your trading session, ensuring that you do not leave positions active overnight or during periods you wish to be out of the market.
How it Works
The script constantly monitors the current server time and compares it against the user-defined closing hour and minute. Once the clock reaches the specified time, the tool scans your active trades and executes a closure for each one.
Key Features and Customizations:
- Time Scheduling: You can define the exact hour and minute when you want all trades to be shut down. For example, if you set it to 07:00, the script will begin closing positions as soon as that time is reached.
- Account Security: There is an optional security feature that allows you to lock the script to a specific account number. If enabled, the script will only function on your designated account and will display a warning message if run on any other account.
- Targeting Options: You can choose how broad the script's reach should be:
- Specific Instrument: It can be set to close trades for only the specific currency pair or asset currently on your screen.
- Global Closure: If left as default, it can be configured to close all open positions across all symbols in your platform.
- Trade Execution: The script automatically identifies whether a position is a "Buy" or a "Sell" and executes the correct closing command based on current market prices (Bid/Ask). It also includes a "Slippage" setting to ensure the trades are executed even if the price moves slightly during the split second of processing.
- Visual Feedback: To help you keep track of what the script is doing, you can customize the colors of the labels that appear on your trade history when a position is closed (e.g., Blue for Buy closures and Red for Sell closures).
Summary of Use
This tool is ideal for traders who want to strictly limit their exposure to market volatility during certain hours. Instead of manually closing every trade before going to sleep or taking a break, this script automates the process, providing a consistent and reliable way to "flat" your account at a pre-determined time.
//+------------------------------------------------------------------+
//| 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