Alternative to the missing event "OnPendingOrderDeleted"?

Created at 20 Jan 2014, 17: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!
Cerunnos's avatar

Cerunnos

Joined 27.06.2013

Alternative to the missing event "OnPendingOrderDeleted"?
20 Jan 2014, 17:16


Hi guys. If after a period of 2 hours my Stop Order was deleted (expiry time = 2 in PlaceStopOrder () ), then I want to set a counter variable to zero. Unfortunately there is no event like OnPendingOrderDeleted. Any idea how I could it solve otherwise?


@Cerunnos
Replies

Spotware
21 Jan 2014, 09:07

We are planning to enhance the API with more trading event handlers. For the time being you can check if the pending order exists by setting a label when it is placed. If there is no position and no order with this label then it has expired.


@Spotware

Old Account
21 Jan 2014, 09:51

You think somethin like this would work?

        private int _ExpTime;
        private int _Counter;
        private bool _PosOpend;

        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
        }

        protected override void OnTick()
        {
            if (Symbol.Ask == Symbol.Bid)
            {
                PlaceStopOrder(TradeType.Sell, Symbol, 1000, Symbol.Ask, "Lable", 10, 10, DateTime.Now.AddHours(2));
                _ExpTime = Server.Time.Hour + 2;
                _PosOpend = false;
            }

            if (_ExpTime == Server.Time.Hour && !_PosOpend)
                _Counter = 0;
            else
                _Counter = _Counter + 1;


        }
        private void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            _PosOpend = true;
        }

 


@Old Account

Old Account
21 Jan 2014, 09:56

RE:

The if should be if(....)

if (Symbol.Ask == Symbol.Bid)

You think somethin like this would work?

        private int _ExpTime;
        private int _Counter;
        private bool _PosOpend;

        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
        }

        protected override void OnTick()
        {
            if (Symbol.Ask == Symbol.Bid)
            {
                PlaceStopOrder(TradeType.Sell, Symbol, 1000, Symbol.Ask, "Lable", 10, 10, DateTime.Now.AddHours(2));
                _ExpTime = Server.Time.Hour + 2;
                _PosOpend = false;
            }

            if (_ExpTime == Server.Time.Hour && !_PosOpend)
                _Counter = 0;
            else
                _Counter = _Counter + 1;


        }
        private void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            _PosOpend = true;
        }

 

 


@Old Account

Old Account
21 Jan 2014, 10:44

if (_ExpTime == Server.Time.Hour && !_PosOpend)
        _Counter = 0;
    else
        _Counter = _Counter + 1;

Should be:

if (_ExpTime == Server.Time.Hour && !_PosOpend)
        _Counter = 0;

If(_ExpTime == Server.Time.Hour &&_PosOpend

 I think


@Old Account

Old Account
21 Jan 2014, 10:46

RE:

MRSV said:

if (_ExpTime == Server.Time.Hour && !_PosOpend)
        _Counter = 0;
    else
        _Counter = _Counter + 1;

Should be:

if (_ExpTime == Server.Time.Hour && !_PosOpend)
        _Counter = 0;

If(_ExpTime == Server.Time.Hour &&_PosOpend

 I think

if (_ExpTime == Server.Time.Hour && !_PosOpend)
        _Counter = 0;

If(_ExpTime == Server.Time.Hour &&_PosOpend)
_Counter = _Counter + 1;

*


@Old Account

Cerunnos
21 Jan 2014, 11:24

Thanks, I would add:



 if( _ExpTime == Server.Time.Hour && PendingOrders.Count == 0 && !_PosOpend)
        _Counter = 0;

For my Robot I found a very easy solution:

OnTick()
{
....
if(PendingOrders.Count > 0)
   return;
....
....
}

 


@Cerunnos

fzlogic
21 Jan 2014, 12:27

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class NewRobot : Robot
    {
        private int _counter;

        [Parameter(DefaultValue = 5000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = 1)]
        public int expMinutes { get; set; }

        protected override void OnTick()
        {
            if (_counter < 2)
            {
                PlaceStopOrder(TradeType.Sell, Symbol, Volume, Symbol.Ask - Symbol.PipSize * 10, 
                          "Lable", 10, 10, Server.Time.AddMinutes(expMinutes));
                _counter++;
                Print(_counter);
            }
            else
            {
                var pendingOrder = PendingOrders.FirstOrDefault(order => order.Label == "Lable");

                if (pendingOrder == null)
                {
                    Position position = Positions.Find("Lable");
                    if (position == null)
                    {
                        _counter = 0;
                        Print("order expired counter = {0}", _counter);
                    }
                }
            }
        }
    }
}

 


@fzlogic