Skip to content

Commit bd8ebcd

Browse files
committed
Addressed review comments
1 parent 80b0647 commit bd8ebcd

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

Algorithm.CSharp/LiquidateRegressionAlgorithm.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
116
using System;
217
using System.Collections.Generic;
318
using System.Linq;
4-
using QuantConnect.Algorithm.Framework.Portfolio;
5-
using QuantConnect.Data;
619
using QuantConnect.Interfaces;
720
using QuantConnect.Orders;
821

922
namespace QuantConnect.Algorithm.CSharp
1023
{
24+
/// <summary>
25+
/// A regression test algorithm that places market and limit orders, then liquidates all holdings,
26+
/// ensuring orders are canceled and the portfolio is empty.
27+
/// </summary>
1128
public class LiquidateRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
1229
{
1330
protected Symbol _spy;
1431
protected Symbol _ibm;
1532
public override void Initialize()
1633
{
17-
SetStartDate(2018, 1, 5);
34+
SetStartDate(2018, 1, 4);
1835
SetEndDate(2018, 1, 10);
1936
SetCash(100000);
2037
_spy = AddEquity("SPY", Resolution.Daily).Symbol;
2138
_ibm = Symbol("IBM R735QTJ8XC9X");
22-
var security = AddSecurity(_ibm, Resolution.Daily);
39+
AddSecurity(_ibm, Resolution.Daily);
2340

2441
// Schedule Rebalance method to be called on specific dates
2542
Schedule.On(DateRules.On(2018, 1, 5), TimeRules.Midnight, Rebalance);
@@ -33,9 +50,26 @@ public virtual void Rebalance()
3350

3451
// Place a LimitOrder to sell 1 share at a price below the current market price
3552
LimitOrder(_ibm, 1, Securities[_ibm].Price - 5);
53+
54+
55+
3656
LimitOrder(_spy, 1, Securities[_spy].Price - 5);
57+
var spyOrder = Transactions.GetOpenOrders(_spy).FirstOrDefault();
58+
// Ensure there is an open order for SPY
59+
if (spyOrder == null)
60+
{
61+
throw new RegressionTestException("There should be an open order for SPY.");
62+
}
63+
64+
// Liquidate SPY orders and verify cancellation
65+
SetHoldings(_spy, 1, true);
66+
var spyCancelOrder = Transactions.GetOrderById(spyOrder.Id);
67+
if (spyCancelOrder.Status != OrderStatus.Canceled)
68+
{
69+
throw new RegressionTestException("The SPY order should be cancelled.");
70+
}
3771

38-
// Liquidate all holdings immediately
72+
// Liquidate all remaining holdings immediately
3973
PerformLiquidation();
4074
}
4175

@@ -48,10 +82,10 @@ public override void OnEndOfAlgorithm()
4882
{
4983
// Check if there are any orders that should have been canceled
5084
var orders = Transactions.GetOrders().ToList();
51-
var cnt = orders.Where(e => e.Status != OrderStatus.Canceled).Count();
52-
if (cnt > 0)
85+
var nonCanceledOrdersCount = orders.Where(e => e.Status != OrderStatus.Canceled).Count();
86+
if (nonCanceledOrdersCount > 0)
5387
{
54-
throw new RegressionTestException($"There are {cnt} orders that should have been cancelled");
88+
throw new RegressionTestException($"There are {nonCanceledOrdersCount} orders that should have been cancelled");
5589
}
5690

5791
// Check if there are any holdings left in the portfolio
@@ -61,7 +95,7 @@ public override void OnEndOfAlgorithm()
6195
var holdings = kvp.Value;
6296
if (holdings.Quantity != 0)
6397
{
64-
throw new RegressionTestException("There are holdings in portfolio");
98+
throw new RegressionTestException($"There are {holdings.Quantity} holdings of {symbol} in the portfolio");
6599
}
66100
}
67101
}
@@ -84,7 +118,7 @@ public override void OnEndOfAlgorithm()
84118
/// <summary>
85119
/// Data Points count of all timeslices of algorithm
86120
/// </summary>
87-
public long DataPoints => 44;
121+
public long DataPoints => 53;
88122

89123
/// <summary>
90124
/// Data Points count of the algorithm history
@@ -96,7 +130,7 @@ public override void OnEndOfAlgorithm()
96130
/// </summary>
97131
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
98132
{
99-
{"Total Orders", "3"},
133+
{"Total Orders", "8"},
100134
{"Average Win", "0%"},
101135
{"Average Loss", "0%"},
102136
{"Compounding Annual Return", "0%"},
@@ -115,14 +149,14 @@ public override void OnEndOfAlgorithm()
115149
{"Beta", "0"},
116150
{"Annual Standard Deviation", "0"},
117151
{"Annual Variance", "0"},
118-
{"Information Ratio", "-5.634"},
119-
{"Tracking Error", "0.024"},
152+
{"Information Ratio", "-10.398"},
153+
{"Tracking Error", "0.045"},
120154
{"Treynor Ratio", "0"},
121155
{"Total Fees", "$0.00"},
122156
{"Estimated Strategy Capacity", "$0"},
123157
{"Lowest Capacity Asset", ""},
124158
{"Portfolio Turnover", "0%"},
125-
{"OrderListHash", "3dc667d309559a7df141959a22aef64c"}
159+
{"OrderListHash", "f03dec5648d761ca660e0ea51403aa92"}
126160
};
127161
}
128162
}

Algorithm.CSharp/RegressionTests/LiquidateUsingSetHoldingsRegressionAlgorithm.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
using System;
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
216
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
517
using QuantConnect.Algorithm.Framework.Portfolio;
618

719
namespace QuantConnect.Algorithm.CSharp.RegressionTests
820
{
21+
/// <summary>
22+
/// A regression test algorithm that uses SetHoldings to liquidate the portfolio by setting holdings to zero.
23+
/// </summary>
924
public class LiquidateUsingSetHoldingsRegressionAlgorithm : LiquidateRegressionAlgorithm
1025
{
1126
public override void PerformLiquidation()

Algorithm/QCAlgorithm.Trading.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ private void SetHoldingsImpl(Symbol symbol, decimal orderQuantity, bool liquidat
14151415
//If they triggered a liquidate
14161416
if (liquidateExistingHoldings)
14171417
{
1418-
Liquidate();
1418+
Liquidate(symbol);
14191419
}
14201420

14211421
//Calculate total unfilled quantity for open market orders

0 commit comments

Comments
 (0)