11//
2- // Copyright 2018-19, Artur Zas
2+ // Copyright 2018-2021, Artur Zas
3+ // GNU General Public License v3.0 -> https://github.com/9nix6/Median-and-Turbo-Renko-indicator-bundle/blob/master/LICENSE
34// https://www.az-invest.eu
45// https://www.mql5.com/en/users/arturz
56//
@@ -8,46 +9,100 @@ class CTimeControl
89{
910 private :
1011
12+ int scheduleID ;
13+
1114 int startHH ;
1215 int startMM ;
1316 string start ;
17+ datetime startOfSession ;
1418
1519 int endHH ;
1620 int endMM ;
1721 string end ;
22+ datetime endOfSession ;
1823
1924 bool scheduleEnabled ;
2025
2126 public :
2227
28+ CTimeControl (int id = 0 ) { scheduleID = id ; };
29+
2330 void SetValidTraingHours (string _from = " 0:00" , string _to = " 0:00" );
31+ void SetValidTraingHours (bool _unused , string _timeSpan = " 0:00-0:00" );
2432 bool IsTradingTimeValid ();
2533 bool IsScheduleEnabled () { return scheduleEnabled ; };
34+ void UpdateSessionDateTime (int addSecods = 0 );
35+ datetime GetSessionStartTime () { return startOfSession ; };
36+ datetime GetSessionEndTime () { return endOfSession ; };
37+ void MoveSessionStartToNow ();
38+ string ToString ();
39+
40+ private :
41+
2642 void StringToHHMM (string value , int &HH , int &MM );
43+ bool StringToHHMMRange (string value , int &startHH , int &startMM , string & _start , int &endHH , int &endMM , string & _end );
44+ void SetScheduleState ();
2745};
2846
29- void CTimeControl ::SetValidTraingHours (string _from ,string _to )
47+ void CTimeControl ::SetValidTraingHours (string _from , string _to )
3048{
3149 this .start = _from ;
3250 this .end = _to ;
3351
3452 StringToHHMM (this .start , this .startHH , this .startMM );
3553 StringToHHMM (this .end , this .endHH , this .endMM );
54+ UpdateSessionDateTime ();
3655
37- if (this .startHH == 0 && this .startMM == 0 && this .endHH == 0 && this .endMM == 0 )
56+ SetScheduleState ();
57+ }
58+
59+ void CTimeControl ::MoveSessionStartToNow ()
60+ {
61+ datetime now = TimeCurrent ();
62+
63+ MqlDateTime temp ;
64+ TimeToStruct (now ,temp );
65+
66+ startHH = temp .hour ;
67+ startMM = temp .min ;
68+ start = StringFormat (" %02d:%02d" , startHH , startMM );
69+
70+ UpdateSessionDateTime (temp .sec + 1 );
71+ }
72+
73+ void CTimeControl ::UpdateSessionDateTime (int addSecods = 0 )
74+ {
75+ datetime now = TimeCurrent ();
76+
77+ MqlDateTime temp ;
78+ TimeToStruct (now ,temp );
79+
80+ if (addSecods > 0 )
3881 {
39- scheduleEnabled = false ;
82+ string startTimeTemp = StringFormat (" %s:%02d" , this .start , addSecods );
83+ startOfSession = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +startTimeTemp );
4084 }
4185 else
4286 {
43- scheduleEnabled = true ;
87+ startOfSession = StringToTime (( string ) temp . year + " . " +( string ) temp . mon + " . " +( string ) temp . day + " " + this . start );
4488 }
89+
90+ endOfSession = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +this .end );
91+ }
92+
93+ void CTimeControl ::SetValidTraingHours (bool _unused , string _timeSpan = " 0:00-0:00" )
94+ {
95+ if (!StringToHHMMRange (_timeSpan , this .startHH , this .startMM , this .start , this .endHH , this .endMM , this .end ))
96+ return ;
97+
98+ UpdateSessionDateTime ();
99+ SetScheduleState ();
45100}
46101
47102bool CTimeControl ::IsTradingTimeValid ()
48103{
49104 if (scheduleEnabled == false )
50- return true ;
105+ return true ;
51106
52107 datetime now = TimeCurrent ();
53108
@@ -57,12 +112,32 @@ bool CTimeControl::IsTradingTimeValid()
57112 datetime _start = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +this .start );
58113 datetime _end = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +this .end );
59114
60- if (( now >= _start ) && ( now <= _end ) )
115+ if (_start <= now && now <= _end )
61116 return true ;
62117 else
63118 return false ;
64119}
65120
121+ string CTimeControl ::ToString ()
122+ {
123+ string tradingScheduleName = " Trading schedule " ;
124+
125+ tradingScheduleName += (scheduleID != 0 )
126+ ? (string )scheduleID +" "
127+ : " " ;
128+
129+ if (IsScheduleEnabled ())
130+ {
131+ return tradingScheduleName +" ON (" +this .start +" to " +this .end +" ) | trading " +(IsTradingTimeValid ()
132+ ? " enabled"
133+ : " disabled" );
134+ }
135+ else
136+ {
137+ return tradingScheduleName +" NOT USED" ;
138+ }
139+ }
140+
66141void CTimeControl ::StringToHHMM (string value , int &HH , int &MM )
67142{
68143 MqlDateTime temp ;
@@ -73,4 +148,46 @@ void CTimeControl::StringToHHMM(string value, int &HH, int &MM)
73148
74149 HH = temp .hour ;
75150 MM = temp .min ;
76- }
151+ }
152+
153+ bool CTimeControl ::StringToHHMMRange (string value , int &_startHH , int &_startMM , string & _start , int &_endHH , int &_endMM , string & _end )
154+ {
155+ string result [];
156+ int count = StringSplit (value , ' -' , result );
157+ if (count != 2 )
158+ return false ;
159+
160+ MqlDateTime temp ;
161+ TimeToStruct (TimeCurrent (),temp );
162+
163+ // Start time
164+ _start = result [0 ];
165+ datetime fullDateTime = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +_start );
166+ TimeToStruct (fullDateTime ,temp );
167+
168+ startHH = temp .hour ;
169+ startMM = temp .min ;
170+
171+ // End time
172+ TimeToStruct (TimeCurrent (),temp );
173+ _end = result [1 ];
174+ fullDateTime = StringToTime ((string )temp .year +" ." +(string )temp .mon +" ." +(string )temp .day +" " +_end );
175+ TimeToStruct (fullDateTime ,temp );
176+
177+ endHH = temp .hour ;
178+ endMM = temp .min ;
179+
180+ return true ;
181+ }
182+
183+ void CTimeControl ::SetScheduleState ()
184+ {
185+ if (this .startHH == 0 && this .startMM == 0 && this .endHH == 0 && this .endMM == 0 )
186+ {
187+ scheduleEnabled = false ;
188+ }
189+ else
190+ {
191+ scheduleEnabled = true ;
192+ }
193+ }
0 commit comments