Beispiel #1
0
            public override GetMaximumOrderQuantityResult GetMaximumOrderQuantityForTargetBuyingPower(
                GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters)
            {
                var quantity = base.GetMaximumOrderQuantityForTargetBuyingPower(parameters).Quantity;

                quantity = Math.Floor(quantity / 100) * 100;
                return(new GetMaximumOrderQuantityResult(quantity));
            }
Beispiel #2
0
 /// <summary>
 /// Get the maximum market order quantity to obtain a position with a given buying power percentage.
 /// Will not take into account free buying power.
 /// </summary>
 /// <param name="parameters">An object containing the portfolio, the security and the target signed buying power percentage</param>
 /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns>
 public override GetMaximumOrderQuantityResult GetMaximumOrderQuantityForTargetBuyingPower(
     GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters)
 {
     if (Math.Abs(parameters.TargetBuyingPower) > 1)
     {
         throw new InvalidOperationException(
                   "Futures do not allow specifying a leveraged target, since they are traded using margin which already is leveraged. " +
                   $"Possible target buying power goes from -1 to 1, target provided is: {parameters.TargetBuyingPower}");
     }
     return(base.GetMaximumOrderQuantityForTargetBuyingPower(parameters));
 }
 /// <summary>
 /// Get the maximum market order quantity to obtain a position with a given buying power percentage.
 /// Will not take into account free buying power.
 /// </summary>
 /// <param name="parameters">An object containing the portfolio, the security and the target signed buying power percentage</param>
 /// <returns>Returns the maximum allowed market order quantity and if zero, also the reason</returns>
 public GetMaximumOrderQuantityResult GetMaximumOrderQuantityForTargetBuyingPower(GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters)
 {
     using (Py.GIL())
     {
         return((_model.GetMaximumOrderQuantityForTargetBuyingPower(parameters)
                 as PyObject).GetAndDispose <GetMaximumOrderQuantityResult>());
     }
 }
        public GetMaximumOrderQuantityResult GetMaximumOrderQuantityForTargetBuyingPower(
            GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters
            )
        {
            EnsureSecurityExists(parameters.Security);
            var expected = SecurityModel.GetMaximumOrderQuantityForTargetBuyingPower(parameters);

            if (reentry)
            {
                return(expected);
            }

            reentry = true;
            var security      = parameters.Security;
            var positionGroup = Portfolio.Positions[new PositionGroupKey(PositionGroupModel, security)];
            var actual        = PositionGroupModel.GetMaximumLotsForTargetBuyingPower(
                new GetMaximumLotsForTargetBuyingPowerParameters(
                    parameters.Portfolio,
                    positionGroup,
                    parameters.TargetBuyingPower,
                    parameters.MinimumOrderMarginPortfolioPercentage,
                    parameters.SilenceNonErrorReasons
                    )
                );

            var lotSize = security.SymbolProperties.LotSize;

            Assert.AreEqual(expected.IsError, actual.IsError,
                            $"{PositionGroupModel.GetType().Name}:{nameof(GetMaximumOrderQuantityForTargetBuyingPower)}: " +
                            $"ExpectedQuantity: {expected.Quantity} ActualQuantity: {actual.NumberOfLots * lotSize} {Environment.NewLine}" +
                            $"ExpectedReason: {expected.Reason}{Environment.NewLine}" +
                            $"ActualReason: {actual.Reason}"
                            );

            // we're not comparing group quantities, which is the number of position lots, but rather the implied
            // position quantities resulting from having that many lots.
            var resizedPositionGroup = positionGroup.WithQuantity(actual.NumberOfLots);
            var position             = resizedPositionGroup.GetPosition(security.Symbol);

            var bpmOrder   = new MarketOrder(security.Symbol, expected.Quantity, parameters.Portfolio.Securities.UtcTime);
            var pgbpmOrder = new MarketOrder(security.Symbol, position.Quantity, parameters.Portfolio.Securities.UtcTime);

            var bpmOrderValue   = bpmOrder.GetValue(security);
            var pgbpmOrderValue = pgbpmOrder.GetValue(security);

            var bpmOrderFees   = security.FeeModel.GetOrderFee(new OrderFeeParameters(security, bpmOrder)).Value.Amount;
            var pgbpmOrderFees = security.FeeModel.GetOrderFee(new OrderFeeParameters(security, pgbpmOrder)).Value.Amount;

            var bpmMarginRequired   = bpmOrderValue + bpmOrderFees;
            var pgbpmMarginRequired = pgbpmOrderValue + pgbpmOrderFees;

            Assert.AreEqual(expected.Quantity, position.Quantity,
                            $"{PositionGroupModel.GetType().Name}:{nameof(GetMaximumOrderQuantityForTargetBuyingPower)}: " +
                            $"ExpectedReason: {expected.Reason}{Environment.NewLine}" +
                            $"ActualReason: {actual.Reason}"
                            );

            Assert.AreEqual(expected.Reason, actual.Reason,
                            $"{PositionGroupModel.GetType().Name}:{nameof(GetMaximumOrderQuantityForTargetBuyingPower)}: " +
                            $"ExpectedReason: {expected.Reason}{Environment.NewLine}" +
                            $"ActualReason: {actual.Reason}"
                            );

            reentry = false;
            return(expected);
        }