Pending Orders Cancelled executed for other Pending Orders cancellation

Created at 19 Sep 2021, 15:31
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!
M4

m4trader4

Joined 19.09.2021

Pending Orders Cancelled executed for other Pending Orders cancellation
19 Sep 2021, 15:31


Hi

When ever a pending order is cancelled the PendingOrders.Cancelled += PendingOrders_Cancelled which is correct, in the PendingOrders_Cancelled()  "ExecuteMarketRangeOrder" is executed with the cancelled orders details of entryprice, volume etc.. 

But when there are other pending orders of the same symbol which are cancelled from the order tab (X button click in orders tab) PendingOrders_Cancelled is executed which should not happen. I want the details of  executed pending orders which got cancelled and want to execute a "ExecuteMarketRangeOrder()" on those cancelled pending orders. 

 

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 PendingOrdersV1 : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            PendingOrders.Cancelled += PendingOrders_Cancelled;
        }

        private void PendingOrders_Cancelled(PendingOrderCancelledEventArgs obj)
        {

            var PO = obj.PendingOrder;
            Print("Pending order with id {0} was cancelled. Reason: {1}", obj.PendingOrder.Id, obj.Reason);          
            Print("Price=" + obj.PendingOrder.TargetPrice + "TType=" + obj.PendingOrder.TradeType + "=SymName=" + obj.PendingOrder.SymbolName + "=lot=" + obj.PendingOrder.VolumeInUnits);
            switch (obj.PendingOrder.TradeType)
            {
                case TradeType.Buy:

                    Print("Cancelled TradeType Buy");
                    ExecuteMarketRangeOrder(PO.TradeType, PO.SymbolName, PO.VolumeInUnits, PO.StopLimitRangePips.Value, PO.TargetPrice, "", PO.StopLoss.Value, PO.TakeProfit.Value);                    
                    break;

                case TradeType.Sell:
                    Print("Cancelled TradeType Sell");
                    ExecuteMarketRangeOrder(PO.TradeType, PO.SymbolName, PO.VolumeInUnits, PO.StopLimitRangePips.Value, PO.TargetPrice, "", PO.StopLoss.Value, PO.TakeProfit.Value);
                    break;
            }

        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

 


@m4trader4
Replies

amusleh
20 Sep 2021, 08:46 ( Updated at: 04 Oct 2021, 10:22 )

Hi,

The PendingOrders.Cancelled event will be called whenever a pending order is canceled, and that's how it should work.

I didn't understand your point on its not correct to call the Cancelled event when trader cancels the order, why?

If trader manually or a cBot via automate API cancels a pending order the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Cancelled.

If the order had an expiry time and it got expired the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Expired.

If the order got rejected before getting filled then the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Rejected.

You can use the reason property to filter the event and only execute your code if the reasons is expired or rejected.


@amusleh

m4trader4
20 Sep 2021, 19:06

RE:

amusleh said:

Hi,

The PendingOrders.Cancelled event will be called whenever a pending order is canceled, and that's how it should work.

I didn't understand your point on its not correct to call the Cancelled event when trader cancels the order, why?

If trader manually or a cBot via automate API cancels a pending order the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Cancelled.

If the order had an expiry time and it got expired the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Expired.

If the order got rejected before getting filled then the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Rejected.

You can use the reason property to filter the event and only execute your code is the reasons is expired or rejected.

Hi

Case Scenario: Current Market price = 9.0

Cbot or Manually 5 pending orders are placed

Order#1 10.00 with StopLimitPriceRange  of  00.00 - 00.40 

Order#2 15.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#3 20.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#4 25.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#1 is filled @ 10.10 

Order#2 is not filled @ 15.00 So the PendingOrders_Cancelled called, then the ExecuteMarketRangeOrder is executed and Order#2 is filled.

After Order#2, Cbot or Manually cancel other orders #3, #4, #5 PendingOrders_Cancelled is called and ExecuteMarketRangeOrder is executed possibly may get filled. which should not happen. And Cbot cannot be stopped as there are other function are being executed.

So Need details of the executed pending orders details, with these captured details ExecuteMarketRangeOrder orders are placed and executed or any other logic.

 

 


@m4trader4

amusleh
21 Sep 2021, 08:29

RE: RE:

m4trader4 said:

amusleh said:

Hi,

The PendingOrders.Cancelled event will be called whenever a pending order is canceled, and that's how it should work.

I didn't understand your point on its not correct to call the Cancelled event when trader cancels the order, why?

If trader manually or a cBot via automate API cancels a pending order the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Cancelled.

If the order had an expiry time and it got expired the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Expired.

If the order got rejected before getting filled then the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Rejected.

You can use the reason property to filter the event and only execute your code is the reasons is expired or rejected.

Hi

Case Scenario: Current Market price = 9.0

Cbot or Manually 5 pending orders are placed

Order#1 10.00 with StopLimitPriceRange  of  00.00 - 00.40 

Order#2 15.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#3 20.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#4 25.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#1 is filled @ 10.10 

Order#2 is not filled @ 15.00 So the PendingOrders_Cancelled called, then the ExecuteMarketRangeOrder is executed and Order#2 is filled.

After Order#2, Cbot or Manually cancel other orders #3, #4, #5 PendingOrders_Cancelled is called and ExecuteMarketRangeOrder is executed possibly may get filled. which should not happen. And Cbot cannot be stopped as there are other function are being executed.

So Need details of the executed pending orders details, with these captured details ExecuteMarketRangeOrder orders are placed and executed or any other logic.

 

 

When your order #2 is cancelled then how it gets filled again? I didn't got it at all.

You can use order label/comment or a boolean flag to avoid executing your code on PendingOrders_Cancelled event handler when you cancel your other orders.


@amusleh

m4trader4
22 Sep 2021, 12:56

RE: RE: RE:

 

When your order #2 is cancelled then how it gets filled again? I didn't got it at all.

You can use order label/comment or a boolean flag to avoid executing your code on PendingOrders_Cancelled event handler when you cancel your other orders.

For Order#2 which is not filled, PendingOrders_Cancelled is called and in that ExecuteMarketRangeOrder is executed using the parameters passed by PendingOrders.Cancelled event. 

Orders "label/Comment" will not be unique, or to have a unique identifier.  Let me know your suggestion of unique identifier.

There should be a intermittent order state :  Executed - Cancelled, Not Executed - Cancelled

 

 

 


@m4trader4

amusleh
23 Sep 2021, 09:07

RE: RE: RE: RE:

m4trader4 said:

 

When your order #2 is cancelled then how it gets filled again? I didn't got it at all.

You can use order label/comment or a boolean flag to avoid executing your code on PendingOrders_Cancelled event handler when you cancel your other orders.

For Order#2 which is not filled, PendingOrders_Cancelled is called and in that ExecuteMarketRangeOrder is executed using the parameters passed by PendingOrders.Cancelled event. 

Orders "label/Comment" will not be unique, or to have a unique identifier.  Let me know your suggestion of unique identifier.

There should be a intermittent order state :  Executed - Cancelled, Not Executed - Cancelled

 

 

 

Hi,

I think there is a misunderstanding, I still don't know what's the issue you have.

Regarding unique identifier you can use Order/position IDs.


@amusleh

m4trader4
03 Oct 2021, 10:33

Hi

Sorry for delayed reply.

The scenario:

I place Buy Pending orders #1 with Price 10 with stop limit range 00.00 - 00.40 pips. The order is executed and not filled due to limit range (out of range). As there were no sellers to sell in the limit range ( 00.00 - 00.40), so the order status is cancelled. 

With the PendingOrderEvents cancelled event, the details of the order (Qty, SL, TP ) are captured and new marketrange order is executed. So far its good. The Buy Pending Order is executed, the order is unfilled (Cancelled), then improvised with marketrange order and executed (Filled).

 

There are pending Buy orders which are placed manually/cbot, When these orders are cancelled (Which are not executed) using "X" button in the order tab, the PendingOrderEvents is executed with a cancelled event status. Which should not happen.  

My logic or intention is the PendingOrdersEvents.Cancelled should be called only for the executed orders not for "X" button cancelled orders.

Hope i am able to explain better.

 

 

 


@m4trader4

amusleh
04 Oct 2021, 10:18

RE:

m4trader4 said:

Hi

Sorry for delayed reply.

The scenario:

I place Buy Pending orders #1 with Price 10 with stop limit range 00.00 - 00.40 pips. The order is executed and not filled due to limit range (out of range). As there were no sellers to sell in the limit range ( 00.00 - 00.40), so the order status is cancelled. 

With the PendingOrderEvents cancelled event, the details of the order (Qty, SL, TP ) are captured and new marketrange order is executed. So far its good. The Buy Pending Order is executed, the order is unfilled (Cancelled), then improvised with marketrange order and executed (Filled).

 

There are pending Buy orders which are placed manually/cbot, When these orders are cancelled (Which are not executed) using "X" button in the order tab, the PendingOrderEvents is executed with a cancelled event status. Which should not happen.  

My logic or intention is the PendingOrdersEvents.Cancelled should be called only for the executed orders not for "X" button cancelled orders.

Hope i am able to explain better.

 

 

 

Hi,

I still don't understand why the PendingOrdersEvents.Cancelled event should not be triggered when a user manually cancels a pending order?

The order is canceled and the PendingOrdersEvents.Cancelled must be called, it doesn't matter who or how the order got canceled.

If your stop limit order didn't get filled on your specified limit range then it will be canceled, and it will be considered a canceled pending order.

So the event will be triggered whenever a pending order gets canceled, it doesn't matter from which channel (API) the order got canceled.


@amusleh

amusleh
04 Oct 2021, 10:50

Hi,

If your Market Range order is partially filled and you want to execute a new order for the not filled part of your order or do something else you can use the PendingOrders.Filled event instead of PendingOrders.Cancelled event:

       private void PendingOrders_Filled(PendingOrderFilledEventArgs obj)
        {
            // if not all volume filled
            if (obj.Position.VolumeInUnits != obj.PendingOrder.VolumeInUnits)
            {
                // Do something
            }
        }

 


@amusleh

m4trader4
09 Oct 2021, 12:01

RE:

Hi Ahmad

Market range order is either filled or not filled, there is no partial fill so PendingOrders_Filled will not help. I am looking at intermediate state : 

Order Executed - Status Cancelled

Order Not Executed - Status Cancelled


@m4trader4