Download MetaTrader 5

Incremental Bollinger bands calculation ...

To add comments, please log in or register
Carl Schreiber
Moderator
7835
Carl Schreiber  

Hi,

due to a paper linked here by Fernando (thanks!) I tried to use this by indicators. First I wanted to compare it to the original - to check whether it is correct or weird.

First I compare the 'selfmade' BB (Tomato, sma dotted) against the original BB from the terminal (Magenta, solid) - as you can see (Comment err=0.0) no difference!

Then I calculated the incremental BB acc. to equation 24, 25:

Formula 24,25

which I realized as follows:

      // start incremental sma BB - should be the same               // equation no. 24,25
            sum² = (Close[i]-SmaBuff[i+1])*(Close[i]-SmaBuff[i]);
            SmaIncrTmp[i] = SmaIncrTmp[i+1] + sum²;
            //    SmaIncrDev[i] = BandsDeviations*MathSqrt(SmaIncrTmp[i]/BandsPeriod); // this is even worse!!
            SmaIncrDev[i] = BandsDeviations*MathSqrt(SmaIncrTmp[i])/BandsPeriod;
            SmaIncrUP[i]  = SmaBuff[i] + SmaIncrDev[i];
            SmaIncrLO[i]  = SmaBuff[i] - SmaIncrDev[i];
            if (i<limit*0.8) {
               errBBincr += fabs(SmaIncrUP[i] - iBands(_Symbol,_Period,BandsPeriod,BandsDeviations,0,PRICE_CLOSE,MODE_UPPER,i))/_Point;
            }

According to my understanding I assumed that the resulting bands (Lime-Green) should be close (not exactly the same of course!) to the original BB - but they are far away. Am I doing something wrong?

Chart-Shot

At last I coded the incremental BB (=incremental calculation of the variance) of the ema - which is close to the original BBs. It could be improved it you increase α by e.g. 3.0*α:

      // start incremental emaBB      
            //sum² = (Close[i] - EmaBuff[i])*(Close[i] - EmaBuff[i+1]);
            //EmaDev[i]   = EmaDev[i+1]==0.0? sum² : (1.0-α)*EmaDev[i+1] + (1.0-α)*sum²;  // equation no. 142
            double coeff1 = 3.0;
            sum² = MathPow(Close[i] - EmaBuff[i+1],2);
            EmaDev[i]   = EmaDev[i+1]==0.0? sum² :  (1.0-coeff1*α)*( EmaDev[i+1] + coeff1*α*sum²);  // equation no. 143
            EmaDevUp[i] = EmaBuff[i] + BandsDeviations * sqrt( EmaDev[i] );
            EmaDevLo[i] = EmaBuff[i] - BandsDeviations * sqrt( EmaDev[i] );
      // end incremental emaBB

This now looks like:

emaBB with diff coeff

As you can see the emaBB squeezes like the orgBB but it is more volatile due the the 'big' coeff1*α.

So this kind of BB - the incremental emaBB seem to be an alernative to the org BB which has to calculate at each tick Bandperiod times the squared difference - if this matters for someone.

But why is the incremental smaBB not working - anybody with an idea?

Files:
whroeder1
16808
whroeder1  
Carl Schreiber: According to my understanding I assumed that the resulting bands (Lime-Green) should be close (not exactly the same of course!) to the original BB - but they are far away. Am I doing something wrong?
The code produces one STD, and you have the BB set to (the default) two STD?
Carl Schreiber
Moderator
7835
Carl Schreiber  

The attached code shows that I use for everything the same setup:

extern int    BandsPeriod     = 20;
extern int    BandsShift      = 0;
extern double BandsDeviations = 2.0;

So any BB uses a multiplier of 2.0.

Fernando Carreiro
3887
Fernando Carreiro  
Carl Schreiber: due to a paper linked here by Fernando (thanks!) I tried to use this by indicators. First I wanted to compare it to the original - to check whether it is correct or weird.

First I compare the 'selfmade' BB (Tomato, sma dotted) against the original BB from the terminal (Magenta, solid) - as you can see (Comment err=0.0) no difference!

Then I calculated the incremental BB acc. to equation 24, 25:

According to my understanding I assumed that the resulting bands (Lime-Green) should be close (not exactly the same of course!) to the original BB - but they are far away. Am I doing something wrong?

