당사 팬 페이지에 가입하십시오
TradeState - the EA trading mode switcher depending on the time - MetaTrader 5용 라이브러리
- 조회수:
- 7052
- 평가:
- 게시됨:
- 2016.01.20 09:57
- 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동
Have you ever restricted the operation of your EA depending on time? For example, how does one make the EA not perform any trading activities during the night session? Or, if you trade on the derivatives section of Moscow Exchange, have you ever closed every positions before the market closure of before the weekend? How about more complex structures? For example, how does one make the EA able to open and close positions up to 7pm and to not perform any new trades after 7pm? Actually it is possible to get such flexible configuration. The CTradeState special module will help with that.
How does the CTradeState work? The module returns one of the six states, defined using the ENUM_TRADE_STATE structure:
//+------------------------------------------------------------------+ //| Determines the trade state of the EA | //+------------------------------------------------------------------+ enum ENUM_TRADE_STATE { TRADE_BUY_AND_SELL, // Buying and selling are allowed. TRADE_BUY_ONLY, // Only buying is allowed. Selling is not allowed. TRADE_SELL_ONLY, // Only selling is allowed. Buying is not allowed. TRADE_STOP, // Trading is not allowed. Close all positions immediately. Do not accept new entry signals. TRADE_WAIT, // Control over the opened positions is lost. New signals are ignored. Useful during the news releases. TRADE_NO_NEW_ENTRY // Entry signals are ignored. Although the opened positions are maintained according to the trading logic. };
In your Expert Advisor you must perform a set of actions, depending on which specific modifier from the given structure the CTradeState returns (the value is returned using the GetTradeState method).
Before using the trade modes, they must be set, for which the SetTradeState method must be called. The prototype of the SetTradeState method is as follows:
//+------------------------------------------------------------------+ //| Sets the trade state TradeState | //| INPUT: | //| time_begin - Time, from which the trade state starts | //| functioning. | //| time_end - Time, until which the trade state functions | //| day_of_week - Day of the week, to which the setting of trade | //| state is applied to. Corresponds to the modifiers | //| ENUM_DAY_OF_WEEK or the modifier ALL_DAYS_OF_WEEK | //| state - The trade state. | //| Warning, date component in time_begin and time_end is ignored. | //+------------------------------------------------------------------+ void CTradeState::SetTradeState(datetime time_begin,datetime time_end, int day_of_week, ENUM_TRADE_STATE state);
For example, for the trade state module to return the TRADE_WAIT modifier (control over the opened positions is stopped, new positions are not opened) at the time from 18:45 to 18:49 inclusive, it is necessary to call SetTradeState with the following parameters:
TradeState.SetTradeState(D'18:45', D'18:59', ALL_DAYS_OF_WEEK, TRADE_WAIT);
It is possible to specify not only the trade mode start and end times, but also the day of the week, for which this mode is active. If the trading mode needs to be specified for every day of the week, the ALL_DAYS_OF_WEEK modifier needs to be used. If the trading mode needs to be specified only for a certain day of the week, specify one of the values of the ENUM_DAY_OF_WEEK system enumeration in the day_of_week variable.
In order to get the current trading mode, use the GetTradeState method.
As an example, consider a combination of modes, which can be used for a useful trading on the derivatives section of Moscow Exchange:
Time | Mode | Description |
---|---|---|
10:00 - 10:01 | TRADE_WAIT | Time of market opening. The moment of opening is characterized by high volatility and price spikes. Trading activities in these moments are associated with high risk, so in the first minutes of trading it is better to refrain from trading, for that the EA needs to be set to the waiting mode for that time period. |
14:00 - 14:03 | TRADE_WAIT | Time Of Intermediate Clearing. In this time interval the market does not work too, so the EA needs to be set to the TRADE_WAIT mode as well. |
18:45 - 18:49 | TRADE_WAIT | Time Of Main Clearing. At this time the market is also closed, and trading is disabled. The TRADE_WAIT mode is active. |
23:50 - 9:59 | TRADE_WAIT | Market is closed, trading is disabled. EA mode is TRADE_WAIT. |
Friday, from 15:00 | TRADE_NO_NEW_ENTRY | Friday — the last trading day of the week. In order not to leave open positions for the weekend, they need to be closed at the last day of trading. Therefore, there is no point in opening new positions on the last trading day just to close them a few hours later. For these very reasons the NO_NEW_ENTRY mode is used. Every Friday, starting from 15:00, new entry signals are ignored. The existing positions can only be closed. |
Friday, 23:40-23:50 | TRADE_STOP | The time before the market closes. This is the time when all the positions must be closed. EA switches to the TRADE_STOP mode at 23:40, closes its open position and switches to the waiting mode. |
Saturday, Sunday | TRADE_WAIT | Trading is not performed during the weekend. Due to the transfer of holidays, some Saturdays may be work days. The Exchange is working on such days. This is a very rare occurrence, and such "work" days must be avoided due to low volatility and statistical uncertainty. Trading on these days must be disabled regardless whether it is a working day or not. |
Usage example. This combination of modes is set using the following sequence of SetTradeState calls (example as a script):
//+------------------------------------------------------------------+ //| TestTradeState.mq5 | //| Copyright 2015, Vasiliy Sokolov. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, Vasiliy Sokolov." #property link "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 start function | //+------------------------------------------------------------------+ void OnStart() { TradeState.SetTradeState(D'15:00', D'23:39', FRIDAY, TRADE_NO_NEW_ENTRY); TradeState.SetTradeState(D'10:00', D'10:01', ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState(D'14:00', D'14:03', ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState(D'18:45', D'18:59', ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState(D'23:50', D'23:59', ALL_DAYS_OF_WEEK, TRADE_STOP); TradeState.SetTradeState(D'0:00', D'9:59', ALL_DAYS_OF_WEEK, TRADE_WAIT); TradeState.SetTradeState(D'23:40', D'23:49', FRIDAY, TRADE_STOP); TradeState.SetTradeState(D'00:00', D'23:59', SATURDAY, TRADE_WAIT); TradeState.SetTradeState(D'00:00', D'23:59', SUNDAY, TRADE_WAIT); printf("10:00 - " + EnumToString(TradeState.GetTradeState(D'10:00'))); printf("14:01 - " + EnumToString(TradeState.GetTradeState(D'14:01'))); printf("18:50 - " + EnumToString(TradeState.GetTradeState(D'18:50'))); printf("23:50 - " + EnumToString(TradeState.GetTradeState(D'23:51'))); printf("Friday, > 15:00 - " + EnumToString(TradeState.GetTradeState(D'2015.11.27 15:00'))); printf("Saturday - " + EnumToString(TradeState.GetTradeState(D'2015.11.28'))); printf("Sunday - " + EnumToString(TradeState.GetTradeState(D'2015.11.29'))); printf("Default State - " + EnumToString(TradeState.GetTradeState(D'11:40'))); } //+------------------------------------------------------------------+
As a check, this script implements a printing of the mode that corresponds to the specific trading time.
It is important to realize that the proposed module is just an auxiliary tool. The decision to stop trading (and to close all positions) must be made by the expert itself. However, it is quite easy to create different time zones and trade modes for any expert using this module.
MetaQuotes Ltd에서 러시아어로 번역함.
원본 코드: https://www.mql5.com/ru/code/14335
Robot that trades the open sentiment of the market.
CIsSession - class to set time intervals (sessions)This simple class can be used to adjust, for example, trading ranges, or to enable / disable certain actions by time or day of the week.
The PA_Oscillator with the timeframe selection option available in the input parameters.
LinearMomentum_HTFThe LinearMomentum with the timeframe selection option available in the input parameters.