Ejemplo n.º 1
0
        private bool HasSufficientBuyingPowerForOrder(decimal orderQuantity, Security security, IAlgorithm algo)
        {
            var order = new MarketOrder(security.Symbol, orderQuantity, DateTime.UtcNow);

            _fakeOrderProcessor.AddTicket(order.ToOrderTicket(algo.Transactions));
            var hashSufficientBuyingPower = security.BuyingPowerModel.HasSufficientBuyingPowerForOrder(algo.Portfolio,
                                                                                                       security, new MarketOrder(security.Symbol, orderQuantity, DateTime.UtcNow));

            return(hashSufficientBuyingPower.IsSufficient);
        }
Ejemplo n.º 2
0
        public void NegativeMarginRemaining(bool isError, int target, int side)
        {
            var algo     = GetAlgorithm();
            var security = InitAndGetSecurity(algo, 5);

            security.Holdings.SetHoldings(security.Price, 1000 * side);
            algo.Portfolio.CashBook.Add(algo.AccountCurrency, -100000, 1);
            var fakeOrderProcessor = new FakeOrderProcessor();

            algo.Transactions.SetOrderProcessor(fakeOrderProcessor);

            Assert.IsTrue(algo.Portfolio.MarginRemaining < 0);

            var quantity = security.BuyingPowerModel.GetMaximumOrderQuantityForTargetBuyingPower(
                new GetMaximumOrderQuantityForTargetBuyingPowerParameters(algo.Portfolio,
                                                                          security,
                                                                          target * side,
                                                                          0)).Quantity;

            if (!isError)
            {
                Assert.AreEqual(1000 * side * -1, quantity);
            }
            else
            {
                // even if we don't have margin 'GetMaximumOrderQuantityForTargetBuyingPower' doesn't care
                Assert.AreNotEqual(0, quantity);
            }

            var order = new MarketOrder(security.Symbol, quantity, new DateTime(2020, 1, 1));

            fakeOrderProcessor.AddTicket(order.ToOrderTicket(algo.Transactions));
            var actual = security.BuyingPowerModel.HasSufficientBuyingPowerForOrder(
                new HasSufficientBuyingPowerForOrderParameters(algo.Portfolio,
                                                               security,
                                                               order));

            Assert.AreEqual(!isError, actual.IsSufficient);
        }