UTE/Strategy/TradeState.mqh

104 lines
9.6 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:34:43 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TradeState.mqh |
//| Copyright 2016, Vasiliy Sokolov, St-Petersburg, Russia |
//| https://www.mql5.com/ru/users/c-4 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Vasiliy Sokolov."
#property link "https://www.mql5.com/ru/users/c-4"
#define ALL_DAYS_OF_WEEK 7
//+------------------------------------------------------------------+
//| ?@545;O5B B>@3>2>5 A>AB>O=85 M:A?5@B0. |
//+------------------------------------------------------------------+
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
};
//+------------------------------------------------------------------+
//| >4C;L B>@3>2KE A>AB>O=89 TradeState |
//+------------------------------------------------------------------+
class CTradeState
{
private:
ENUM_TRADE_STATE m_state[60*24*7]; // 0A:0 B>@3>2KE A>AB>O=89
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);
};
//+------------------------------------------------------------------+
//| 568< ?>-C<>;G0=8N TRADE_BUY_AND_SELL |
//+------------------------------------------------------------------+
CTradeState::CTradeState(void)
{
ArrayInitialize(m_state,TRADE_BUY_AND_SELL);
}
//+------------------------------------------------------------------+
//| 568< ?>-C<>;G0=8N 70405BAO 7=0G5=85< default_state |
//+------------------------------------------------------------------+
CTradeState::CTradeState(ENUM_TRADE_STATE default_state)
{
ArrayInitialize(m_state,default_state);
}
//+------------------------------------------------------------------+
//| #AB0=02;8205B B>@3>2>5 A>AB>O=85 TradeState |
//| INPUT: |
//| time_begin - @5<O, =0G8=0O A :>B>@>3> 459AB2C5B B>@3>2>5 |
//| A>AB>O=85. |
//| time_end - @5<O, 4> :>B>@>3> 459AB2C5B B>@3>2>5 A>AB>O=85 |
//| day_of_week - 5=L =545;8, =0 :>B>@K9 @0A?@>AB@0=O5BAO CAB0=>2:0 |
//| B>@3>2>3> A>AB>O=8O. !>>B25BAB2C5B <>48D8:0B>@0< |
//| ENUM_DAY_OF_WEEK 8;8 <>48D8:0B>@C ALL_DAYS_OF_WEEK |
//| state - ">@3>2>5 A>AB>O=85. |
//| =8<0=85, :><?>=5=B0 40BK 2 time_begin 8 time_end 83=>@8@C5BAO. |
//+------------------------------------------------------------------+
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<ALL_DAYS_OF_WEEK; day++)
{
if(day!=day_of_week && day_of_week!=ALL_DAYS_OF_WEEK)
continue;
int i_day=day*60*24;
int i_begin=i_day+(btime.hour*60)+btime.min;
int i_end = i_day + (etime.hour*60) + etime.min;
for(int i = i_begin; i <= i_end; i++)
m_state[i]=state;
}
}
//+------------------------------------------------------------------+
//| >72@0I05B @0=55 7040==>5 4;O B5:CI53> 2@5<5=8 B>@3>2>5 A>AB>O=85|
//+------------------------------------------------------------------+
ENUM_TRADE_STATE CTradeState::GetTradeState(void)
{
return GetTradeState(TimeCurrent());
}
//+------------------------------------------------------------------+
//| >72@0I05B @0=55 7040==>5 B>@3>2>5 A>AB>O=85 4;O ?5@540==>3> |
//| 2@5<5=8. |
//+------------------------------------------------------------------+
ENUM_TRADE_STATE CTradeState::GetTradeState(datetime time_current)
{
MqlDateTime dt;
TimeToStruct(time_current,dt);
int i_day = dt.day_of_week*60*24;
int index = i_day + (dt.hour*60) + dt.min;
return m_state[index];
}
//+------------------------------------------------------------------+