Skip to content

Commit

Permalink
Improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Dec 20, 2024
1 parent a088af8 commit 8cd03e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
16 changes: 13 additions & 3 deletions Algorithm.CSharp/IndexSecurityCanBeTradableRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ public virtual void SetUpIndexSecurity()
public override void OnData(Slice slice)
{
_signalExportManagerTest.GetPortfolioTargetsFromPortfolio(out PortfolioTarget[] targets);

if (targets.Where(x => x.Symbol.SecurityType == SecurityType.Index).Any())
if (Securities[_index].IsTradable)
{
throw new RegressionTestException($"Index is not a tradable security, so no portfolio target with index security type should have been created");
if (!targets.Where(x => x.Symbol.SecurityType == SecurityType.Index).Any())
{
throw new RegressionTestException($"Index {_index} is marked as tradable security, but no portfolio target with index security type was created");
}
}
else
{
if (targets.Where(x => x.Symbol.SecurityType == SecurityType.Index).Any())
{
throw new RegressionTestException($"Index is not a tradable security, so no portfolio target with index security type should have been created");
}
}

if (!Portfolio.Invested)
{
SetHoldings(_equity, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ private IEnumerable<PortfolioTarget> GetPortfolioTargets(decimal totalPortfolioV
{
continue;
}
else if ((security is Securities.Index.Index) && Securities.Index.Index.ManualSetIsTradable)
{
continue;
}

var marginParameters = MaintenanceMarginParameters.ForQuantityAtCurrentPrice(security, holding.Quantity);
var adjustedPercent = security.BuyingPowerModel.GetMaintenanceMargin(marginParameters) / totalPortfolioValue;
Expand Down
9 changes: 3 additions & 6 deletions Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3824,14 +3824,11 @@ public static void ProcessSecurityChanges(this IAlgorithm algorithm, SecurityCha
{
foreach (var security in securityChanges.AddedSecurities)
{
if (!(security is Securities.Index.Index))
if (security.Type == SecurityType.Index && !Securities.Index.Index.ManualSetIsTradable)
{
security.IsTradable = true;
}
else if (security.IsTradable)
{
Securities.Index.Index.ManualSetIsTradable = true;
continue;
}
security.IsTradable = true;

// uses TryAdd, so don't need to worry about duplicates here
algorithm.Securities.Add(security);
Expand Down
14 changes: 14 additions & 0 deletions Common/Securities/Index/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ namespace QuantConnect.Securities.Index
/// <seealso cref="Security"/>
public class Index : Security
{
private bool _isTradable;

/// <summary>
/// Gets or sets whether or not this security should be considered tradable
/// </summary>
public override bool IsTradable {
get => _isTradable;
set
{
if (value) ManualSetIsTradable = true;
_isTradable = value;
}
}

/// <summary>
/// Field to check if the user has manually set IsTradable field to true
/// </summary>
Expand Down

0 comments on commit 8cd03e0

Please sign in to comment.