Market Bias (CEREBR) Indicator for ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
Author Message:

Here's how it works : The script tries to determine the overall direction of the market, using smoothed Heiken Ashi candles. The coloring system (using bright and dark colors) is an attempt to detect strong market and weak market conditions. There's also an oscillator within the script, but for now it isn't plotted.

bHKeA7d.png


Credits to @jackvmk, I used part of his open-script code in this indicator.\

I have considered using the slope of the indicator plot as a filter for ranging market conditions. The plot goes relatively flat in 'flat' markets. However, I have not done anything about that yet. Maybe some other time.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Professeur_X
#indicator(title='HA Market Bias', shorttitle='HA Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023

input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input show_HA = yes;        # "Show HA Plot/ Market Bias"
input show_Lines = yes;     # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input MovAvg = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"

def na = Double.NaN;
#---- Colors
DefineGlobalColor("green1" , CreateColor(33,166,153));
DefineGlobalColor("green2" , CreateColor(24,104,96));
DefineGlobalColor("Red1" , CreateColor(166,38,51));
DefineGlobalColor("Red2" , CreateColor(104,24,32));
#// Calculations {
def o = MovingAverage(MovAvg, open, Period);
def c = MovingAverage(MovAvg, close, Period);
def h = MovingAverage(MovAvg, high, Period);
def l = MovingAverage(MovAvg, low, Period);

def haclose = (o + h + l + c) / 4;
def xhaopen = (o + c) / 2;
def haopen = if IsNaN(xhaopen[1]) then (o + c) / 2 else (xhaopen[1] + haclose[1]) / 2;
def hahigh = Max(h, Max(haopen, haclose));
def halow = Min(l, Min(haopen, haclose));

def o2 = MovingAverage(MovAvg, haopen, Smoothing);
def c2 = MovingAverage(MovAvg, haclose, Smoothing);
def h2 = MovingAverage(MovAvg, hahigh, Smoothing);
def l2 = MovingAverage(MovAvg, halow, Smoothing);

def ha_avg = (h2 + l2) / 2;
 
#// Oscillator {

def osc_bias = 100 *(c2 - o2);
def osc_smooth = MovingAverage(MovAvg, osc_bias, OscillatorPeriod);

def sigcolor =
  if (osc_bias > 0) and (osc_bias >= osc_smooth) then 2 else
  if (osc_bias > 0) and (osc_bias < osc_smooth) then 1 else
  if (osc_bias < 0) and (osc_bias <= osc_smooth) then -2 else
  if (osc_bias < 0) and (osc_bias > osc_smooth) then -1 else 0;

#// Plots {
plot p_h = if !show_Lines then na else h2;#, "Bias High", color=color(na), display=display.none, editable=false)
p_h.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);
plot p_l = if !show_Lines then na else l2;#, "Bias Low", color=color(na), display=display.none, editable=false)
p_l.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);
plot p_avg = if !show_Lines then na else ha_avg;#, "Bias Avergae", color=color(na), display=display.none, editable=false)
p_avg.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);

AddCloud (if show_cloud and sigcolor==2 then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and sigcolor>0 then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and sigcolor==-2 then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and sigcolor<0 then h2 else na, l2, GlobalColor("Red2"));


# Plot the new Chart
def col = if show_HA then o2 > c2 else na;

AddChart(high = if col then na else h2, low = if col then na else l2 , open = if col then na else c2,  close = if col then na else o2,
         type = ChartType.CANDLE, growcolor =  CreateColor(38,166,154));

AddChart(high = if !col then na else h2, low = if !col then na else l2 , open = if !col then na else o2,  close = if !col then na else c2,
         type = ChartType.CANDLE, growcolor =  CreateColor(239,83,80));


# --- END CODE
 
Last edited by a moderator:
@samer800 This indicator looks most interesting. Thank you for creating it. Would it be possible to add an option to hide the regular candlesticks and also to add some of the non traditional moving averages as options such as: TEMA, LSMA, VWMA, ALMA, McGinley? Thank you for your amazing contributions!

P.S. Love the idea for using the slope of the indicator plot as a filter for ranging market conditions.
 
@samer800 This indicator looks most interesting. Thank you for creating it. Would it be possible to add an option to hide the regular candlesticks and also to add some of the non traditional moving averages as options such as: TEMA, LSMA, VWMA, ALMA, McGinley? Thank you for your amazing contributions!

P.S. Love the idea for using the slope of the indicator plot as a filter for ranging market conditions.
all credit goes to the original creator of the indicator, I just converted to TOS.
I will try to more Moving averages.
 
Author Message:

Here's how I works : The script tries to determine the overall direction of the market, using smoothed Heiken Ashi candles. The coloring system (using bright and dark colors) is an attempt to detect strong market and weak market conditions. There's also an oscillator within the script, but for now it isn't plotted.

bHKeA7d.png


Credits to @jackvmk, I used part of his open-script code in this indicator.\

I have considered using the slope of the indicator plot as a filter for ranging market conditions. The plot goes relatively flat in 'flat' markets. However, I have not done anything about that yet. Maybe some other time.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Professeur_X
#indicator(title='HA Market Bias', shorttitle='HA Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023

input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input show_HA = yes;        # "Show HA Plot/ Market Bias"
input show_Lines = yes;     # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input MovAvg = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"

def na = Double.NaN;
#---- Colors
DefineGlobalColor("green1" , CreateColor(33,166,153));
DefineGlobalColor("green2" , CreateColor(24,104,96));
DefineGlobalColor("Red1" , CreateColor(166,38,51));
DefineGlobalColor("Red2" , CreateColor(104,24,32));
#// Calculations {
def o = MovingAverage(MovAvg, open, Period);
def c = MovingAverage(MovAvg, close, Period);
def h = MovingAverage(MovAvg, high, Period);
def l = MovingAverage(MovAvg, low, Period);

def haclose = (o + h + l + c) / 4;
def xhaopen = (o + c) / 2;
def haopen = if IsNaN(xhaopen[1]) then (o + c) / 2 else (xhaopen[1] + haclose[1]) / 2;
def hahigh = Max(h, Max(haopen, haclose));
def halow = Min(l, Min(haopen, haclose));

def o2 = MovingAverage(MovAvg, haopen, Smoothing);
def c2 = MovingAverage(MovAvg, haclose, Smoothing);
def h2 = MovingAverage(MovAvg, hahigh, Smoothing);
def l2 = MovingAverage(MovAvg, halow, Smoothing);

def ha_avg = (h2 + l2) / 2;
 
#// Oscillator {

def osc_bias = 100 *(c2 - o2);
def osc_smooth = MovingAverage(MovAvg, osc_bias, OscillatorPeriod);

def sigcolor =
  if (osc_bias > 0) and (osc_bias >= osc_smooth) then 2 else
  if (osc_bias > 0) and (osc_bias < osc_smooth) then 1 else
  if (osc_bias < 0) and (osc_bias <= osc_smooth) then -2 else
  if (osc_bias < 0) and (osc_bias > osc_smooth) then -1 else 0;

#// Plots {
plot p_h = if !show_Lines then na else h2;#, "Bias High", color=color(na), display=display.none, editable=false)
p_h.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);
plot p_l = if !show_Lines then na else l2;#, "Bias Low", color=color(na), display=display.none, editable=false)
p_l.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);
plot p_avg = if !show_Lines then na else ha_avg;#, "Bias Avergae", color=color(na), display=display.none, editable=false)
p_avg.AssignValueColor(if sigcolor==2 then GlobalColor("green1") else
                     if sigcolor==1 then GlobalColor("green2") else
                     if sigcolor==-2 then GlobalColor("Red1") else
                     if sigcolor==-1 then GlobalColor("Red2") else Color.GRAY);

