Overall Statistics
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        SimpleMovingAverage sma;
        ExponentialMovingAverage ema;
        SecurityHolding SPY;
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
			
            //Start and End Date range for the backtest:
            SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            
            //Cash allocation
            SetCash(25000);
            
            // register for hourly SPY data
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
            
            // define our 6 period SMA, we'll pump 4-hour data into this guy
            sma = new SimpleMovingAverage("SMA6", 6);
            // define out 9 period EMA, we'll pump 4-hour data into this guy as well
            ema = new ExponentialMovingAverage("EMA19", 19);
            
            // define a 4 hour consolidator, each consolidator can only be bound to
            // a single symbol, so if we wanted to also do AAPL data, we would need
            // another consolidator for AAPL
            var fourHourSpy = ResolveConsolidator("SPY", TimeSpan.FromHours(4));
            
            // register our sma to receive data from our fourHourSpy consolidator, making our
            // sma a 6 period 4-hour SMA
            RegisterIndicator("SPY", sma, fourHourSpy);
            
            // register our ema to receive data from our fourHourSpy consolidator, making our
            // ema a 6 period 4-hour SMA
            RegisterIndicator("SPY", ema, fourHourSpy);
            
            // Plot our indicators on each new update
            PlotIndicator("SPY", sma, ema);
            
            SPY = Portfolio["SPY"];
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {
            if (!sma.IsReady || !ema.IsReady) return;
            
            const decimal threshold = 0.000075m;
            if (SPY.Quantity <= 0 && ema > sma*(1+threshold))
            {
                SetHoldings("SPY", .75m);
            }
            else if (SPY.Quantity >= 0 && ema < sma*(1-threshold))
            {
                SetHoldings("SPY", -.75m);
            }
        }
    }
}