Skip to content

Commit

Permalink
Addressed new comments review
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueNina committed Jan 21, 2025
1 parent 9bbba46 commit 2fc8f99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
15 changes: 12 additions & 3 deletions Algorithm.CSharp/LiquidateRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,21 @@ public virtual void Rebalance()
throw new RegressionTestException("There should be an open order for SPY.");
}
// Liquidate SPY orders and verify cancellation
SetHoldings(_spy, 1, true);
var orderProperties = new OrderProperties { TimeInForce = TimeInForce.GoodTilCanceled };
SetHoldings(_spy, 1, true, "LiquidatedTest", orderProperties);
var spyCancelOrder = Transactions.GetOrderById(spyOrder.Id);
if (spyCancelOrder.Status != OrderStatus.Canceled)
{
throw new RegressionTestException("The SPY order should be cancelled.");
}
if (spyCancelOrder.Tag != "LiquidatedTest")
{
throw new RegressionTestException("The SPY order should have the tag LiquidatedTest.");
}
if (spyCancelOrder.Properties.TimeInForce != TimeInForce.GoodTilCanceled)
{
throw new RegressionTestException("The SPY order should have the TimeInForce set to GoodTilCanceled.");
}

// Liquidate all remaining holdings immediately
PerformLiquidation();
Expand Down Expand Up @@ -125,7 +134,7 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
public virtual Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "8"},
{"Average Win", "0%"},
Expand Down Expand Up @@ -153,7 +162,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "f03dec5648d761ca660e0ea51403aa92"}
{"OrderListHash", "8d47e98571918500e95df416bfe21fdf"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/

using System.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp.RegressionTests
{
Expand All @@ -25,7 +27,50 @@ public class LiquidateUsingSetHoldingsRegressionAlgorithm : LiquidateRegressionA
{
public override void PerformLiquidation()
{
SetHoldings(new List<PortfolioTarget>(), true);
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", "8"},
{"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", "25c4614305b6849ddec931fdf453af55"}
};
}
}
6 changes: 4 additions & 2 deletions Algorithm/QCAlgorithm.Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,8 @@ public void SetHoldings(List<PortfolioTarget> targets, bool liquidateExistingHol
//If they triggered a liquidate
if (liquidateExistingHoldings)
{
Liquidate();
var liquidateTag = string.IsNullOrWhiteSpace(tag) ? "Liquidated" : tag;
Liquidate(tag: liquidateTag, orderProperties: orderProperties);
}

foreach (var portfolioTarget in targets
Expand Down Expand Up @@ -1415,7 +1416,8 @@ private void SetHoldingsImpl(Symbol symbol, decimal orderQuantity, bool liquidat
//If they triggered a liquidate
if (liquidateExistingHoldings)
{
Liquidate(symbol);
var liquidateTag = string.IsNullOrWhiteSpace(tag) ? "Liquidated" : tag;
Liquidate(symbol, tag: liquidateTag, orderProperties: orderProperties);
}

//Calculate total unfilled quantity for open market orders
Expand Down

0 comments on commit 2fc8f99

Please sign in to comment.