Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor liquidation logic #8544

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 System.Linq;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp.RegressionTests
{
/// <summary>
/// Tests liquidating all portfolio holdings except a specific symbol, verifying canceled orders and correct tags.
/// </summary>
public class LiquidateAllExceptSpecifiedSymbolRegressionAlgorithm : LiquidateRegressionAlgorithm
{
public override void Rebalance()
{
// Place a MarketOrder
MarketOrder(Ibm, 10);

// Place a LimitOrder to sell 1 share at a price below the current market price
LimitOrder(Ibm, 1, Securities[Ibm].Price - 5);

// Liquidate the remaining symbols in the portfolio, except for SPY
var orderProperties = new OrderProperties { TimeInForce = TimeInForce.GoodTilCanceled };
SetHoldings(Spy, 1, true, "LiquidatedTest", orderProperties);
}

public override void OnEndOfAlgorithm()
{
// Retrieve all orders from the Transactions for analysis
var orders = Transactions.GetOrders().ToList();

// Count orders that were canceled
var canceledOrdersCount = orders.Where(order => order.Status == OrderStatus.Canceled).Count();

// Expectation 1: There should be exactly 4 canceled orders.
// This occurs because Rebalance is called twice, and each call to Rebalance
// (e.g., LimitOrder or MarketOrder) that get canceled due to the Liquidate call in SetHoldings.
if (canceledOrdersCount != 4)
{
throw new RegressionTestException($"Expected 4 canceled orders, but found {canceledOrdersCount}.");
}

// Count orders that were not canceled
var nonCanceledOrdersCount = orders.Where(order => order.Status != OrderStatus.Canceled).Count();

// Expectation 2: There should be exactly 1 non-canceled order after the Liquidate call.
// This occurs because all holdings except SPY are liquidated, and a new order is placed for SPY.
if (nonCanceledOrdersCount != 1)
{
throw new RegressionTestException($"Expected 1 non-canceled order, but found {nonCanceledOrdersCount}.");
}

// Verify all tags are "LiquidatedTest"
var invalidTags = orders.Where(order => order.Tag != "LiquidatedTest").ToList();
if (invalidTags.Count != 0)
{
var invalidTagsDetails = string.Join(", ", invalidTags.Select(order => $"OrderID {order.Id}, Tag: {order.Tag}"));
throw new RegressionTestException($"All orders should have the tag 'LiquidatedTest', but found invalid tags: {invalidTagsDetails}.");
}
}

public override Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "5"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "36.497%"},
{"Drawdown", "0.200%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "100569.90"},
{"Net Profit", "0.570%"},
{"Sharpe Ratio", "9.031"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "86.638%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "-0.003"},
{"Beta", "0.559"},
{"Annual Standard Deviation", "0.028"},
{"Annual Variance", "0.001"},
{"Information Ratio", "-8.867"},
{"Tracking Error", "0.023"},
{"Treynor Ratio", "0.447"},
{"Total Fees", "$1.95"},
{"Estimated Strategy Capacity", "$850000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "14.23%"},
{"OrderListHash", "611f320cf76c36e8cdcb1938e4154682"}
};
}
}
144 changes: 144 additions & 0 deletions Algorithm.CSharp/LiquidateRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* 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 System.Linq;
using QuantConnect.Interfaces;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// A regression test algorithm that places market and limit orders, then liquidates all holdings,
/// ensuring orders are canceled and the portfolio is empty.
/// </summary>
public class LiquidateRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
protected Symbol Spy { get; private set; }
protected Symbol Ibm { get; private set; }
public override void Initialize()
{
SetStartDate(2018, 1, 4);
SetEndDate(2018, 1, 10);
Spy = AddEquity("SPY", Resolution.Daily).Symbol;
Ibm = AddEquity("IBM", Resolution.Daily).Symbol;

// Schedule Rebalance method to be called on specific dates
Schedule.On(DateRules.On(2018, 1, 5), TimeRules.Midnight, Rebalance);
Schedule.On(DateRules.On(2018, 1, 8), TimeRules.Midnight, Rebalance);
}

public virtual void Rebalance()
{
// Place a MarketOrder
MarketOrder(Ibm, 10);

// Place a LimitOrder to sell 1 share at a price below the current market price
LimitOrder(Ibm, 1, Securities[Ibm].Price - 5);

LimitOrder(Spy, 1, Securities[Spy].Price - 5);

// Liquidate all remaining holdings immediately
PerformLiquidation();
}

public virtual void PerformLiquidation()
{
Liquidate();
}

public override void OnEndOfAlgorithm()
{
// Check if there are any orders that should have been canceled
var orders = Transactions.GetOrders().ToList();
var nonCanceledOrdersCount = orders.Where(e => e.Status != OrderStatus.Canceled).Count();
if (nonCanceledOrdersCount > 0)
{
throw new RegressionTestException($"There are {nonCanceledOrdersCount} orders that should have been cancelled");
}

// Check if there are any holdings left in the portfolio
foreach (var kvp in Portfolio)
{
var symbol = kvp.Key;
var holdings = kvp.Value;
if (holdings.Quantity != 0)
{
throw new RegressionTestException($"There are {holdings.Quantity} holdings of {symbol} in the portfolio");
}
}
}

/// <summary>
/// Final status of the algorithm
/// </summary>
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public List<Language> Languages { get; } = new() { Language.CSharp };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 53;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 0;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public virtual Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "6"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "100000"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "-10.398"},
{"Tracking Error", "0.045"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "9423c872a626fb856b7c377686c28d85"}
};
}
}
76 changes: 76 additions & 0 deletions Algorithm.CSharp/LiquidateUsingSetHoldingsRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp.RegressionTests
{
/// <summary>
/// A regression test algorithm that uses SetHoldings to liquidate the portfolio by setting holdings to zero.
/// </summary>
public class LiquidateUsingSetHoldingsRegressionAlgorithm : LiquidateRegressionAlgorithm
{
public override void PerformLiquidation()
{
var properties = new OrderProperties { TimeInForce = TimeInForce.GoodTilCanceled };
SetHoldings(new List<PortfolioTarget>(), true, "LiquidatedTest", properties);
var orders = Transactions.GetOrders().ToList();
var orderTags = orders.Where(e => e.Tag == "LiquidatedTest").ToList();
if (orderTags.Count != orders.Count)
{
throw new RegressionTestException("The tag was not set on all orders");
}
var orderProperties = orders.Where(e => e.Properties.TimeInForce == TimeInForce.GoodTilCanceled).ToList();
if (orderProperties.Count != orders.Count)
{
throw new RegressionTestException("The properties were not set on all orders");
}
}

public override Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "6"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "100000"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "-10.398"},
{"Tracking Error", "0.045"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "2cdbee112f22755f26f640c97c305aae"}
};
}
}
Loading
Loading