//+------------------------------------------------------------------+ //| TradeState.mqh | //| Copyright 2016, Vasiliy Sokolov, St-Petersburg, Russia | //| https://www.mql5.com/en/users/c-4 | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, Vasiliy Sokolov." #property link "https://www.mql5.com/en/users/c-4" #define ALL_DAYS_OF_WEEK 7 //+------------------------------------------------------------------+ //| Determines the EA's trading state. | //+------------------------------------------------------------------+ enum ENUM_TRADE_STATE { TRADE_BUY_AND_SELL, // Buy And Sell TRADE_BUY_ONLY, // Buy Only TRADE_SELL_ONLY, // Sell Only TRADE_STOP, // Stop TRADE_WAIT, // Wait TRADE_NO_NEW_ENTRY // No New Entry }; //+------------------------------------------------------------------+ //| Module of trading states TradeState                             | //+------------------------------------------------------------------+ class CTradeState { private: ENUM_TRADE_STATE m_state[60*24*7]; // Mask of trading states public: CTradeState(void); CTradeState(ENUM_TRADE_STATE default_state); ENUM_TRADE_STATE GetTradeState(void); ENUM_TRADE_STATE GetTradeState(datetime time_current); void SetTradeState(datetime time_begin,datetime time_end,int day_of_week,ENUM_TRADE_STATE state); }; //+------------------------------------------------------------------+ //| Default mode is TRADE_BUY_AND_SELL | //+------------------------------------------------------------------+ CTradeState::CTradeState(void) { ArrayInitialize(m_state,TRADE_BUY_AND_SELL); } //+------------------------------------------------------------------+ //| The default mode is set by the value of default_state | //+------------------------------------------------------------------+ CTradeState::CTradeState(ENUM_TRADE_STATE default_state) { ArrayInitialize(m_state,default_state); } //+------------------------------------------------------------------+ //| Sets the TradeState trading state | //| INPUT: | //| time_begin - beginning of period during which trading state | //| is valid. | //| time_end - the time till which the trading state is valid | //| day_of_week - Day of the week, to which the setting of trade | //| state applies to. Corresponds to the modifiers | //| ENUM_DAY_OF_WEEK or the ALL_DAYS_OF_WEEK modifier | //| state - Trading state. | //| Note: date components in time_begin and time_end are ignored. | //+------------------------------------------------------------------+ void CTradeState::SetTradeState(datetime time_begin,datetime time_end,int day_of_week,ENUM_TRADE_STATE state) { if(time_begin>time_end) { string sb = TimeToString(time_begin, TIME_MINUTES); string se = TimeToString(time_end, TIME_MINUTES); printf("Time "+sb+" must be more time "+se); return; } MqlDateTime btime,etime; TimeToStruct(time_begin,btime); TimeToStruct(time_end,etime); for(int day=0; day