Files
quantconnect--lean/Common/Securities/IOrderProvider.cs
T
Jhonathan Abreu 546afd2a61
Syntax Tests / build (push) Has been cancelled
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Async orders in execution models (#8872)
* Changed default async to true and passed target.Tag

* Make execution models place orders asynchronously if specified

* Add unit tests

* Execution model default to asynchronous orders.

Also, minor fixes for tickets remaining fill quantity potential race conditions

* Add SecurityHolding.UnrealizedQuantity property

It gets the holding quantity the security will have once all open orders are filled.
Added for thread safety reasons when execution models place asynchronous orders and need to calculate the actual quantity needed to reach the target of there are open orders

* Some cleanup

* Adjust projected holdings quantity on splits

* Minor fix

* More changes and cleanup

* Minor fix

* Improvements for thread safety

* Add IOrderProvider.GetProjectedHoldings to get projected holdings atomically

* Minor unit tests fix

* Add ProjectedHoldings DTO class

* Address peer review

---------

Co-authored-by: arthiondaena <arthiondaena@gmail.com>
2025-07-21 13:11:03 -04:00

122 lines
5.7 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using QuantConnect.Orders;
namespace QuantConnect.Securities
{
/// <summary>
/// Represents a type capable of fetching Order instances by its QC order id or by a brokerage id
/// </summary>
public interface IOrderProvider
{
/// <summary>
/// Gets the current number of orders that have been processed
/// </summary>
int OrdersCount { get; }
/// <summary>
/// Get the order by its id
/// </summary>
/// <param name="orderId">Order id to fetch</param>
/// <returns>A clone of the order with the specified id, or null if no match is found</returns>
Order GetOrderById(int orderId);
/// <summary>
/// Gets the Lean orders by its brokerage id
/// </summary>
/// <param name="brokerageId">The brokerage id to fetch</param>
/// <returns>The orders matching the brokerage id, or null if no match is found</returns>
List<Order> GetOrdersByBrokerageId(string brokerageId);
/// <summary>
/// Gets and enumerable of <see cref="OrderTicket"/> matching the specified <paramref name="filter"/>
/// </summary>
/// <param name="filter">The filter predicate used to find the required order tickets. If null is specified then all tickets are returned</param>
/// <returns>An enumerable of <see cref="OrderTicket"/> matching the specified <paramref name="filter"/></returns>
IEnumerable<OrderTicket> GetOrderTickets(Func<OrderTicket, bool> filter = null);
/// <summary>
/// Gets and enumerable of opened <see cref="OrderTicket"/> matching the specified <paramref name="filter"/>
/// </summary>
/// <param name="filter">The filter predicate used to find the required order tickets. If null is specified then all tickets are returned</param>
/// <returns>An enumerable of opened <see cref="OrderTicket"/> matching the specified <paramref name="filter"/></returns>
IEnumerable<OrderTicket> GetOpenOrderTickets(Func<OrderTicket, bool> filter = null);
/// <summary>
/// Gets the order ticket for the specified order id. Returns null if not found
/// </summary>
/// <param name="orderId">The order's id</param>
/// <returns>The order ticket with the specified id, or null if not found</returns>
OrderTicket GetOrderTicket(int orderId);
/// <summary>
/// Gets all orders matching the specified filter. Specifying null will return an enumerable
/// of all orders.
/// </summary>
/// <param name="filter">Delegate used to filter the orders</param>
/// <returns>All orders this order provider currently holds by the specified filter</returns>
IEnumerable<Order> GetOrders(Func<Order, bool> filter = null);
/// <summary>
/// Gets open orders matching the specified filter. Specifying null will return an enumerable
/// of all open orders.
/// </summary>
/// <param name="filter">Delegate used to filter the orders</param>
/// <returns>All filtered open orders this order provider currently holds</returns>
List<Order> GetOpenOrders(Func<Order, bool> filter = null);
/// <summary>
/// Calculates the projected holdings for the specified security based on the current open orders.
/// </summary>
/// <param name="security">The security</param>
/// <returns>
/// The projected holdings for the specified security, which is the sum of the current holdings
/// plus the sum of the open orders quantity.
/// </returns>
ProjectedHoldings GetProjectedHoldings(Security security);
}
/// <summary>
/// Provides extension methods for the <see cref="IOrderProvider"/> interface
/// </summary>
public static class OrderProviderExtensions
{
/// <summary>
/// Gets the order by its brokerage id
/// </summary>
/// <param name="orderProvider">The order provider to search</param>
/// <param name="brokerageId">The brokerage id to fetch</param>
/// <returns>The first order matching the brokerage id, or null if no match is found</returns>
public static List<Order> GetOrdersByBrokerageId(this IOrderProvider orderProvider, long brokerageId)
{
return orderProvider.GetOrdersByBrokerageId(brokerageId.ToStringInvariant());
}
/// <summary>
/// Gets the order by its brokerage id
/// </summary>
/// <param name="orderProvider">The order provider to search</param>
/// <param name="brokerageId">The brokerage id to fetch</param>
/// <returns>The first order matching the brokerage id, or null if no match is found</returns>
public static List<Order> GetOrdersByBrokerageId(this IOrderProvider orderProvider, int brokerageId)
{
return orderProvider.GetOrdersByBrokerageId(brokerageId.ToStringInvariant());
}
}
}