?> QuantXpress – Deliver high performance automated trading and connectivity solutions



Smart Order Command


Smart Order Command is one of the powerful features of the BlitzTrader API to enable quant developer with a standardized mechanism of managing the order state of an instrument with ease. Earlier we encounter with individual orders APIs to manage sending new order, modifying and cancelling it based on strategy logic. We need to understand some limitation with earlier approach to manage the order of an instrument.
Order modification and cancellation request need exchange order ID which can only be accessible once order is accepted by the exchange. For the same reason developers need to handle order accepted event and cache various state of the order across the different orders event of the strategy.
Smart Order Command is intelligent way to manage instrument order state from single unified command. It intelligently decides the intention when to fire a fresh new order, modifying exiting order state or cancelling it.
It also prevents and provides a secure way of avoiding some mistakes of generating new orders in loop conditions. Smart Order Command is represented by interface IOrderCommand and framework can provide template object by requesting with a function call GetSmartOrderCommand.
GetSmartOrderCommand takes unique name identifier, order TimeInForce attribute and reference to your Instrument Variable to return the Smart Order Command. Following is important functions used to control the order routing behaviour:
 
void Set(bool condition, OrderSide side, OrderType orderType, int orderQuantiy, double orderPrice, double orderStopPrice);

Parameter:

condition : Indicating a condition by strategy to route the order or not. If condition is false, the existing order is cancelled. If condition is true, based in order state, it either send a fresh new order or modify the existing open order with a new order attributes.
side : Side of the order (Buy or Sell)
orderType : Type of the order(Market, Limit, Stop)
orderQuantity : order quantity of a new order state
orderPrice : limit order price of a new order state. This is valid for ordertype Limit.
orderStopPrice : stop order price of a new order state. This is valid for ordertype Stop.
If condition is false, all other attributes does not hold any significance and smart order command initiate a cancel order request.
 
bool Reset(out string errorString);

Reset is called to clean the smart order command state and is done before a fresh logic start or the end of logical iteration.
Following code snippet demonstrate how to use Smart Order Command
 
private IVObject _ivObject = new IVObject("Instrument",
                                          "Tradable Instrument",
                                          true,
                                          InstrumentType.Equity | InstrumentType.Futures | InstrumentType.Options,
                                          MarketDataType.All,
                                          OrderEventType.All);
private IOrderCommand _entryOrderCommand = null;
private IOrderCommand _exitOrderCommand = null;

private IVInfo _ivInfo = null;
..........................................
.........................................
protected override void OnInitialize()
{
    _ivInfo = base.GetIVInfo(_ivObject);

    _entryOrderCommand = base.GetSmartOrderCommand("Entry", TimeInForce.GFD, _ivObject);
    _exitOrderCommand = base.GetSmartOrderCommand("Exit", TimeInForce.GFD, _ivObject);

     if (_entryOrderCommand == null ||
         _exitOrderCommand == null)
                throw new Exception("Strategy Initialization Failed. Reason : Smart Order is not Initialized.");

     TraceLogInfo("Strategy Initialize Successfully"); 
}
............................................
............................................
............................................

OrderSide entrySide = GetEntryOrderSide();

if(_opportunitHit)
    _entryOrderCommand.Set(true, 
                            entrySide,
                            OrderType.Limit,
                            OrderQty,
                            entryOrderPrice,
                            0);

if(_entryOrderCommand.TotalTradedQuantity > 0)
{
    ..........................
    ..........................            

    OrderSide exitSide = GetExitOrderSide();
     _exitOrderCommand.Set(true,
                           exitSide,
                           OrderType.Limit,
                           _entryOrderCommand.TotalTradedQuantity,
                           exitOrderPrice,
                           0);
}
 ..........................
 ..........................            

if (_entryOrderCommand.TotalTradedQuantity > 0 &&
                   _entryOrderCommand.TotalTradedQuantity == _exitOrderCommand.TotalTradedQuantity)
{
    CompletedRounds++;
    base.TraceLogInfo("Transaction Completed.");
    string errorString = string.Empty;

    if (!_entryOrderCommand.Reset(out errorString))
        base.TraceLogError("Smart Order Resetting Failed. Reason : " + errorString);

    if (!_exitOrderCommand.Reset(out errorString))
        base.TraceLogError("Smart Order Resetting Failed. Reason : " + errorString);
 }

//Register a callback function on smart order command event
_ entryOrderCommand.OnOrderRejected += OnSmartOrderRejected;
_ entryOrderCommand.OnOrderSendingFailed += OnSmartOrderSendingFailed
_ entryOrderCommand.OnOrderTraded += OnSmartOrderTraded;
 ..........................
 ..........................
private void OnSmartOrderRejected(IOrderCommand orderCommand, string reason)
{
    this.Stop("Smart order[" + orderCommand.SmartOrderID + "] Rejected. Rejection Reason : " + reason);
 }

private void OnSmartOrderSendingFailed(IOrderCommand orderCommand, string reason)
{
    this.Stop("Smart order[" + orderCommand.SmartOrderID + "] sending failed. Reason : " + reason);
}

private void OnSmartOrderTraded(IOrderCommand orderCommand)
{
    if (orderCommand.SmartOrderID == _ entryOrderCommand.SmartOrderID)
    {
    
    }
}