Order

<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Strategy >

Order

Previous page Return to chapter overview Next page

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

Account

The Account the order resides

AverageFillPrice

A double value representing the average fill price of an order

Filled

An int value representing the filled amount of an order

FromEntrySignal

A string representing the user defined fromEntrySignal parameter on an order

Gtd

A DateTime structure representing when the order will be canceled

HasOverfill

A bool value representing if the order is an overfill. For use when using Unmanaged orders and IgnoreOverFill

Instrument

An Instrument value representing the instrument of an order

IsBacktestOrder

A 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.

IsLiveUntilCancelled

A 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

LimitPrice

A double value representing the limit price of an order

LimitPriceChanged

A double value representing the new limit price of an order. Used with Account.Change()

Name

A string representing the name of an order which can be provided by the entry or exit signal name

Oco

A string representing the OCO (one cancels other) id of an order

OrderAction

Represents the action of the order.  Possible values are:
 

OrderAction.Buy

OrderAction.BuyToCover

OrderAction.Sell

OrderAction.SellShort

OrderId

A string representing the broker issued order id value (this value can change)

OrderState

The current state of the order.  See the order state values table below

OrderType

The type of order submitted.  Possible values are:

OrderType.Limit

OrderType.Market
OrderType.MIT

OrderType.StopMarket

OrderType.StopLimit

Quantity

An int value representing the quantity of an order

QuantityChanged

An int value representing the new quantity of an order. Used with Account.Change()

StopPrice

A double value representing the stop price of an order

StopPriceChanged

A double value representing the new stop price of an order. Used with Account.Change()

Time

A DateTime structure representing the last time the order changed state

TimeInForce

Determines the life of the order.  Possible values are:

TimeInForce.Day

TimeInForce.Gtc

ToString()

A string representation of an order

 

 

OrderState Values

OrderState.Initialized

Order is initialized in NinjaTrader

OrderState.Submitted

Order is submitted to the broker

OrderState.Accepted

Order is accepted by the broker or exchange

OrderState.TriggerPending

Order is pending submission

OrderState.Working

Order is working in the exchange queue

OrderState.ChangePending

Order change is pending in NinjaTrader

OrderState.ChangeSubmitted

Order change is submitted to the broker

OrderState.CancelPending

Order cancellation is pending in NinjaTrader

OrderState.CancelSubmitted

Order cancellation is submitted to the broker

OrderState.Cancelled

Order cancellation is confirmed by the exchange

OrderState.Rejected

Order is rejected

OrderState.PartFilled

Order is partially filled

OrderState.Filled

Order is completely filled

OrderState.Unknown

An 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

ns


private Order entryOrder = null;
 
protected override void OnBarUpdate()
{
  if (entryOrder == null && Close[0] > Open[0])
      EnterLong("myEntryOrder");
}
 
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
  // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
  // 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
  if (order.Name == "myEntryOrder")
      entryOrder = order;
 
  if (entryOrder != null && entryOrder == order)
  {
      Print(order.ToString());
      if (order.OrderState == OrderState.Filled)
          entryOrder = null;
  }
}