PendingOrder


Summary

Provides access to properties of pending orders

Syntax

public interface PendingOrder

Members

NameTypeSummary
Cancel MethodShortcut for Robot.CancelPendingOrder method
Comment PropertyUser assigned Order Comment
ExpirationTime PropertyThe order Expiration time
The Timezone used is set in the Robot attribute
HasTrailingStop PropertyWhen HasTrailingStop set to true,
server updates Stop Loss every time position moves in your favor.
Id PropertyUnique order Id.
Label PropertyUser assigned identifier for the order.
ModifyExpirationTime MethodShortcut for Robot.ModifyPendingOrder method to change Expiration Time
ModifyStopLimitRange MethodShortcut for Robot.ModifyPendingOrder method to change Stop Limit Range
ModifyStopLossPips MethodShortcut for Robot.ModifyPendingOrder method to change Stop Loss
ModifyTakeProfitPips MethodShortcut for Robot.ModifyPendingOrder method to change Take Profit
ModifyTargetPrice MethodShortcut for Robot.ModifyPendingOrder method to change Target Price
ModifyVolume MethodShortcut for Robot.ModifyPendingOrder method to change VolumeInUnits
OrderType PropertySpecifies whether this order is Stop or Limit.
Quantity PropertyQuantity (lots) of this order
StopLimitRangePips PropertyMaximum limit from order target price, where order can be executed.
StopLoss PropertyThe order stop loss in price
StopLossPips PropertyThe order stop loss in pips
StopLossTriggerMethod PropertyTrigger method for position's StopLoss
StopOrderTriggerMethod PropertyDetermines how pending order will be triggered in case it's a StopOrder
SymbolName PropertyGets the symbol name.
TakeProfit PropertyThe order take profit in price
TakeProfitPips PropertyThe order take profit in pips
TargetPrice PropertyThe order target price.
TradeType PropertySpecifies whether this order is to buy or sell.
VolumeInUnits PropertyVolume of this order.

Example 1

PlaceLimitOrder(TradeType.Buy, Symbol, 10000,Symbol.Bid);
var order = LastResult.PendingOrder;
Print("The pending order's ID: {0}", order.Id);

Example 2

