Cancel pending orders at endo of the day

Created at 06 Dec 2018, 21:16
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!
DelFonseca's avatar

DelFonseca

Joined 25.06.2017

Cancel pending orders at endo of the day
06 Dec 2018, 21:16


Good night, I ask for help because I need to cancel pending orders at the end of the day. I tested the following code but it is not the best, I believe giving instructions specifically on changing Day candle would be the best.

Thank you so much.


protected override void OnTick()
{
if (Server.Time.Hour == 21 && Server.Time.Minute == 59)
            {
                foreach (var _PendingOrders in PendingOrders)
                {
                    if (_PendingOrders.Label == Label)
                    {
                        Print("Cancel by time");
                        CancelPendingOrder(_PendingOrders);
                    }
                }
            }
}

@DelFonseca
Replies

PanagiotisCharalampous
07 Dec 2018, 10:07

Hi DelTrader,

I would suggest  the below approach

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public string Label { get; set; }

        MarketSeries _daySeries;
        DateTime _lastDayOpen;
        protected override void OnStart()
        {
            _daySeries = MarketData.GetSeries(TimeFrame.Daily);
            _lastDayOpen = _daySeries.OpenTime.LastValue;
        }

        protected override void OnTick()
        {
            if (_lastDayOpen != _daySeries.OpenTime.LastValue)
            {
                _lastDayOpen = _daySeries.OpenTime.LastValue;
                foreach (var _PendingOrders in PendingOrders)
                {
                    if (_PendingOrders.Label == Label)
                    {
                        Print("Cancel by time");
                        CancelPendingOrder(_PendingOrders);
                    }
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

DelFonseca
08 Dec 2018, 19:48

Thank you very much Panagiotis, It's perfect. You'r the greatest.
Best Regards!


@DelFonseca