MWho is in Control For ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
@APOT7 member request: https://usethinkscript.com/threads/convert-tradingview-mwho-is-in-control.13290/
use the below, should work with you. I added option to enable/Disable the wicks in calculating HH/LL.

ksyUO5c.png

pcGX3GS.png


CODE 1 for Bull or Bear

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input useWick = no;
input src = close;
input length = 13;
input signal = 3;
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 10;

def na = Double.NaN;

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}

#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1],price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}

def lowest = Lowest(if(useWick,low,src), length);
def highest = Highest(if(useWick,high,src), length);
def diffBull = (src - lowest) * 100 / lowest;
def diffBear = (highest - src) * 100 / highest;
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - Average(diffBullS, signal) >= 0,2,1);
def diffBearColor = If(diffBearS - Average(diffBearS, signal) >= 0,2,1);
def diffColor = If(differential - Average(differential, signal) >= 0, 1, 0);

#-- Plot
plot Diff = If(DisplayDifferential, differential, na);#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if IsNaN(close) then na else 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = If(BearSide, (If(smooth, diffBearS, diffBear)), na);#, color=diffBearColor, linewidth=2, style=columns)
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);#, color=diffBullColor, linewidth=2, style=columns)
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot dBear = If(BearSide, diffBearS, na);#, color=diffBearColor, linewidth=2)
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);#, color=diffBullColor, linewidth=2)
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);


#-- END Code

CODE 2 : modified it and combined 2 similar indicators with different view.

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input ChartStyle = {Default "Bulls or Bears", "Sentiment"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA};
input useWick = no;
input src = hl2;
input length = 18;
input signalLength = 3;
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 5;

def na = Double.NaN;
def style = if ChartStyle==ChartStyle."Sentiment" then 1 else 0;

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1],price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
input src = close;
input len = 18;
input lowerBand = 5;
input smooth = yes;
input useWick = no;
    def lowest = lowest(if(useWick,low,src),len);
    def highest = highest(if(useWick,high,src),len);
    def diffBull = (src-lowest)*100/lowest;
    def diffBear = (highest-src)*100/highest;
    def bbs = diffBull*100/(diffBear + diffBull);
    def bbSentiment = if(smooth,ssFilter(bbs,lowerBand),bbs);
  plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#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 * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "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 == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}

def lowest = Lowest(if(useWick,low,src), length);
def highest = Highest(if(useWick,high,src), length);
def diffBull = if(style,na,(src - lowest) * 100 / lowest);
def diffBear = if(style,na,(highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, signalLength,maType) >= 0,1,0);
def diffBearColor = If(diffBearS - ma(diffBearS, signalLength,maType) >= 0,1,0);
def diffColor = If(differential - ma(differential, signalLength,maType) >= 0, 1, 0);

#-- Plot
plot Diff = if(style,na,If(DisplayDifferential, differential, na));#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if(style,na,if(IsNaN(close), na,0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = -If(BearSide,(If(smooth, diffBearS, diffBear)), na);
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot dBear = -If(BearSide, diffBearS, na);
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB=76.4; def OS=23.6;

def bbSentiment = if(style,sentiment(src, length, lowerBand, smooth, useWick),na);

def Sentcolor= if(bbSentiment>=bbSentiment[1],1,0);
def Sigcolor= if(bbSentiment>=50, 1,0);

#// Output
plot OBline = if(!style,na,if(isNaN(close),na,OB));
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = if(!style,na,if(isNaN(close),na,OS));
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = if(!style,na,if(isNaN(close),na,100));
"100".SetDefaultColor(Color.RED);
plot "50" = if(!style,na,if(isNaN(close),na,50));#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = if(!style,na,if(isNaN(close),na,0));
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(SentimentArea,0,CreateColor(0,230,118));
AddCloud(SentimentArea,100,CreateColor(255,82,82));

#-- END Code

CODE 3 - Added more chart style option with Sentiment ratio (SR) label.

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input ChartStyle = {default "Bulls or Bears", "Mountains", "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input src = hl2;
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def h= high; def l= low; def c= close;
def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
def Mountain =  ChartStyle == ChartStyle."Mountains";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(style==1, na, (src - lowest) * 100 / lowest);
def diffBear = If(style==1, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(style==1 or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(style==1 or !BearSide or IsNaN(c), na , if(Mountain,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(style==1, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(style!=2,na,If(BearSide, If(smooth,if(Mountain,diffBearS,-diffBearS),if(Mountain,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mountain,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(style!=2,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(style!=2, na, If(BearSide,if(Mountain,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(style!=2, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(style!=0, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(style!=0, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(style!=1,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(style!=1 or IsNaN(c), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(style!=1 or IsNaN(c), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(style!=1 or IsNaN(c), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(style!=1 or IsNaN(c), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(style!=1 or IsNaN(c), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
#-- END Code
 
Last edited:
@APOT7 member request: https://usethinkscript.com/threads/convert-tradingview-mwho-is-in-control.13290/
use the below, should work with you. I added option to enable/Disable the wicks in calculating HH/LL.

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input useWick = no;
input src = close;
input length = 13;
input signal = 3;
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 10;

def na = Double.NaN;

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}

#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1],price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}

def lowest = Lowest(if(useWick,low,src), length);
def highest = Highest(if(useWick,high,src), length);
def diffBull = (src - lowest) * 100 / lowest;
def diffBear = (highest - src) * 100 / highest;
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - Average(diffBullS, signal) >= 0,2,1);
def diffBearColor = If(diffBearS - Average(diffBearS, signal) >= 0,2,1);
def diffColor = If(differential - Average(differential, signal) >= 0, 1, 0);

#-- Plot
plot Diff = If(DisplayDifferential, differential, na);#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if IsNaN(close) then na else 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = If(BearSide, (If(smooth, diffBearS, diffBear)), na);#, color=diffBearColor, linewidth=2, style=columns)
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);#, color=diffBullColor, linewidth=2, style=columns)
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot dBear = If(BearSide, diffBearS, na);#, color=diffBearColor, linewidth=2)
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);#, color=diffBullColor, linewidth=2)
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);


#-- END Code
quick question regarding.

1) #//Output ....can there be a option for average to be EMA, SMA, HUll, WMA, etc. It is possible to add?
2) would you be able to add option to change colors of the candles, so they are easier to see when one is moving up or down, when two colors mix?
3) Is possible to show to show the value of bull green line and red bean line on Right side on the axis? I only see one value, based on who is in control.
 
Last edited:
quick question regarding.

1) #//Output ....can there be a option for average to be EMA, SMA, HUll, WMA, etc. It is possible to add?
2) would you be able to add option to change colors of the candles, so they are easier to see when one is moving up or down, when two colors mix?
3) Is possible to show to show the value of bull green line and red bean line on Right side on the axis? I only see one value, based on who is in control.
check CODE 2 may like more :)
 
check CODE 2 may like more :)
Thank you!! I like them both. I made some little changes to code 1. Would you be able to check and see if the code is correct? @samer800

Code 1

#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input useWick = no;
input src = close;
input length = 13;
input signal = 3;
input maType = {default SMA, EMA, SMMA, WMA, VWMA, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA};
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 10;

def na = Double.NaN;

script nz {
input data = close;
input repl = 0;
def ret_val = if isNaN(data) then repl else data;
plot return = ret_val;
}

#ssFilter( price, lowerBand ) =>
script ssFilter {
input price = close;
input lowerBand = 10;
def PI = 3.14159265358979;
def angle = Sqrt(2) * PI / lowerBand;
def a1 = Exp(-angle);
def b1 = 2 * a1 * Cos(angle);
def c2 = b1;
def c3 = -a1 * a1;
def c1 = 1 - c2 - c3;
def filt = c1 * (price + nz(price[1],price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
plot return = filt;
}

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#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 = 14;
def v = volume;
def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
input source = close;
input length = 14;
input type = "SMA";
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 == "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 == "HMA" then HullMovingAvg(source, length ) else Double.NaN;
plot return = multiMa;
}

def lowest = Lowest(if(useWick,low,src), length);
def highest = Highest(if(useWick,high,src), length);
def diffBull = (src - lowest) * 100 / lowest;
def diffBear = (highest - src) * 100 / highest;
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - Average(diffBullS, signal) >= 0,2,1);
def diffBearColor = If(diffBearS - Average(diffBearS, signal) >= 0,2,1);
def diffColor = If(differential - Average(differential, signal) >= 0, 1, 0);

#-- Plot
plot Diff = If(DisplayDifferential, differential, na);#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if IsNaN(close) then na else 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = If(BearSide, (If(smooth, diffBearS, diffBear)), na);#, color=diffBearColor, linewidth=2, style=columns)
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);#, color=diffBullColor, linewidth=2, style=columns)
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot dBear = If(BearSide, diffBearS, na);#, color=diffBearColor, linewidth=2)
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);#, color=diffBullColor, linewidth=2)
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot Breakout= .05;
Breakout.setdefaultColor(color.blue);
Breakout.SetPaintingStrategy(PaintingStrategy.horizontal);
Breakout.setlineweight(1);

#-- END Code
 
@samer800 The code 2 that you have, Would you be able to add the option of bull and bear overlay on top of each other like the way you had on code 1.
Thank you for all the hard work! I appreciate it.
 
Thank you!! I like them both. I made some little changes to code 1. Would you be able to check and see if the code is correct? @samer800

Code 1

#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input useWick = no;
input src = close;
input length = 13;
input signal = 3;
input maType = {default SMA, EMA, SMMA, WMA, VWMA, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA};
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 10;

def na = Double.NaN;

script nz {
input data = close;
input repl = 0;
def ret_val = if isNaN(data) then repl else data;
plot return = ret_val;
}

#ssFilter( price, lowerBand ) =>
script ssFilter {
input price = close;
input lowerBand = 10;
def PI = 3.14159265358979;
def angle = Sqrt(2) * PI / lowerBand;
def a1 = Exp(-angle);
def b1 = 2 * a1 * Cos(angle);
def c2 = b1;
def c3 = -a1 * a1;
def c1 = 1 - c2 - c3;
def filt = c1 * (price + nz(price[1],price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
plot return = filt;
}

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#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 = 14;
def v = volume;
def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
input source = close;
input length = 14;
input type = "SMA";
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 == "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 == "HMA" then HullMovingAvg(source, length ) else Double.NaN;
plot return = multiMa;
}

def lowest = Lowest(if(useWick,low,src), length);
def highest = Highest(if(useWick,high,src), length);
def diffBull = (src - lowest) * 100 / lowest;
def diffBear = (highest - src) * 100 / highest;
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - Average(diffBullS, signal) >= 0,2,1);
def diffBearColor = If(diffBearS - Average(diffBearS, signal) >= 0,2,1);
def diffColor = If(differential - Average(differential, signal) >= 0, 1, 0);

#-- Plot
plot Diff = If(DisplayDifferential, differential, na);#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if IsNaN(close) then na else 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = If(BearSide, (If(smooth, diffBearS, diffBear)), na);#, color=diffBearColor, linewidth=2, style=columns)
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);#, color=diffBullColor, linewidth=2, style=columns)
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot dBear = If(BearSide, diffBearS, na);#, color=diffBearColor, linewidth=2)
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor==2 then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);#, color=diffBullColor, linewidth=2)
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor==2 then Color.GREEN else Color.DARK_GREEN);

plot Breakout= .05;
Breakout.setdefaultColor(color.blue);
Breakout.SetPaintingStrategy(PaintingStrategy.horizontal);
Breakout.setlineweight(1);

#-- END Code
my understanding that you want to add breakout line. I amended little the code with adjustable threshold option. check it out.

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input useWick = no;
input src = close;
input length = 13;
input signal = 3;
input maType = {default SMA, EMA, SMMA, WMA, VWMA, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA};
input BullSide = yes;# title="Display Bull Side")
input BearSide = yes;#(true, title="Display Bear Side")
input DisplayDifferential = no;#(false, title="Display Differential")
input smooth = yes;#(true, title="Smooth")
input lowerBand = 10;
input threshold = 0.05;

def na = Double.NaN;

script nz {
    input data = close;
    input repl = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}

#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#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 = 14;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "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 == "HMA" then HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}

def lowest = Lowest(If(useWick, low, src), length);
def highest = Highest(If(useWick, high, src), length);
def diffBull = (src - lowest) * 100 / lowest;
def diffBear = (highest - src) * 100 / highest;
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);

#// Output

def diffBullColor = If(diffBullS - Average(diffBullS, signal) >= 0, 2, 1);
def diffBearColor = If(diffBearS - Average(diffBearS, signal) >= 0, 2, 1);
def diffColor = If(differential - Average(differential, signal) >= 0, 1, 0);

#-- Plot
plot Breakout = if IsNaN(close) then na else threshold;
Breakout.SetDefaultColor(Color.WHITE);
AddCloud(Breakout,0,Color.GRAY);

plot Diff = If(DisplayDifferential, differential, na);#, color=diffColor, linewidth=2)
Diff.SetLineWeight(2);
Diff.AssignValueColor(if diffColor then Color.CYAN else Color.MAGENTA);

plot ZeroLine = if IsNaN(close) then na else 0;
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = If(BearSide, (If(smooth, diffBearS, diffBear)), na);#, color=diffBearColor, linewidth=2, style=columns)
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if bear>=threshold then if diffBearColor == 2 then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = If(BullSide, (If(smooth, diffBullS, diffBull)), na);#, color=diffBullColor, linewidth=2, style=columns)
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull>=threshold then if diffBullColor == 2 then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(BearSide, diffBearS, na);#, color=diffBearColor, linewidth=2)
dBear.SetLineWeight(2);
dBear.AssignValueColor(if diffBearColor == 2 then Color.RED else Color.DARK_RED);

plot dBull = If(BullSide, diffBullS, na);#, color=diffBullColor, linewidth=2)
dBull.SetLineWeight(2);
dBull.AssignValueColor(if diffBullColor == 2 then Color.GREEN else Color.DARK_GREEN);

#-- END Code
 
@samer800 The code 2 that you have, Would you be able to add the option of "bull and bear" overlay (on top of each other like the way you had on code 1), "bull or bear" and "Sentiment". Also option to add threshold to spot breakout.

Thank you for all the hard work!
 
@samer800 The code 2 that you have, Would you be able to add the option of "bull and bear" overlay (on top of each other like the way you had on code 1), "bull or bear" and "Sentiment". Also option to add threshold to spot breakout.

Thank you for all the hard work!
added CODE 3. ... has all indicator options. try it .

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input ChartStyle = {default "Bulls or Bears", "Mountains", "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input src = hl2;
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def h= high; def l= low; def c= close;
def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
def Mountain =  ChartStyle == ChartStyle."Mountains";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(style==1, na, (src - lowest) * 100 / lowest);
def diffBear = If(style==1, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(style==1 or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(style==1 or !BearSide or IsNaN(c), na , if(Mountain,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(style==1, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(style!=2,na,If(BearSide, If(smooth,if(Mountain,diffBearS,-diffBearS),if(Mountain,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mountain,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(style!=2,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(style!=2, na, If(BearSide,if(Mountain,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(style!=2, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(style!=0, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(style!=0, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(style!=1,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(style!=1 or IsNaN(c), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(style!=1 or IsNaN(c), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(style!=1 or IsNaN(c), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(style!=1 or IsNaN(c), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(style!=1 or IsNaN(c), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
#-- END Code
 
added CODE 3. ... has all indicator options. try it .

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input ChartStyle = {default "Bulls or Bears", "Mountains", "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input src = hl2;
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def h= high; def l= low; def c= close;
def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
def Mountain =  ChartStyle == ChartStyle."Mountains";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(style==1, na, (src - lowest) * 100 / lowest);
def diffBear = If(style==1, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(style==1 or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(style==1 or !BearSide or IsNaN(c), na , if(Mountain,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(style==1, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(style!=2,na,If(BearSide, If(smooth,if(Mountain,diffBearS,-diffBearS),if(Mountain,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mountain,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(style!=2,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(style!=2, na, If(BearSide,if(Mountain,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(style!=2, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(style!=0, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(style!=0, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(style!=1,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(style!=1 or IsNaN(c), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(style!=1 or IsNaN(c), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(style!=1 or IsNaN(c), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(style!=1 or IsNaN(c), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(style!=1 or IsNaN(c), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
#-- END Code
Would you be able to explain the SR values? @samer800

For the code 3,
@samer800

For the code 3.

Is there a way to calculate the imbalance/difference between Columns (green/red-mountain view) and have the total difference between buyer/seller display in a label?

More buyer > then seller. (Display the value and label -green as well if the value is going up or down.
Seller>then buyer (display the value and label red as well as if the value is going up or down) .
If it's zero, zero then neutral.



@samer800

Code 3 in mountain view

Would it be possible to add a chart label on the indicator to display the the value of "dbull" and dbear" lines in mountain view of the indicator or the difference of dbull and dbear?

Thank you.
 
dBull dBear label, add to the bottom of your script:
Ruby:
AddLabel(yes, "dBull: " +round(dBull,2) +" | dBear: " +round(dBear,2) +" | diff: " +(dBull-dBear), color.blue);
 
added CODE 3. ... has all indicator options. try it .

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input ChartStyle = {default "Bulls or Bears", "Mountains", "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input src = hl2;
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def h= high; def l= low; def c= close;
def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
def Mountain =  ChartStyle == ChartStyle."Mountains";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input length = 14;
    input type = "SMA";
    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 == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(style==1, na, (src - lowest) * 100 / lowest);
def diffBear = If(style==1, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(style==1 or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(style==1 or !BearSide or IsNaN(c), na , if(Mountain,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(style==1, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(style!=2,na,If(BearSide, If(smooth,if(Mountain,diffBearS,-diffBearS),if(Mountain,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mountain,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(style!=2,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(style!=2, na, If(BearSide,if(Mountain,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(style!=2, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(style!=0, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(style!=0, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(style!=1,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(style!=1 or IsNaN(c), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(style!=1 or IsNaN(c), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(style!=1 or IsNaN(c), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(style!=1 or IsNaN(c), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(style!=1 or IsNaN(c), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
#-- END Code

Would you be able to make this (code 3) into multi-time-frame? Thank you!
 
Would you be able to make this (code 3) into multi-time-frame? Thank you!
check the below

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Updated by Sam4Cok@Samer800 (Added MTF option)    - 03/2023
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input UseChartTimeframe = yes;
input Timeframe    = AggregationPeriod.FIFTEEN_MIN;
input ChartStyle = {"Bulls or Bears", "Mountains",default "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input Source = {Default "hl2", "close"};
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def srcIn = Source==Source."hl2";
def current = GetAggregationPeriod();
def tf = if UseChartTimeframe then current else Timeframe;
def h= high(Period=tf); def l= low(Period=tf); def c= close(Period=tf); def v = volume(Period=tf);
def src = if srcIn then hl2(Period=tf) else C;
def BulMount =  ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains";
def senti = ChartStyle == ChartStyle."Sentiment";
def Mount =  ChartStyle == ChartStyle."Mountains";
def style = !senti and !BulMount;#if ChartStyle == ChartStyle."Sentiment" then 1 else
#            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
#def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
#            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    input v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input vol    = volume;
    input length = 14;
    input type = "SMA";
    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, vol) 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 == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(senti, na, (src - lowest) * 100 / lowest);
def diffBear = If(senti, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, v, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, v, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential,v, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(senti or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(senti or !BearSide or IsNaN(c), na , if(Mount,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(senti, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(!BulMount,na,If(BearSide, If(smooth,if(Mount,diffBearS,-diffBearS),if(Mount,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mount,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(!BulMount,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(!BulMount, na, If(BearSide,if(Mount,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(!BulMount, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(!style, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(!style, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(!senti,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(!senti or IsNaN(close), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(!senti or IsNaN(close), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(!senti or IsNaN(close), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(!senti or IsNaN(close), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(!senti or IsNaN(close), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
AddLabel(BulMount, "bull(" + Round(dBull, 2) + ")", if diffBullColor then Color.GREEN else Color.LIGHT_GREEN);
AddLabel(BulMount, "bear(" + Round(dBear, 2) + ")", if diffBearColor then Color.RED else Color.PINK);

#-- END Code
 
I need one more favor. From Code 3 in mountain view, The value of green bar and red bar, can it be displayed in a label? Thank you

sorry for the confusion. The histogram (code 3 mountain view) has Green, dark green, red and dark red colors. I wanted the values of the current histogram in a label . The bulls are green and dark green VS bears- red and dark red. Whatever color that it is showing on the histogram, I wanted the value in a label.
I hope this helps. If not, I will post a picture of the chart.
Thank you !

check the below

CSS:
#/ Madrid : 24/Apr/2015 : 00:07 : Who is in Control : WhosNControl : 2.0
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine who is in control. Bulls or Bears.
# Converted and Mod by Sam4Cok@Samer800 - 11/2022
# Updated by Sam4Cok@Samer800 (Added MTF option)    - 03/2023
# Update --- Added Multiple chart style with MA options and Sentiment ratio (SR) label.
declare lower;
#study("MWho is in Control", shorttitle="MWhosNControl", precision=2)
input UseChartTimeframe = yes;
input Timeframe    = AggregationPeriod.FIFTEEN_MIN;
input ChartStyle = {"Bulls or Bears", "Mountains",default "Sentiment", "MACD Style"};
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, ZLSMA, ZLDEMA, ZLTEMA, HMA};
input SentimentLabel = yes;
input useWick = no;
input Source = {Default "hl2", "close"};
input length = 18;
input signalLength = 3;
input BullSide = yes;    # "Display Bull Side"
input BearSide = yes;    # "Display Bear Side"
input smooth = yes;      # "Smooth"
input thresholdBull = 0.25;
input thresholdBear = 0.25;
input lowerBand = 5;

def na = Double.NaN;
def srcIn = Source==Source."hl2";
def current = GetAggregationPeriod();
def tf = if UseChartTimeframe then current else Timeframe;
def h= high(Period=tf); def l= low(Period=tf); def c= close(Period=tf); def v = volume(Period=tf);
def src = if srcIn then hl2(Period=tf) else C;
def BulMount =  ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains";
def senti = ChartStyle == ChartStyle."Sentiment";
def Mount =  ChartStyle == ChartStyle."Mountains";
def style = !senti and !BulMount;#if ChartStyle == ChartStyle."Sentiment" then 1 else
#            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;
#def style = if ChartStyle == ChartStyle."Sentiment" then 1 else
#            if ChartStyle == ChartStyle."Bulls or Bears" or ChartStyle == ChartStyle."Mountains" then 2 else 0;

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#ssFilter( price, lowerBand ) =>
script ssFilter {
    input price = close;
    input lowerBand = 10;
    def PI = 3.14159265358979;
    def angle = Sqrt(2) * PI / lowerBand;
    def a1 = Exp(-angle);
    def b1 = 2 * a1 * Cos(angle);
    def c2 = b1;
    def c3 = -a1 * a1;
    def c1 = 1 - c2 - c3;
    def filt = c1 * (price + nz(price[1], price)) / 2 + c2 * nz(filt[1]) + c3 * nz(filt[2]);
    plot return = filt;
}
#sentiment(src, len, lowerBand, smooth, useWick) =>
script sentiment {
    input src = close;
    input len = 18;
    input lowerBand = 5;
    input smooth = yes;
    input useWick = no;
    def lowest = Lowest(If(useWick, low, src), len);
    def highest = Highest(If(useWick, high, src), len);
    def diffBull = (src - lowest) * 100 / lowest;
    def diffBear = (highest - src) * 100 / highest;
    def bbs = diffBull * 100 / (diffBear + diffBull);
    def bbSentiment = If(smooth, ssFilter(bbs, lowerBand), bbs);
    plot return = bbSentiment;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#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;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    input v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script Ma {
    input source = close;
    input vol    = volume;
    input length = 14;
    input type = "SMA";
    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, vol) 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 == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
def lowest = Lowest(If(useWick, l, src), length);
def highest = Highest(If(useWick, h, src), length);
def diffBull = If(senti, na, (src - lowest) * 100 / lowest);
def diffBear = If(senti, na, (highest - src) * 100 / highest);
def diffBullS = ssFilter(diffBull, lowerBand);
def diffBearS = ssFilter(diffBear, lowerBand);
def differential = If(smooth, diffBullS - diffBearS, diffBull - diffBear);
def hisBull = if(differential<0, na, differential);
def hisBear = if(differential>=0,na,differential);
#// Output

def diffBullColor = If(diffBullS - ma(diffBullS, v, signalLength, maType) >= 0, 1, 0);
def diffBearColor = If(diffBearS - ma(diffBearS, v, signalLength, maType) >= 0, 1, 0);
def diffColor = If(differential - ma(differential,v, signalLength, maType) >= 0, 1, 0);

#-- Plot
def BreakoutBull = if(senti or !BullSide or IsNaN(c), na ,thresholdBull);
AddCloud(BreakoutBull, 0, Color.GRAY, Color.GRAY, yes);

def BreakoutBear = if(senti or !BearSide or IsNaN(c), na , if(Mount,thresholdBear,-thresholdBear));
AddCloud(0, BreakoutBear, Color.GRAY, Color.GRAY, yes);

plot ZeroLine = If(senti, na, If(IsNaN(c), na, 0));
ZeroLine.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);

plot Bear = if(!BulMount,na,If(BearSide, If(smooth,if(Mount,diffBearS,-diffBearS),if(Mount,diffBear,-diffBear)), na));
Bear.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bear.AssignValueColor(if (if(Mount,Bear>=BreakoutBear,Bear<=BreakoutBear)) then if diffBearColor then Color.RED else Color.DARK_RED else Color.GRAY);

plot Bull = if(!BulMount,na,If(BullSide, (If(smooth, diffBullS, diffBull)), na));
Bull.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bull.AssignValueColor(if Bull >= BreakoutBull then if diffBullColor then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot dBear = If(!BulMount, na, If(BearSide,if(Mount,diffBearS,-diffBearS), na));
#dBear.SetLineWeight(if Histogram then 1 else 2);
dBear.AssignValueColor(if diffBearColor then Color.RED else Color.DARK_RED);

plot dBull = If(!BulMount, na,If(BullSide, diffBullS, na));
#dBull.SetLineWeight(if Histogram then 1 else 2);
dBull.AssignValueColor(if diffBullColor then Color.GREEN else Color.DARK_GREEN);

plot HistDiffBull = If(!style, na,if(BullSide,hisBull,na));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBull.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBull.SetLineWeight(3);
HistDiffBull.AssignValueColor(if HistDiffBull>=thresholdBull then if HistDiffBull > HistDiffBull[1] then Color.GREEN else Color.DARK_GREEN else Color.GRAY);

plot HistDiffBear = If(!style, na,if(BearSide,hisBear,na));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#HistDiffBull.SetDefaultColor(GetColor(5));
HistDiffBear.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
HistDiffBear.SetLineWeight(3);
HistDiffBear.AssignValueColor(if HistDiffBear<=thresholdBear then if HistDiffBear < HistDiffBear[1] then Color.RED else Color.DARK_RED else Color.GRAY);

#//@version=5
#// Madrid : 150424FR0007 : Bull Bear Sentiment : 2.0
#// Madrid : 220120TH2201 : Conversion to PS version 5
#// This measures the difference in PCT from the last Lowest/Highest Point
#// to determine the overall sentiment
#indicator("Madrid Bull Bear Sentiment", shorttitle="MSentiment", precision=0)

def OB = 76.4;
def OS = 23.6;

def bbSentiment = If(!senti,na, sentiment(src, length, lowerBand, smooth, useWick));

def Sentcolor = If(bbSentiment >= bbSentiment[1], 1, 0);
def Sigcolor = If(bbSentiment >= 50, 1, 0);

#// Output
plot OBline = If(!senti or IsNaN(close), na, OB);
OBline.SetStyle(Curve.MEDIUM_DASH);
OBline.SetDefaultColor(Color.GRAY);
plot OSline = If(!senti or IsNaN(close), na, OS);
OSline.SetStyle(Curve.MEDIUM_DASH);
OSline.SetDefaultColor(Color.GRAY);

plot "100" = If(!senti or IsNaN(close), na, 100);
"100".SetDefaultColor(Color.RED);
plot "50" = If(!senti or IsNaN(close), na, 50);#,
"50".AssignValueColor(if Sigcolor then Color.GREEN else Color.RED);
"50".SetStyle(Curve.POINTS);
plot "0" = If(!senti or IsNaN(close), na, 0);
"0".SetDefaultColor(Color.GREEN);

def SentimentArea = bbSentiment;
plot SentimentLine = bbSentiment;
SentimentLine.AssignValueColor(if Sentcolor then Color.GREEN else Color.RED);
AddCloud(if BullSide then SentimentArea else na, 0, CreateColor(0, 230, 118));
AddCloud(if BearSide then SentimentArea else na, 100, CreateColor(255, 82, 82));

#---- AddLabel
def Sent = sentiment(src, length, lowerBand, smooth, useWick);
def Sentiment = Round(Sent,2);
def PosSent = Sentiment>=50;
def NegSent = Sentiment<50;

AddLabel(SentimentLabel and PosSent,"SR(" + Sentiment + ")",Color.LIGHT_GREEN);
AddLabel(SentimentLabel and NegSent,"SR(" + Sentiment + ")",Color.LIGHT_RED);
AddLabel(BulMount, "bull(" + Round(dBull, 2) + ")", if diffBullColor then Color.GREEN else Color.LIGHT_GREEN);
AddLabel(BulMount, "bear(" + Round(dBear, 2) + ")", if diffBearColor then Color.RED else Color.PINK);

#-- END Code


T3Z1NOs.png


see the image @samer800 @MerryDay

I wanted to add labels of 4values you see on the right. There are 4 different values. Dbull is .07508, D bear .06303, Bull bar .09873, bear bar .03697.
In the code 3, when viewing under mountain view, you have helped add Dbear and DBull values in a label. I wanted to other two values that you see on the pic. Please help! Thank you.
 
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
339 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