# The Bollinger Bands EasyLanguage Tutorial

In this Bollinger Bands EasyLanguage Tutorial, you will learn how to build the Bollinger Bands indicator for TradeStation and MultiCharts and add interesting features like the slope and the over-price signal.

What are the Bollinger Bands? They represent two standard deviations of a moving average.

So let’s start drawing a moving average:

```
Vars:
High_Yesterday(0),
Low_Yesterday(0),
Range_Yesterday(0),
Range_Yesterday_String(“”);
High_Yesterday = High[1];
Low_Yesterday = Low[1];
Range_Yesterday = High_Yesterday – Low_Yesterday;
Range_Yesterday_String = NumToStr(Range_Yesterday,2);
Plot1 (Range_Yesterday_String);
```

Then we calculate the standard deviation of the moving average.

The standard deviation indicates how much each value moves away from the moving average.

Once calculated, we will multiply this result by a multiplier.

```
Inputs:
Length(20),
Molt(2);
Var:
BB_Line(0),
StDev(0),
UpperBand(0),
LowerBand(0);
BB_Line = Average(Close, Length);
StDev = StandardDev( Close, Length, 1 );
UpperBand = BB_Line + Molt * StDev ;
LowerBand = BB_Line + Molt * StDev * -1 ;
Plot1(BB_Line, “BB_Line”, Black, Default, 2);
Plot2(UpperBand, “Upper”, Blue, Default, 2);
Plot3(LowerBand, “Lower”, Blue, Default, 2);
```

We completed the first step of our PowerLanguage and EasyLanguage Tutorial. We have created a lean code for our Bollinger Bands.

Look carefully at the difference between our code and the classic indicator you find on your TradeStation and MultiCharts.

Put your hands in the original code may seem complicated.

You can easily modify the indicator with our short and straightforward code.

At this point, we can add some interesting functions; for example, we would like to know the direction of the trend of our middle line.

## Bollinger Bands EasyLanguage Tutorial Slope Function

To avoid making the indicator code-heavy, we construct a function that calculates the direction of the trend:

```
Inputs:
Length(Numericsimple);
Vars:
BB_Line(0),
BB_Line2(0),
Trend(0);
BB_Line = Average(Close, Length);
BB_Line2 = Average(Close, Length*5);
If BB_Line > BB_Line2 Then
Trend = 1;
If BB_Line < BB_Line2 Then
Trend = -1;
Bollinger_Bands_Trend_Function=Trend;
```

In practice, we use two moving averages, one fast and the other five times slower.

In this way, we will only have to enter the input of the first moving average that will be the same used to calculate the Bollinger Bands.

Let’s assume we use a classic 20-period average for the calculation of Bollinger Bands.

The function that calculates the trend will multiply the average by 20 times by five times.

Then it will compare the two averages to verify the direction of the trend. The fast-moving average is above the slow-moving average; the trend is bullish and vice versa.

Then the function passes the value relative to the direction of the trend to the indicator:

+1 uptrend

-1 bearish trend

```
If Bollinger_Bands_Trend_Function(length) = 1 Then
Color1=DarkGreen;
If Bollinger_Bands_Trend_Function(Length) = -1 Then
Color1=Red;
```

The indicator will use this data to change the color of the moving average of our Bollinger Bands.

## Bollinger Bands Signal PowerLanguage EasyLanguage Indicator

At this point, we add another useful function to the indicator.

The Bollinger Bands are an excellent tool for measuring volatility and price excesses.

It is always interesting to know when prices break the Upper or Lower Lines because we are in the presence of price excess.

In markets, mean-reverting can generate excellent signals, as it could work using it in reverse in trend follower markets.

When the previous day’s price closes outside one of the bands, the indicator signals it by drawing a point.

However, it may be necessary to decrease the standard deviation value to increase the number of signals or vice versa to increase it to operate with less frequency.

We could use it to filter signals together with the relative strength index; in this case, it will be convenient to lower the Bollinger Bands’ standard deviation value.

Obviously, it is possible to create an audible alarm or send a message by e-mail when this opportunity occurs.

The code in PowerLanguage and EasyLanguage is straightforward:

```
If Signal_Function then
Begin
If Close[1] > UpperBand Then
Plot4(High+(High*0.005), “Sell Signal”, Red, default, 5);
If Close[1] < LowerBand Then
Plot5(Low-(Low*0.005), “Buy Signal”, DarkGreen, default, 5);
End;
```

The important thing is to evaluate if the standard deviation used is correct for this type of signal.

Using the standard parameter of 2 standard deviations on a 20-period average should be excellent for many instruments.

However, it may be necessary to decrease the standard deviation value to increase the number of signals or vice versa to increase it to operate with less frequency.

We could use it to filter signals together with the relative strength index; in this case, it will be convenient to lower the Bollinger Bands’ standard deviation value.

You can find many free TradeStation EasyLanguage Tutorials and Indicators here:

Learn more about EasyLanguage Number to String: