Lean
Lean copied to clipboard
Exposure Series Does Show Zero When Portfolio is Not Invested
Expected Behavior
When the Portfolio doesn't hold any positions, the Exposure series should show the ratio of 0.
Actual Behavior
When the Portfolio doesn't hold any positions, the series is not updated, resulting in the following chart:

Potential Solution
Review SampleExposure method. The method only accounts for securities that hold stock:
foreach (var holding in Algorithm.Portfolio.Values.Where(x => x.HoldStock))
{
// ...
}
so if that enumerable is empty, there is no new sample.
Reproducing the Problem
class SleepyBlueWolf(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("SPY", Resolution.Hour)
def OnData(self, data: Slice):
if self.Time.month == 6:
self.Liquidate()
return
if not self.Portfolio.Invested:
self.SetHoldings("SPY", 1)
def OnEndOfDay(self, symbol):
self.Plot('Holding', symbol, int(self.Portfolio[symbol].Invested))
Checklist
- [x] I have completely filled out this template
- [x] I have confirmed that this issue exists on the current
masterbranch - [x] I have confirmed that this is not a duplicate issue by searching issues
- [x] I have provided detailed steps to reproduce the issue