/// <summary> /// Get the maximum market position group order quantity to obtain a delta in the buying power used by a position group. /// The deltas sign defines the position side to apply it to, positive long, negative short. /// </summary> /// <param name="parameters">An object containing the portfolio, the position group and the delta buying power</param> /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns> /// <remarks>Used by the margin call model to reduce the position by a delta percent.</remarks> public override GetMaximumLotsResult GetMaximumLotsForDeltaBuyingPower( GetMaximumLotsForDeltaBuyingPowerParameters parameters ) { if (parameters.PositionGroup.Count != 1) { return(parameters.Error( $"{nameof(SecurityPositionGroupBuyingPowerModel)} only supports position groups containing exactly one position." )); } var position = parameters.PositionGroup.Single(); var security = parameters.Portfolio.Securities[position.Symbol]; var result = security.BuyingPowerModel.GetMaximumOrderQuantityForDeltaBuyingPower( new GetMaximumOrderQuantityForDeltaBuyingPowerParameters( parameters.Portfolio, security, parameters.DeltaBuyingPower, parameters.MinimumOrderMarginPortfolioPercentage ) ); var quantity = result.Quantity * security.SymbolProperties.LotSize; return(new GetMaximumLotsResult(quantity, result.Reason, result.IsError)); }
/// <summary> /// Get the maximum market position group order quantity to obtain a delta in the buying power used by a position group. /// The deltas sign defines the position side to apply it to, positive long, negative short. /// </summary> /// <param name="parameters">An object containing the portfolio, the position group and the delta buying power</param> /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns> /// <remarks>Used by the margin call model to reduce the position by a delta percent.</remarks> public virtual GetMaximumLotsResult GetMaximumLotsForDeltaBuyingPower( GetMaximumLotsForDeltaBuyingPowerParameters parameters ) { // we convert this delta request into a target buying power request through projection // by determining the currently used (reserved) buying power and adding the delta to // arrive at a target buying power percentage var currentPositionGroup = parameters.Portfolio.Positions[parameters.PositionGroup.Key]; var usedBuyingPower = parameters.PositionGroup.BuyingPowerModel.GetReservedBuyingPowerForPositionGroup( parameters.Portfolio, currentPositionGroup ); var signedUsedBuyingPower = Math.Sign(currentPositionGroup.Quantity) * usedBuyingPower; var targetBuyingPower = signedUsedBuyingPower + parameters.DeltaBuyingPower; var targetBuyingPowerPercent = parameters.Portfolio.TotalPortfolioValue != 0 ? targetBuyingPower / parameters.Portfolio.TotalPortfolioValue : 0; return(GetMaximumLotsForTargetBuyingPower(new GetMaximumLotsForTargetBuyingPowerParameters( parameters.Portfolio, parameters.PositionGroup, targetBuyingPowerPercent, parameters.MinimumOrderMarginPortfolioPercentage ))); }