Skip to content

Commit

Permalink
Second approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Nov 6, 2024
1 parent c384d88 commit ce025f7
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions Engine/Results/BaseResultsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public abstract class BaseResultsHandler
// used for resetting out/error upon completion
private static readonly TextWriter StandardOut = Console.Out;
private static readonly TextWriter StandardError = Console.Error;
private static readonly HashSet<SecurityType> _shortHoldingsSecurityTypes;
private static readonly HashSet<SecurityType> _longHoldingsSecurityTypes;

private string _hostName;

Expand Down Expand Up @@ -743,21 +745,17 @@ protected virtual void SampleExposure(DateTime time, decimal currentPortfolioVal

// Split up our holdings in one enumeration into long and shorts holding values
// only process those that we hold stock in.
var shortHoldings = new Dictionary<SecurityType, decimal>();
var longHoldings = new Dictionary<SecurityType, decimal>();
foreach (var security in Algorithm.Portfolio.Securities.Values)
var shortHoldings = _shortHoldingsSecurityTypes.ToDictionary(k => k, v => 0m);
var longHoldings = _shortHoldingsSecurityTypes.ToDictionary(k => k, v => 0m);
foreach (var holding in Algorithm.Portfolio.Values)
{
if (security.Symbol.SecurityType == SecurityType.Future && ((Future)security).Expiry < Algorithm.Time)
{
security.Holdings.SetHoldings(security.Holdings.AveragePrice, 0);
}

var holding = security.Holdings;
// Ensure we have a value for this security type in both our dictionaries
if (!longHoldings.ContainsKey(holding.Symbol.SecurityType))
{
longHoldings.Add(holding.Symbol.SecurityType, 0);
shortHoldings.Add(holding.Symbol.SecurityType, 0);
_longHoldingsSecurityTypes.Add(holding.Symbol.SecurityType);
_shortHoldingsSecurityTypes.Add(holding.Symbol.SecurityType);
}

var holdingsValue = holding.HoldingsValue;
Expand Down

0 comments on commit ce025f7

Please sign in to comment.