18 using System.Collections.Generic;
33 private List<ISignalExportTarget> _signalExports;
43 private bool _isLiveWarningModeLog;
52 _algorithm = algorithm;
53 _isLiveWarningModeLog =
false;
62 _signalExports ??=
new List<ISignalExportTarget>();
64 _signalExports.AddRange(signalExports);
92 var totalPortfolioValue = _algorithm.Portfolio.TotalPortfolioValue;
93 if (totalPortfolioValue <= 0)
95 _algorithm.Error(
"Total portfolio value was less than or equal to 0");
112 if (!_algorithm.LiveMode)
114 if (!_isLiveWarningModeLog)
116 _algorithm.Debug(
"Portfolio targets are only sent in live mode");
117 _isLiveWarningModeLog =
true;
123 if (portfolioTargets ==
null || portfolioTargets.Length == 0)
125 _algorithm.Debug(
"No portfolio target given");
129 var targets =
new List<PortfolioTarget>(portfolioTargets);
133 Algorithm = _algorithm
137 foreach (var signalExport
in _signalExports)
139 result &= signalExport.Send(signalExportTargetParameters);
147 foreach (var holding
in _algorithm.Portfolio.Values)
149 var security = _algorithm.Securities[holding.Symbol];
154 if (!security.IsTradable && !security.Symbol.IsCanonical())
160 var adjustedPercent = Math.Abs(security.BuyingPowerModel.GetInitialMarginRequirement(marginParameters) / totalPortfolioValue);
164 var holdingPercent = adjustedPercent * security.BuyingPowerModel.GetLeverage(security);
168 var adjustedHoldingPercent = (holdingPercent * totalPortfolioValue) / _algorithm.Portfolio.TotalPortfolioValueLessFreeBuffer;
169 if (holding.Quantity < 0)
171 adjustedHoldingPercent *= -1;
174 yield
return new PortfolioTarget(holding.Symbol, adjustedHoldingPercent);