public void GetInitialMarginRequiredForOrderTest() { var security = GetSecurity(Symbols.AAPL); var buyingPowerModel = new TestSecurityMarginModel(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 GetInitialMarginRequiredForOrderTest() { var security = GetSecurity(Symbols.AAPL); var buyingPowerModel = new TestSecurityMarginModel(2); security.BuyingPowerModel = buyingPowerModel; var order = new MarketOrder(security.Symbol, 100, DateTime.Now); var actual = buyingPowerModel.GetInitialMarginRequiredForOrder( new InitialMarginRequiredForOrderParameters(new IdentityCurrencyConverter(Currencies.USD), 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(); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); var security = algorithm.AddSecurity(_symbol.ID.SecurityType, _symbol.ID.Symbol); security.FeeModel = _feeModels[feeType]; security.SetLeverage(leverage); var buyingPowerModel = new TestSecurityMarginModel(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(); }