Order Management
Transaction Manager
Introduction
The algorithm transactions manager (SecurityTransactionManager
) contains a collection of helper methods to quickly access all your orders. To access the transaction manager, use the Transactions
property of your algorithm. If you save a reference to your order tickets, you shouldn't need to use the transaction manager. LEAN updates the order tickets as the brokerage processes your orders.
Get a Single Order Ticket
If you didn't save a reference to the order ticket when you created an order, you can call the GetOrderTicket
method to get it. You need to pass the order ID to the method. If you don't have the order ID, you can use the LastOrderID
property to get the order ID of the most recent order.
var orderId = Transactions.LastOrderID; var ticket = Transactions.GetOrderTicket(orderId);
order_id = self.Transactions.LastOrderID ticket = self.Transactions.GetOrderTicket(order_id)
Get Order Tickets
To get order tickets, call the GetOrderTickets
or GetOpenOrderTickets
method. You can pass in a filter function to filter all of the order tickets or pass a Symbol
to get the order tickets for a specific asset.
// Get all order tickets var orderTickets = Transactions.GetOrderTickets(); // Get order tickets that pass a filter var filteredOrderTickets = Transactions.GetOrderTickets(orderTicket => orderTicket.Symbol == symbol); // Get all open order tickets var openOrderTickets = Transactions.GetOpenOrderTickets(); // Get all open order tickets for a symbol var symbolOpenOrderTickets = Transactions.GetOpenOrderTickets(symbol); // Get open order tickets that pass a filter var filteredOpenOrderTickets = Transactions.GetOpenOrderTickets(orderTicket => orderTicket.Quantity > 10);
# Get all order tickets order_tickets = self.Transactions.GetOrderTickets() # Get order tickets that pass a filter filtered_order_tickets = self.Transactions.GetOrderTickets(lambda order_ticket: order_ticket.Symbol == symbol) # Get all open order tickets open_order_tickets = self.Transactions.GetOpenOrderTickets() # Get all open order tickets for a symbol symbol_open_order_tickets = self.Transactions.GetOpenOrderTickets(symbol) # Get open order tickets that pass a filter filtered_open_order_tickets = self.Transactions.GetOpenOrderTickets(lambda order_ticket: order_ticket.Quantity > 10)
Get Open Orders
To get a list of open orders, call the GetOpenOrders
method. This method returns a list of
Order
objects.
// Get all open orders var openOrders = Transactions.GetOpenOrders(); // Get all open orders that pass a filter var filteredOpenOrders = Transactions.GetOpenOrders(x => x.Quantity > 10); // Get a list of all open orders for a symbol var symbolOpenOrders = Transactions.GetOpenOrders(symbol);
# Get all open orders all_open_orders = self.Transactions.GetOpenOrders() # Get all open orders that pass a filter filtered_open_orders = self.Transactions.GetOpenOrders(lambda x: x.Quantity > 10) # Retrieve a list of all open orders for a symbol symbol_open_orders = self.Transactions.GetOpenOrders(symbol)
Order
objects have the following attributes:
Get Remaining Order Quantity
To get the unfilled quantity of open orders, call the GetOpenOrdersRemainingQuantity
method.
// Get the quantity of all open orders var allOpenQuantity = Transactions.GetOpenOrdersRemainingQuantity(); // Get the quantity of open orders that pass a filter var filteredOpenQuantity = Transactions.GetOpenOrdersRemainingQuantity( orderTicket => orderTicket.Quantity > 10 ); // Get the quantity of open orders for a symbol var symbolOpenQuantity = Transactions.GetOpenOrdersRemainingQuantity(symbol);
# Get the quantity of all open orders all_open_quantity = self.Transactions.GetOpenOrdersRemainingQuantity() # Get the quantity of open orders that pass a filter filtered_open_quantity = self.Transactions.GetOpenOrdersRemainingQuantity( lambda order_ticket: order_ticket.Quantity > 10 ) # Get the quantity of open orders for a symbol symbol_open_quantity = self.Transactions.GetOpenOrdersRemainingQuantity(symbol)
Cancel Orders
To cancel open orders, call the CancelOpenOrders
method. This method returns a list of OrderTicket
objects that correspond to the canceled orders.
// Cancel all open orders var allCancelledOrders = Transactions.CancelOpenOrders(); // Cancel orders related to IBM and apply a tag var ibmCancelledOrders = Transactions.CancelOpenOrders("IBM", "Hit stop price");
# Cancel all open orders all_cancelled_orders = self.Transactions.CancelOpenOrders() # Cancel orders related to IBM and apply a tag ibm_cancelled_orders = self.Transactions.CancelOpenOrders("IBM", "Hit stop price")