Only open trades during specific session

Created at 07 Jul 2021, 12:06
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CT

ctid3999979

Joined 05.07.2021

Only open trades during specific session
07 Jul 2021, 12:06


Hi

I'm wanting to create a condition that my cBot can only open trades during the London session. I'm looking at using Symbol.MarketHours.Sessions.Contains but I can't find any documentation on how to use it. I've been looking at the code and it's needing a TSource.

I was hoping I could just create an if statement along the lines of:

if (Symbol.MarketHours.Sessions.Contains("London"))
{
    Some code...
}

 


@ctid3999979
Replies

amusleh
07 Jul 2021, 12:26

The symbol sessions gives you the time of week that you can trade the symbol not the trading session you are after. if you want to trade on specific time period of the day you can use the server time and if its in your trading range you can execute your trading operations, ex:

using cAlgo.API;
using System;
using System.Globalization;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class TradeTime : Robot
    {
        private TimeSpan _startTime, _endTime;

        [Parameter("Start Time", DefaultValue = "07:00:00", Group = "Time Filter")]
        public string StartTime { get; set; }

        [Parameter("End Time", DefaultValue = "16:00:00", Group = "Time Filter")]
        public string EndTime { get; set; }

        private DateTime CurrentTime
        {
            get { return Server.TimeInUtc.Add(-Application.UserTimeOffset); }
        }

        private bool IsTimeCorrect
        {
            get { return (_startTime > _endTime && (CurrentTime.TimeOfDay >= _startTime || CurrentTime.TimeOfDay <= _endTime)) || (_startTime < _endTime && (CurrentTime.TimeOfDay >= _startTime && CurrentTime.TimeOfDay <= _endTime)); }
        }

        protected override void OnStart()
        {
            if (!TimeSpan.TryParse(StartTime, CultureInfo.InvariantCulture, out _startTime))
            {
                Print("Invalid start time input");

                Stop();
            }

            if (!TimeSpan.TryParse(EndTime, CultureInfo.InvariantCulture, out _endTime))
            {
                Print("Invalid end time input");

                Stop();
            }
        }

        protected override void OnBar()
        {
            // Return if current time is not in start/end time range
            if (!IsTimeCorrect) return;
        }
    }
}

 


@amusleh

ctid3999979
07 Jul 2021, 12:37

RE:

amusleh said:

The symbol sessions gives you the time of week that you can trade the symbol not the trading session you are after. if you want to trade on specific time period of the day you can use the server time and if its in your trading range you can execute your trading operations, ex:

using cAlgo.API;
using System;
using System.Globalization;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class TradeTime : Robot
    {
        private TimeSpan _startTime, _endTime;

        [Parameter("Start Time", DefaultValue = "07:00:00", Group = "Time Filter")]
        public string StartTime { get; set; }

        [Parameter("End Time", DefaultValue = "16:00:00", Group = "Time Filter")]
        public string EndTime { get; set; }

        private DateTime CurrentTime
        {
            get { return Server.TimeInUtc.Add(-Application.UserTimeOffset); }
        }

        private bool IsTimeCorrect
        {
            get { return (_startTime > _endTime && (CurrentTime.TimeOfDay >= _startTime || CurrentTime.TimeOfDay <= _endTime)) || (_startTime < _endTime && (CurrentTime.TimeOfDay >= _startTime && CurrentTime.TimeOfDay <= _endTime)); }
        }

        protected override void OnStart()
        {
            if (!TimeSpan.TryParse(StartTime, CultureInfo.InvariantCulture, out _startTime))
            {
                Print("Invalid start time input");

                Stop();
            }

            if (!TimeSpan.TryParse(EndTime, CultureInfo.InvariantCulture, out _endTime))
            {
                Print("Invalid end time input");

                Stop();
            }
        }

        protected override void OnBar()
        {
            // Return if current time is not in start/end time range
            if (!IsTimeCorrect) return;
        }
    }
}

 

Thanks for the quick response and clarification. I'd read an older post about using the server time but wasn't sure if the API had been updated since then.


@ctid3999979