Overall Statistics Total Trades0Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio0Tracking Error0Treynor Ratio0Total Fees\$0.00
```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/Lean/tree/master/Algorithm
*/
public class BollingerAlgo : QCAlgorithm
{
private List<Symbol> toTradeLong  = new List<Symbol>();
private List<Symbol> toTradeShort = new List<Symbol>();

public override void Initialize()
{

//Start and End Date range for the backtest:
SetStartDate(2014, 1, 1);

//Cash
SetCash(25000);

UniverseSettings.Resolution = Resolution.Hour;
UniverseSettings.Leverage = 0;

//screen universe based on if the price is above/below bollinger bands
return (from c in coarse
let bb = BB(c.Symbol, 20, 2, MovingAverageType.Exponential, Resolution.Hour)
where (c.Price > bb.UpperBand || c.Price < bb.LowerBand)
orderby c.DollarVolume descending
select c.Symbol).Take(500);
});

}

//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
{
//list to put stocks to trade

//check which type of order to make (short or long)
foreach(Symbol Key in data.Keys)
{
int check = checkBollinger(Key, data[Key]);
if(check == 1){
}else if(check == -1){
}
}

}

//check whether a stock is above/below upper/lower bands.
public int checkBollinger(Symbol sym, TradeBar data){
var bb = BB(sym, 20, 2, MovingAverageType.Exponential, Resolution.Hour);

if (bb.UpperBand > data.Close){
//above
return 1;
}else if(bb.UpperBand < data.Close){
//below
return -1;
}else{
//within both
return 0;
}
}

if(Portfolio.HoldStock){
Liquidate();
}
//wait until all positions are closed
do
{
// nothing
} while (Portfolio.HoldStock);

//set each list to contain no more than 3 items
if(lengthLong > 3){
lengthLong = 3;
}
if(lengthShort > 3){
lengthShort = 3;
}
//divide the cash to long and short allocations
decimal cash = Portfolio.Cash;
if(lengthLong != 0){
//half to long, dive the half up
decimal cashLong = (cash / 2 ) / lengthLong;

//iterate through and place orders
for(int i = 0; i < lengthLong; i ++){
double weight = 0.5 / lengthLong;
SetHoldings(sym.Value, weight);
}
}
if(lengthShort != 0){
//half to short, dive the half up
decimal cashShort = (cash / 2 ) / lengthShort;

//iterate through and place orders
for(int i = 0; i < lengthShort; i ++){