Overall Statistics
Total Trades
60
Average Win
0.17%
Average Loss
-0.07%
Compounding Annual Return
9.636%
Drawdown
0.200%
Expectancy
0.965
Net Profit
1.899%
Sharpe Ratio
6.661
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
2.68
Alpha
0.049
Beta
2.079
Annual Standard Deviation
0.013
Annual Variance
0
Information Ratio
5.257
Tracking Error
0.013
Treynor Ratio
0.043
Total Fees
$60.00
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();
			}
		}
    }
}