AddCloud (if show_cloud and sigcolor==2 then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and sigcolor>0 then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and sigcolor==-2 then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and sigcolor<0 then h2 else na, l2, GlobalColor("Red2"));


# Plot the new Chart
def col = if show_HA then o2 > c2 else na;

AddChart(high = if col then na else h2, low = if col then na else l2 , open = if col then na else c2,  close = if col then na else o2,
         type = ChartType.CANDLE, growcolor =  CreateColor(38,166,154));

AddChart(high = if !col then na else h2, low = if !col then na else l2 , open = if !col then na else o2,  close = if !col then na else c2,
         type = ChartType.CANDLE, growcolor =  CreateColor(239,83,80));


# --- END CODE
The look of this one is very interesting. Great work.
 
@samer800 This indicator looks most interesting. Thank you for creating it. Would it be possible to add an option to hide the regular candlesticks and also to add some of the non traditional moving averages as options such as: TEMA, LSMA, VWMA, ALMA, McGinley? Thank you for your amazing contributions!

P.S. Love the idea for using the slope of the indicator plot as a filter for ranging market conditions.
check below... Added more mov Avg options.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Professeur_X
#indicator(title='HA Market Bias', shorttitle='HA Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
# Update - Added more MovingAverage Avg options

input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input show_HA = yes;        # "Show HA Plot/ Market Bias"
input show_Lines = yes;     # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input maType  = {SMA,default EMA, SMMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
#input MovAvg = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"

def na = Double.NaN;
#---- Colors
DefineGlobalColor("green1" , CreateColor(33, 166, 153));
DefineGlobalColor("green2" , CreateColor(24, 104, 96));
DefineGlobalColor("Red1" , CreateColor(166, 38, 51));
DefineGlobalColor("Red2" , CreateColor(104, 24, 32));
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
#rms(source, length)=>
script rms {
    input source = close;
    input length = 14;
    def rms = Sqrt(Sum(Power(source, 2), length) / length);
    plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
    input values = close;
    input length = 20;
    def stddev = length / 4;
    def indices = length - 1;
    def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
    def sum = Sum(values * weights, length);
    def gMA = sum / Sum(weights, length);
    plot return = gMA;
}
#pine_swma(source) =>
script swma {
    input source = close;
    def swma = source[3] * 1 / 6 + source[2] * 2 / 6 +  source[1] * 2 / 6 + source[0] * 1 / 6;
    plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;

    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;

    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));

    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);

    plot ALMA = sum  / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 , mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)), src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * nz(v, 1), len) / SimpleMovingAvg(nz(v, 1), len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input type = "SMA";
    input source = close;
    input length = 14;
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "RMS"   then RMS(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "ALMA" then ALMA(source, length) else
        if type == "SWMA" then SWMA(source) else
        if type == "Gaussian"   then Gaussian(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}

#// Calculations {
def o = multiMa(maType, open, Period);
def c = multiMa(maType, close, Period);
def h = multiMa(maType, high, Period);
def l = multiMa(maType, low, Period);

def haclose = (o + h + l + c) / 4;
def xhaopen = (o + c) / 2;
def haopen = if IsNaN(xhaopen[1]) then (o + c) / 2 else (xhaopen[1] + haclose[1]) / 2;
def hahigh = Max(h, Max(haopen, haclose));
def halow = Min(l, Min(haopen, haclose));

def o2 = multiMa(maType, haopen, Smoothing);
def c2 = multiMa(maType, haclose, Smoothing);
def h2 = multiMa(maType, hahigh, Smoothing);
def l2 = multiMa(maType, halow, Smoothing);

def ha_avg = (h2 + l2) / 2;
    
#// Oscillator {

def osc_bias = 100 * (c2 - o2);
def osc_smooth = multiMa(maType, osc_bias, OscillatorPeriod);

def sigcolor =
  if (osc_bias > 0) and (osc_bias >= osc_smooth) then 2 else 
  if (osc_bias > 0) and (osc_bias < osc_smooth) then 1 else
  if (osc_bias < 0) and (osc_bias <= osc_smooth) then -2 else
  if (osc_bias < 0) and (osc_bias > osc_smooth) then -1 else 0;

#// Plots {
plot p_h = if !show_Lines then na else h2;#, "Bias High", color=color(na), display=display.none, editable=false)
p_h.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);
plot p_l = if !show_Lines then na else l2;#, "Bias Low", color=color(na), display=display.none, editable=false)
p_l.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);
plot p_avg = if !show_Lines then na else ha_avg;#, "Bias Avergae", color=color(na), display=display.none, editable=false)
p_avg.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);

