Overall Statistics
using QuantConnect.Indicators;

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
    	private BollingerBands _bb;
        //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);         
            //Cash allocation
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            // Create a Bolling Band indicator with a simple moving average and bands distanced by 2 std-deviation
            _bb = BB("SPY", 20, 2, MovingAverageType.Simple, Resolution.Daily);
            //Chart - Master Container for the Chart:
            var stockPlot = new Chart("Trade Plot");
            //On the Trade Plotter Chart we want 3 series: trades and price:
            stockPlot.AddSeries(new Series("Buy", SeriesType.Scatter, 0));
            stockPlot.AddSeries(new Series("Sell", SeriesType.Scatter, 0));
            stockPlot.AddSeries(new Series("Price", SeriesType.Line, 0));

        //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(!_bb.IsReady) return;
            var price = data["SPY"].Close;
            var qnty = Portfolio["SPY"].Quantity;
            // Enter long position if flat or short and price below the lower band
            if(qnty <= 0 && price < _bb.LowerBand)
            	SetHoldings("SPY", +1m);
            	Debug("Long SPY on " + Time.ToShortDateString());
            	Plot("Trade Plot", "Buy", price);
            // Enter short position if flat or long and price above the upper band
            if(qnty >= 0 && price > _bb.UpperBand)
            	SetHoldings("SPY", -1m);
            	Debug("Short SPY on " + Time.ToShortDateString());
            	Plot("Trade Plot", "Sell", price);
        public override void OnEndOfDay()
        	if(_bb.IsReady) Plot("Trade Plot", _bb.UpperBand, _bb.MiddleBand, _bb.LowerBand);