Libraries: Control_Trade_Sessions

 

Control_Trade_Sessions:

Library for trading session control. At startup it counts the time of trading sessions for all 7 days of the week (on Sat and Sun there can be cryptocurrency trading), up to 10 sessions per day. Then in OnTick() you can do checks, and if a tick came in outside the trading session, you can exit further processing of it.

Control_Trade_Sessions

Author: Aleksei Kuznetsov

 

That’s all nonsense. If a broker fills in the instrument specifications in a way that doesn’t match the actual quotes, your library will be lying just as much as the broker.

You can run an example on Finam.

 
Alexey Viktorov #:

That’s all nonsense. If a broker fills in the instrument specifications in a way that doesn’t match the actual price quotes, then your library will be lying just as blatantly as the broker.

You can run the example on Finam.

We’ve established who’s to blame. But what can be done?

I can add an option to import sessions by day via sinput files.
 
A brilliant library, thank you! The main selling point is its speed. It’s just the ticket for an optimiser.
 
Forester #:
We’ve figured out who’s to blame. But what can we do about it?

I could add an option to import sessions by day using sinput.

I’ve added this option. See the description and code.

 
I've sped up the code a little more. Previously, at every tick outside a trading session, the time was calculated and compared with the session arrays:
. Now, when sessions change, I calculate not only the end time of the current session but also the start time of the next session. As a result, there is now just one check:
if(time < this.NextTradeStart ) { return false; }
 
Forester a trading session, the time would be calculated and compared against the session arrays:
. Now, when sessions change, I calculate not only the end time of the current session but also the start time of the next session. As a result, there is now just one check:

This is the most correct approach for calculating whether a time falls within any given interval. For example, if trading is to take place from 12:00 to 15:00, then a similar ‘NextTime’ approach should be used.

Of all the third-party code I’ve seen, yours is the only one that does this.

 
 
I haven’t looked into it in great detail, but it seems that the current implementation assumes that a session must fall strictly within a single day, which isn’t the case when there is a significant time zone difference; that is, for example, if European sessions are traded via an Asian broker, or vice versa. In other words, there are cases where the ‘To’ time is earlier than the ‘From’ time if you strip the date from the datetime.
 
Stanislav Korotky #:
With custom symbols, you can simulate any tricky scenario involving sessions and check that the library is working correctly. Fortunately, changing sessions does not require the quotes to be rewritten.
 
Stanislav Korotky #:
I haven’t looked into it in great detail, but it seems that the current implementation assumes that a session must fall strictly within a single day, which isn’t the case when there is a significant difference in time zones; that is, for example, if European sessions are traded via an Asian broker, or vice versa. In other words, there are cases where the ‘To’ time is earlier than the ‘From’ time if you ignore the date in the datetime.
It’s simply a comparison based on time.
if(!TradeSession.isSessionTrade(TimeCurrent())){Print("Market closed. OnTick return");return;}
Instead of
TimeCurrent()
You can set any value you like, by the way.

Session times are taken from the broker or can be set manually.