Overall Statistics
namespace QuantConnect
{
    public class OrderSubmitHelp : QCAlgorithm
    {
        public override void Initialize()
        {
            SetStartDate(2013, 10, 4);
            SetEndDate(2013, 10, 16);
            SetCash(10000);
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
            
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            
        }


        public void OnData(TradeBars data)
        {
        	//Make first order and check buying power
            if (Portfolio.Invested == false && Portfolio.Cash == 10000)
            {
                var orderQuantity = CalculateOrderQuantity("SPY", 0.98); //Easy position size
                MarketOrder("SPY", orderQuantity, false, "Entry " + "SPY");
                Log("Equity Unused = " + Portfolio.Cash + "Margin Remaining = " + Portfolio.MarginRemaining);
                Log("Portfolio Holdings = " + Portfolio.TotalHoldingsValue + " Portfolio Invested = " + Portfolio.Invested);
                Log("Buying Power Short = " + Portfolio.GetBuyingPower("SPY", OrderDirection.Sell));
                Log("Buying Power Long = " + Portfolio.GetBuyingPower("SPY", OrderDirection.Buy));
            }
            //Liquidate, then immediately reorder (DOESN"T WORK WITH IB Brokers Model)
            if (Portfolio.Invested == true)
            {
                Log("Begin Liquidate and reorder function");
                var liqOrder = Liquidate("SPY","Liquidated");
                Log("Equity Unused = " + Portfolio.Cash + "Margin Remaining = " + Portfolio.MarginRemaining);
                Log("Portfolio Holdings = " + Portfolio.TotalHoldingsValue + " Portfolio Invested = " + Portfolio.Invested);
                Log("Buying Power Short = " + Portfolio.GetBuyingPower("SPY", OrderDirection.Sell));
                Log("Buying Power Long = " + Portfolio.GetBuyingPower("SPY", OrderDirection.Buy));

                Log("Attempt to resubmit new order"); // This throws insufficient buying power
                var orderQuantity = CalculateOrderQuantity("SPY", 0.98); //Easy position size
                MarketOrder("SPY", orderQuantity, false, "Reorder " + "SPY");
                Log("Order Quantity = " + orderQuantity);
            }

        }
    }
}