Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Control_Trade_Sessions - MetaTrader 5 Library - MT4/MT5 Resources

author EAcpu | 7 reads | 0 comments |

Control_Trade_Sessions - library for MetaTrader 5

Library for trading session control. When launched, it counts trading session times for 7 days a week (cryptocurrency trading may occur on Saturdays and Sundays), with a maximum of 10 sessions per day. Then in OnTick() you can check and if the quote appears outside the trading session, you can exit further processing of it.

Usage example:

 //+------------------------------------------------------------------+
//|testControl_Trade_Sessions.mq5 |
//+------------------------------------------------------------------+
// #define LoadSessionFromInputs // Read sessions from inputs
#include // Connect the code that controls the trading period

When integer is initialized () { return ( initialization successful ); } Blank check () {
static TRADE_SESSIONS trading sessions ( _symbol ); // Get market open/close times and save them into an array by days of the week. Called once when starting Expert Advisor (see static). if (!TradeSession.isSessionTrade( timecurrent ())){ print ( "Market Closed. OnTick Return" ); return ;}
 
//your action }

The result is a printout like this:

2022.01.10 00:05:00 Days: 0 Trading Sessions: 1: 00:00-00:00 - это воскресенье
2022.01.10 00:05:00 Day: 1 Trading session: 1: 00:15-23:55
2022.01.10 00:05:00 Day: 2 Trading Period: 1: 00:15-23:55
2022.01.10 00:05:00 Days: 3 Trading Sessions: 1: 00:15-23:55
2022.01.10 00:05:00 Days: 4 Trading Sessions: 1: 00:15-23:55
2022.01.10 00:05:00 Days: 5 Trading Sessions: 1: 00:15-23:55
2022.01.10 00:05:00 Days: 6 Trading Sessions: 1: 00:00-00:00
2022.05.30 00:05:00 The market is closed. OnTick Return
2022.05.30 00:05:01 The market is closed. OnTick Return
...
2022.05.30 00:14:36 ​​The market is closed. OnTick Return
2022.05.30 00:14:40 The market is closed. OnTick Return
2022.05.30 23:55:00 The market is closed. OnTick Return
2022.05.30 23:55:00 The market is closed. OnTick Return
...
2022.05.30 23:59:30 The market is closed. OnTick Return
2022.05.31 00:00:01 The market is closed. OnTick Return
...

 if (time< this.NextTradeStop ) { return true ; }
Where NextTradeStop is the end time of the current trading session.

On every price change outside of trading hours, there is also 1 check:
 if (time< this.NextTradeStart ) { return false ; }
Only on transitions between sessions, the array is accessed to get the time for the next session.

For example, on my DC, the trading period is 00:15 to 23:55. On the first tick after 00:15 NextTradeStop will be set to 23:55 and then only this condition will be checked throughout the day.

your trading meeting

You can also manually specify the time of the trading session. To activate this option, add the row.
 #define load session from input // read session from input

It will create 7 inputs for entering trading sessions by day of the week.

Enter the session time without spaces, strictly use: - and,

In the parameters tab it looks like this:

If the session end time is less than the start time, for example 20:00-8:00, the session will last until 8:00 the next day. This is useful when the trading session is in a different time zone than the server time.

The time of the trading session can also be specified in the ticker, no input is required. The LoadFromInputs() function was created for this purpose. It can be called without input, but directly from code with an array of strings, as shown in the example.

 string s = [ "00:15-17:45,17:55-23:55" , "00:00-24:00" , ....] blank load from input( string &s[]){...}


If the Expert Advisor is multi-currency and different instruments have different trading session times, you can create a separate TRADE_SESSIONS instance for each instrument and call LoadFromInputs() with the session data and check isSessionTrade(). To do this, you must modify code similar to this example.

Overview Grid Master EA is an automated trading system that implements a two-way grid strategy. It places multiple pending orders above and below the current market price, profiting from market oscillations in both directions.

This filter was created to simplify the process of finding assets trading at discounted prices. Initial use may take slightly longer due to the data loading process for all selected instruments. The tool can scan all available broker assets or be limited to specific asset classes.

The simplest EA trading can analyze price changes on a given number of bars and open corresponding positions.

The Expert Advisor processes pending orders Buy Stop and Sell Stop according to the time specified in its input parameters.


Attachment download

📎 testcontrol_trade_sessions.mq5 (1.46 KB)

📎 Control_Trade_Sessions.mqh (13.48 KB)

Source: MQL5 #48059

Verification code Refresh