Overall Statistics
Total Trades
451
Average Win
2.22%
Average Loss
-1.18%
Compounding Annual Return
12.457%
Drawdown
16.900%
Expectancy
0.192
Net Profit
61.885%
Sharpe Ratio
0.995
Loss Rate
59%
Win Rate
41%
Profit-Loss Ratio
1.88
Alpha
-0.015
Beta
0.977
Annual Standard Deviation
0.125
Annual Variance
0.016
Information Ratio
-1.069
Tracking Error
0.017
Treynor Ratio
0.127
Total Fees
$451.00
namespace QuantConnect 
{   
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        private OrderTicket EntryOrder { get; set; }
        private Func<QCAlgorithm, string, decimal, OneCancelsOtherTicketSet> OnOrderFilledEvent { get; set; }
        private OneCancelsOtherTicketSet ProfitLossOrders { get; set; }
        
        public override void Initialize() 
        {
            SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            SetCash(25000);
            
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
        }

        public void OnData(TradeBars data) 
        {   
            if (!Portfolio.HoldStock) 
            {
                int quantity = (int)Math.Floor(Portfolio.Cash / data["SPY"].Close);

	            this.OnOrderFilledEvent = (algo, symbol, filledPrice) =>
	            {
	                return new OneCancelsOtherTicketSet(
	                    algo.LimitOrder(symbol, -quantity, filledPrice + 4m, "Profit Target"),
	                    algo.StopMarketOrder(symbol, -quantity, filledPrice - 2m, "Stop Loss"));
	            };
	
	            this.EntryOrder = MarketOrder("SPY", quantity, false, "Entry");                
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            if (EntryOrder != null)
            {
                this.EntryOrder = null;
            }

            if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled)
            {
                if (this.OnOrderFilledEvent != null)
                {
                    this.ProfitLossOrders = OnOrderFilledEvent(this, orderEvent.Symbol, orderEvent.FillPrice);
                    OnOrderFilledEvent = null;
                } 
                else if (this.ProfitLossOrders != null)
                {
                    this.ProfitLossOrders.Filled();
                    this.ProfitLossOrders = null;
                }
            }
        }        
    }
}
namespace QuantConnect {

    public class OneCancelsOtherTicketSet
    {
        public OneCancelsOtherTicketSet(params OrderTicket[] orderTickets)
        {
            this.OrderTickets = orderTickets;
        }

        private OrderTicket[] OrderTickets { get; set; }

        public void Filled()
        {
            // Cancel all the outstanding tickets.
            foreach (var orderTicket in this.OrderTickets)
            {
                if (orderTicket.Status == OrderStatus.Submitted)
                {
                    orderTicket.Cancel();
                }
            }
        }
    }

}