Gaussian Rainbow MA Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
The Gaussian Rainbow indicator is quite popular among Forex traders to help identify trends, retracements, and sideways market. Though, it's nothing special. Just a trend following indicator built from gaussian filtration.

You can use it for short-term, medium term and long term trading. I heard it makes a great companion for scalping along with a momentum indicator or oscillator. Similar to using a single or double MAs, when the stock is trading above the rainbow then we assume it's trading in an uptrend. When the candles are below the rainbow then it's bearish.

A few examples of what the Rainbow indicator looks like:

NkVXGoY.png

rJRides.png


thinkScript Code

Code:
# Gaussian Rainbow
# Eric R
# 11.24.2013

declare upper;

# Study Inputs
input gaussian       = { "ONE", "TWO", "THREE", default "FOUR" };
input data           = HL2;
input length         = 10;
input gaussianOrder  = 3;
input cloud          = yes;

# Definitions
def w;
def beta;
def alpha;

def rbi1;  
def rbi2;  
def rbi3;  
def rbi4;
def rbi5;  
def rbi6;  
def rbi7;  
def rbi8;

# Alpha Calculation
w                = ( 2 * double.Pi / length );
beta             = ( 1 - cos( w ) ) / ( power( 1.414, 2.0 / gaussianOrder ) - 1 );
alpha            = ( -beta + sqRt( beta * beta + 2 * beta) );

# Average Calculations
switch( gaussian ) {

case "ONE":
rbi1         =       alpha * data + ( 1 - alpha ) * rbi1[1];
rbi2         =       alpha * rbi1 + ( 1 - alpha ) * rbi2[1];
rbi3         =       alpha * rbi2 + ( 1 - alpha ) * rbi3[1];
rbi4         =       alpha * rbi3 + ( 1 - alpha ) * rbi4[1];
rbi5         =       alpha * rbi4 + ( 1 - alpha ) * rbi5[1];
rbi6         =       alpha * rbi5 + ( 1 - alpha ) * rbi6[1];
rbi7         =       alpha * rbi6 + ( 1 - alpha ) * rbi7[1];
rbi8         =       alpha * rbi7 + ( 1 - alpha ) * rbi8[1];

case "TWO":
rbi1         =       power( alpha, 2 )     * data    +
2 *      ( 1 - alpha )    * rbi1[1] -
power( 1 - alpha, 2 ) * rbi1[2];

rbi2         =       power( alpha, 2 )     * rbi1    +
2 *      ( 1 - alpha )    * rbi2[1] -
power( 1 - alpha, 2 ) * rbi2[2];

rbi3         =       power( alpha, 2 )     * rbi2    +
2 *      ( 1 - alpha )    * rbi3[1] -
power( 1 - alpha, 2 ) * rbi3[2];

rbi4         =       power( alpha, 2 )     * rbi3    +
2 *      ( 1 - alpha )    * rbi4[1] -
power( 1 - alpha, 2 ) * rbi4[2];

rbi5         =       power( alpha, 2 )     * rbi4    +
2 *      ( 1 - alpha )    * rbi5[1] -
power( 1 - alpha, 2 ) * rbi5[2];

rbi6         =       power( alpha, 2 )     * rbi5    +
2 *      ( 1 - alpha )    * rbi6[1] -
power( 1 - alpha, 2 ) * rbi6[2];

rbi7         =       power( alpha, 2 )     * rbi6    +
2 *      ( 1 - alpha )    * rbi7[1] -
power( 1 - alpha, 2 ) * rbi7[2];

rbi8         =       power( alpha, 2 )     * rbi7    +
2 *      ( 1 - alpha )    * rbi8[1] -
power( 1 - alpha, 2 ) * rbi8[2];

case "THREE":
rbi1        =       power( alpha, 3 )     * data    +
3 *      ( 1 – alpha )    * rbi1[1] -
3 * power( 1 - alpha, 2 ) * rbi1[2] +
power( 1 - alpha, 3 ) * rbi1[3];

rbi2        =       power( alpha, 3 )     * rbi1    +
3 *      ( 1 – alpha )    * rbi2[1] -
3 * power( 1 - alpha, 2 ) * rbi2[2] +
power( 1 - alpha, 3 ) * rbi2[3];

rbi3        =       power( alpha, 3 )     * rbi2    +
3 *      ( 1 – alpha )    * rbi3[1] -
3 * power( 1 - alpha, 2 ) * rbi3[2] +
power( 1 - alpha, 3 ) * rbi3[3];

rbi4        =       power( alpha, 3 )     * rbi3    +
3 *      ( 1 – alpha )    * rbi4[1] -
3 * power( 1 - alpha, 2 ) * rbi4[2] +
power( 1 - alpha, 3 ) * rbi4[3];

rbi5        =       power( alpha, 3 )     * rbi4    +
3 *      ( 1 – alpha )    * rbi5[1] -
3 * power( 1 - alpha, 2 ) * rbi5[2] +
power( 1 - alpha, 3 ) * rbi5[3];

rbi6        =       power( alpha, 3 )     * rbi5    +
3 *      ( 1 – alpha )    * rbi6[1] -
3 * power( 1 - alpha, 2 ) * rbi6[2] +
power( 1 - alpha, 3 ) * rbi6[3];

rbi7        =       power( alpha, 3 )     * rbi6    +
3 *      ( 1 – alpha )    * rbi7[1] -
3 * power( 1 - alpha, 2 ) * rbi7[2] +
power( 1 - alpha, 3 ) * rbi7[3];

rbi8        =       power( alpha, 3 )     * rbi7    +
3 *      ( 1 – alpha )    * rbi8[1] -
3 * power( 1 - alpha, 2 ) * rbi8[2] +
power( 1 - alpha, 3 ) * rbi8[3];

case "FOUR":
rbi1        =       power( alpha, 4 )     * data    +
4 *      ( 1 – alpha )    * rbi1[1] –
6 * power( 1 - alpha, 2 ) * rbi1[2] +
4 * power( 1 - alpha, 3 ) * rbi1[3] -
power( 1 - alpha, 4 ) * rbi1[4];

rbi2        =       power( alpha, 4 )     * rbi1    +
4 *      ( 1 – alpha )    * rbi2[1] –
6 * power( 1 - alpha, 2 ) * rbi2[2] +
4 * power( 1 - alpha, 3 ) * rbi2[3] -
power( 1 - alpha, 4 ) * rbi2[4];

rbi3        =       power( alpha, 4 )     * rbi2    +
4 *      ( 1 – alpha )    * rbi3[1] –
6 * power( 1 - alpha, 2 ) * rbi3[2] +
4 * power( 1 - alpha, 3 ) * rbi3[3] -
power( 1 - alpha, 4 ) * rbi3[4];

rbi4        =       power( alpha, 4 )     * rbi3    +
4 *      ( 1 – alpha )    * rbi4[1] –
6 * power( 1 - alpha, 2 ) * rbi4[2] +
4 * power( 1 - alpha, 3 ) * rbi4[3] -
power( 1 - alpha, 4 ) * rbi4[4];

rbi5        =       power( alpha, 4 )     * rbi4    +
4 *      ( 1 – alpha )    * rbi5[1] –
6 * power( 1 - alpha, 2 ) * rbi5[2] +
4 * power( 1 - alpha, 3 ) * rbi5[3] -
power( 1 - alpha, 4 ) * rbi5[4];

rbi6        =       power( alpha, 4 )     * rbi5    +
4 *      ( 1 – alpha )    * rbi6[1] –
6 * power( 1 - alpha, 2 ) * rbi6[2] +
4 * power( 1 - alpha, 3 ) * rbi6[3] -
power( 1 - alpha, 4 ) * rbi6[4];

rbi7        =       power( alpha, 4 )     * rbi6    +
4 *      ( 1 – alpha )    * rbi7[1] –
6 * power( 1 - alpha, 2 ) * rbi7[2] +
4 * power( 1 - alpha, 3 ) * rbi7[3] -
power( 1 - alpha, 4 ) * rbi7[4];

rbi8        =       power( alpha, 4 )     * rbi7    +
4 *      ( 1 – alpha )    * rbi8[1] –
6 * power( 1 - alpha, 2 ) * rbi8[2] +
4 * power( 1 - alpha, 3 ) * rbi8[3] -
power( 1 - alpha, 4 ) * rbi8[4];
}

# Plots
plot ga1             = rbi1;
plot ga2             = rbi2;
plot ga3             = rbi3;
plot ga4             = rbi4;
plot ga5             = rbi5;
plot ga6             = rbi6;
plot ga7             = rbi7;
plot ga8             = rbi8;

# Look and Feel
ga1.assignValueColor( color.MAGENTA );
ga2.assignValueColor( color.PLUM );
ga3.assignValueColor( color.BLUE );
ga4.assignValueColor( color.CYAN );
ga5.assignValueColor( color.GREEN );
ga6.assignValueColor( color.YELLOW );
ga7.assignValueColor( color.DARK_ORANGE );
ga8.assignValueColor( color.RED );

ga1.hideTitle();
ga2.hideTitle();
ga3.hideTitle();
ga4.hideTitle();
ga5.hideTitle();
ga6.hideTitle();
ga7.hideTitle();
ga8.hideTitle();

# Cloud Plots
addCloud( if cloud == yes then ga1 else double.NaN, ga8, color.RED, color.BLACK );
addCloud( if cloud == yes then ga1 else double.NaN, ga7, color.DARK_ORANGE, color.BLACK );
addCloud( if cloud == yes then ga1 else double.NaN, ga6, color.YELLOW, color.YELLOW );
addCloud( if cloud == yes then ga1 else double.NaN, ga5, color.GREEN, color.GREEN );
addCloud( if cloud == yes then ga1 else double.NaN, ga4, color.BLUE, color.BLUE );
addCloud( if cloud == yes then ga1 else double.NaN, ga3, color.PLUM, color.PLUM );

Shareable Link

https://tos.mx/GK5TrQ
 

markos

Well-known member
VIP
He made it quite pretty. Biggest problem is that moving ave's lag but at least longer term it could help keep someone on trend as you've alluded to.
 

henry1224

Active member
VIP
Question, is it possible to add the option to displace the averages?
here is the displaced version
Code:
# Gaussian Rainbow
# Eric R
# 11.24.2013

declare upper;

# Study Inputs
input gaussian       = { "ONE", "TWO", "THREE", default "FOUR" };
input data           = HL2;
input length         = 10;
input Displace =1;
input gaussianOrder  = 3;
input cloud          = yes;

# Definitions
def w;
def beta;
def alpha;

def rbi1; 
def rbi2; 
def rbi3; 
def rbi4;
def rbi5; 
def rbi6; 
def rbi7; 
def rbi8;

# Alpha Calculation
w                = ( 2 * double.Pi / length );
beta             = ( 1 - cos( w ) ) / ( power( 1.414, 2.0 / gaussianOrder ) - 1 );
alpha            = ( -beta + sqRt( beta * beta + 2 * beta) );