At last I coded the incremental BB (=incremental calculation of the variance) of the ema - which is close to the original BBs. It could be improved it you increase α by e.g. 3.0*α:

As you can see the emaBB squeezes like the orgBB but it is more volatile due the the 'big' coeff1*α.

So this kind of BB - the incremental emaBB seem to be an alernative to the org BB which has to calculate at each tick Bandperiod times the squared difference - if this matters for someone.

But why is the incremental smaBB not working - anybody with an idea?

Carl, I had already posted for you a working version of EMA Bands here. Why don't you just look at the code and compare it to your rendition!

EDIT: PS! Also the "Incremental Simple Moving Variance" (equations 24 & 25) in the paper is not the variance for a "SMA". The type of Moving Average in the paper is better known by traders as the "Smoothed Moving Average". Using that for trying to make a standard "Bollinger" will not succeed. You would be creating a "Smoothed Moving Average Bollinger" and not that classical SMA Bollinger.

I know it can be confusing but mathematicians and traders sometimes call things by different names!

"Incremental Simple Moving Average" = "Cumulative Moving Average" = "SSMA - Smoothed Moving Average" (not "SMA - Simple Moving Average").

EDIT2: Actually I need to correct the previous edit, because the "Incremental Simple Moving Average" is not exactly the same as a "Smoothed" although the results are similar. It is as the other name implies "Cumulative". As it progresses, the period changes, increasing by 1 at every Bar. So on the first the period is "n=1" but by the last bar the period is equivalent to "n=Bars". It looks similar to the "Smoothed" when using a fixed period (n= constant) but technically they are different.

EDIT3: Scratch all of the above because I myself am mixing "Apples" and "Oranges", so lets start again.

The "Incremental Simple Moving Variance" (equations 24 & 25) in the paper is not the variance & standard deviation for a "SMA - Simple Moving Average". The equations in the paper for the "Incremental Simple Moving Standard Deviation" would require a variable period, starting at (n=1) for the first bar and up to (n=Bars) for the last bar. If you use a (n=constant), then the Standard Deviation will be continually growing as the calculation progresses, hence why you have such "wide" banding.

In essence you cannot use the equations in the paper for the classical Bollinger Bands. You can only use the other equations of Bollinger varaints with EMA (or SSMA) or LWMA, but not for SMA.

Also, the "SSMA - Smoothed Moving Average", is actually a variant of the EMA, so my previous scratched statements are incorrect. The weight for the SSMA is (1 / Period) while for an EMA it is (2 / ( Period + 1 ) ).

Carl Schreiber
Moderator
7835
Carl Schreiber  

The formula S[n] for the emaBB is:

emaBB

As I don't calculate the ema myself but take the one from the terminal - so I don't have to use the "latter form" but can use equation (143).

By the way it is the same as the latter form if you just do:

diff = x - mean
incr = alpha*diff
mean = mean + incr
variance = (1-alpha)*(variance + diff*incr) = (1-alpha)*(variance + diff*diff*alpha)

So I calculate in only two lines (s.a. and here without my added coeff):

            sum² = MathPow(Close[i] - EmaBuff[i+1],2);
            EmaDev[i]   = EmaDev[i+1]==0.0? sum² :  (1.0-α)*( EmaDev[i+1] + α*sum²);  // equation no. 143

So I think we do pretty much the same - no?

Concerning the sma-variance I read (right after equation 12 and as the first sentence of the next chapter "Incremental variance":

"Knuth notes [1] that equation 12 is prone to loss of precision because it takes the difference between two
large sums of similar size, and suggests equation 24 as an alternative that avoids this problem."

So from the name of the chapter "Incremental variance" and that Knuth suggests a more precise method than equation 12 (which is used by the default BB) I assume that at the end in equation 24,25 I get a more precise variance than 12 provides - isn't that correct? But what I get is practically not usable!

Fernando Carreiro
3887
Fernando Carreiro  
Carl Schreiber

Concerning the sma-variance I read (right after equation 12 and as the first sentence of the next chapter "Incremental variance":

"Knuth notes [1] that equation 12 is prone to loss of precision because it takes the difference between two large sums of similar size, and suggests equation 24 as an alternative that avoids this problem."

So from the name of the chapter "Incremental variance" and that Knuth suggests a more precise method than equation 12 (which is used by the default BB) I assume that at the end in equation 24,25 I get a more precise variance than 12 provides - isn't that correct? But what I get is practically not usable!

Since, I heavily edited my previous post and it took a while for it to settle, so please have a look at it again! It will explain why you are having those problems and why you can't use those equations for the Classical Bollinger Bands.

EDIT: PS! I can still use the equations 24 & 25 if you recalculate it only over the constant period window on every new bar as a less error prone alternative in substituting the one used by MetqQuotes (which is based on the sum of squares that is more prone to round errors).

Carl Schreiber
Moderator
7835
Carl Schreiber  

Fernado,

I don't agree here: "... has a variable period, starting at (n=1) for the first bar and up to (n=Bars) for the last bar."

if you look at eq. 7:


you see our normal calculation of the variance with n as the common known BandsPeriod. There is no hint that the meaning of n has changed.

And as S[n] means the new, the most recent variance I do understand x[n] as the actual applied price (in my case Close[i]). Only this way eq. 25

makes sense divided by the number of element from which the sma was calculated.

Carl Schreiber
Moderator
7835
Carl Schreiber  

Even more clearly is my interpretation if you look at eq. 47 of the weighted ma where he sums up the weights:

to use W[n] in the same way later:


For a simple sma with n periods all n "weigths" are equal 1 and their sum = n!

Fernando Carreiro
3887
Fernando Carreiro  
Carl Schreiber: I don't agree here: "... has a variable period, starting at (n=1) for the first bar and up to (n=Bars) for the last bar."

you see our normal calculation of the variance with n as the common known BandsPeriod. There is no hint that the meaning of n has changed.

And as S[n] means the new, the most recent variance I do understand x[n] as the actual applied price (in my case Close[i]). Only this way eq. 25

makes sense divided by the number of element from which the sma was calculated.

In the paper, it is clear that "n" is for the entire sample/data size and not for a rolling window. The variance calculation is cumulative.  For the EMA, that problem does not exist because there is no explicit dependency on the "n" period, even though the value is also cumulative (keeping tracking from the very first bar). But for the "simple" one, you either have to known "n" before hand, or you have to make it sequential (and also make adjustments to the MA equation as well to compensate).

You can use equations 24 & 25 (and 4) in your indicator, but only if you recalculate over the constant period window only on every new bar as a less error prone alternative in substituting the one used by MetaQuotes (which is based on the sum of squares that is more prone to rounding errors).

EDIT: I have to correct myself here again because I have been calling it "Incremental Simple Moving Average and Variance" which is incorrect. The correct name used by the paper is "Incremental Mean and Variance" which is more indicative of the "cumulative" effect and having no "moving window" in its calculations.

Carl Schreiber
Moderator
7835
Carl Schreiber  
Fernando Carreiro:

In the paper, it is clear that "n" is for the entire sample/data size and not for a rolling window. The variance calculation is cumulative.  For the EMA, that problem does not exist because there is no explicit dependency on the "n" period, even though the value is also cumulative (keeping tracking from the very first bar). But for the "simple" one, you either have to known "n" before hand, or you have to make it sequential (and also make adjustments to the MA equation as well to compensate).

You can use equations 24 & 25 (and 4) in your indicator, but only if you recalculate over the constant period window only on every new bar as a less error prone alternative in substituting the one used by MetaQuotes (which is based on the sum of squares that is more prone to rounding errors).

EDIT: I have to correct myself here again because I have been calling it "Incremental Simple Moving Average and Variance" which is incorrect. The correct name used by the paper is "Incremental Mean and Variance" which is more indicative of the "cumulative" effect and having no "moving window" in its calculations.

Can you provide a way to calculate the eq. 24,25?
Fernando Carreiro
3887
Fernando Carreiro  
Carl Schreiber:
Can you provide a way to calculate the eq. 24,25?

As I stated, use them only over the "window", not over the entire bar history.

For example, if your period is 5 bars, and you are currently evaluating bar [i], as a series (0 being the first bar), then run the equations only over the range [i-4] to [i] when n = 5.

Then when evaluating bar [i+1], run the equations over [i-3] to [i+1], etc.

12
To add comments, please log in or register