public void GetMarginRemainingTests()
        {
            const int     quantity       = 1000;
            const decimal leverage       = 2;
            var           orderProcessor = new OrderProcessor();
            var           portfolio      = GetPortfolio(orderProcessor, quantity);

            var security         = GetSecurity(Symbols.AAPL);
            var buyingPowerModel = new TestSecurityMarginBuyingPowerModel(leverage);

            security.BuyingPowerModel = buyingPowerModel;
            portfolio.Securities.Add(security);

            security.Holdings.SetHoldings(1m, quantity);
            var actual1 = buyingPowerModel.GetMarginRemaining(portfolio, security, OrderDirection.Buy);

            Assert.AreEqual(quantity / leverage, actual1);

            var actual2 = buyingPowerModel.GetMarginRemaining(portfolio, security, OrderDirection.Sell);

            Assert.AreEqual(quantity, actual2);

            security.Holdings.SetHoldings(1m, -quantity);
            var actual3 = buyingPowerModel.GetMarginRemaining(portfolio, security, OrderDirection.Sell);

            Assert.AreEqual(quantity / leverage, actual3);

            var actual4 = buyingPowerModel.GetMarginRemaining(portfolio, security, OrderDirection.Buy);

            Assert.AreEqual(quantity, actual4);
        }
        public void GetInitialMarginRequiredForOrderTest()
        {
            var security         = GetSecurity(Symbols.AAPL);
            var buyingPowerModel = new TestSecurityMarginBuyingPowerModel(2);

            security.BuyingPowerModel = buyingPowerModel;
            var order  = new MarketOrder(security.Symbol, 100, DateTime.Now);
            var actual = buyingPowerModel.GetInitialMarginRequiredForOrder(security, order);

            Assert.AreEqual(0, actual);
        }
        public void Run(Position initialPosition, Position finalPosition, FeeType feeType, PriceMovement priceMovement, int leverage)
        {
            //Console.WriteLine("----------");
            //Console.WriteLine("PARAMETERS");
            //Console.WriteLine("Initial position: " + initialPosition);
            //Console.WriteLine("Final position: " + finalPosition);
            //Console.WriteLine("Fee type: " + feeType);
            //Console.WriteLine("Price movement: " + priceMovement);
            //Console.WriteLine("Leverage: " + leverage);
            //Console.WriteLine("----------");
            //Console.WriteLine();

            var algorithm = new QCAlgorithm();

            var security = algorithm.AddSecurity(_symbol.ID.SecurityType, _symbol.ID.Symbol);

            security.FeeModel = _feeModels[feeType];
            security.SetLeverage(leverage);

            var buyingPowerModel = new TestSecurityMarginBuyingPowerModel(leverage);

            security.BuyingPowerModel = buyingPowerModel;

            algorithm.SetCash(Cash);

            Update(security, BasePrice);

            decimal        targetPercentage;
            OrderDirection orderDirection;
            MarketOrder    order;
            decimal        orderFee;
            OrderEvent     fill;
            decimal        orderQuantity;
            decimal        freeMargin;
            decimal        requiredMargin;

            if (initialPosition != Position.Zero)
            {
                targetPercentage = (decimal)initialPosition;
                orderDirection   = initialPosition == Position.Long ? OrderDirection.Buy : OrderDirection.Sell;
                orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
                order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
                freeMargin       = buyingPowerModel.GetMarginRemaining(algorithm.Portfolio, security, orderDirection);
                requiredMargin   = buyingPowerModel.GetInitialMarginRequiredForOrder(security, order);

                //Console.WriteLine("Current price: " + security.Price);
                //Console.WriteLine("Target percentage: " + targetPercentage);
                //Console.WriteLine("Order direction: " + orderDirection);
                //Console.WriteLine("Order quantity: " + orderQuantity);
                //Console.WriteLine("Free margin: " + freeMargin);
                //Console.WriteLine("Required margin: " + requiredMargin);
                //Console.WriteLine();

                Assert.That(Math.Abs(requiredMargin) <= freeMargin);

                orderFee = security.FeeModel.GetOrderFee(security, order);
                fill     = new OrderEvent(order, DateTime.UtcNow, orderFee)
                {
                    FillPrice = security.Price, FillQuantity = orderQuantity
                };
                algorithm.Portfolio.ProcessFill(fill);

                //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
                //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
                //Console.WriteLine();

                if (priceMovement == PriceMovement.RisingSmall)
                {
                    Update(security, HighPrice);
                }
                else if (priceMovement == PriceMovement.FallingSmall)
                {
                    Update(security, LowPrice);
                }
                else if (priceMovement == PriceMovement.RisingLarge)
                {
                    Update(security, VeryHighPrice);
                }
                else if (priceMovement == PriceMovement.FallingLarge)
                {
                    Update(security, VeryLowPrice);
                }
            }

            targetPercentage = (decimal)finalPosition;
            orderDirection   = finalPosition == Position.Long || (finalPosition == Position.Zero && initialPosition == Position.Short) ? OrderDirection.Buy : OrderDirection.Sell;
            orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
            order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
            freeMargin       = buyingPowerModel.GetMarginRemaining(algorithm.Portfolio, security, orderDirection);
            requiredMargin   = buyingPowerModel.GetInitialMarginRequiredForOrder(security, order);

            //Console.WriteLine("Current price: " + security.Price);
            //Console.WriteLine("Target percentage: " + targetPercentage);
            //Console.WriteLine("Order direction: " + orderDirection);
            //Console.WriteLine("Order quantity: " + orderQuantity);
            //Console.WriteLine("Free margin: " + freeMargin);
            //Console.WriteLine("Required margin: " + requiredMargin);
            //Console.WriteLine();

            Assert.That(Math.Abs(requiredMargin) <= freeMargin);

            orderFee = security.FeeModel.GetOrderFee(security, order);
            fill     = new OrderEvent(order, DateTime.UtcNow, orderFee)
            {
                FillPrice = security.Price, FillQuantity = orderQuantity
            };
            algorithm.Portfolio.ProcessFill(fill);

            //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
            //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
            //Console.WriteLine();
        }