Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
14.458%
Drawdown
5.500%
Expectancy
0
Net Profit
0%
Sharpe Ratio
1.293
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.059
Beta
0.557
Annual Standard Deviation
0.089
Annual Variance
0.008
Information Ratio
0.184
Tracking Error
0.079
Treynor Ratio
0.206
Total Fees
$2.55
namespace QuantConnect.Algorithm.CSharp
{ 
    public class PastClose : QCAlgorithm
    {
    	private Symbol _spy;
        
        private RollingWindow<TradeBar> Bar;
        private RollingWindow<BollingerBandState> BBWin;
        private BollingerBands _bb;
        
        public override void Initialize()
        {
            SetStartDate(2016, 01, 01);  //Set Start Date
            SetEndDate(2016, 12, 31);    //Set End Date
            SetCash(100000);             //Set Strategy Cash

            AddEquity("SPY", Resolution.Daily);
            _spy = Securities["SPY"].Symbol;
            
            Bar = new RollingWindow<TradeBar>(2);
            BBWin = new RollingWindow<BollingerBandState>(2);
            
            _bb = BB(_spy, 30, 2);
        }

        public void OnData(TradeBars data)
        {
        	Bar.Add(data[_spy]);
        	BBWin.Add(new BollingerBandState(_bb));
            
            if (!Bar.IsReady) return;
            
            if (Bar[0].Close < BBWin[0].UpperBand  && Bar[1].Close > BBWin[1].UpperBand && !Portfolio.HoldStock) 
            {
            	SetHoldings(_spy, 1);
            }
        }
    }
    
    // class to hold the current state of a bollinger band instance
    public class BollingerBandState
    {
    	public readonly decimal UpperBand;
        public readonly decimal MiddleBand;
        public readonly decimal LowerBand;
        public readonly decimal StandardDeviation;
        public BollingerBandState(BollingerBands bb)
        {
            UpperBand = bb.UpperBand;
            MiddleBand = bb.MiddleBand;
            LowerBand = bb.LowerBand;
            StandardDeviation = bb.StandardDeviation;
        }
    }
}