Overall Statistics
namespace QuantConnect.Algorithm.CSharp{
   
    public class BasicTemplateAlgorithm : QCAlgorithm{
    	
		String symbol = "SPY";
		
		SimpleMovingAverage smaFast;
		SimpleMovingAverage smaSlow;
		
		RollingWindow<Decimal> high = new RollingWindow<Decimal>(200);
		RollingWindow<Decimal> low = new RollingWindow<Decimal>(200);
		RollingWindow<Decimal> close = new RollingWindow<Decimal>(200);
		RollingWindow<Decimal> open = new RollingWindow<Decimal>(200);
		
		bool longEntry = false;
		
		int size = 100;
		
		private OrderTicket CurrentOrder;
    	private OrderTicket StopLoss;
    	private OrderTicket ProfitTarget;
    	
    	const decimal StopLossPercent = 0.002m;
    	const decimal TakeProfitPercent = 0.006m;

        public override void Initialize(){
        	
        	//SetStartDate(DateTime.Now.Date.AddDays(-600));
            SetStartDate(2018, 01, 01);  
            SetEndDate(DateTime.Now.Date.AddDays(-1));    
            SetCash(100000);             

            AddEquity(symbol, Resolution.Minute);
            smaFast = SMA(symbol, 100, Resolution.Minute);
            smaSlow = SMA(symbol, 200, Resolution.Minute);

        }

       
        public override void OnData(Slice data){
        	
        	high.Add(data[symbol].High);
        	low.Add(data[symbol].Low);
        	close.Add(data[symbol].Close);
        	open.Add(data[symbol].Open);
        	
        	if(!smaSlow.IsReady) return;

        	if(!Portfolio.Invested && isHighest(high, 9) && smaFast > smaSlow){
        		if(dippEntry(open, close, 9, 6)){
        			CurrentOrder = Order(symbol, size);
            		StopLoss = StopMarketOrder(symbol, -size, close[0]*(1m - StopLossPercent));
  					ProfitTarget = LimitOrder(symbol, -size, close[0]*(1m + TakeProfitPercent));
        		}
        	}
        }
        
        public bool isHighest(RollingWindow<Decimal> high, int n){
        	int count = 0;
        	for(int i = n+1; i < high.Count; i++){
        		if(high[n] >= high[i]){
        			count++;
        		}
        	}if(count == high.Count-(n+1)){
        		return true;
        	}else return false;
        }
        
        public bool dippEntry(RollingWindow<Decimal> open,RollingWindow<Decimal> close, int n, int p){
        	int count = 0;
        	for(int i = 0; i < n; i++){
        		if(open[i] > close[i]){
        			count++;
        		}
        	}if(count == p){
        		return true;
        	}else return false;
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent){
			if (!orderEvent.Status.IsClosed()){
				return;
			}
			
			if (ProfitTarget == null || StopLoss == null){
				return;
			}
			
			var filledOrderId = orderEvent.OrderId;

			if (ProfitTarget.OrderId == filledOrderId){
				StopLoss.Cancel();
			}

			if (StopLoss.OrderId == filledOrderId){
				ProfitTarget.Cancel();
			}
		}
    }
}