Overall Statistics
Total Trades
4
Average Win
18.98%
Average Loss
-1.49%
Compounding Annual Return
9592.519%
Drawdown
2.200%
Expectancy
5.850
Net Profit
17.202%
Sharpe Ratio
4.812
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
12.70
Alpha
-7.659
Beta
682.364
Annual Standard Deviation
1.017
Annual Variance
1.035
Information Ratio
4.797
Tracking Error
1.017
Treynor Ratio
0.007
Total Fees
$10.46
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.015m, "Take Profit: " + "SPY");
                StopMarketOrder("SPY", -orderQuantity, Securities["SPY"].Price * 0.985m, "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");
                StopMarketOrder("NVEE", orderQuantity, Securities["NVEE"].Price * 0.985m, "Take Profit: " + "NVEE");
                LimitOrder("NVEE", orderQuantity, Securities["NVEE"].Price * 1.015m, "Stop Loss: " + "NVEE");
            }
        }
    }
}