using cAlgo.API;
using System;
using System.Globalization;
namespace cAlgo.Robots
{
    // This sample bot shows how to place different types of pending orders
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PendingOrderPlacingSample : Robot
    {
        [Parameter("Type", DefaultValue = PendingOrderType.Limit)]
        public PendingOrderType OrderType { get; set; }
        [Parameter("Direction", DefaultValue = TradeType.Buy)]
        public TradeType OrderTradeType { get; set; }
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        [Parameter("Distance (Pips)", DefaultValue = 20, MinValue = 1)]
        public double DistanceInPips { get; set; }
        [Parameter("Stop (Pips)", DefaultValue = 10, MinValue = 0)]
        public double StopInPips { get; set; }
        [Parameter("Target (Pips)", DefaultValue = 10, MinValue = 0)]
        public double TargetInPips { get; set; }
        [Parameter("Limit Range (Pips)", DefaultValue = 10, MinValue = 1)]
        public double LimitRangeInPips { get; set; }
        [Parameter("Expiry", DefaultValue = "00:00:00")]
        public string Expiry { get; set; }
        [Parameter("Label")]
        public string Label { get; set; }
        [Parameter("Comment")]
        public string Comment { get; set; }
        [Parameter("Trailing Stop", DefaultValue = false)]
        public bool HasTrailingStop { get; set; }
        [Parameter("Stop Loss Method", DefaultValue = StopTriggerMethod.Trade)]
        public StopTriggerMethod StopLossTriggerMethod { get; set; }
        [Parameter("Stop Order Method", DefaultValue = StopTriggerMethod.Trade)]
        public StopTriggerMethod StopOrderTriggerMethod { get; set; }
        [Parameter("Async", DefaultValue = false)]
        public bool IsAsync { get; set; }
        protected override void OnStart()
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            DistanceInPips *= Symbol.PipSize;
            var stopLoss = StopInPips == 0 ? null : (double?)StopInPips;
            var takeProfit = TargetInPips == 0 ? null : (double?)TargetInPips;
            TimeSpan expiry;
            if (!TimeSpan.TryParse(Expiry, CultureInfo.InvariantCulture, out expiry))
            {
                Print("Invalid expiry");
                Stop();
            }
            var expiryTime = expiry != TimeSpan.FromSeconds(0) ? (DateTime?)Server.Time.Add(expiry) : null;
            TradeResult result = null;
            switch (OrderType)
            {
                case PendingOrderType.Limit:
                    var limitPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask - DistanceInPips : Symbol.Ask + DistanceInPips;
                    if (IsAsync)
                        PlaceLimitOrderAsync(OrderTradeType, SymbolName, volumeInUnits, limitPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, OnCompleted);
                    else
                        result = PlaceLimitOrder(OrderTradeType, SymbolName, volumeInUnits, limitPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod);
                    break;
                case PendingOrderType.Stop:
                    var stopPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask + DistanceInPips : Symbol.Ask - DistanceInPips;
                    if (IsAsync)
                        PlaceStopOrderAsync(OrderTradeType, SymbolName, volumeInUnits, stopPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod, OnCompleted);
                    else
                        result = PlaceStopOrder(OrderTradeType, SymbolName, volumeInUnits, stopPrice, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod);
                    break;
                case PendingOrderType.StopLimit:
                    var stopLimitPrice = OrderTradeType == TradeType.Buy ? Symbol.Ask + DistanceInPips : Symbol.Ask - DistanceInPips;
                    if (IsAsync)
                        PlaceStopLimitOrderAsync(OrderTradeType, SymbolName, volumeInUnits, stopLimitPrice, LimitRangeInPips, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod, OnCompleted);
                    else
                        result = PlaceStopLimitOrder(OrderTradeType, SymbolName, volumeInUnits, stopLimitPrice, LimitRangeInPips, Label, stopLoss, takeProfit, expiryTime, Comment, HasTrailingStop, StopLossTriggerMethod, StopOrderTriggerMethod);
                    break;
                default:
                    Print("Invalid order type");
                    throw new ArgumentOutOfRangeException("OrderType");
            }
            if (!IsAsync) OnCompleted(result);
        }
        private void OnCompleted(TradeResult result)
        {
            if (!result.IsSuccessful) Print("Error: ", result.Error);
            Stop();
        }
    }
}

Example 3

