Order Management
Order Tickets
Track Orders
As the state of your order updates over time, your order ticket automatically updates. To track an order, you can check any of the preceding order ticket properties.
To get an order field, call the Get
method with an OrderField
.
var limitPrice = ticket.Get(OrderField.LimitPrice);
limit_price = ticket.Get(OrderField.LimitPrice)
The OrderField
enumeration has the following members:
In addition to using order tickets to track orders, you can receive order events through the OnOrderEvent
event handler.
Update Orders
To update an order, use its OrderTicket
. You can update other orders until they are filled or the brokerage prevents modifications. You just can't update orders during warm up and initialization.
Updatable Properties
The specific properties you can update depends on the order type. The following table shows the properties you can update for each order type.
Order Type | Updatable Properties | ||||
---|---|---|---|---|---|
Tag | Quantity | LimitPrice | TriggerPrice | StopPrice | |
Market Order | |||||
Limit Order | ![]() | ![]() | ![]() | ||
Limit If Touched Order | ![]() | ![]() | ![]() | ![]() | |
Stop Market Order | ![]() | ![]() | ![]() | ||
Stop Limit Order | ![]() | ![]() | ![]() | ![]() | |
Market On Open Order | ![]() | ![]() | |||
Market On Close Order | ![]() | ![]() |
Update Methods
To update an order, pass an UpdateOrderFields
object to the Update
method. The method returns an OrderResponse
to signal the success or failure of the update request.
// Create an order var ticket = LimitOrder("SPY", 100, 221.05, tag: "New SPY trade"); // Update the order tag and limit price var response = ticket.Update(new UpdateOrderFields() { Tag = "Our New Tag for SPY Trade", LimitPrice = 222.00 }); // Check the OrderResponse if (response.IsSuccess) { Debug("Order updated successfully"); }
# Create an order ticket = self.LimitOrder("SPY", 100, 221.05, False, "New SPY trade") # Update the order tag and limit price updateSettings = UpdateOrderFields() updateSettings.LimitPrice = 222.00 updateSettings.Tag = "Limit Price Updated for SPY Trade" response = ticket.Update(updateSettings) # Check the OrderResponse if response.IsSuccess: self.Debug("Order updated successfully")
To update individual fields of an order, call any of the following methods:
UpdateLimitPrice
UpdateQuantity
UpdateStopPrice
UpdateTag
var limitResponse = ticket.UpdateLimitPrice(limitPrice, tag); var quantityResponse = ticket.UpdateQuantity(quantity, tag); var stopResponse = ticket.UpdateStopPrice(stopPrice, tag); var tagResponse = ticket.UpdateTag(tag);
response = ticket.UpdateLimitPrice(limitPrice, tag) response = ticket.UpdateQuantity(quantity, tag) response = ticket.UpdateStopPrice(stopPrice, tag) response = ticket.UpdateTag(tag)
Update Order Requests
When you update an order, LEAN creates an UpdateOrderRequest
object, which have the following attributes:
To get a list of UpdateOrderRequest
objects for an order, call the UpdateRequests
method.
var updateRequests = ticket.UpdateRequests();
update_requests = ticket.UpdateRequests()
Workaround for Brokerages That Don’t Support Updates
Not all brokerages fully support order updates. To check what functionality your brokerage supports for order updates, see the Orders section of the documentation for your brokerage model. If your brokerage doesn't support order updates and you want to update an order, cancel the order. When you get an order event that confirms the order is no longer active, place a new order.
public override void OnData(Slice slice) { // Cancel the order _ticket.Cancel(); } public override void OnOrderEvent(OrderEvent orderEvent) { if (_ticket != null && orderEvent.OrderId == _ticket.OrderId && orderEvent.Status == OrderStatus.Canceled) { // Place a new order var quantity = _ticket.Quantity - _ticket.QuantityFilled; var limitPrice = Securities[_ticket.Symbol].Price + 1; _ticket = LimitOrder(_ticket.Symbol, quantity, limitPrice); } }
def OnData(self, slice: Slice) -> None: # Cancel the order self.ticket.Cancel() def OnOrderEvent(self, orderEvent: OrderEvent) -> None: if self.ticket is not None \ and orderEvent.OrderId == self.ticket.OrderId \ and orderEvent.Status == OrderStatus.Canceled: # Place a new order quantity = self.ticket.Quantity - self.ticket.QuantityFilled limit_price = self.Securities[self.ticket.Symbol].Price + 1 self.ticket = self.LimitOrder(self.ticket.Symbol, quantity, limit_price)
Cancel Orders
To cancel an order, call the Cancel
method on the OrderTicket
. The method returns an OrderResponse
object to signal the success or failure of the cancel request.
// Create an order and save its ticket var ticket = LimitOrder("SPY", 100, 221.05, tag: "SPY Trade to Cancel"); // Cancel the order var response = ticket.Cancel("Canceled SPY trade"); // Use the order response object to read the status if (response.IsSuccess) { Debug("Order successfully canceled"); }
# Create an order and save its ticket ticket = self.LimitOrder("SPY", 100, 221.05, False, "SPY Trade to Cancel") # Cancel the order response = ticket.Cancel("Canceled SPY Trade") # Use the order response object to read the status if response.IsSuccess: self.Debug("Order successfully canceled")
You can't cancel market orders because they are immediately transmitted to the brokerage. You also can't cancel any orders during warm up and initialization.
When you cancel an order, LEAN creates a CancelOrderRequest
, which have the following attributes:
To get the CancelOrderRequest
for an order, call the CancelRequest
method. The method returns null
None
if the order hasn't been cancelled.
var request = ticket.CancelOrderRequest();
request = ticket.CancelOrderRequest()
Order Response
When you update or cancel an order, LEAN returns an OrderReponse
object, which have the following attributes:
If your order changes fail, check the ErrorCode
or ErrorMessage
. For more information about specific order errors, see the Order Response Error Reference.
To get most recent order response, call the GetMostRecentOrderResponse
method.
var response = ticket.GetMostRecentOrderResponse();
response = ticket.GetMostRecentOrderResponse()