/// <summary> /// Check if there is sufficient capital to execute this order. /// </summary> /// <param name="portfolio">Our portfolio</param> /// <param name="order">Order we're checking</param> /// <returns>True if suficient capital.</returns> public bool GetSufficientCapitalForOrder(SecurityPortfolioManager portfolio, Order order) { if (Math.Abs(GetOrderRequiredBuyingPower(order)) > portfolio.GetBuyingPower(order.Symbol, order.Direction)) { //Log.Debug("Symbol: " + order.Symbol + " Direction: " + order.Direction.ToString() + " Quantity: " + order.Quantity); //Log.Debug("GetOrderRequiredBuyingPower(): " + Math.Abs(GetOrderRequiredBuyingPower(order)) + " PortfolioGetBuyingPower(): " + portfolio.GetBuyingPower(order.Symbol, order.Direction)); return(false); } return(true); }
/// <summary> /// Check if there is sufficient capital to execute this order. /// </summary> /// <param name="portfolio">Our portfolio</param> /// <param name="order">Order we're checking</param> /// <returns>True if suficient capital.</returns> private bool GetSufficientCapitalForOrder(SecurityPortfolioManager portfolio, Order order) { //First simple check, when don't hold stock, this will always increase portfolio regardless of direction if (Math.Abs(GetOrderRequiredBuyingPower(order)) > portfolio.GetBuyingPower(order.Symbol, order.Direction)) { Log.Debug("GetOrderRequiredBuyingPower(): " + Math.Abs(GetOrderRequiredBuyingPower(order)) + " PortfolioGetBuyingPower(): " + portfolio.GetBuyingPower(order.Symbol, order.Direction)); return(false); } else { return(true); } }
/// <summary> /// Given this portfolio and order, what would the final portfolio holdings be if it were filled. /// </summary> /// <param name="portfolio">Portfolio we're running</param> /// <param name="order">Order requested to process </param> /// <returns>decimal final holdings </returns> private decimal GetExpectedFinalHoldings(SecurityPortfolioManager portfolio, Order order) { decimal expectedFinalHoldings = 0; if (portfolio.TotalAbsoluteHoldings > 0) { foreach (Security company in Securities.Values) { if (order.Symbol == company.Symbol) { //If the same holding, we must check if its long or short. expectedFinalHoldings += Math.Abs(company.Holdings.HoldingValue + (order.Price * (decimal)order.Quantity)); Log.Debug("HOLDINGS: " + company.Holdings.HoldingValue + " - " + "ORDER: (P: " + order.Price + " Q:" + order.Quantity + ") EXPECTED FINAL HOLDINGS: " + expectedFinalHoldings + " BUYING POWER: " + portfolio.GetBuyingPower(order.Symbol)); } else { //If not the same asset, then just add the absolute holding to the final total: expectedFinalHoldings += company.Holdings.AbsoluteHoldings; } } } else { //First purchase: just make calc abs order size: expectedFinalHoldings = (order.Price * (decimal)order.Quantity); } return(expectedFinalHoldings); }