Overall Statistics
Total Trades
2
Average Win
0.79%
Average Loss
0%
Compounding Annual Return
25.273%
Drawdown
2.500%
Expectancy
0
Net Profit
0.785%
Sharpe Ratio
1.501
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
1.809
Beta
-85.846
Annual Standard Deviation
0.153
Annual Variance
0.023
Information Ratio
1.376
Tracking Error
0.153
Treynor Ratio
-0.003
Total Fees
$2.00
using System;
using QuantConnect.Algorithm;
using QuantConnect.Brokerages;
using QuantConnect.Data.Market;
using QuantConnect.Orders;

namespace QuantConnect
{
    public class OrderSubmitHelp : QCAlgorithm
    {
        //One cancels the other
        private OrderTicket EntryOrder { get; set; }
        public override void Initialize()
        {
            SetStartDate(2013, 10, 04);
            SetEndDate(2013, 10, 16); 
            SetCash(10000);
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
            //Default add SPY
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            AddSecurity(SecurityType.Equity, "NVEE", Resolution.Minute);
        }

        //One cancels the other
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            Order actualOrder = Transactions.GetOrderById(orderEvent.OrderId);
            switch (orderEvent.Status)
            {
                case OrderStatus.Submitted:
                    Log(actualOrder.ToString());
                    break;
                case OrderStatus.Filled:
                    Log("\t => " + orderEvent.ToString());
                    if (actualOrder.Type != OrderType.Market) { Liquidate(orderEvent.Symbol); }
                    //Liquidate(orderEvent.Symbol);
                    break;
                case OrderStatus.Canceled:
                    Log("\t => " + orderEvent.ToString() + "\n");
                    break;
                default:
                    break;
            }
        }

        public void OnData(TradeBars data)
        {
            if (data.Time.Date == new DateTime(2013,10,04) && Portfolio.Invested==false)
            {
                var orderQuantity = CalculateOrderQuantity("SPY", 0.98); //Easy position size

                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));
                //One cancels the other order
                MarketOrder("SPY", orderQuantity, false, "Entry " + "SPY");
                LimitOrder("SPY", orderQuantity, Securities["SPY"].Price * 1.5m, "Take Profit: " + "SPY");
                StopMarketOrder("SPY", -orderQuantity, Securities["SPY"].Price * 0.5m, "Stop Loss: " + "SPY");
            }
            if (data.Time.Date == new DateTime(2013, 10, 14) && data.Time.TimeOfDay == new TimeSpan(09, 31, 0))
            {
                Liquidate();
                var orderQuantity = CalculateOrderQuantity("NVEE", 0.98); //Easy position size

                Log("Equity Unused = " + Portfolio.Cash + "Margin Remaining = " + Portfolio.MarginRemaining);
                Log("Portfolio Holdings = " + Portfolio.TotalHoldingsValue + " Portfolio Invested = " + Portfolio.Invested);
                Log("Buying Power Short = " + Portfolio.GetBuyingPower("NVEE", OrderDirection.Sell));
                Log("Buying Power Long = " + Portfolio.GetBuyingPower("NVEE", OrderDirection.Buy));
                //One cancels the other order
                MarketOrder("NVEE", -orderQuantity, false, "Entry " + "NVEE");
                LimitOrder("NVEE", orderQuantity, Securities["NVEE"].Price * 0.5m, "Take Profit: " + "NVEE");
                StopMarketOrder("NVEE", orderQuantity, Securities["NVEE"].Price * 1.5m, "Stop Loss: " + "NVEE");
            }
        }
    }
}