@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.
CODE 1 for Bull or Bear
CODE 2 : modified it and combined 2 similar indicators with different view.
CODE 3 - Added more chart style option with Sentiment ratio (SR) label.
use the below, should work with you. I added option to enable/Disable the wicks in calculating HH/LL.
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: