Libraries: TradeState - the EA trading mode switcher depending on the time

 

TradeState - the EA trading mode switcher depending on the time:

The class restricts the EA trading by time. It has flexible configuration options, which allow to set a custom number of the time zones, and also to allow trading only on the specified week days.

//+------------------------------------------------------------------+
//| 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);

Author: Vasiliy Sokolov

 

Stolen. Now you can make a schedule for the week.

Here's a situation. It took me a long time to make a weekly schedule. Everything works. Suddenly, hello, New Year's Eve. Holidays. We need a schedule taking into account the holidays. It's a pity to break the weekly schedule. Solution: I create an additional instance of weekly schedule with prohibition of trading on holidays. Question: should I expand m_state to the size of the year century? Or how do you solve this issue beyond one week?

 
Konstantin Gruzdev:

Stolen. Now you can make a schedule for the week.

Here's a situation. It took me a long time to make a weekly schedule. Everything works. Suddenly, hello, New Year's Eve. Holidays. We need a schedule taking into account the holidays. It's a pity to break the weekly schedule. Solution: I create an additional instance of weekly schedule with prohibition of trading on holidays. Question: should I expand m_state to the size of the year century? Or how do you solve this issue beyond one week?

Yes, there is such a task. I am still thinking about how to do it. I haven't come to a beautiful solution yet. But I have an understanding of how it should look like.

  1. The SetTradeState(...) interface itself suggests that if we set the date in the variables time_begin and time_end in the format D'hh:mm', then this mode will be in effect all the time for all days that satisfy the day_of_week condition. If the date is set specifically, specifying the year, month and day in the format D'YYYYY.MM.DD hh:mm', then the set mode will have to act specifically on this time range.
  2. Apparently, the second time variant will have to be placed in another data type (it can be an array of some structures or something else). Then we will have to search for the trade mode in two different ways in two different collections and compare the modes, deciding which mode should act. In this case, the GetTradeState interface should remain unchanged.

Among the niceties you can implement loading of trade states from XML. You can enter all holidays into XML once, for a year in advance, and then they will be automatically loaded in all Expert Advisors. It is quite easy to do this, as there is already an excellent library in MQL5 for working with XML.
 

+5

day_of_week - more often it is a group of days: PN..PT or "except Friday" or "except Monday and Friday".

 
Alexander Puzanov:

+5

day_of_week - more often it is a group of days: PN..PT or "except Friday" or "except Monday and Friday".

The group of days is formed as follows: let's say we want to trade on Mondays, Tuesdays and Fridays. Then we prohibit trading on Wednesday and Thursday:

//+------------------------------------------------------------------+
//|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 default mode Buy And Sell
//+------------------------------------------------------------------+
//| Script programme start function|
//+------------------------------------------------------------------+
void OnStart()
{
   TradeState.SetTradeState(D'00:00', D'23:59', WEDNESDAY, TRADE_STOP);
   TradeState.SetTradeState(D'00:00', D'23:59', THURSDAY, TRADE_STOP);
}
I.e. each day of the week is configured individually to the minute.
 
Got it.
 
The mql5 master doesn't see it, why?
 
I think we should introduce the field of typical spread. For example, during the Rollover and the first hour after it, the spread can increase by 2-3 times. Night scalpers could process such widening and trade more accurately.
I implement this in my Expert Advisors, though not in the class.
 

how to replace D'00:00' with normal digits 00:00 ?????? datetime is annoying(((((

In TradeState.SetTradeState(D'00:00:00', D'23:59', WEDNESDAY, TRADE_STOP);

 
ponochka:

how to change D'00:00' to the usual digits 00:00 ????? datetime is annoying((((((

In TradeState.SetTradeState(D'00:00:00', D'23:59', WEDNESDAY, TRADE_STOP);

Please:

TradeState.SetTradeState(0, 86399, WEDNESDAY, TRADE_STOP);
 
#include <Strategy\TradeState.mqh>
CTradeState TradeState(TRADE_BUY_AND_SELL);

void OnTick()
  {
     ......здесь работаем с закрытием (тралом и прочим)....
 TradeState.SetTradeState(ontime, offtime, ALL_DAYS_OF_WEEK, TRADE_NO_NEW_ENTRY);
     ......остальной код, в частности для открытия....
  }

correct location of the code?

I apologise in advance for dumb questions!!! just a newbie)))))))