Skip to content

Commit 4faea7b

Browse files
committed
Skip zero price and target quantity case
1 parent 6982f67 commit 4faea7b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Common/Algorithm/Framework/Portfolio/SignalExports/Collective2SignalExport.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ private bool ConvertTypeOfSymbol(Symbol targetSymbol, out string typeOfSymbol)
209209
/// <returns>Number of shares hold of the given position</returns>
210210
protected int ConvertPercentageToQuantity(IAlgorithm algorithm, PortfolioTarget target)
211211
{
212+
if (algorithm.Securities[target.Symbol].Price == 0 && target.Quantity == 0)
213+
{
214+
algorithm.Debug($"Warning: The price for {target.Symbol} is 0, and the target quantity is 0. Returning 0 as the calculated quantity.");
215+
return 0;
216+
}
212217
var numberShares = PortfolioTarget.Percent(algorithm, target.Symbol, target.Quantity);
213218
if (numberShares == null)
214219
{

Tests/Algorithm/Framework/Portfolio/SignalExportTargetTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ public void SignalExportManagerGetsCorrectPortfolioTargetArray(SecurityType secu
279279
Assert.AreEqual(quantity, targetQuantity, 1);
280280
}
281281

282+
[Test]
283+
public void SignalExportManagerDoesNotThrowOnZeroPrice()
284+
{
285+
var algorithm = new AlgorithmStub(true);
286+
algorithm.SetDateTime(new DateTime(2024, 02, 16, 11, 53, 30));
287+
288+
var security = algorithm.AddSecurity(Symbols.SPY);
289+
// Set the market price to 0 to simulate the edge case being tested
290+
security.SetMarketPrice(new Tick { Value = 0 });
291+
292+
using var manager = new Collective2SignalExportHandler("", 0);
293+
// Ensure ConvertPercentageToQuantity does not throw when price is 0
294+
Assert.DoesNotThrow(() =>
295+
{
296+
var result = manager.ConvertPercentageToQuantity(algorithm, new PortfolioTarget(Symbols.SPY, 0));
297+
Assert.AreEqual(0, result);
298+
});
299+
}
300+
282301
[Test]
283302
public void SignalExportManagerHandlesIndexOptions()
284303
{

0 commit comments

Comments
 (0)