using cAlgo.API;
using System;
using System.Globalization;
using System.Linq;
namespace cAlgo.Robots
{
    // This sample shows how to modify a pending order
    // It uses order comment to find the order, you can use order label instead if you want to
    // Set stop loss and take profit to 0 if you don't want to change it
    // Leave expiry parameter empty if you don't want to change it or 0 if you want to remove it
    // If you don't want to change the target price set it to 0
    // If you don't want to change the volume set it to 0
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PendingOrderModificationSample : Robot
    {
        [Parameter("Order Comment")]
        public string OrderComment { get; set; }
        [Parameter("Order Label")]
        public string OrderLabel { get; set; }
        [Parameter("Target Price", DefaultValue = 0.0)]
        public double TargetPrice { get; set; }
        [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
        public double StopLossInPips { get; set; }
        [Parameter("Stop Loss Trigger Method", DefaultValue = StopTriggerMethod.Trade)]
        public StopTriggerMethod StopLossTriggerMethod { get; set; }
        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public double TakeProfitInPips { get; set; }
        [Parameter("Expiry (HH:mm:ss)")]
        public string Expiry { get; set; }
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        [Parameter("Has Trailing Stop", DefaultValue = false)]
        public bool HasTrailingStop { get; set; }
        [Parameter("Order Trigger Method", DefaultValue = StopTriggerMethod.Trade)]
        public StopTriggerMethod OrderTriggerMethod { get; set; }
        [Parameter("Limit Range (Pips)", DefaultValue = 10)]
        public double LimitRangeInPips { get; set; }
        protected override void OnStart()
        {
            PendingOrder order = null;
            if (!string.IsNullOrWhiteSpace(OrderComment) && !string.IsNullOrWhiteSpace(OrderComment))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase) && string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
            }
            else if (!string.IsNullOrWhiteSpace(OrderComment))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase));
            }
            else if (!string.IsNullOrWhiteSpace(OrderLabel))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
            }
            if (order == null)
            {
                Print("Couldn't find the order, please check the comment and label");
                Stop();
            }
            var targetPrice = TargetPrice == 0 ? order.TargetPrice : TargetPrice;
            var orderSymbol = Symbols.GetSymbol(order.SymbolName);
            var stopLossInPips = StopLossInPips == 0 ? order.StopLossPips : (double?)StopLossInPips;
            var takeProfitInPips = TakeProfitInPips == 0 ? order.TakeProfitPips : (double?)TakeProfitInPips;
            DateTime? expiryTime;
            if (string.IsNullOrWhiteSpace(Expiry))
            {
                expiryTime = order.ExpirationTime;
            }
            else if (Expiry.Equals("0", StringComparison.OrdinalIgnoreCase))
            {
                expiryTime = null;
            }
            else
            {
                var expiryTimeSpan = default(TimeSpan);
                if (!TimeSpan.TryParse(Expiry, CultureInfo.InvariantCulture, out expiryTimeSpan))
                {
                    Print("Your provided value for expiry is not valid, please use HH:mm:ss format");
                    Stop();
                }
                expiryTime = expiryTimeSpan == default(TimeSpan) ? null : (DateTime?)Server.Time.Add(expiryTimeSpan);
            }
            var volumeInUnits = VolumeInLots == 0 ? order.VolumeInUnits : orderSymbol.QuantityToVolumeInUnits(VolumeInLots);
            if (order.OrderType == PendingOrderType.Limit)
            {
                ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod);
            }
            else if (order.OrderType == PendingOrderType.Stop)
            {
                ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod, OrderTriggerMethod);
            }
            else if (order.OrderType == PendingOrderType.StopLimit)
            {
                ModifyPendingOrder(order, targetPrice, stopLossInPips, takeProfitInPips, expiryTime, volumeInUnits, HasTrailingStop, StopLossTriggerMethod, OrderTriggerMethod, LimitRangeInPips);
            }
        }
    }
}

Example 4

using cAlgo.API;
using System;
using System.Linq;
namespace cAlgo.Robots
{
    // This sample shows how to cancel a pending order
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PendingOrderCancelationSample : Robot
    {
        [Parameter("Order Comment")]
        public string OrderComment { get; set; }
        [Parameter("Order Label")]
        public string OrderLabel { get; set; }
        protected override void OnStart()
        {
            PendingOrder order = null;
            if (!string.IsNullOrWhiteSpace(OrderComment) && !string.IsNullOrWhiteSpace(OrderLabel))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase) && string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
            }
            else if (!string.IsNullOrWhiteSpace(OrderComment))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Comment, OrderComment, StringComparison.OrdinalIgnoreCase));
            }
            else if (!string.IsNullOrWhiteSpace(OrderLabel))
            {
                order = PendingOrders.FirstOrDefault(iOrder => string.Equals(iOrder.Label, OrderLabel, StringComparison.OrdinalIgnoreCase));
            }
            if (order == null)
            {
                Print("Couldn't find the order, please check the comment and label");
                Stop();
            }
            CancelPendingOrder(order);
        }
    }
}

Example 5

using cAlgo.API;
namespace cAlgo.Robots
{
    // This sample shows how to use PendingOrders events
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PendingOrderEvents : Robot
    {
        protected override void OnStart()
        {
            PendingOrders.Cancelled += PendingOrders_Cancelled;
            PendingOrders.Modified += PendingOrders_Modified;
            PendingOrders.Filled += PendingOrders_Filled;
        }
        private void PendingOrders_Filled(PendingOrderFilledEventArgs obj)
        {
            var pendingOrderThatFilled = obj.PendingOrder;
            var filledPosition = obj.Position;
        }
        private void PendingOrders_Modified(PendingOrderModifiedEventArgs obj)
        {
            var modifiedOrder = obj.PendingOrder;
        }
        private void PendingOrders_Cancelled(PendingOrderCancelledEventArgs obj)
        {
            var cancelledOrder = obj.PendingOrder;
            var cancellationReason = obj.Reason;
        }
    }
}
Reference