namespace QuantConnect.Algorithm.CSharp
{
public class PastClose : QCAlgorithm
{
private Symbol _spy;
private RollingWindow<decimal> Close;
private RollingWindow<TradeBar> Bar;
private RollingWindow<BollingerBandState> BB;
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;
Close = new RollingWindow<decimal>(2);
Bar = new RollingWindow<TradeBar>(2);
BB = new RollingWindow<BollingerBandState>(2);
_bb = BB(_spy, 30, 2);
}
public void OnData(TradeBars data)
{
Close.Add(data[_spy].Close);
Bar.Add(data[_spy]);
BB.Add(new BollingerBandState(_bb));
if (!Close.IsReady) return;
if (!Bar.IsReady) return;
if (!Portfolio.Invested && Close[0] > Close[1])
{
SetHoldings(_spy, 1);
Debug(Time + " -> Close[0] . Close[1]: " + Close[0] + " . " + Close[1]);
Debug(Time + " -> Bar[0].Close . Bar[1].Close: " + Bar[0].Close + " . " + Bar[1].Close);
}
Debug(Time + " -> BB UpperBand: " + BB[0].UpperBand + " > " + BB[1].UpperBand);
}
}
// 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;
}
}
}