TradeState - Time-based EA trading mode switcher - MetaTrader 5 library
Have you ever limited the operation of an EA based on time? For example, how to prevent the EA from performing any trading activity during night hours? Or, if you trade on the derivatives section of the Moscow Exchange, have you ever closed all your positions before the weekend close? What about more complex structures? For example, how to enable an EA to open and close positions before 7pm and not execute any new trades after 7pm? In fact, such flexible configurations are possible. The CTradeState special module will help with this.
How does CTradeState work? This module returns one of six states defined using the ENUM_TRADE_STATE structure:
//+------------------------------------------------------------------+ //|Determine the EA's trading status | //+------------------------------------------------------------------+ Enumeration ENUM_TRADE_STATE { Transactions buy and sell, // allows buying and selling. TRADE_BUY_ONLY, // Only purchases allowed. No sales allowed. For trade sales only, // only sales are allowed. No purchases allowed. Trading is stopped, // no transactions allowed. Close all positions immediately. No new entry signals will be accepted. Trade waits, // loses control of the opened position. New signals are ignored. Useful during press releases. TRADE_NO_NEW_ENTRY // Entry signal is ignored. Although the opening position is maintained according to the trading logic. };
In your Expert Advisor, you must perform a set of actions depending on the specific modifier that CTradeState returns from the given structure (use the GetTradeState method to return the value).
Before using the trading mode, it must be set, for which the SetTradeState method must be called. The prototype of the SetTradeState method is as follows:
//+------------------------------------------------------------------+ //|Set trade status TradeState | //|Input: | //| time_begin - the time when the trade status started | //| comes into play. | //| time_end - the time when the transaction status comes into effect | //| day_of_week - day of the week, trading settings | //| The state is applied to. Corresponds to modifier | //| ENUM_DAY_OF_WEEK or modifier ALL_DAYS_OF_WEEK | //| state - trading status. | //|Warning, the date part in time_begin and time_end will be ignored. | //+------------------------------------------------------------------+ Blank CTradeState::Set transaction status ( datetime_start , datetime_end , integer day_of_week, ENUM_TRADE_STATE status);
For example, to have the trading status module return the TRADE_WAIT modifier (stop controlling existing positions and not open new positions) from 18:45 to 18:49 (inclusive), you need to call SetTradeState with the following parameters:
TradeState.SetTradeState( Day '18:45' , Day '18:59' , ALL_DAYS_OF_WEEK, TRADE_WAIT);
Not only can you specify the start and end time of the trading mode, but also the days of the week on which the mode is active. If you need to specify the trading mode for each day of the week, you need to use the ALL_DAYS_OF_WEEK modifier. If you need to specify the trading mode only for a day of the week, specify one of the values of theENUM_DAY_OF_WEEK system enumeration in the day_of_week variable.
To get the current trading mode, use the GetTradeState method.
For example, consider a combination of patterns that can be used for useful trading in the derivatives section of the Moscow Exchange:
Usage examples. This mode combination is set using the following sequence of SetTradeState calls (example is script):
//+------------------------------------------------------------------+ //| Test transaction status.mq5 | //| Copyright 2015, Vasily Sokolov. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #Property Copyright "Copyright 2015, Vasily Sokolov." #Property associated with "http://www.mql5.com" # Property version "1.00" #Include <Strategy\TradeState.mqh> CTradeState TradeState(TRADE_BUY_AND_SELL); // Set the default mode to buy and sell //+------------------------------------------------------------------+ //|Script program startup function | //+------------------------------------------------------------------+ blank start () { TradeState.SetTradeState( Day '15:00' , Day '23:39' , Friday , TRADE_NO_NEW_ENTRY); TradeState.SetTradeState( Day '10:00' , Day '10:01' , ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState( Day '14:00' , Day '14:03' , ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState( Day '18:45' , Day '18:59' , ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState( Day '23:50' , Day '23:59' , ALL_DAYS_OF_WEEK, TRADE_STOP); TradeState.SetTradeState( Day '0:00' , Day '9:59' , ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState( Day '23:40' , Day '23:49' , Friday , trading stops); TradeState.SetTradeState( Day '00:00' , Day '23:59' , Saturday , TRADE_WAIT); TradeState.SetTradeState( Day '00:00' , Day '23:59' , Sunday , TRADE_WAIT); Print function ( "10:00——" + enumeration to string (TradeState.GetTradeState(day'10:00' ))); Print function ( "14:01——" + enumeration to string (TradeState.GetTradeState(day'14:01' ))); Print function ( "18:50——" + enumeration to string (TradeState.GetTradeState(day'18:50' ))); Print function ( "23:50——" + enumeration to string (TradeState.GetTradeState(day'23:51' ))); Print function ( "Friday, > 15:00 - " + enumeration to string (TradeState.GetTradeState( D'2015.11.27 15:00' ))); Print function ( "Saturday-" + enumeration to string (TradeState.GetTradeState( D'2015.11.28' ))); Print function ( "Sunday-" + enumeration to string (TradeState.GetTradeState( D'2015.11.29' ))); Print function ( "Default state -" + enumeration to string (TradeState.GetTradeState(day'11:40' ))); } //+------------------------------------------------------------------+
As a check, this script implements the printing of patterns corresponding to specific trading times.
It is important to realize that the proposed module is only an auxiliary tool. The decision to stop trading (and close all positions) must be made by the experts themselves. However, creating different time zones and trading modes is very easy for any expert using this module.
Attachment download
📎 tradestate.mqh (5.15 KB)
Source: MQL5 #14335
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •