Libraries: Control_Trade_Sessions - page 2

 
Forester #:
It’s simply a comparison based on time.
Instead of that, you can set any value you like, by the way.

Session times are taken from the broker, or you can set them manually.

I was referring to the internal code, not the call or the input parameter.

datetime Sec = TimeSec(time); //current second of the day
...
if(( Sec >= this.TradeTimeFrom[i][DayOfWeek] ) && ( Sec < this.TradeTimeTo[i][DayOfWeek] ))

Here, Sec is set to a 24-hour range, but there is no from-to boundary. For example, if the requested time is 3 am and the session trades from 9 pm to 8 am, it should return true, but the condition will not be met and will return false.

 
Stanislav Korotky #:

I was talking about the internal code, not the call and the input parameter.

Here, Sec is expressed in a 24-hour range, but there is no ‘from-to’ boundary. For example, if the requested time is 3 am and the session trades from 9 pm to 8 am, it should return ‘true’, but the condition will not be met and will return ‘false’.

This can be configured using two sessions:
21-24 and 0-8

Is this a theoretical question, or do dealing centres actually have such ‘from-to’ ranges?

 
Forester #:

This can be set using two sessions:
21–24 and 0–8

Is this just a theoretical question, or do distribution centres actually have such ‘from-to’ ranges?

What do you mean by ‘set using two sessions’? – after all, these are retrieved from the MT5 settings, not set by us. And in MT5, one session equals one entry. With some dealing centres (due to their geographical distance from the market), this may be the case. In this case, SymbolInfoSessionTrade returns the ‘to’ time taking into account the additional day (at least that’s how it used to be – I haven’t checked just now), i.e. the condition ‘from < to’ has always held true.
 
Stanislav Korotky #:
What does ‘set two sessions’ mean?

It is possible to specify sessions manually for each day in the sunput files. The programme will operate based on these.
Enables
#define LoadSessionFromInputs // Read sessions from inputs

Stanislav Korotky #:
In this case, SymbolInfoSessionTrade returns the ‘to’ time taking into account the additional day (at least that’s how it used to be – I haven’t checked right now), i.e. the condition‘from < to’ was always maintained.

So 20:00 – 32:00?

If there’s a working example from the dealing centre that we can test on, we can adapt the code to that example and check it.
At the moment, there’s a simple solution – manually split the session in two at midnight. And you can specify which day – for example, on Saturday until 08:00 – the remainder from Friday, or on Sunday from 20:00 – the start of Monday.

 
Forester #:

So, 8.00 pm – 3.00 am?

Yes. I don’t have a ready-made data centre with that kind of server in mind. We’ll have to look somewhere in New Zealand.
 
Stanislav Korotky #:
Yes. I don’t have a ready-made data centre with a server like that in mind. I’ll have to look for one somewhere in New Zealand.

I did it like this using the custom character editor:


 
Forester #:

I set up the custom symbol editor as follows:

I programmed sessions with a transition to the next day for the options shown in the screenshot, i.e. 20:00–08:00.
I modified the function:
      for (int i = 0; i < this.TradeSessions[DayOfWeek]; i++){//Iterate through the sessions on this day of the week
         if(( Sec >= this.TradeTimeFrom[i][DayOfWeek] )){ //session started – find the end
            if(Sec < this.TradeTimeTo[i][DayOfWeek]){//It’s not over yet – remember that
               this.NextTradeStop = (datetime)(day * 86400) + this.TradeTimeTo[i][DayOfWeek]; // the end time of the current session; there’s no need to check the next one until then = (the start of today + the session time) 
               //Print("new Stop ", this.NextTradeStop);
               return true; 
            }else if (this.TradeTimeTo[i][DayOfWeek] < this.TradeTimeFrom[i][DayOfWeek]){// end < start; for example, 20:00–04:00 – carries over to the next day + 1 day to the time
               this.NextTradeStop = (datetime)((day+1) * 86400) + this.TradeTimeTo[i][DayOfWeek]; // the end time of the current session; there’s no need to check the next one until then = (the start of today + the session time) 
               //Print("new Stop ", this.NextTradeStop);
               return true;
            }
         }
      }

The added code checks that the end time is < the start time of the session and then simply adds one day to the end time.

I tested this with the following sessions :

Day: 1 Trading sessions: 1: 23:55–00:05
Day: 2 Trading sessions: 1: 00:15–00:17


First session carried over to the following day:

29 June 2020 23:55:03 29 June 2020 23:55:03
29 June29 June 2020 23:55:09 29 June 2020 23:55:09
2929 June 2020 23:56:39 29 June 2020 23:56:39
2929 June 2020 23:57:01 29 June 2020 23:57:01
29 June 2020 23:59:59 29 June29 June 2020 23:59:59
30 June 2020 00:00:00 30 June 2020 00:00:00
30 June 202030 June 2020 00:00:08 30 June 2020 00:00:08
30 June 2020.30 June 2020 00:01:52 30 June 2020 00:01:52
30 June 2020 00:02:02 30 June 202030 June 00:02:02
30 June 2020 00:02:30 30 June 2020 00:02:30
30 June 2020.30 June 2020 00:03:25 30 June 2020 00:03:25
3030 June 2020 00:03:31 30 June 2020 00:03:31

Second session, as usual, in a single day:
30 June 2020 00:15:29 30 June 2020 00:15:29
30 June 2020 00:16:01 30 June 2020.30 June 2020 00:16:01
30 June 2020 00:16:09 30 June 2020 00:16:09

The first session is in a week’s time:
6 July 2020 23:55:01 6 July 2020 23:55:01
6 July 20206 July 2020 23:55:09 6 July 2020 23:55:09