Order

View as Markdown

Definition

Represents a read only interface that exposes information regarding an order.

  • An Order object returned from calling an order method is dynamic in that its properties will always reflect the current state of an order.
  • The property <order>.OrderId is NOT a unique value, since it can change throughout an order’s lifetime. Please see the Advance Order Handling section on “Transitioning order references from historical to live” for details on how to handle.
  • The property <order>.Oco WILL be appended with a suffix when the strategy transitions from historical to real-time to ensure the OCO id is unique across multiple strategies for live orders.
  • To check for equality you can compare Order objects directly.

Methods and Properties

ParameterDescription
AccountThe Account the order resides
AverageFillPriceA double value representing the average fill price of an order
FilledAn int value representing the filled amount of an order
FromEntrySignalA string representing the user defined fromEntrySignal parameter on an order
GtdA DateTime structure representing when the order will be canceled
HasOverfillA bool value representing if the order is an overfill. For use when using Unmanaged orders and IgnoreOverFill
InstrumentAn Instrument value representing the instrument of an order
IsBacktestOrderA bool that indicates if the order was generated while processing historical data. For use with GetRealtimeOrder() when transitioning historical order objects to live order objects when strategies transition to from State.Historical to State.Realtime.
IsLiveUntilCancelledA bool that when true, indicates the order will be canceled by managed order handling at expiration
IsTerminalState()A static method used to determine if the an order’s OrderState is in considered terminal and no longer active
LimitPriceA double value representing the limit price of an order
LimitPriceChangedA double value representing the new limit price of an order. Used with Account.Change()
NameA string representing the name of an order which can be provided by the entry or exit signal name
OcoA string representing the OCO (one cancels other) id of an order
OrderActionRepresents the action of the order. Possible values are:
• OrderAction.Buy
• OrderAction.BuyToCover
• OrderAction.Sell
• OrderAction.SellShort
OrderIdA string representing the broker issued order id value (this value can change)
OrderStateThe current state of the order. See the order state values table below
OrderTypeThe type of order submitted. Possible values are:
• OrderType.Limit
• OrderType.Market
• OrderType.MIT
• OrderType.StopMarket
• OrderType.StopLimit
QuantityAn int value representing the quantity of an order
QuantityChangedAn int value representing the new quantity of an order. Used with Account.Change()
StopPriceA double value representing the stop price of an order
StopPriceChangedA double value representing the new stop price of an order. Used with Account.Change()
TimeA DateTime structure representing the last time the order changed state
TimeInForceDetermines the life of the order. Possible values are:
• TimeInForce.Day
• TimeInForce.Gtc
ToString()A string representation of an order

OrderState Values

Order StateDescription
OrderState.InitializedOrder is initialized in NinjaTrader
OrderState.SubmittedOrder is submitted to the broker
OrderState.AcceptedOrder is accepted by the broker or exchange
OrderState.TriggerPendingOrder is pending submission
OrderState.WorkingOrder is working in the exchange queue
OrderState.ChangePendingOrder change is pending in NinjaTrader
OrderState.ChangeSubmittedOrder change is submitted to the broker
OrderState.CancelPendingOrder cancellation is pending in NinjaTrader
OrderState.CancelSubmittedOrder cancellation is submitted to the broker
OrderState.CancelledOrder cancellation is confirmed by the exchange
OrderState.RejectedOrder is rejected
OrderState.PartFilledOrder is partially filled
OrderState.FilledOrder is completely filled
OrderState.UnknownAn unknown order state. Default if broker does not report current order state.

Critical: In a historical backtest, orders will always reach a “Working” state. In real-time, some stop orders may only reach “Accepted” state if they are simulated/held on a brokers server.

Examples

1private Order entryOrder = null;
2
3protected override void OnBarUpdate()
4{
5 if (entryOrder == null && Close[0] > Open[0])
6 EnterLong("myEntryOrder");
7}
8
9protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
10{
11 // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
12 // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
13 if (order.Name == "myEntryOrder")
14 entryOrder = order;
15
16 if (entryOrder != null && entryOrder == order)
17 {
18 Print(order.ToString());
19 if (order.OrderState == OrderState.Filled)
20 entryOrder = null;
21 }
22}