AddCloud (if show_cloud and sigcolor == 2 then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and sigcolor > 0 then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and sigcolor == -2 then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and sigcolor < 0 then h2 else na, l2, GlobalColor("Red2"));


# Plot the new Chart
def col = if show_HA then o2 > c2 else na;

AddChart(high = if col then na else h2, low = if col then na else l2 , open = if col then na else c2,  close = if col then na else o2,
         type = ChartType.CANDLE, growcolor =  CreateColor(38, 166, 154));

AddChart(high = if !col then na else h2, low = if !col then na else l2 , open = if !col then na else o2,  close = if !col then na else c2,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));


# --- END CODE
 
@sameer800 thank you for creating/converting this one.
 
Last edited by a moderator:
I added some regression lines for overbought/oversold levels. http://tos.mx/ZTanGCH

1691328305483.png

Ruby:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Professeur_X
#indicator(title='HA Market Bias', shorttitle='HA Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
# Update - Added more MovingAverage Avg options

input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input show_HA = no;        # "Show HA Plot/ Market Bias"
input show_Lines = yes;     # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input maType  = {SMA,default EMA, SMMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
#input MovAvg = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"

def na = Double.NaN;
#---- Colors
DefineGlobalColor("green1" , CreateColor(33, 166, 153));
DefineGlobalColor("green2" , CreateColor(24, 104, 96));
DefineGlobalColor("Red1" , CreateColor(166, 38, 51));
DefineGlobalColor("Red2" , CreateColor(104, 24, 32));
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
#rms(source, length)=>
script rms {
    input source = close;
    input length = 14;
    def rms = Sqrt(Sum(Power(source, 2), length) / length);
    plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
    input values = close;
    input length = 20;
    def stddev = length / 4;
    def indices = length - 1;
    def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
    def sum = Sum(values * weights, length);
    def gMA = sum / Sum(weights, length);
    plot return = gMA;
}
#pine_swma(source) =>
script swma {
    input source = close;
    def swma = source[3] * 1 / 6 + source[2] * 2 / 6 +  source[1] * 2 / 6 + source[0] * 1 / 6;
    plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;

    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;

    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));

    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);

    plot ALMA = sum  / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 , mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)), src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * nz(v, 1), len) / SimpleMovingAvg(nz(v, 1), len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input type = "SMA";
    input source = close;
    input length = 14;
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "RMS"   then RMS(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "ALMA" then ALMA(source, length) else
        if type == "SWMA" then SWMA(source) else
        if type == "Gaussian"   then Gaussian(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}

#// Calculations {
def o = multiMa(maType, open, Period);
def c = multiMa(maType, close, Period);
def h = multiMa(maType, high, Period);
def l = multiMa(maType, low, Period);

def haclose = (o + h + l + c) / 4;
def xhaopen = (o + c) / 2;
def haopen = if IsNaN(xhaopen[1]) then (o + c) / 2 else (xhaopen[1] + haclose[1]) / 2;
def hahigh = Max(h, Max(haopen, haclose));
def halow = Min(l, Min(haopen, haclose));

def o2 = multiMa(maType, haopen, Smoothing);
def c2 = multiMa(maType, haclose, Smoothing);
def h2 = multiMa(maType, hahigh, Smoothing);
def l2 = multiMa(maType, halow, Smoothing);

def ha_avg = (h2 + l2) / 2;
    
#// Oscillator {

def osc_bias = 100 * (c2 - o2);
def osc_smooth = multiMa(maType, osc_bias, OscillatorPeriod);

def sigcolor =
  if (osc_bias > 0) and (osc_bias >= osc_smooth) then 2 else
  if (osc_bias > 0) and (osc_bias < osc_smooth) then 1 else
  if (osc_bias < 0) and (osc_bias <= osc_smooth) then -2 else
  if (osc_bias < 0) and (osc_bias > osc_smooth) then -1 else 0;

#// Plots {
plot p_h = if !show_Lines then na else h2;#, "Bias High", color=color(na), display=display.none, editable=false)
p_h.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);
plot p_l = if !show_Lines then na else l2;#, "Bias Low", color=color(na), display=display.none, editable=false)
p_l.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);
plot p_avg = if !show_Lines then na else ha_avg;#, "Bias Average", color=color(na), display=display.none, editable=false)
p_avg.AssignValueColor(if sigcolor == 2 then GlobalColor("green1") else
                     if sigcolor == 1 then GlobalColor("green2") else
                     if sigcolor == -2 then GlobalColor("Red1") else
                     if sigcolor == -1 then GlobalColor("Red2") else Color.GRAY);

