Order Management

Order Tickets

Introduction

When you create an order, you get an OrderTicket object to manage your order.

Properties

OrderTicket objects have the following attributes:

Track Orders

As the state or 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 = orderTicket.Get(OrderField.LimitPrice);
limit_price = order_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. 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 TypeUpdatable Properties
Market Order
Limit OrderTag, Quantity, LimitPrice
Limit If Touched OrderTag, Quantity, TriggerPrice, LimitPrice
Stop Market OrderTag, Quantity, StopPrice
Stop Limit OrderTag, Quantity, StopPrice, LimitPrice
Market On Open OrderTag, Quantity
Market On Close OrderTag, Quantity

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)

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 = orderTicket.UpdateRequests();
update_requests = order_ticket.UpdateRequests()

Not all brokerages support order updates. To check if your brokerage supports order updates, see the brokerage integration documentation.

Cancel Orders

To cancel an order, call the Cancel method on the OrderTicket. You can't cancel market orders because they transmitted to the brokerage immediately. 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")

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 nullNone if the order hasn't been cancelled.

var request = orderTicket.CancelOrderRequest();
request = order_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 = orderTicket.GetMostRecentOrderResponse();
response = order_ticket.GetMostRecentOrderResponse()

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: