Clickalgo Knowledge Center
Return to Website

cTrader Position & Pending Order Events

The cTrader API has events that capture when a position or pending order is opened, closed or modified, our API guide will help you easily integrate this code into your cBot.

 

Position Opened Event

To check when a position has opened we can subscribe to an event and this event will also be raised if a position opens manually or by another cBot. A useful feature of capturing this event would be to send an alert to the trader when a pending order is filled and it becomes an open position.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        //Subscribe to the positions opened event. Positions.Opened += PositionsOnOpened; 
        // open a simple market order ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10, 10); 
    }
    
    // method is called when a position opens. 
    private void PositionsOnOpened(PositionOpenedEventArgs args)
    {
        var pos = args.Position;
        Print("Position opened at {0}", pos.EntryPrice);
    }
}

 

Position Closed Event

The same applies to capture a closed position event, we just need to subscribe to the event in the OnStart method. It is also possible to capture all of the positions attributes as well as the reason why it closed.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        // Subscribe to the positions closed event. 
        Positions.Closed += PositionsOnClosed;
        
        // open a simple market order 
        ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "myLabel", 10, 10);
    }
    
    // method is called when a position closes. 
    private void PositionsOnClosed(PositionClosedEventArgs args)
    {
        // the reason for closing can be captured. 
        switch (args.Reason)
        {
            case PositionCloseReason.StopLoss:
                Print("Position closed as stop loss was hit");
                break;
            case PositionCloseReason.StopOut:
                Print("Position closed as it was stopped out");
                break;
            case PositionCloseReason.TakeProfit:
                Print("Position closed as take profit was hit");
                break;
        }
    }
}

 

Position Modified Event

It is also possible to subscribe to the events when an open position has been modified manually by the trader or by an automated trading system (cBot). The events that can be captured are as follows:

  • ModifyStopLossPips
  • ModifyStopLossPrice
  • ModifyTakeProfitPips
  • ModifyTakeProfitPrice

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ClickAlgoBot : Robot
    {
        protected override void OnStart()
        {
            // Subscribe to the positions modified event. 
            Positions.Modified += OnPositionsModified;
        }
        
        // method is called when a position is modified. 
        private void OnPositionsModified(PositionModifiedEventArgs args)
        {
            var pos = args.Position;
            Print("Position modified with a new stop loss of {0} pips", pos.ModifyStopLossPips);
        }
    }
}

 

Pending Order Created Event

When a pending order is created you can capture this event, this is not when it is filled, but instead when it is either manually created by the trader or an automated trading system (cBot).

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ClickAlgoBot : Robot
    {
        protected override void OnStart()
        {
            // Subscribe to the positions closed event. 
            PendingOrders.Created += OnPendingOrdersCreated;
        }
        
        void OnPendingOrdersCreated(PendingOrderCreatedEventArgs obj)
        {
            var order = obj.PendingOrder;
            Print(" A {0} Pending order has been created.", order.OrderType);
        }
    }
}

 

Pending Order Modified Event

It is also possible to subscribe to the events to a pending order that has been modified manually by the trader or by an automated trading system (cBot). The events that can be captured are as follows:

  • ModifyExpiryTime
  • ModifyStopLimitRange
  • ModifyStopLossPips
  • ModifyTakeProfitPips
  • ModifyTargetPrice
  • ModifyVolume

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ClickAlgoBot : Robot
    {
        protected override void OnStart()
        {
            // Subscribe to the positions closed event. 
            PendingOrders.Modified += OnPendingOrdersModified;

        }

        private void OnPendingOrdersModified(PendingOrderModifiedEventArgs obj)
        {
            var order = obj.PendingOrder;
            Print("Pending order modified with a new target price of {0}", order.ModifyTargetPrice);
        }
    }
}

 

Pending Order Cancelled Event

You can also capture when a pending order has been cancelled.

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ClickAlgoBot : Robot
    {
        protected override void OnStart()
        {
            // Subscribe to the positions closed event. 
            PendingOrders.Cancelled += OnPendingOrdersCancelled;
        }

        void OnPendingOrdersCancelled(PendingOrderCancelledEventArgs obj)
        {
            // the reason for pending order cancelled 
            switch (obj.Reason)
            {
                case PendingOrderCancellationReason.Cancelled:
                    Print("Pending order was cancelled");
                    break;

                case PendingOrderCancellationReason.Expired:
                    Print("Pending order has expired");
                    break;

                case PendingOrderCancellationReason.Rejected:
                    Print("Pending order was rejected");
                    break;
            }
        }
    }
}