AddCloud (if show_cloud and sigcolor == 2 then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and sigcolor > 0 then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and sigcolor == -2 then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and sigcolor < 0 then h2 else na, l2, GlobalColor("Red2"));


# Plot the new Chart
def col = if show_HA then o2 > c2 else na;

AddChart(high = if col then na else h2, low = if col then na else l2 , open = if col then na else c2,  close = if col then na else o2,
         type = ChartType.CANDLE, growcolor =  CreateColor(38, 166, 154));

AddChart(high = if !col then na else h2, low = if !col then na else l2 , open = if !col then na else o2,  close = if !col then na else c2,
         type = ChartType.CANDLE, growcolor =  CreateColor(239, 83, 80));

# --- END CODE

#Regression Bands
input Bands = yes;
input deviations1 = 1.00;  #set your deviation units here.
input deviations = 1.618;  #set your deviation units here.
input length = 500; #set your channel lookback period here.
input price = close;

def stdDeviation = StDevAll(price, length);
plot HighBand = if Bands then p_avg + deviations * stdDeviation else Double.NaN;
HighBand.SetDefaultColor(Color.RED);
plot LowBand = if Bands then p_avg - deviations * stdDeviation else Double.NaN;
LowBand.SetDefaultColor(Color.GREEN);
plot HighBand1 = if Bands then p_avg + deviations1 * stdDeviation else Double.NaN;
HighBand1.SetDefaultColor(Color.LIGHT_RED);
plot LowBand1 = if Bands then p_avg - deviations1 * stdDeviation else Double.NaN;

plot midHI = (HighBand1 - p_avg)/2 + p_avg;
midHI.setDefaultColor(color.gray);
plot midLO = (p_avg - LowBand1)/2 + LowBand1;
midLO.setDefaultColor(color.gray);

LowBand1.SetDefaultColor(Color.LIGHT_GREEN);
DefineGlobalColor("Bullish", Color.DARK_GREEN);
DefineGlobalColor("Bearish", Color.DARK_RED);
LowBand1.HideTitle();
LowBand1.HideBubble();
HighBand1.HideTitle();
HighBand1.HideBubble();
LowBand.HideTitle();
LowBand.HideBubble();
HighBand.HideTitle();
HighBand.HideBubble();

AddCloud(HighBand, HighBand1, GlobalColor("Bearish"), GlobalColor("Bullish"));
AddCloud(LowBand1, LowBand, GlobalColor("Bullish"), GlobalColor("Bearish"));
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
376 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top