# Average Calculations
switch( gaussian ) {

case "ONE":
rbi1         =       (alpha * data + ( 1 - alpha ) * rbi1[1]);
rbi2         =       (alpha * rbi1 + ( 1 - alpha ) * rbi2[1] );;
rbi3         =       (alpha * rbi2 + ( 1 - alpha ) * rbi3[1] );;
rbi4         =       (alpha * rbi3 + ( 1 - alpha ) * rbi4[1] );;
rbi5         =       (alpha * rbi4 + ( 1 - alpha ) * rbi5[1] );;
rbi6         =       (alpha * rbi5 + ( 1 - alpha ) * rbi6[1] );;
rbi7         =       (alpha * rbi6 + ( 1 - alpha ) * rbi7[1] );;
rbi8         =       (alpha * rbi7 + ( 1 - alpha ) * rbi8[1] );;

case "TWO":
rbi1         =       (power( alpha, 2 )     * data    +
2 *      ( 1 - alpha )    * rbi1[1] -
power( 1 - alpha, 2 ) * rbi1[2] );;

rbi2         =       (power( alpha, 2 )     * rbi1    +
2 *      ( 1 - alpha )    * rbi2[1] -
power( 1 - alpha, 2 ) * rbi2[2] );;

rbi3         =       (power( alpha, 2 )     * rbi2    +
2 *      ( 1 - alpha )    * rbi3[1] -
power( 1 - alpha, 2 ) * rbi3[2] );;

rbi4         =       (power( alpha, 2 )     * rbi3    +
2 *      ( 1 - alpha )    * rbi4[1] -
power( 1 - alpha, 2 ) * rbi4[2] );;

rbi5         =       (power( alpha, 2 )     * rbi4    +
2 *      ( 1 - alpha )    * rbi5[1] -
power( 1 - alpha, 2 ) * rbi5[2] );;

rbi6         =       (power( alpha, 2 )     * rbi5    +
2 *      ( 1 - alpha )    * rbi6[1] -
power( 1 - alpha, 2 ) * rbi6[2] );;

rbi7         =       (power( alpha, 2 )     * rbi6    +
2 *      ( 1 - alpha )    * rbi7[1] -
power( 1 - alpha, 2 ) * rbi7[2] );;

rbi8         =       (power( alpha, 2 )     * rbi7    +
2 *      ( 1 - alpha )    * rbi8[1] -
power( 1 - alpha, 2 ) * rbi8[2] );;

case "THREE":
rbi1        =       power( alpha, 3 )     * data    +
3 *      ( 1 – alpha )    * rbi1[1] -
3 * power( 1 - alpha, 2 ) * rbi1[2] +
power( 1 - alpha, 3 ) * rbi1[3];;

rbi2        =       (power( alpha, 3 )     * rbi1    +
3 *      ( 1 – alpha )    * rbi2[1] -
3 * power( 1 - alpha, 2 ) * rbi2[2] +
power( 1 - alpha, 3 ) * rbi2[3] );;

rbi3        =       (power( alpha, 3 )     * rbi2    +
3 *      ( 1 – alpha )    * rbi3[1] -
3 * power( 1 - alpha, 2 ) * rbi3[2] +
power( 1 - alpha, 3 ) * rbi3[3] );;

rbi4        =       (power( alpha, 3 )     * rbi3    +
3 *      ( 1 – alpha )    * rbi4[1] -
3 * power( 1 - alpha, 2 ) * rbi4[2] +
power( 1 - alpha, 3 ) * rbi4[3] );;

rbi5        =       (power( alpha, 3 )     * rbi4    +
3 *      ( 1 – alpha )    * rbi5[1] -
3 * power( 1 - alpha, 2 ) * rbi5[2] +
power( 1 - alpha, 3 ) * rbi5[3] );;

rbi6        =       (power( alpha, 3 )     * rbi5    +
3 *      ( 1 – alpha )    * rbi6[1] -
3 * power( 1 - alpha, 2 ) * rbi6[2] +
power( 1 - alpha, 3 ) * rbi6[3] );;

rbi7        =       (power( alpha, 3 )     * rbi6    +
3 *      ( 1 – alpha )    * rbi7[1] -
3 * power( 1 - alpha, 2 ) * rbi7[2] +
power( 1 - alpha, 3 ) * rbi7[3] );;

rbi8        =      ( power( alpha, 3 )     * rbi7    +
3 *      ( 1 – alpha )    * rbi8[1] -
3 * power( 1 - alpha, 2 ) * rbi8[2] +
power( 1 - alpha, 3 ) * rbi8[3] );;

case "FOUR":
rbi1        =       (power( alpha, 4 )     * data    +
4 *      ( 1 – alpha )    * rbi1[1] –
6 * power( 1 - alpha, 2 ) * rbi1[2] +
4 * power( 1 - alpha, 3 ) * rbi1[3] -
power( 1 - alpha, 4 ) * rbi1[4] );;

rbi2        =       (power( alpha, 4 )     * rbi1    +
4 *      ( 1 – alpha )    * rbi2[1] –
6 * power( 1 - alpha, 2 ) * rbi2[2] +
4 * power( 1 - alpha, 3 ) * rbi2[3] -
power( 1 - alpha, 4 ) * rbi2[4] );;

rbi3        =       (power( alpha, 4 )     * rbi2    +
4 *      ( 1 – alpha )    * rbi3[1] –
6 * power( 1 - alpha, 2 ) * rbi3[2] +
4 * power( 1 - alpha, 3 ) * rbi3[3] -
power( 1 - alpha, 4 ) * rbi3[4] );;

rbi4        =       (power( alpha, 4 )     * rbi3    +
4 *      ( 1 – alpha )    * rbi4[1] –
6 * power( 1 - alpha, 2 ) * rbi4[2] +
4 * power( 1 - alpha, 3 ) * rbi4[3] -
power( 1 - alpha, 4 ) * rbi4[4] );;

rbi5        =       (power( alpha, 4 )     * rbi4    +
4 *      ( 1 – alpha )    * rbi5[1] –
6 * power( 1 - alpha, 2 ) * rbi5[2] +
4 * power( 1 - alpha, 3 ) * rbi5[3] -
power( 1 - alpha, 4 ) * rbi5[4] );;

rbi6        =       (power( alpha, 4 )     * rbi5    +
4 *      ( 1 – alpha )    * rbi6[1] –
6 * power( 1 - alpha, 2 ) * rbi6[2] +
4 * power( 1 - alpha, 3 ) * rbi6[3] -
power( 1 - alpha, 4 ) * rbi6[4] );;

rbi7        =       (power( alpha, 4 )     * rbi6    +
4 *      ( 1 – alpha )    * rbi7[1] –
6 * power( 1 - alpha, 2 ) * rbi7[2] +
4 * power( 1 - alpha, 3 ) * rbi7[3] -
power( 1 - alpha, 4 ) * rbi7[4] );;

rbi8        =       (power( alpha, 4 )     * rbi7    +
4 *      ( 1 – alpha )    * rbi8[1] –
6 * power( 1 - alpha, 2 ) * rbi8[2] +
4 * power( 1 - alpha, 3 ) * rbi8[3] -
power( 1 - alpha, 4 ) * rbi8[4] );;
}

# Plots
plot ga1             = rbi1[Displace];
plot ga2             = rbi2[Displace];
plot ga3             = rbi3[Displace];
plot ga4             = rbi4[Displace];
plot ga5             = rbi5[Displace];
plot ga6             = rbi6[Displace];
plot ga7             = rbi7[Displace];
plot ga8             = rbi8[Displace];

# Look and Feel
ga1.assignValueColor( color.MAGENTA );
ga2.assignValueColor( color.PLUM );
ga3.assignValueColor( color.BLUE );
ga4.assignValueColor( color.CYAN );
ga5.assignValueColor( color.GREEN );
ga6.assignValueColor( color.YELLOW );
ga7.assignValueColor( color.DARK_ORANGE );
ga8.assignValueColor( color.RED );

ga1.hideTitle();
ga2.hideTitle();
ga3.hideTitle();
ga4.hideTitle();
ga5.hideTitle();
ga6.hideTitle();
ga7.hideTitle();
ga8.hideTitle();

# Cloud Plots
addCloud( if cloud == yes then ga1 else double.NaN, ga8, color.RED, color.BLACK );
addCloud( if cloud == yes then ga1 else double.NaN, ga7, color.DARK_ORANGE, color.BLACK );
addCloud( if cloud == yes then ga1 else double.NaN, ga6, color.YELLOW, color.YELLOW );
addCloud( if cloud == yes then ga1 else double.NaN, ga5, color.GREEN, color.GREEN );
addCloud( if cloud == yes then ga1 else double.NaN, ga4, color.BLUE, color.BLUE );
addCloud( if cloud == yes then ga1 else double.NaN, ga3, color.PLUM, color.PLUM );
 

henry1224

Active member
VIP
Could you share your settings that makes it leading?
The poster asked if there was a way to displace the averages.

I provided a way by displacing the plotted averages
IE:
# Plots
plot ga1 = rbi1[Displace];
plot ga2 = rbi2[Displace];
plot ga3 = rbi3[Displace];
plot ga4 = rbi4[Displace];
plot ga5 = rbi5[Displace];
plot ga6 = rbi6[Displace];
plot ga7 = rbi7[Displace];
plot ga8 = rbi8[Displace];

How you use this is up to you?

Moving them to the right, you can catch more of movement, move them to the left, you will run into a delay of data because it will need to wait until the next bar is formed before it will calculate a value
 

henry1224

Active member
VIP
This is a question for BenTen, are the pink horizontal Lines, hand drawn support & resistance, or are they some sort of formulary?
 

Similar threads

Top