Overall Statistics
Total Trades
11
Average Win
0%
Average Loss
0%
Compounding Annual Return
24.574%
Drawdown
32.600%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0.791
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.227
Beta
-0.09
Annual Standard Deviation
0.275
Annual Variance
0.076
Information Ratio
0.38
Tracking Error
0.317
Treynor Ratio
-2.409
Total Fees
$11.99
namespace QuantConnect 
{   
    public class DonchianBreakout : QCAlgorithm
    {
        //Variables
        int quantity = 0;
        decimal close = 0;
        string symbol = "SPY";
        RollingWindow<decimal> _top = new RollingWindow<decimal>(2);
        RollingWindow<decimal> _bottom = new RollingWindow<decimal>(2);
        decimal top = 0;
        decimal bottom = 0;
        
        Maximum max;
        Minimum min;
        
        public override void Initialize() 
        {
			SetCash(25000);
            SetStartDate(2010, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));			
			int period = 5;

			AddSecurity(SecurityType.Equity, symbol, Resolution.Daily);

            max = MAX(symbol, period, Resolution.Daily);
            min = MIN(symbol, period, Resolution.Daily);
        }
        
		private DateTime previous;
		
        public void OnData(TradeBars data) 
        {   
			if (!max.IsReady) 
				return;
				
			if (previous.Date == data.Time.Date) 
				return;	

            close = Securities[symbol].Close;
            int quantity = Convert.ToInt32(Portfolio.Cash/close);
            int holdings = Portfolio[symbol].Quantity;
            var top = max; //NEED TO FIGURE OUT HOW TO LAG THIS
            var bottom = min; //NEED TO FIGURE OUT HOW TO LAG THIS
            Console.WriteLine("close " + close);
            Console.WriteLine("top " + top);
            Console.WriteLine("bottom " + bottom);
            Console.WriteLine("quantity " + quantity);
            
            _top.Add(new IndicatorDataPoint(Time, top));
            	if(!_top.IsReady) return;
			var historicMax = _top[1];
			Console.WriteLine("max lagged " + historicMax);
			
			_bottom.Add(bottom);
            	if(!_bottom.IsReady) return;
			var historicMin = _bottom[1];
			Console.WriteLine("min lagged " + historicMin);
            
            if (close > historicMax && holdings < 1) 
            {
                Order(symbol, quantity);
                Debug("Long");
            }
        	
        	if (close < historicMin && holdings > 0)
            {
                Order(symbol, -quantity);
                Debug("Short");
            } 
            Plot("High", historicMax);
            Plot("Low", historicMin);
            Plot("Close", close);
        }        
    }
}