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
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
get_order_ticket
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
last_order_id
property to get the order ID of the most recent order.
var orderId = Transactions.LastOrderId; var ticket = Transactions.GetOrderTicket(orderId);
order_id = self.transactions.last_order_id ticket = self.transactions.get_order_ticket(order_id)
Get Order Tickets
To get order tickets, call the GetOrderTickets
get_order_tickets
or GetOpenOrderTickets
get_open_order_tickets
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.get_order_tickets() # Get order tickets that pass a filter filtered_order_tickets = self.transactions.get_order_tickets(lambda order_ticket: order_ticket.symbol == symbol) # Get all open order tickets open_order_tickets = self.transactions.get_open_order_tickets() # Get all open order tickets for a symbol symbol_open_order_tickets = self.transactions.get_open_order_tickets(symbol) # Get open order tickets that pass a filter filtered_open_order_tickets = self.transactions.get_open_order_tickets(lambda order_ticket: order_ticket.quantity > 10)
Get a Single Order
To get a clone of a specific order, call the GetOrderById
get_order_by_id
method with the order Id. To get the order Id, use the OrderId
order_id
property of the order ticket or use the LastOrderID
property if you want the most recent order.
var orderId = Transactions.LastOrderID; var order = Transactions.GetOrderById(orderId);
order_id = self.transactions.last_order_id order = self.transactions.get_order_by_id(order_id)
Order objects are immutable and changes to the order object will not impact the trade. To make an update to an order you must use Order Tickets.
Order
objects have the following attributes:
Get Orders
To get a list of orders, call the GetOrders
get_orders
, GetOpenOrders
get_open_orders
, or GetOrdersByBrokerageId
get_orders_by_brokerage_id
method. These method returns a list of
Order
objects.
// Get all completed orders var completedOrders = Transactions.GetOrders(); // Get all completed orders that pass a filter var filteredCompletedOrders = Transactions.GetOrders(x => x.Quantity > 10); // Get a list of all completed orders for a symbol var symbolCompletedOrders = Transactions.GetOrders(x => x.Symbol == symbol); // 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 and completed orders that correspond to an Id that the brokerage assigned in live trading var ordersByBrokerageId = Transactions.GetOrdersByBrokerageId(brokerageId);
# Get all completed orders completed_Orders = self.transactions.get_orders() # Get all completed orders that pass a filter filtered_completed_orders = self.transactions.get_orders(lambda x: x.quantity > 10) # Retrieve a list of all completed orders for a symbol symbol_completed_orders = self.transactions.get_orders(lambda x: x.symbol == symbol) # Get all open orders open_orders = self.transactions.get_open_orders() # Get all open orders that pass a filter filtered_open_orders = self.transactions.get_open_orders(lambda x: x.quantity > 10) # Retrieve a list of all open orders for a symbol symbol_open_orders = self.transactions.get_open_orders(symbol) # Get all open and completed orders that correspond to an Id that the brokerage assigned in live trading orders_by_brokerage_id = self.transactions.get_orders_by_brokerage_id(brokerageId)
Order
objects have the following attributes:
The OrdersCount
orders_count
property gets the current number of orders that have been processed.
Get Remaining Order Quantity
To get the unfilled quantity of open orders, call the GetOpenOrdersRemainingQuantity
get_open_orders_remaining_quantity
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.get_open_orders_remaining_quantity() # Get the quantity of open orders that pass a filter filtered_open_quantity = self.transactions.get_open_orders_remaining_quantity( lambda order_ticket: order_ticket.quantity > 10 ) # Get the quantity of open orders for a symbol symbol_open_quantity = self.transactions.get_open_orders_remaining_quantity(symbol)
Cancel Orders
To cancel open orders, call the CancelOpenOrders
cancel_open_orders
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.cancel_open_orders() # Cancel orders related to IBM and apply a tag ibm_cancelled_orders = self.transactions.cancel_open_orders("IBM", "Hit stop price")