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 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.LIMIT_PRICE)

The OrderField enumeration has the following members:

In addition to using order tickets to track orders, you can receive order events through the OnOrderEventon_order_event 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 TypeUpdatable Properties
TagQuantityLimitPriceTriggerPriceStopPrice
Market Order
Limit Ordergreen checkgreen checkgreen check
Limit If Touched Ordergreen checkgreen checkgreen checkgreen check
Stop Market Ordergreen checkgreen checkgreen check
Stop Limit Ordergreen checkgreen checkgreen checkgreen check
Market On Open Ordergreen checkgreen check
Market On Close Ordergreen checkgreen check

Update Methods

To update an order, pass an UpdateOrderFields object to the Updateupdate 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.limit_order("SPY", 100, 221.05, False, "New SPY trade")

# Update the order tag and limit price
updateSettings = UpdateOrderFields()
updateSettings.limit_price = 222.00
updateSettings.tag = "Limit Price Updated for SPY Trade"
response = ticket.update(updateSettings)

# Check the OrderResponse
if response.is_success:
    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.update_requests()

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 on_data(self, slice: Slice) -> None:
    # Cancel the order
    self.ticket.Cancel()

def on_order_event(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.limit_order("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.is_success:
    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 nullNone if the order hasn't been cancelled.

var request = ticket.cancel_order_request();
request = ticket.cancel_order_request()

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.get_most_recent_order_response();
response = ticket.get_most_recent_order_response()

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: