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.
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
CODE - V1.
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.
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: