Market Bias (CEREBR) Indicator for ThinkOrSwim

samer800

Moderator - 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.

UPDATED CODE - Added MTF, Bar Color, StDev, Label

CSS:
#// Indicator for TOS
#// © Professeur_X
#indicator(title='Market Bias (CEREBR)', shorttitle='Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
# Update - Added MTF, Bar Color, StDev, Label, by Sam4Cok@Samer800    - 08/2024

input colorBars = yes;
input showMarketBiasLabel = yes;
input timeframe = AggregationPeriod.FIVE_MIN;
input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input ShowHaCandles = yes;  # "Show HA Plot/ Market Bias"
input ShowMarketBias = yes; # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input movAvgType  = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"
input ShowStDev = no;       # "Show StDev"
input stdev_length = 20;    # "St. Dev Length"
input stdev_mult = 3.0;     # "St. Dev Multiplier"

def na = Double.NaN;
def isrealtime = !isNaN(close);
def current = GetAggregationPeriod();
def HTF = Max(current, timeframe);
#---- 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(movAvgType, open(Period = HTF), Period);
def c = MovingAverage(movAvgType, close (Period = HTF), Period);
def h = MovingAverage(movAvgType, high (Period = HTF), Period);
def l = MovingAverage(movAvgType, low (Period = HTF), Period);

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

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

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

def osc_bias = 100 * (c2 - o2);
def osc_smooth = MovingAverage(movAvgType, osc_bias, OscillatorPeriod);
def sigcol = if isrealtime then
  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 sigcol[1] else sigcol[1];

#// Plots {
plot p_h   = if !ShowMarketBias then na else h2;    # "Bias High",
plot p_avg = if !ShowMarketBias then na else ha_avg;# "Bias Avergae"
plot p_l   = if !ShowMarketBias then na else l2;    # "Bias Low"
p_h.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
p_avg.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
p_l.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
#// StDev calculations
def stdevHigh = stdev(h2, stdev_length);
def stdevLow  = stdev(l2, stdev_length);
def upper_stdev = h2 + stdevHigh * stdev_mult;
def lower_stdev = l2 - stdevLow * stdev_mult;

plot UpperStdv = if ShowStDev then upper_stdev else na; #, "Upper StDev"
plot LowerStdv = if ShowStDev then lower_stdev else na; #, "Lower StDev"
UpperStdv.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
LowerStdv.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
#-- Clouds
def col = if isrealtime then sigcol else na;
AddCloud (if show_cloud and (col==2 or col[-1]==2) then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and (col==1 or col[-1]==1) then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and (col==-2 or col[-1]==-2) then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and (col==-1 or col[-1]==-1) then h2 else na, l2, GlobalColor("Red2"));

# Plot the new Chart
def col0 = if ShowHaCandles then sigcol==0 else na;
def col2 = if ShowHaCandles then sigcol==2 else na;
def col1 = if ShowHaCandles then sigcol==1 else na;
def colN2 = if ShowHaCandles then sigcol==-2 else na;
def colN1 = if ShowHaCandles then sigcol==-1 else na;

AddChart(open = if col2 then o2 else na, high = h2 , low = l2 ,   close = if col2 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("green1"));
AddChart(open = if col1 then o2 else na, high = h2 , low = l2 ,   close = if col1 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("green2"));
AddChart(open = if colN2 then o2 else na, high = h2 , low = l2 ,   close = if colN2 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("Red1"));
AddChart(open = if colN1 then o2 else na, high = h2 , low = l2 ,   close = if colN1 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("Red2"));
AddChart(open = if col0 then o2 else na, high = h2 , low = l2 ,   close = if col0 then c2 else na,
         type = ChartType.CANDLE, growcolor = Color.GRAY);

# Label

AddLabel(showMarketBiasLabel,
                 if sigcol == 2 then "Strong Bullish Bias" else
                 if sigcol == 1 then "Weak Bullish Bias" else
                 if sigcol ==-2 then "Strong Bearish Bias" else
                 if sigcol ==-1 then "Weak Bearish Bias" else "No Bias",
                 if sigcol == 2 then Color.GREEN else
                 if sigcol == 1 then Color.DARK_GREEN else
                 if sigcol ==-2 then Color.RED else
                 if sigcol ==-1 then Color.DARK_RED else Color.GRAY);
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if sigcol == 2 then GlobalColor("green1") else
                 if sigcol == 1 then GlobalColor("green2") else
                 if sigcol == -2 then GlobalColor("Red1") else
                 if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);

# --- END CODE

CODE - V1.

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:
@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:
Can someone please help me make a scanner for the Bias Indicator?

I cant seem to get it to work TOS says too complicated for a scan... I will post the script below....
I am trying to get it to scan for stocks that closed the weekly candle close crossed the Market Bias Level... its probably stupid easy to do if you know what your doing... im just not good at this stuff!

any help would be greatly appreciated, thanks!!
 
Last edited by a moderator:
Can someone please help me make a scanner for the Bias Indicator?

I cant seem to get it to work TOS says too complicated for a scan... I will post the script below....
I am trying to get it to scan for stocks that closed the weekly candle close crossed the Market Bias Level... its probably stupid easy to do if you know what your doing... im just not good at this stuff!

any help would be greatly appreciated, thanks!!

You did not provide specificity as to which of the many plots that you are seeking for price to cross.
The below script signals when close crosses the Heiken Ashi Moving Average.

The below script has been pared down to be less complex.
Yes, it will work in the ToS Scan Hacker.
x3odDs1.png

Ruby:
# Price Crosses The Heiken Ashi MovingAverage
#indicator(title='HA Market Bias', shorttitle='HA Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023

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;
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;
 
plot upArrow = close crosses above ha_avg ;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(color.cyan) ;
upArrow.SetLineWeight(3);

#plot dnArrow = close crosses below ha_avg ;
#dnArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
#dnArrow.SetDefaultColor(color.magenta) ;
#dnArrow.SetLineWeight(3);
 
Last edited:
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.

View attachment 17338

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.

UPDATED CODE - Added MTF, Bar Color, StDev, Label

CSS:
#// Indicator for TOS
#// © Professeur_X
#indicator(title='Market Bias (CEREBR)', shorttitle='Market Bias', overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
# Update - Added MTF, Bar Color, StDev, Label, by Sam4Cok@Samer800    - 08/2024

input colorBars = yes;
input showMarketBiasLabel = yes;
input timeframe = AggregationPeriod.FIVE_MIN;
input show_cloud = yes;     # "Show HA Plot/ Market Bias"
input ShowHaCandles = yes;  # "Show HA Plot/ Market Bias"
input ShowMarketBias = yes; # "Show HA Plot/ Market Bias"
input Period = 60;          # 'Period', group="HA Market Bias")
input Smoothing = 10;       # 'Smoothing', group="HA Market Bias")
input movAvgType  = AverageType.EXPONENTIAL;
input OscillatorPeriod = 7; # "Oscillator Period"
input ShowStDev = no;       # "Show StDev"
input stdev_length = 20;    # "St. Dev Length"
input stdev_mult = 3.0;     # "St. Dev Multiplier"

def na = Double.NaN;
def isrealtime = !isNaN(close);
def current = GetAggregationPeriod();
def HTF = Max(current, timeframe);
#---- 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(movAvgType, open(Period = HTF), Period);
def c = MovingAverage(movAvgType, close (Period = HTF), Period);
def h = MovingAverage(movAvgType, high (Period = HTF), Period);
def l = MovingAverage(movAvgType, low (Period = HTF), Period);

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

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

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

def osc_bias = 100 * (c2 - o2);
def osc_smooth = MovingAverage(movAvgType, osc_bias, OscillatorPeriod);
def sigcol = if isrealtime then
  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 sigcol[1] else sigcol[1];

#// Plots {
plot p_h   = if !ShowMarketBias then na else h2;    # "Bias High",
plot p_avg = if !ShowMarketBias then na else ha_avg;# "Bias Avergae"
plot p_l   = if !ShowMarketBias then na else l2;    # "Bias Low"
p_h.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
p_avg.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
p_l.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
#// StDev calculations
def stdevHigh = stdev(h2, stdev_length);
def stdevLow  = stdev(l2, stdev_length);
def upper_stdev = h2 + stdevHigh * stdev_mult;
def lower_stdev = l2 - stdevLow * stdev_mult;

plot UpperStdv = if ShowStDev then upper_stdev else na; #, "Upper StDev"
plot LowerStdv = if ShowStDev then lower_stdev else na; #, "Lower StDev"
UpperStdv.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
LowerStdv.AssignValueColor(if sigcol == 2 then GlobalColor("green1") else
                     if sigcol == 1 then GlobalColor("green2") else
                     if sigcol == -2 then GlobalColor("Red1") else
                     if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);
#-- Clouds
def col = if isrealtime then sigcol else na;
AddCloud (if show_cloud and (col==2 or col[-1]==2) then h2 else na, l2, GlobalColor("green1"));
AddCloud (if show_cloud and (col==1 or col[-1]==1) then h2 else na, l2, GlobalColor("green2"));
AddCloud (if show_cloud and (col==-2 or col[-1]==-2) then h2 else na, l2, GlobalColor("Red1"));
AddCloud (if show_cloud and (col==-1 or col[-1]==-1) then h2 else na, l2, GlobalColor("Red2"));

# Plot the new Chart
def col0 = if ShowHaCandles then sigcol==0 else na;
def col2 = if ShowHaCandles then sigcol==2 else na;
def col1 = if ShowHaCandles then sigcol==1 else na;
def colN2 = if ShowHaCandles then sigcol==-2 else na;
def colN1 = if ShowHaCandles then sigcol==-1 else na;

AddChart(open = if col2 then o2 else na, high = h2 , low = l2 ,   close = if col2 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("green1"));
AddChart(open = if col1 then o2 else na, high = h2 , low = l2 ,   close = if col1 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("green2"));
AddChart(open = if colN2 then o2 else na, high = h2 , low = l2 ,   close = if colN2 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("Red1"));
AddChart(open = if colN1 then o2 else na, high = h2 , low = l2 ,   close = if colN1 then c2 else na,
         type = ChartType.CANDLE, growcolor = GlobalColor("Red2"));
AddChart(open = if col0 then o2 else na, high = h2 , low = l2 ,   close = if col0 then c2 else na,
         type = ChartType.CANDLE, growcolor = Color.GRAY);

# Label

AddLabel(showMarketBiasLabel,
                 if sigcol == 2 then "Strong Bullish Bias" else
                 if sigcol == 1 then "Weak Bullish Bias" else
                 if sigcol ==-2 then "Strong Bearish Bias" else
                 if sigcol ==-1 then "Weak Bearish Bias" else "No Bias",
                 if sigcol == 2 then Color.GREEN else
                 if sigcol == 1 then Color.DARK_GREEN else
                 if sigcol ==-2 then Color.RED else
                 if sigcol ==-1 then Color.DARK_RED else Color.GRAY);
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if sigcol == 2 then GlobalColor("green1") else
                 if sigcol == 1 then GlobalColor("green2") else
                 if sigcol == -2 then GlobalColor("Red1") else
                 if sigcol == -1 then GlobalColor("Red2") else Color.GRAY);

# --- END CODE

CODE - V1.

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
Thanks for the great indicator. Does the 5 min default timeframe mean it will show labels (whether its bullish or bearish) based on 5 min timeframe OR it will work only on 5 min timeframe?
 
Just to clarify if I'm on daily timeframe with this 5 min default setting and if the label says bullish on daily timeframe, does it mean it's bullish on daily or 5 min timeframe?
 
Thanks for the great indicator. Does the 5 min default timeframe mean it will show labels (whether its bullish or bearish) based on 5 min timeframe OR it will work only on 5 min timeframe?

Just to clarify if I'm on daily timeframe with this 5 min default setting and if the label says bullish on daily timeframe, does it mean it's bullish on daily or 5 min timeframe?

This is one of the amazing @samer800 coolest tricks to date:
def HTF = Max(current, timeframe);

The code calculates on the maximum timeframe.
Therefore, if your chart is set to daily and the default timeframe is 5-min; the script uses the highest timeframe: DAILY.

It works the opposite direction also. If the default timeframe is changed to DAILY; no matter what interday timeframe your chart is, the script uses the highest timeframe: DAILY.

Pretty neat, eh?
 
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
290 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