Clickalgo Knowledge Center
Return to Website

Pending Orders - Create, Modify & Close

You can create Limit and stop orders using the cTrader API & Microsoft C# to automatically submit an order with an automated trading system (cBot) based on your trade rules. All orders can be executed by using either synchronous or asynchronous methods. Limit and Stop orders are Pending Orders. They are created in a similar way to market orders. Their parameters differ in that the target price must be specified and there is no market range.

 

Synchronous Pending Orders

The example code below creates a buy limit order with the target price of 1.0270 and a sell stop order of 1.0075, the parameters differ from a Market order in that the target price must be specified and there is no market range.

 [Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        PlaceLimitOrder(TradeType.Buy, Symbol, 10000, 1.027, "myLimitOrder");
        PlaceStopOrder(TradeType.Sell, Symbol, 10000, 1.0075, "myStopOrder");
    }
}

Asynchronous Pending Orders

It is also possible to create an asynchronous pending order that will send the request to the broker and continue processing other requests without the need to wait for the reply from the broker for the transaction result, the example below shows a simple example to submit a Stop pending order for EURUSD with a position size of 1K units and entry price of 1.0270, a callback method is used when the trade operation result is returned from the broker.

 

 [Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        TradeOperation operation = PlaceStopOrderAsync(TradeType.Buy, "EURUSD", 1000, 1.027, StopOrderCallBack);
        if (operation.IsExecuting)
        {
            Print("Operation Is Executing");
        }
    }
    private void StopOrderCallBack(TradeResult tradeResult)
    {
        if (tradeResult.IsSuccessful)
            Print("Stop OrderPlaced");
    }
}

 

The full list of parameters available is shown below, this is the same for the synchronous and asynchronous orders apart from the last value which is called callback and is used by the asynchronous order for the late reply.

 

cTrader Pending Order Parameters

 

Stop-Limit Pending Orders

stop-limit order is a conditional trade over a set timeframe that combines the features of a stop with those of a limit order and is used to mitigate risk. The order will be executed at a specified price, or better after a given stop price has been reached.  A stop-limit order will require two price points.

  1. Stop, The start of the specified target price for the trade.
  2. Limit, The outside of the price target for the trade.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        PlaceStopLimitOrder(TradeType.Buy, "EURUSD", 1000, 1.027, 2);
    }
}

 

The example above opens a Buy Stop-Limit order for EURUSD with a volume of 1K units and an entry price of 1.0270, the last value (2) is the StopLimitRangePips which is not possible with a standard limit or stop order.

 

cTrader stop-limit order parameters

 

Cancelling Pending Orders

Imagine you have 50 pending orders open and you wish to close them as fast as possible, the best way to do this is to send the requests for cancellation for all orders at the same time and not one after the other while waiting for a response from the broker, to do this you would use Asyncronnous logic.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
    }
    protected void CloseStopOrders()
    {
        // Get all Stop orders var stopOrders = PendingOrders.Where(o => o.OrderType == PendingOrderType.Stop) foreach (var order in stopOrders) { CancelPendingOrderAsync(order); } 
    }
}

 

The example above will find all the stop orders and cancel them asynchronously, it will only cancel stop orders and not any other pending order type that may be still active, if you wanted to close all orders you would use the code snippet below.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
    }
    protected void CloseAllOrders()
    {
        foreach (var order in PendingOrders)
        {
            CancelPendingOrderAsync(order);
        }
    }
}

 

Modifying Pending Orders

As with the market orders, you can also modify any active pending orders. To modify the target price of a pending order, the protection levels or the expiration date time we use syntax such as in the following example.

 

[Robot()]
public class Sample_cBot : Robot
{
    protected override void OnStart()
    {
        var price = Symbol.Ask + 10 * Symbol.PipSize;
        DateTime? expiry = Server.Time.AddHours(12);
        PlaceStopOrder(TradeType.Buy, Symbol, 10000, price, "myLabel", 10, 10, expiry);
    }
    protected override void OnBar()
    {
        foreach (var order in PendingOrders)
        {
            if (order.Label == "myLabel")
            {
                double newPrice = Symbol.Ask + 5 * Symbol.PipSize;
                ModifyPendingOrder(order, newPrice, order.StopLossPips, order.TakeProfitPips, order.ExpirationTime);
            }
        }
    }
}

 

The example above will place a stop order when the cBot starts with an expiry date of 12 hours in the future and when the candle closes the OnBar method is called and this order is retrieved by using the unique label name of MyLabel, by using a label name for the position it will allow you to modify only that order and not any others. The method ModifyPendingOrder is used in this example to change the target price (new rice).

 

Trading API Shortcuts

All orders have added the shortcuts for modification or close/cancel actions can be done by calling the methods directly on the order instance.

  • PendingOrder.ModifyVolume
  • PendingOrder.ModifyExpirationTime
  • PendingOrder.ModifyTargetPrice
  • PendingOrder.ModifyStopLossPips
  • PendingOrder.ModifyTakeProfitPips
  • PendingOrder.ModifyStopLimitRange
  • PendingOrder.Cancel