Files
quantconnect--lean/Tests/Brokerages/BaseOrderTestParameters.cs
T
Roman Yavnikov 3f8fd6ac3d
Syntax Tests / build (push) Has been cancelled
Python Virtual Environments / 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
Build & Test Lean / build (push) Has been cancelled
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Feature: support ComboLimit Order type in TastyTrade and refactor BrokerageTests (#9003)
* feat: support new OrderType in TastytradeBrokerageModel

* feat: extension GetGroupQuantity

* refactor: WaitOneAssertFail to return bool

* refactor: GetOpenOrders in BrokerageTests

* feat: display holdings when begin/end test run

* refactor: brokerageTest class to support handle combo order
feat: comboOrderTestParameters
feat: tastytrade option strategy algo

* refactor: Tastytrade Algo BullCallSpread

* feat: Bull/Bear-CallSpread test cases in BrokerageTests
feat: ComboLimitOrderTestParameters

* fix: remove static in ComboLimitOrderTestParameters of ExpectedStatus

* feat: restrict submit combo limit cross zero orders in Tastytrade
test:feat: submit combo limit cross zero test cases in tastytrade

* test:fix: missed test helper to c9907b23

* refactor: GetGroupQuantityByEachLegQuantity
feat: overload GreatestCommonDivisor
test:feat: GetGroupQuantityByEachLegQuantity

* test:feat: extra test cases to GetGroupQuantity to 925c214a

* test:refactor: take out Cancel Status update in HandleEvents()

* feat: prop Direction in Leg

* Revert "feat: prop Direction in Leg"

This reverts commit 4f92f97b630a157d3972c2321473d87c330c26c8.

* refactor: ComboLimitOrderTestParameters

* test:refactor: CancelOrders

* test:feat: add missed xml description

* test:feat: support ModifyUntilFilled Combo orders

* test:feat: Long/Short-Combo unit tests

* test:fix: compare all status if it is combo order type

* test:refactor: applyOrderUpdate in gracefully way in LimitOrderTestParameters

* remover: Tastytrade algo

* test:feat: create new BaseOrderTestParameters with helper methods
test:rename: method in ComboOrderTestParameters
test:remove: duplication
2025-10-06 23:42:30 +03:00

83 lines
4.3 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 QuantConnect.Orders;
using QuantConnect.Logging;
using System.Collections.Generic;
namespace QuantConnect.Tests.Brokerages
{
public abstract class BaseOrderTestParameters
{
/// <summary>
/// Calculates the adjusted limit price for an order based on its direction
/// and a price adjustment factor, ensuring the price moves toward being filled.
/// </summary>
/// <param name="orderDirection">The direction of the order (Buy or Sell).</param>
/// <param name="previousLimitPrice">The previous limit price of the order.</param>
/// <param name="targetMarketPrice">The target market price used to adjust the limit price.</param>
/// <param name="priceAdjustmentFactor">The factor by which the price is adjusted.</param>
/// <returns>The new, adjusted limit price.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the order direction is not Buy or Sell.</exception>
protected virtual decimal CalculateAdjustedLimitPrice(OrderDirection orderDirection, decimal previousLimitPrice, decimal targetMarketPrice, decimal priceAdjustmentFactor)
{
var adjustmentLimitPrice = orderDirection switch
{
OrderDirection.Buy => Math.Max(previousLimitPrice * priceAdjustmentFactor, targetMarketPrice * priceAdjustmentFactor),
OrderDirection.Sell => Math.Min(previousLimitPrice / priceAdjustmentFactor, targetMarketPrice / priceAdjustmentFactor),
_ => throw new NotSupportedException("Unsupported order direction: " + orderDirection)
};
Log.Trace($"{nameof(CalculateAdjustedLimitPrice)}: {orderDirection} | Prev: {previousLimitPrice}, Target: {targetMarketPrice}, AdjustmentFactor: {priceAdjustmentFactor}, Result: {adjustmentLimitPrice}");
return adjustmentLimitPrice;
}
/// <summary>
/// Rounds the given price to the nearest increment defined by the underlying symbol's minimum price variation.
/// </summary>
/// <param name="price">The original price to round.</param>
/// <param name="minimumPriceVariation">The minimum tick size or price increment for the symbol.</param>
/// <returns>The price rounded to the nearest valid increment.</returns>
protected virtual decimal RoundPrice(decimal price, decimal minimumPriceVariation)
{
var roundOffPlaces = minimumPriceVariation.GetDecimalPlaces();
var roundedPrice = Math.Round(price / roundOffPlaces) * roundOffPlaces;
Log.Trace($"{nameof(BaseOrderTestParameters)}.{nameof(RoundPrice)}: Price = {price}, Minimum Price increment = {minimumPriceVariation}, Rounded price = {roundedPrice}");
return roundedPrice;
}
protected void ApplyUpdateOrderRequests(IReadOnlyCollection<Order> orders, UpdateOrderFields fields)
{
foreach (var order in orders)
{
ApplyUpdateOrderRequest(order, fields);
}
}
protected void ApplyUpdateOrderRequest(Order order, UpdateOrderFields fields)
{
order.ApplyUpdateOrderRequest(new UpdateOrderRequest(DateTime.UtcNow, order.Id, fields));
}
/// <summary>
/// Base class for defining order test parameters.
/// Implement <see cref="ToString"/> to provide a descriptive name
/// for displaying the test case in <c>Visual Studio Test Explorer</c>.
/// </summary>
/// <returns>A string representing the test parameters for display purposes.</returns>
public abstract override string ToString();
}
}