Overall Statistics
Total Trades
40
Average Win
2.50%
Average Loss
-1.65%
Compounding Annual Return
12.652%
Drawdown
14.900%
Expectancy
0.741
Net Profit
57.749%
Sharpe Ratio
0.989
Loss Rate
31%
Win Rate
69%
Profit-Loss Ratio
1.51
Alpha
0.077
Beta
0.394
Annual Standard Deviation
0.128
Annual Variance
0.016
Information Ratio
0.003
Tracking Error
0.141
Treynor Ratio
0.32
Total Fees
$72.88
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);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            //Cash allocation
            SetCash(25000);
            
            //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));
            AddChart(stockPlot);
        }

        //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);
        }
    }
}