Code to close all positions on Friday?

Created at 07 Oct 2021, 19:10
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!
UM

umadrat2012

Joined 18.09.2021

Code to close all positions on Friday?
07 Oct 2021, 19:10


Good Day guys,

 

I want to manually run my cbot from Monday to Friday, But I want to backtest it by having a code that closes all trades on Friday at 5 pm. Does someone have a sample code or something which when  I backtest my cbot all open positions will be closed on Friday at 5pm?

 

Thank you very much.


@umadrat2012
Replies

amusleh
08 Oct 2021, 07:14

Hi,

Try this sample cBot:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private TimeSpan _closeTime;

        [Parameter("Close Time", DefaultValue = "17:00:00")]
        public string CloseTime { get; set; }

        [Parameter("Close Day", DefaultValue = DayOfWeek.Friday)]
        public DayOfWeek CloseDay { get; set; }

        protected override void OnStart()
        {
            if (TimeSpan.TryParse(CloseTime, CultureInfo.InvariantCulture, out _closeTime) == false)
            {
                Print("Incorrect close time value");

                Stop();
            }

            Timer.Start(1);
        }

        protected override void OnTimer()
        {
            if (Server.Time.DayOfWeek == CloseDay && Server.Time.TimeOfDay >= _closeTime)
            {
                var positionsCopy = Positions.ToArray();

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 


@amusleh

umadrat2012
08 Oct 2021, 07:48

RE:

amusleh said:

Hi,

Try this sample cBot:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private TimeSpan _closeTime;

        [Parameter("Close Time", DefaultValue = "17:00:00")]
        public string CloseTime { get; set; }

        [Parameter("Close Day", DefaultValue = DayOfWeek.Friday)]
        public DayOfWeek CloseDay { get; set; }

        protected override void OnStart()
        {
            if (TimeSpan.TryParse(CloseTime, CultureInfo.InvariantCulture, out _closeTime) == false)
            {
                Print("Incorrect close time value");

                Stop();
            }

            Timer.Start(1);
        }

        protected override void OnTimer()
        {
            if (Server.Time.DayOfWeek == CloseDay && Server.Time.TimeOfDay >= _closeTime)
            {
                var positionsCopy = Positions.ToArray();

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 

Thank you very much, implementing this into my cbot seems to be working


@umadrat2012