Skip to content

Trading Sessions

Following the release of cTrader Windows 4.5, the API now includes the MarketSessions interface. It allows for attaining information about current market sessions and using this data in your cBots/indicators.

Trading Sessions in Algo Development

The MarketSession type is an enum with values representing various trading sessions (such as Singapore or London).

In turn, the MarketSessions property is of the MarketSession type. To get all current market sessions, use it as follows.

1
    Print("Current Sessions: {0}", MarketSessions); 

The value of the MarketSessions property should match the sessions that are displayed in the 'Trading sessions' field in the lower-left corner of the cTrader UI.

Image title

You can use the HasFlag method to check whether the current sessions contain a specific session.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MarketSessionsTest : Robot
    {
        [Parameter("Working Session", DefaultValue = MarketSession.London)]
        public MarketSession WorkingSession {get; set;}

        protected override void OnTick()
        {

            MarketSession exampleSession = MarketSession.London;

            if (MarketSessions.HasFlag(exampleSession))
              {

                // Insert your preferred method logic here.

              }
                return;

        }
    }
}

You can also handle the MarketSessionsChanged event to detect and react to any changes in market sessions. The MarketSessionChangedEventArgs class has two properties, namely NewSessions and PreviousSessions, which work as follows.

  • NewSessions contains all current sessions including any sessions that have just started. The value of NewSessions is always equal to the value of the MarketSessions property of a cBot/indicator.
  • PreviousSessions also contains all current sessions but it also contains any past sessions that have just ended. Its value is equal to the value of the MarketSessions property of a cBot/indicator before the MarketSessionsChanged was triggered.

For a more detailed look at how NewSessions and PreviousSessions work, refer to the below example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            MarketSessionsChanged += OnMarketSessionsChanged;
            Print($"Starting sessions: {this.MarketSessions}");
        }

        private void OnMarketSessionsChanged(MarketSessionChangedEventArgs obj)
        {
            Print($"Session(s) changed, previous sessions: {obj.PreviousSessions}");
            Print($"Sessions(s) changed, current sessions: {obj.NewSessions}");
        }

        protected override void OnTick() { }

        protected override void OnStop() { }
    }
}

In the log, you should see two new entries for each session change. These entries will inform you of what the past trading sessions were and what they are now.

Trading Sessions During Backtesting

The MarketSessions property works on both live and backtesting environments. During backtesting, this property will contain the sessions relative to the chosen backtest timings. In other words, you can use it to access trading sessions that were active during a specific historic trading period.