namespace QuantConnect
{
/*
* QuantConnect University: Bollinger Bands Example:
*/
public class BollingerBandsAlgorithm : QCAlgorithm
{
string _symbol = "UNG";
BollingerBands _bb;
RelativeStrengthIndex _rsi;
AverageTrueRange _atr;
ExponentialMovingAverage _ema;
SimpleMovingAverage _sma;
MovingAverageConvergenceDivergence _macd;
decimal _price;
int blocks = 10;
decimal start_cash = 25000;
bool transaction = false;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Initialize
SetStartDate(2014, 1, 1);
SetEndDate(2014, 12, 31);
SetCash(start_cash);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);
//Set up Indicators:
_bb = BB(_symbol, 20, 1, MovingAverageType.Simple, Resolution.Minute);
_rsi = RSI(_symbol, 14, MovingAverageType.Simple, Resolution.Minute);
_atr = ATR(_symbol, 14, MovingAverageType.Simple, Resolution.Minute);
_ema = EMA(_symbol, 14, Resolution.Minute);
_sma = SMA(_symbol, 14, Resolution.Minute);
_macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Simple, Resolution.Minute);
}
public void OnData(TradeBars data)
{
if (!_bb.IsReady || !_rsi.IsReady) return;
if(transaction == false)
{
_price = data[_symbol].Close;
int quantity = (int)Math.Floor(start_cash/10/data[_symbol].Close);
if ((!Securities[_symbol].HoldStock) && (transaction == false))
{
//int quantity = (int)Math.Floor(Portfolio.Cash / data[_symbol].Close);
//Order function places trades: enter the string symbol and the quantity you want:
if(_price <_bb.LowerBand)
{
Order(_symbol, quantity);
transaction = true;
Debug("Buy first " + quantity.ToString());
}
}
/*
if((_price > Securities[_symbol].Holdings.AveragePrice) && (Securities[_symbol].Holdings.HoldingsValue > 10000) && (transaction == false))
{
Order(_symbol,-Securities[_symbol].Holdings.Quantity/2);
transaction = true;
//Debug("Dump half " + (-Securities[_symbol].Holdings.Quantity/2).ToString());
}
*/
Debug("Price : " + _price.ToString("F2") + " avg = " + Securities[_symbol].Holdings.AveragePrice.ToString("F2") +" num: " + Securities[_symbol].Holdings.Quantity.ToString("F2"));
if (((_price * (decimal)0.95) > (Securities[_symbol].Holdings.AveragePrice)) && (_price < _bb.LowerBand) && (transaction == false))
{
Order(_symbol, quantity);
transaction = true;
Debug("Buy Low " + quantity.ToString());
}
/*
if((_price > _bb.UpperBand) && (transaction == false))
{
if(Securities[_symbol].Holdings.Quantity > quantity)
{
Order(_symbol,-quantity);
transaction = true;
//Debug("Sell High " + quantity.ToString());
}
else
{
Liquidate(_symbol);
//Debug("Liquidate");
transaction = true;
}
}
*/
}
}
// Fire plotting events once per day:
public override void OnEndOfDay() {
//if (!_bb.IsReady) return;
Plot("BB", "Price", _price);
Plot("BB", _bb.UpperBand, _bb.MiddleBand, _bb.LowerBand);
//Plot("RSI", _rsi);
//Plot("ATR", _atr);
//Plot("MACD", "Price", _price);
//Plot("MACD", _macd.Fast, _macd.Slow);
Plot("Averages", _ema, _sma);
transaction = false;
//Debug("Holdings SPY on " + Time.ToShortDateString() + " = " + Securities[_symbol].Holdings.Quantity.ToString());
//Debug("Value = " + Securities[_symbol].Holdings.HoldingsValue.ToString("F2"));
}
}
}