Triple Ehlers Market State for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
4rMLUe7.png


Author Message:

Clear trend identification is an important aspect of finding the right side to trade, another is getting the best buying/selling price on a pullback, retracement or reversal. Triple Ehlers Market State can do both.

More Info : https://www.tradingview.com/v/6vcvpsLs/

CODE:
CSS:
# https://www.tradingview.com/v/6vcvpsLs/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#//@Woody_Bearbash
#indicator('Triple Market State', 'Triple MS', false, format.price, 2)
# Converted by Sam4Cok@Samer800    - 09/2023

declare lower;
input Theme = {default "Classic", "Night", "Mono"};
input BarColoring = yes;          # 'Bar coloring'
input source  = close;            # 'Source'
input SlowPeriod = 28;            # 'Slow Period'
input SlowThreshold = 9;
input MediumPeriod = 18;          # 'Medium Period'
input MediumThreshold = 15;
input FastPeriod = 9;             # 'Fast Period'
input FastThreshold = 10;
input RecentTrendWeighting = yes; # "Shows on bars ONLY"

def na = Double.NaN;
def last = isNaN(close);
def M1 = Theme == Theme."Classic";
def M2 = Theme == Theme."Night";
def M3 = Theme == Theme."Mono";
def up_ = if M1 then 1 else if M2 then 2 else 3;
def dn_ = if M1 then -1 else if M2 then -2 else -3;
def md_ = if M1 then 11 else if M2 then 22 else 33;

DefineGlobalColor("bullc", CreateColor(65, 198, 7));
DefineGlobalColor("bearc", CreateColor(202, 6, 108));
DefineGlobalColor("consc", CreateColor(216, 131, 5));

DefineGlobalColor("dbullc", Color.DARK_GREEN);
DefineGlobalColor("dbearc", Color.DARK_RED);

DefineGlobalColor("bulla", CreateColor(205, 177, 78));
DefineGlobalColor("beara", CreateColor(192, 104, 227));
DefineGlobalColor("consa", CreateColor(46, 109, 128));

DefineGlobalColor("bullo", CreateColor(194, 189, 189));
DefineGlobalColor("bearo", CreateColor(49, 121, 245));
DefineGlobalColor("conso", CreateColor(216, 131, 5));

#// Functions ? ?
#cc(Series, Period) =>  // Correlation Cycle Function
script cc {
    input Series = close;
    input Perd = 14;
    def PIx2 = 4.0 * ASin(1.0);
    def len = Max(2, Perd);
    def period = len;
    def Rx = fold i = 0 to period with p do
              p + Series[i];
    def Ix = Rx;
    def Rxx = fold i1 = 0 to period with p1 do
              p1 + (Series[i1] * Series[i1]);
    def Ixx = Rxx;
    def Rxy = fold i2 = 0 to period with p2 do
              p2 + (Series[i2] * Cos(PIx2 * i2 / period));
    def Ixy = fold i3 = 0 to period with p3 do
              p3 + (Series[i3] * -Sin(PIx2 * i3 / period));
    def Ryy = fold i4 = 0 to period with p4 do
              p4 + (Cos(PIx2 * i4 / period) *  Cos(PIx2 * i4 / period));
    def Iyy = fold i5 = 0 to period with p5 do
              p5 + (-Sin(PIx2 * i5 / period) * -Sin(PIx2 * i5 / period));
    def Ry  = fold i6 = 0 to period with p6 do
              p6 + (Cos(PIx2 * i6 / period));
    def Iy  = fold i7 = 0 to period with p7 do
              p7 + (-Sin(PIx2 * i7 / period));
    def real_1 = period * Rxx - Rx * Rx;
    def real_2 = period * Ryy - Ry * Ry;
    def real_3 = period * Rxy - Rx * Ry;
    def realPart = if real_1 > 0.0 and real_2 > 0.0 then
                   (real_3) / Sqrt(real_1 * real_2) else realPart[1];
    def imag_1 = period * Ixx - Ix * Ix;
    def imag_2 = period * Iyy - Iy * Iy;
    def imag_3 = period * Ixy - Ix * Iy;
    def imagPart = if imag_1 > 0.0 and imag_2 > 0.0 then
                  (imag_3) / Sqrt(imag_1 * imag_2) else imagPart[1];
    plot real = realPart;
    plot imag = imagPart;
}
#cap(RealPart, ImaginaryPart) =>  // Correlation Angle Phasor Function
script cap {
    input RealPart = 0;
    input ImaginaryPart = 0;
    def HALF_OF_PI = ASin(1.0);
    def Rad = ATan(RealPart / ImaginaryPart) + HALF_OF_PI;
    def DEG_IN_1_RADIAN = 90.0 / HALF_OF_PI;
    def tempAngle = Rad * DEG_IN_1_RADIAN;
    def angle;
    def angle0 = if ImaginaryPart == 0.0 then 0.0 else tempAngle;
    def angle1;
if ImaginaryPart > 0.0 {
    angle1 = angle0 - 180.0;
    } else {
    angle1 = angle0;
}
        angle = if angle1[1] > angle1 and angle1[1] - angle1 < 270.0 then
                angle1[1] else angle1;
    plot out = angle;
}
#mstate(Angle, Degrees) =>  // Market State Function
script mstate {
    input Angle = close;
    input Degrees = 45;
    def thresholdInDegrees = Degrees;
    def temp = AbsValue(Angle - Angle[1]) < thresholdInDegrees;
    def state;
    if Angle >= 0.0 and temp {
        state = 1;
    } else
    if Angle < 0.0 and temp {
        state = -1;
    } else {
        state = 0;#state[1];
    }
    plot out = state;
}
#// Correlation Cycle ? ?

def real1      = cc(source, SlowPeriod).real;
def imaginary1 = cc(source, SlowPeriod).imag;
def real2      = cc(source, MediumPeriod).real;
def imaginary2 = cc(source, MediumPeriod).imag;
def real3      = cc(source, FastPeriod).real;
def imaginary3 = cc(source, FastPeriod).imag;

#// Correlation Angle/Phasor ?
def angle1 = cap(real1, imaginary1);
def angle2 = cap(real2, imaginary2);
def angle3 = cap(real3, imaginary3);

#// Market State ?

def state1 = mstate(angle1, SlowThreshold);
def state2 = mstate(angle2, MediumThreshold);
def state3 = mstate(angle3, FastThreshold);

def colorMS1 = if state1 >  0.5 then up_ else
               if state1 < -0.5 then dn_ else na;
def colorMS2 = if state2 >  0.5 then up_ else
               if state2 < -0.5 then dn_ else na;
def colorMS3 = if state3 >  0.5 then up_ else
               if state3 < -0.5 then dn_ else na;
def color1 = if state1 >  0.5 then up_ else
             if state1 < -0.5 then dn_ else color1[1];
def color2 = if state2 >  0.5 then up_ else
             if state2 < -0.5 then dn_ else color2[1];
def color3 = if state3 >  0.5 then up_ else
             if state3 < -0.5 then dn_ else color3[1];

AddCloud(if colorMS1==1 then state1 else na, -state1,
            GlobalColor("dbullc"), GlobalColor("dbullc"));
AddCloud(if colorMS1==2 then state1 else na, -state1,
            GlobalColor("bulla"), GlobalColor("bulla"));
AddCloud(if colorMS1==3 then state1 else na, -state1,
            GlobalColor("bullo"), GlobalColor("bullo"));
AddCloud(if colorMS1==-1 then state1 else na, -state1,
            GlobalColor("dbearc"), GlobalColor("dbearc"));
AddCloud(if colorMS1==-2 then state1 else na, -state1,
            GlobalColor("beara"), GlobalColor("beara"));
AddCloud(if colorMS1==-3 then state1 else na, -state1,
            GlobalColor("bearo"), GlobalColor("bearo"));

plot Medium = if !last then -state2 else na;  # 'State Medium'
plot Fast   = if !last and state3 then -state3 else na;    # 'State Fast'
Fast.SetLineWeight(2);
Medium.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Fast.SetPaintingStrategy(PaintingStrategy.POINTS);
Medium.AssignValueColor(if color2==1 then GlobalColor("bullc") else
                        if color2==2 then GlobalColor("bulla") else
                        if color2==3 then GlobalColor("bullo") else
                        if color2==-1 then GlobalColor("bearc") else
                        if color2==-2 then GlobalColor("beara") else
                        if color2==-3 then GlobalColor("bearo") else Color.GRAY);
Fast.AssignValueColor(if color3==1 then Color.LIGHT_GREEN else
                      if color3==2 then GlobalColor("bulla") else
                      if color3==3 then GlobalColor("bullo") else
                      if color3==-1 then Color.PINK else
                      if color3==-2 then GlobalColor("beara") else
                      if color3==-3 then GlobalColor("bearo") else Color.GRAY);

plot SlowUp = if !last then state1 else na;    # 'State Slow'
plot SlowDn = if !last then -state1 else na;   # 'State Slow'
SlowUp.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
SlowDn.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
#// Bar Gates ?

def trend_slo = if state1 > 0.5 then 1 else
                if state1 < -0.5 then -1 else
                if state1 < 0.5 and state1 > -0.5 then 2 else trend_slo[1];
def trend_med = if state2 > 0.5 then 1 else
                if state2 < -0.5 then -1 else
                if state2 < 0.5 and state2 > -0.5 then 2 else trend_med[1];
def trend_fas = if state3 > 0.5 then 1 else
                if state3 < -0.5 then -1 else
                if state3 < 0.5 and state3 > -0.5 then 2 else trend_fas[1];
def trend_double = if trend_slo == 1 and trend_med == 1 or
                      trend_slo == 1 and trend_fas == 1 or
                      trend_med == 1 and trend_fas == 1 then 1 else
                   if trend_slo == -1 and trend_med == -1 or
                      trend_slo == -1 and trend_fas == -1 or
                      trend_med == -1 and trend_fas == -1 then -1 else trend_double[1];
#/ Parliament arrays ?
def sig_up = (if trend_slo == 1 then 1 else 0) +
             (if trend_med == 1 then 1 else 0) +
             (if trend_fas == 1 then 1 else 0) +           
             (if trend_double == 1 and RecentTrendWeighting then 1 else 0);
def sigSum_up   = sig_up;

def sig_dn = (if trend_slo == -1 then  1 else 0) +
             (if trend_med == -1 then  1 else 0) +
             (if trend_fas == -1 then  1 else 0) +                     
             (if trend_double == -1 and RecentTrendWeighting then 1 else 0);
def sigSum_dn   = sig_dn;
def mid_signal = sigSum_up == sigSum_dn;

plot Retracement = if mid_signal then 0 else na;    # 'Retracement'
Retracement.SetLineWeight(2);
Retracement.SetPaintingStrategy(PaintingStrategy.POINTS);
Retracement.AssignValueColor(if md_==11 then GlobalColor("consc") else
                        if md_==22 then GlobalColor("consa") else
                        if md_==33 then GlobalColor("conso") else Color.GRAY);
def bar_col = if sigSum_up > sigSum_dn then up_ else
              if sigSum_up < sigSum_dn then dn_ else md_;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if bar_col==11 then GlobalColor("consc") else
                 if bar_col==22 then GlobalColor("consa") else
                 if bar_col==33 then GlobalColor("conso") else
                 if bar_col==1 then GlobalColor("bullc") else
                 if bar_col==2 then GlobalColor("bulla") else
                 if bar_col==3 then GlobalColor("bullo") else
                 if bar_col==-1 then GlobalColor("bearc") else
                 if bar_col==-2 then GlobalColor("beara") else
                 if bar_col==-3 then GlobalColor("bearo") else Color.GRAY);
#-- END of CODE
 

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

4rMLUe7.png


Author Message:

Clear trend identification is an important aspect of finding the right side to trade, another is getting the best buying/selling price on a pullback, retracement or reversal. Triple Ehlers Market State can do both.

More Info : https://www.tradingview.com/v/6vcvpsLs/

CODE:
CSS:
# https://www.tradingview.com/v/6vcvpsLs/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#//@Woody_Bearbash
#indicator('Triple Market State', 'Triple MS', false, format.price, 2)
# Converted by Sam4Cok@Samer800    - 09/2023

declare lower;
input Theme = {default "Classic", "Night", "Mono"};
input BarColoring = yes;          # 'Bar coloring'
input source  = close;            # 'Source'
input SlowPeriod = 28;            # 'Slow Period'
input SlowThreshold = 9;
input MediumPeriod = 18;          # 'Medium Period'
input MediumThreshold = 15;
input FastPeriod = 9;             # 'Fast Period'
input FastThreshold = 10;
input RecentTrendWeighting = yes; # "Shows on bars ONLY"

def na = Double.NaN;
def last = isNaN(close);
def M1 = Theme == Theme."Classic";
def M2 = Theme == Theme."Night";
def M3 = Theme == Theme."Mono";
def up_ = if M1 then 1 else if M2 then 2 else 3;
def dn_ = if M1 then -1 else if M2 then -2 else -3;
def md_ = if M1 then 11 else if M2 then 22 else 33;

DefineGlobalColor("bullc", CreateColor(65, 198, 7));
DefineGlobalColor("bearc", CreateColor(202, 6, 108));
DefineGlobalColor("consc", CreateColor(216, 131, 5));

DefineGlobalColor("dbullc", Color.DARK_GREEN);
DefineGlobalColor("dbearc", Color.DARK_RED);

DefineGlobalColor("bulla", CreateColor(205, 177, 78));
DefineGlobalColor("beara", CreateColor(192, 104, 227));
DefineGlobalColor("consa", CreateColor(46, 109, 128));

DefineGlobalColor("bullo", CreateColor(194, 189, 189));
DefineGlobalColor("bearo", CreateColor(49, 121, 245));
DefineGlobalColor("conso", CreateColor(216, 131, 5));

#// Functions ? ?
#cc(Series, Period) =>  // Correlation Cycle Function
script cc {
    input Series = close;
    input Perd = 14;
    def PIx2 = 4.0 * ASin(1.0);
    def len = Max(2, Perd);
    def period = len;
    def Rx = fold i = 0 to period with p do
              p + Series[i];
    def Ix = Rx;
    def Rxx = fold i1 = 0 to period with p1 do
              p1 + (Series[i1] * Series[i1]);
    def Ixx = Rxx;
    def Rxy = fold i2 = 0 to period with p2 do
              p2 + (Series[i2] * Cos(PIx2 * i2 / period));
    def Ixy = fold i3 = 0 to period with p3 do
              p3 + (Series[i3] * -Sin(PIx2 * i3 / period));
    def Ryy = fold i4 = 0 to period with p4 do
              p4 + (Cos(PIx2 * i4 / period) *  Cos(PIx2 * i4 / period));
    def Iyy = fold i5 = 0 to period with p5 do
              p5 + (-Sin(PIx2 * i5 / period) * -Sin(PIx2 * i5 / period));
    def Ry  = fold i6 = 0 to period with p6 do
              p6 + (Cos(PIx2 * i6 / period));
    def Iy  = fold i7 = 0 to period with p7 do
              p7 + (-Sin(PIx2 * i7 / period));
    def real_1 = period * Rxx - Rx * Rx;
    def real_2 = period * Ryy - Ry * Ry;
    def real_3 = period * Rxy - Rx * Ry;
    def realPart = if real_1 > 0.0 and real_2 > 0.0 then
                   (real_3) / Sqrt(real_1 * real_2) else realPart[1];
    def imag_1 = period * Ixx - Ix * Ix;
    def imag_2 = period * Iyy - Iy * Iy;
    def imag_3 = period * Ixy - Ix * Iy;
    def imagPart = if imag_1 > 0.0 and imag_2 > 0.0 then
                  (imag_3) / Sqrt(imag_1 * imag_2) else imagPart[1];
    plot real = realPart;
    plot imag = imagPart;
}
#cap(RealPart, ImaginaryPart) =>  // Correlation Angle Phasor Function
script cap {
    input RealPart = 0;
    input ImaginaryPart = 0;
    def HALF_OF_PI = ASin(1.0);
    def Rad = ATan(RealPart / ImaginaryPart) + HALF_OF_PI;
    def DEG_IN_1_RADIAN = 90.0 / HALF_OF_PI;
    def tempAngle = Rad * DEG_IN_1_RADIAN;
    def angle;
    def angle0 = if ImaginaryPart == 0.0 then 0.0 else tempAngle;
    def angle1;
if ImaginaryPart > 0.0 {
    angle1 = angle0 - 180.0;
    } else {
    angle1 = angle0;
}
        angle = if angle1[1] > angle1 and angle1[1] - angle1 < 270.0 then
                angle1[1] else angle1;
    plot out = angle;
}
#mstate(Angle, Degrees) =>  // Market State Function
script mstate {
    input Angle = close;
    input Degrees = 45;
    def thresholdInDegrees = Degrees;
    def temp = AbsValue(Angle - Angle[1]) < thresholdInDegrees;
    def state;
    if Angle >= 0.0 and temp {
        state = 1;
    } else
    if Angle < 0.0 and temp {
        state = -1;
    } else {
        state = 0;#state[1];
    }
    plot out = state;
}
#// Correlation Cycle ? ?

def real1      = cc(source, SlowPeriod).real;
def imaginary1 = cc(source, SlowPeriod).imag;
def real2      = cc(source, MediumPeriod).real;
def imaginary2 = cc(source, MediumPeriod).imag;
def real3      = cc(source, FastPeriod).real;
def imaginary3 = cc(source, FastPeriod).imag;

#// Correlation Angle/Phasor ?
def angle1 = cap(real1, imaginary1);
def angle2 = cap(real2, imaginary2);
def angle3 = cap(real3, imaginary3);

#// Market State ?

def state1 = mstate(angle1, SlowThreshold);
def state2 = mstate(angle2, MediumThreshold);
def state3 = mstate(angle3, FastThreshold);

def colorMS1 = if state1 >  0.5 then up_ else
               if state1 < -0.5 then dn_ else na;
def colorMS2 = if state2 >  0.5 then up_ else
               if state2 < -0.5 then dn_ else na;
def colorMS3 = if state3 >  0.5 then up_ else
               if state3 < -0.5 then dn_ else na;
def color1 = if state1 >  0.5 then up_ else
             if state1 < -0.5 then dn_ else color1[1];
def color2 = if state2 >  0.5 then up_ else
             if state2 < -0.5 then dn_ else color2[1];
def color3 = if state3 >  0.5 then up_ else
             if state3 < -0.5 then dn_ else color3[1];

AddCloud(if colorMS1==1 then state1 else na, -state1,
            GlobalColor("dbullc"), GlobalColor("dbullc"));
AddCloud(if colorMS1==2 then state1 else na, -state1,
            GlobalColor("bulla"), GlobalColor("bulla"));
AddCloud(if colorMS1==3 then state1 else na, -state1,
            GlobalColor("bullo"), GlobalColor("bullo"));
AddCloud(if colorMS1==-1 then state1 else na, -state1,
            GlobalColor("dbearc"), GlobalColor("dbearc"));
AddCloud(if colorMS1==-2 then state1 else na, -state1,
            GlobalColor("beara"), GlobalColor("beara"));
AddCloud(if colorMS1==-3 then state1 else na, -state1,
            GlobalColor("bearo"), GlobalColor("bearo"));

plot Medium = if !last then -state2 else na;  # 'State Medium'
plot Fast   = if !last and state3 then -state3 else na;    # 'State Fast'
Fast.SetLineWeight(2);
Medium.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Fast.SetPaintingStrategy(PaintingStrategy.POINTS);
Medium.AssignValueColor(if color2==1 then GlobalColor("bullc") else
                        if color2==2 then GlobalColor("bulla") else
                        if color2==3 then GlobalColor("bullo") else
                        if color2==-1 then GlobalColor("bearc") else
                        if color2==-2 then GlobalColor("beara") else
                        if color2==-3 then GlobalColor("bearo") else Color.GRAY);
Fast.AssignValueColor(if color3==1 then Color.LIGHT_GREEN else
                      if color3==2 then GlobalColor("bulla") else
                      if color3==3 then GlobalColor("bullo") else
                      if color3==-1 then Color.PINK else
                      if color3==-2 then GlobalColor("beara") else
                      if color3==-3 then GlobalColor("bearo") else Color.GRAY);

plot SlowUp = if !last then state1 else na;    # 'State Slow'
plot SlowDn = if !last then -state1 else na;   # 'State Slow'
SlowUp.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
SlowDn.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
#// Bar Gates ?

def trend_slo = if state1 > 0.5 then 1 else
                if state1 < -0.5 then -1 else
                if state1 < 0.5 and state1 > -0.5 then 2 else trend_slo[1];
def trend_med = if state2 > 0.5 then 1 else
                if state2 < -0.5 then -1 else
                if state2 < 0.5 and state2 > -0.5 then 2 else trend_med[1];
def trend_fas = if state3 > 0.5 then 1 else
                if state3 < -0.5 then -1 else
                if state3 < 0.5 and state3 > -0.5 then 2 else trend_fas[1];
def trend_double = if trend_slo == 1 and trend_med == 1 or
                      trend_slo == 1 and trend_fas == 1 or
                      trend_med == 1 and trend_fas == 1 then 1 else
                   if trend_slo == -1 and trend_med == -1 or
                      trend_slo == -1 and trend_fas == -1 or
                      trend_med == -1 and trend_fas == -1 then -1 else trend_double[1];
#/ Parliament arrays ?
def sig_up = (if trend_slo == 1 then 1 else 0) +
             (if trend_med == 1 then 1 else 0) +
             (if trend_fas == 1 then 1 else 0) +          
             (if trend_double == 1 and RecentTrendWeighting then 1 else 0);
def sigSum_up   = sig_up;

def sig_dn = (if trend_slo == -1 then  1 else 0) +
             (if trend_med == -1 then  1 else 0) +
             (if trend_fas == -1 then  1 else 0) +                    
             (if trend_double == -1 and RecentTrendWeighting then 1 else 0);
def sigSum_dn   = sig_dn;
def mid_signal = sigSum_up == sigSum_dn;

plot Retracement = if mid_signal then 0 else na;    # 'Retracement'
Retracement.SetLineWeight(2);
Retracement.SetPaintingStrategy(PaintingStrategy.POINTS);
Retracement.AssignValueColor(if md_==11 then GlobalColor("consc") else
                        if md_==22 then GlobalColor("consa") else
                        if md_==33 then GlobalColor("conso") else Color.GRAY);
def bar_col = if sigSum_up > sigSum_dn then up_ else
              if sigSum_up < sigSum_dn then dn_ else md_;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if bar_col==11 then GlobalColor("consc") else
                 if bar_col==22 then GlobalColor("consa") else
                 if bar_col==33 then GlobalColor("conso") else
                 if bar_col==1 then GlobalColor("bullc") else
                 if bar_col==2 then GlobalColor("bulla") else
                 if bar_col==3 then GlobalColor("bullo") else
                 if bar_col==-1 then GlobalColor("bearc") else
                 if bar_col==-2 then GlobalColor("beara") else
                 if bar_col==-3 then GlobalColor("bearo") else Color.GRAY);
#-- END of CODE
Anyway to make this MTF?
 
4rMLUe7.png


Author Message:

Clear trend identification is an important aspect of finding the right side to trade, another is getting the best buying/selling price on a pullback, retracement or reversal. Triple Ehlers Market State can do both.

More Info : https://www.tradingview.com/v/6vcvpsLs/

CODE:
CSS:
# https://www.tradingview.com/v/6vcvpsLs/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#//@Woody_Bearbash
#indicator('Triple Market State', 'Triple MS', false, format.price, 2)
# Converted by Sam4Cok@Samer800    - 09/2023

declare lower;
input Theme = {default "Classic", "Night", "Mono"};
input BarColoring = yes;          # 'Bar coloring'
input source  = close;            # 'Source'
input SlowPeriod = 28;            # 'Slow Period'
input SlowThreshold = 9;
input MediumPeriod = 18;          # 'Medium Period'
input MediumThreshold = 15;
input FastPeriod = 9;             # 'Fast Period'
input FastThreshold = 10;
input RecentTrendWeighting = yes; # "Shows on bars ONLY"

def na = Double.NaN;
def last = isNaN(close);
def M1 = Theme == Theme."Classic";
def M2 = Theme == Theme."Night";
def M3 = Theme == Theme."Mono";
def up_ = if M1 then 1 else if M2 then 2 else 3;
def dn_ = if M1 then -1 else if M2 then -2 else -3;
def md_ = if M1 then 11 else if M2 then 22 else 33;

DefineGlobalColor("bullc", CreateColor(65, 198, 7));
DefineGlobalColor("bearc", CreateColor(202, 6, 108));
DefineGlobalColor("consc", CreateColor(216, 131, 5));

DefineGlobalColor("dbullc", Color.DARK_GREEN);
DefineGlobalColor("dbearc", Color.DARK_RED);

DefineGlobalColor("bulla", CreateColor(205, 177, 78));
DefineGlobalColor("beara", CreateColor(192, 104, 227));
DefineGlobalColor("consa", CreateColor(46, 109, 128));

DefineGlobalColor("bullo", CreateColor(194, 189, 189));
DefineGlobalColor("bearo", CreateColor(49, 121, 245));
DefineGlobalColor("conso", CreateColor(216, 131, 5));

#// Functions ? ?
#cc(Series, Period) =>  // Correlation Cycle Function
script cc {
    input Series = close;
    input Perd = 14;
    def PIx2 = 4.0 * ASin(1.0);
    def len = Max(2, Perd);
    def period = len;
    def Rx = fold i = 0 to period with p do
              p + Series[i];
    def Ix = Rx;
    def Rxx = fold i1 = 0 to period with p1 do
              p1 + (Series[i1] * Series[i1]);
    def Ixx = Rxx;
    def Rxy = fold i2 = 0 to period with p2 do
              p2 + (Series[i2] * Cos(PIx2 * i2 / period));
    def Ixy = fold i3 = 0 to period with p3 do
              p3 + (Series[i3] * -Sin(PIx2 * i3 / period));
    def Ryy = fold i4 = 0 to period with p4 do
              p4 + (Cos(PIx2 * i4 / period) *  Cos(PIx2 * i4 / period));
    def Iyy = fold i5 = 0 to period with p5 do
              p5 + (-Sin(PIx2 * i5 / period) * -Sin(PIx2 * i5 / period));
    def Ry  = fold i6 = 0 to period with p6 do
              p6 + (Cos(PIx2 * i6 / period));
    def Iy  = fold i7 = 0 to period with p7 do
              p7 + (-Sin(PIx2 * i7 / period));
    def real_1 = period * Rxx - Rx * Rx;
    def real_2 = period * Ryy - Ry * Ry;
    def real_3 = period * Rxy - Rx * Ry;
    def realPart = if real_1 > 0.0 and real_2 > 0.0 then
                   (real_3) / Sqrt(real_1 * real_2) else realPart[1];
    def imag_1 = period * Ixx - Ix * Ix;
    def imag_2 = period * Iyy - Iy * Iy;
    def imag_3 = period * Ixy - Ix * Iy;
    def imagPart = if imag_1 > 0.0 and imag_2 > 0.0 then
                  (imag_3) / Sqrt(imag_1 * imag_2) else imagPart[1];
    plot real = realPart;
    plot imag = imagPart;
}
#cap(RealPart, ImaginaryPart) =>  // Correlation Angle Phasor Function
script cap {
    input RealPart = 0;
    input ImaginaryPart = 0;
    def HALF_OF_PI = ASin(1.0);
    def Rad = ATan(RealPart / ImaginaryPart) + HALF_OF_PI;
    def DEG_IN_1_RADIAN = 90.0 / HALF_OF_PI;
    def tempAngle = Rad * DEG_IN_1_RADIAN;
    def angle;
    def angle0 = if ImaginaryPart == 0.0 then 0.0 else tempAngle;
    def angle1;
if ImaginaryPart > 0.0 {
    angle1 = angle0 - 180.0;
    } else {
    angle1 = angle0;
}
        angle = if angle1[1] > angle1 and angle1[1] - angle1 < 270.0 then
                angle1[1] else angle1;
    plot out = angle;
}
#mstate(Angle, Degrees) =>  // Market State Function
script mstate {
    input Angle = close;
    input Degrees = 45;
    def thresholdInDegrees = Degrees;
    def temp = AbsValue(Angle - Angle[1]) < thresholdInDegrees;
    def state;
    if Angle >= 0.0 and temp {
        state = 1;
    } else
    if Angle < 0.0 and temp {
        state = -1;
    } else {
        state = 0;#state[1];
    }
    plot out = state;
}
#// Correlation Cycle ? ?

def real1      = cc(source, SlowPeriod).real;
def imaginary1 = cc(source, SlowPeriod).imag;
def real2      = cc(source, MediumPeriod).real;
def imaginary2 = cc(source, MediumPeriod).imag;
def real3      = cc(source, FastPeriod).real;
def imaginary3 = cc(source, FastPeriod).imag;

#// Correlation Angle/Phasor ?
def angle1 = cap(real1, imaginary1);
def angle2 = cap(real2, imaginary2);
def angle3 = cap(real3, imaginary3);

#// Market State ?

def state1 = mstate(angle1, SlowThreshold);
def state2 = mstate(angle2, MediumThreshold);
def state3 = mstate(angle3, FastThreshold);

def colorMS1 = if state1 >  0.5 then up_ else
               if state1 < -0.5 then dn_ else na;
def colorMS2 = if state2 >  0.5 then up_ else
               if state2 < -0.5 then dn_ else na;
def colorMS3 = if state3 >  0.5 then up_ else
               if state3 < -0.5 then dn_ else na;
def color1 = if state1 >  0.5 then up_ else
             if state1 < -0.5 then dn_ else color1[1];
def color2 = if state2 >  0.5 then up_ else
             if state2 < -0.5 then dn_ else color2[1];
def color3 = if state3 >  0.5 then up_ else
             if state3 < -0.5 then dn_ else color3[1];

AddCloud(if colorMS1==1 then state1 else na, -state1,
            GlobalColor("dbullc"), GlobalColor("dbullc"));
AddCloud(if colorMS1==2 then state1 else na, -state1,
            GlobalColor("bulla"), GlobalColor("bulla"));
AddCloud(if colorMS1==3 then state1 else na, -state1,
            GlobalColor("bullo"), GlobalColor("bullo"));
AddCloud(if colorMS1==-1 then state1 else na, -state1,
            GlobalColor("dbearc"), GlobalColor("dbearc"));
AddCloud(if colorMS1==-2 then state1 else na, -state1,
            GlobalColor("beara"), GlobalColor("beara"));
AddCloud(if colorMS1==-3 then state1 else na, -state1,
            GlobalColor("bearo"), GlobalColor("bearo"));

plot Medium = if !last then -state2 else na;  # 'State Medium'
plot Fast   = if !last and state3 then -state3 else na;    # 'State Fast'
Fast.SetLineWeight(2);
Medium.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Fast.SetPaintingStrategy(PaintingStrategy.POINTS);
Medium.AssignValueColor(if color2==1 then GlobalColor("bullc") else
                        if color2==2 then GlobalColor("bulla") else
                        if color2==3 then GlobalColor("bullo") else
                        if color2==-1 then GlobalColor("bearc") else
                        if color2==-2 then GlobalColor("beara") else
                        if color2==-3 then GlobalColor("bearo") else Color.GRAY);
Fast.AssignValueColor(if color3==1 then Color.LIGHT_GREEN else
                      if color3==2 then GlobalColor("bulla") else
                      if color3==3 then GlobalColor("bullo") else
                      if color3==-1 then Color.PINK else
                      if color3==-2 then GlobalColor("beara") else
                      if color3==-3 then GlobalColor("bearo") else Color.GRAY);

plot SlowUp = if !last then state1 else na;    # 'State Slow'
plot SlowDn = if !last then -state1 else na;   # 'State Slow'
SlowUp.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
SlowDn.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
#// Bar Gates ?

def trend_slo = if state1 > 0.5 then 1 else
                if state1 < -0.5 then -1 else
                if state1 < 0.5 and state1 > -0.5 then 2 else trend_slo[1];
def trend_med = if state2 > 0.5 then 1 else
                if state2 < -0.5 then -1 else
                if state2 < 0.5 and state2 > -0.5 then 2 else trend_med[1];
def trend_fas = if state3 > 0.5 then 1 else
                if state3 < -0.5 then -1 else
                if state3 < 0.5 and state3 > -0.5 then 2 else trend_fas[1];
def trend_double = if trend_slo == 1 and trend_med == 1 or
                      trend_slo == 1 and trend_fas == 1 or
                      trend_med == 1 and trend_fas == 1 then 1 else
                   if trend_slo == -1 and trend_med == -1 or
                      trend_slo == -1 and trend_fas == -1 or
                      trend_med == -1 and trend_fas == -1 then -1 else trend_double[1];
#/ Parliament arrays ?
def sig_up = (if trend_slo == 1 then 1 else 0) +
             (if trend_med == 1 then 1 else 0) +
             (if trend_fas == 1 then 1 else 0) +          
             (if trend_double == 1 and RecentTrendWeighting then 1 else 0);
def sigSum_up   = sig_up;

def sig_dn = (if trend_slo == -1 then  1 else 0) +
             (if trend_med == -1 then  1 else 0) +
             (if trend_fas == -1 then  1 else 0) +                    
             (if trend_double == -1 and RecentTrendWeighting then 1 else 0);
def sigSum_dn   = sig_dn;
def mid_signal = sigSum_up == sigSum_dn;

plot Retracement = if mid_signal then 0 else na;    # 'Retracement'
Retracement.SetLineWeight(2);
Retracement.SetPaintingStrategy(PaintingStrategy.POINTS);
Retracement.AssignValueColor(if md_==11 then GlobalColor("consc") else
                        if md_==22 then GlobalColor("consa") else
                        if md_==33 then GlobalColor("conso") else Color.GRAY);
def bar_col = if sigSum_up > sigSum_dn then up_ else
              if sigSum_up < sigSum_dn then dn_ else md_;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if bar_col==11 then GlobalColor("consc") else
                 if bar_col==22 then GlobalColor("consa") else
                 if bar_col==33 then GlobalColor("conso") else
                 if bar_col==1 then GlobalColor("bullc") else
                 if bar_col==2 then GlobalColor("bulla") else
                 if bar_col==3 then GlobalColor("bullo") else
                 if bar_col==-1 then GlobalColor("bearc") else
                 if bar_col==-2 then GlobalColor("beara") else
                 if bar_col==-3 then GlobalColor("bearo") else Color.GRAY);
#-- END of CODE
@samer800 could you add a MTF ?
 
Anyway to make this MTF?
@samer800 could you add a MTF ?
try this.

CSS:
# https://www.tradingview.com/v/6vcvpsLs/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#//@Woody_Bearbash
#indicator('Triple Market State', 'Triple MS', false, format.price, 2)
# Converted by Sam4Cok@Samer800    - 09/2023
#-- MTF option added by Sam2Cok@Samer800     - 10/2023
declare lower;
input Theme = {default "Classic", "Night", "Mono"};
input BarColoring = yes;          # 'Bar coloring'
input Price  = FundamentalType.CLOSE;            # 'Source'
input useChartTimeframe = {Default "Yes", "No"};
input customTimeframe   = AggregationPeriod.FIFTEEN_MIN;
input SlowPeriod = 28;            # 'Slow Period'
input SlowThreshold = 9;
input MediumPeriod = 18;          # 'Medium Period'
input MediumThreshold = 15;
input FastPeriod = 9;             # 'Fast Period'
input FastThreshold = 10;
input RecentTrendWeighting = yes; # "Shows on bars ONLY"

def na = Double.NaN;
def last = isNaN(close);
def M1 = Theme == Theme."Classic";
def M2 = Theme == Theme."Night";
def M3 = Theme == Theme."Mono";
def up_ = if M1 then 1 else if M2 then 2 else 3;
def dn_ = if M1 then -1 else if M2 then -2 else -3;
def md_ = if M1 then 11 else if M2 then 22 else 33;

def src = Fundamental(Price);
def srcMTF = Fundamental(Price, Period = customTimeframe);
def inSrc;
Switch (useChartTimeframe) {
Case "No" : inSrc = srcMTF;
Default : inSrc = src;
}

def source = inSrc;

DefineGlobalColor("bullc", CreateColor(65, 198, 7));
DefineGlobalColor("bearc", CreateColor(202, 6, 108));
DefineGlobalColor("consc", CreateColor(216, 131, 5));

DefineGlobalColor("dbullc", Color.DARK_GREEN);
DefineGlobalColor("dbearc", Color.DARK_RED);

DefineGlobalColor("bulla", CreateColor(205, 177, 78));
DefineGlobalColor("beara", CreateColor(192, 104, 227));
DefineGlobalColor("consa", CreateColor(46, 109, 128));

DefineGlobalColor("bullo", CreateColor(194, 189, 189));
DefineGlobalColor("bearo", CreateColor(49, 121, 245));
DefineGlobalColor("conso", CreateColor(216, 131, 5));

#// Functions ? ?
#cc(Series, Period) =>  // Correlation Cycle Function
script cc {
    input Series = close;
    input Perd = 14;
    def PIx2 = 4.0 * ASin(1.0);
    def len = Max(2, Perd);
    def period = len;
    def Rx = fold i = 0 to period with p do
              p + Series[i];
    def Ix = Rx;
    def Rxx = fold i1 = 0 to period with p1 do
              p1 + (Series[i1] * Series[i1]);
    def Ixx = Rxx;
    def Rxy = fold i2 = 0 to period with p2 do
              p2 + (Series[i2] * Cos(PIx2 * i2 / period));
    def Ixy = fold i3 = 0 to period with p3 do
              p3 + (Series[i3] * -Sin(PIx2 * i3 / period));
    def Ryy = fold i4 = 0 to period with p4 do
              p4 + (Cos(PIx2 * i4 / period) *  Cos(PIx2 * i4 / period));
    def Iyy = fold i5 = 0 to period with p5 do
              p5 + (-Sin(PIx2 * i5 / period) * -Sin(PIx2 * i5 / period));
    def Ry  = fold i6 = 0 to period with p6 do
              p6 + (Cos(PIx2 * i6 / period));
    def Iy  = fold i7 = 0 to period with p7 do
              p7 + (-Sin(PIx2 * i7 / period));
    def real_1 = period * Rxx - Rx * Rx;
    def real_2 = period * Ryy - Ry * Ry;
    def real_3 = period * Rxy - Rx * Ry;
    def realPart = if real_1 > 0.0 and real_2 > 0.0 then
                   (real_3) / Sqrt(real_1 * real_2) else realPart[1];
    def imag_1 = period * Ixx - Ix * Ix;
    def imag_2 = period * Iyy - Iy * Iy;
    def imag_3 = period * Ixy - Ix * Iy;
    def imagPart = if imag_1 > 0.0 and imag_2 > 0.0 then
                  (imag_3) / Sqrt(imag_1 * imag_2) else imagPart[1];
    plot real = realPart;
    plot imag = imagPart;
}
#cap(RealPart, ImaginaryPart) =>  // Correlation Angle Phasor Function
script cap {
    input RealPart = 0;
    input ImaginaryPart = 0;
    def HALF_OF_PI = ASin(1.0);
    def Rad = ATan(RealPart / ImaginaryPart) + HALF_OF_PI;
    def DEG_IN_1_RADIAN = 90.0 / HALF_OF_PI;
    def tempAngle = Rad * DEG_IN_1_RADIAN;
    def angle;
    def angle0 = if ImaginaryPart == 0.0 then 0.0 else tempAngle;
    def angle1;
if ImaginaryPart > 0.0 {
    angle1 = angle0 - 180.0;
    } else {
    angle1 = angle0;
}
        angle = if angle1[1] > angle1 and angle1[1] - angle1 < 270.0 then
                angle1[1] else angle1;
    plot out = angle;
}
#mstate(Angle, Degrees) =>  // Market State Function
script mstate {
    input Angle = close;
    input Degrees = 45;
    def thresholdInDegrees = Degrees;
    def temp = AbsValue(Angle - Angle[1]) < thresholdInDegrees;
    def state;
    if Angle >= 0.0 and temp {
        state = 1;
    } else
    if Angle < 0.0 and temp {
        state = -1;
    } else {
        state = 0;#state[1];
    }
    plot out = state;
}
#// Correlation Cycle ? ?

def real1      = cc(source, SlowPeriod).real;
def imaginary1 = cc(source, SlowPeriod).imag;
def real2      = cc(source, MediumPeriod).real;
def imaginary2 = cc(source, MediumPeriod).imag;
def real3      = cc(source, FastPeriod).real;
def imaginary3 = cc(source, FastPeriod).imag;

#// Correlation Angle/Phasor ?
def angle1 = cap(real1, imaginary1);
def angle2 = cap(real2, imaginary2);
def angle3 = cap(real3, imaginary3);

#// Market State ?

def state1 = mstate(angle1, SlowThreshold);
def state2 = mstate(angle2, MediumThreshold);
def state3 = mstate(angle3, FastThreshold);

def colorMS1 = if state1 >  0.5 then up_ else
               if state1 < -0.5 then dn_ else na;
def colorMS2 = if state2 >  0.5 then up_ else
               if state2 < -0.5 then dn_ else na;
def colorMS3 = if state3 >  0.5 then up_ else
               if state3 < -0.5 then dn_ else na;
def color1 = if state1 >  0.5 then up_ else
             if state1 < -0.5 then dn_ else color1[1];
def color2 = if state2 >  0.5 then up_ else
             if state2 < -0.5 then dn_ else color2[1];
def color3 = if state3 >  0.5 then up_ else
             if state3 < -0.5 then dn_ else color3[1];

AddCloud(if colorMS1==1 then state1 else na, -state1,
            GlobalColor("dbullc"), GlobalColor("dbullc"));
AddCloud(if colorMS1==2 then state1 else na, -state1,
            GlobalColor("bulla"), GlobalColor("bulla"));
AddCloud(if colorMS1==3 then state1 else na, -state1,
            GlobalColor("bullo"), GlobalColor("bullo"));
AddCloud(if colorMS1==-1 then state1 else na, -state1,
            GlobalColor("dbearc"), GlobalColor("dbearc"));
AddCloud(if colorMS1==-2 then state1 else na, -state1,
            GlobalColor("beara"), GlobalColor("beara"));
AddCloud(if colorMS1==-3 then state1 else na, -state1,
            GlobalColor("bearo"), GlobalColor("bearo"));

plot Medium = if !last then -state2 else na;  # 'State Medium'
plot Fast   = if !last and state3 then -state3 else na;    # 'State Fast'
Fast.SetLineWeight(2);
Medium.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Fast.SetPaintingStrategy(PaintingStrategy.POINTS);
Medium.AssignValueColor(if color2==1 then GlobalColor("bullc") else
                        if color2==2 then GlobalColor("bulla") else
                        if color2==3 then GlobalColor("bullo") else
                        if color2==-1 then GlobalColor("bearc") else
                        if color2==-2 then GlobalColor("beara") else
                        if color2==-3 then GlobalColor("bearo") else Color.GRAY);
Fast.AssignValueColor(if color3==1 then Color.LIGHT_GREEN else
                      if color3==2 then GlobalColor("bulla") else
                      if color3==3 then GlobalColor("bullo") else
                      if color3==-1 then Color.PINK else
                      if color3==-2 then GlobalColor("beara") else
                      if color3==-3 then GlobalColor("bearo") else Color.GRAY);

plot SlowUp = if !last then state1 else na;    # 'State Slow'
plot SlowDn = if !last then -state1 else na;   # 'State Slow'
SlowUp.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
SlowDn.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
#// Bar Gates ?

def trend_slo = if state1 > 0.5 then 1 else
                if state1 < -0.5 then -1 else
                if state1 < 0.5 and state1 > -0.5 then 2 else trend_slo[1];
def trend_med = if state2 > 0.5 then 1 else
                if state2 < -0.5 then -1 else
                if state2 < 0.5 and state2 > -0.5 then 2 else trend_med[1];
def trend_fas = if state3 > 0.5 then 1 else
                if state3 < -0.5 then -1 else
                if state3 < 0.5 and state3 > -0.5 then 2 else trend_fas[1];
def trend_double = if trend_slo == 1 and trend_med == 1 or
                      trend_slo == 1 and trend_fas == 1 or
                      trend_med == 1 and trend_fas == 1 then 1 else
                   if trend_slo == -1 and trend_med == -1 or
                      trend_slo == -1 and trend_fas == -1 or
                      trend_med == -1 and trend_fas == -1 then -1 else trend_double[1];
#/ Parliament arrays ?
def sig_up = (if trend_slo == 1 then 1 else 0) +
             (if trend_med == 1 then 1 else 0) +
             (if trend_fas == 1 then 1 else 0) +           
             (if trend_double == 1 and RecentTrendWeighting then 1 else 0);
def sigSum_up   = sig_up;

def sig_dn = (if trend_slo == -1 then  1 else 0) +
             (if trend_med == -1 then  1 else 0) +
             (if trend_fas == -1 then  1 else 0) +                     
             (if trend_double == -1 and RecentTrendWeighting then 1 else 0);
def sigSum_dn   = sig_dn;
def mid_signal = sigSum_up == sigSum_dn;

plot Retracement = if mid_signal then 0 else na;    # 'Retracement'
Retracement.SetLineWeight(2);
Retracement.SetPaintingStrategy(PaintingStrategy.POINTS);
Retracement.AssignValueColor(if md_==11 then GlobalColor("consc") else
                        if md_==22 then GlobalColor("consa") else
                        if md_==33 then GlobalColor("conso") else Color.GRAY);
def bar_col = if sigSum_up > sigSum_dn then up_ else
              if sigSum_up < sigSum_dn then dn_ else md_;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if bar_col==11 then GlobalColor("consc") else
                 if bar_col==22 then GlobalColor("consa") else
                 if bar_col==33 then GlobalColor("conso") else
                 if bar_col==1 then GlobalColor("bullc") else
                 if bar_col==2 then GlobalColor("bulla") else
                 if bar_col==3 then GlobalColor("bullo") else
                 if bar_col==-1 then GlobalColor("bearc") else
                 if bar_col==-2 then GlobalColor("beara") else
                 if bar_col==-3 then GlobalColor("bearo") else Color.GRAY);
#-- END of CODE
 
Last edited by a moderator:
try this.

CSS:
# https://www.tradingview.com/v/6vcvpsLs/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#//@Woody_Bearbash
#indicator('Triple Market State', 'Triple MS', false, format.price, 2)
# Converted by Sam4Cok@Samer800    - 09/2023
#-- MTF option added by Sam2Cok@Samer800     - 10/2023
declare lower;
input Theme = {default "Classic", "Night", "Mono"};
input BarColoring = yes;          # 'Bar coloring'
input Price  = FundamentalType.CLOSE;            # 'Source'
input useChartTimeframe = {Default "Yes", "No"};
input customTimeframe   = AggregationPeriod.FIFTEEN_MIN;
input SlowPeriod = 28;            # 'Slow Period'
input SlowThreshold = 9;
input MediumPeriod = 18;          # 'Medium Period'
input MediumThreshold = 15;
input FastPeriod = 9;             # 'Fast Period'
input FastThreshold = 10;
input RecentTrendWeighting = yes; # "Shows on bars ONLY"

def na = Double.NaN;
def last = isNaN(close);
def M1 = Theme == Theme."Classic";
def M2 = Theme == Theme."Night";
def M3 = Theme == Theme."Mono";
def up_ = if M1 then 1 else if M2 then 2 else 3;
def dn_ = if M1 then -1 else if M2 then -2 else -3;
def md_ = if M1 then 11 else if M2 then 22 else 33;

def src = Fundamental(Price);
def srcMTF = Fundamental(Price, Period = customTimeframe);
def inSrc;
Switch (useChartTimeframe) {
Case "No" : inSrc = srcMTF;
Default : inSrc = src;
}

def source = inSrc;

DefineGlobalColor("bullc", CreateColor(65, 198, 7));
DefineGlobalColor("bearc", CreateColor(202, 6, 108));
DefineGlobalColor("consc", CreateColor(216, 131, 5));

DefineGlobalColor("dbullc", Color.DARK_GREEN);
DefineGlobalColor("dbearc", Color.DARK_RED);

DefineGlobalColor("bulla", CreateColor(205, 177, 78));
DefineGlobalColor("beara", CreateColor(192, 104, 227));
DefineGlobalColor("consa", CreateColor(46, 109, 128));

DefineGlobalColor("bullo", CreateColor(194, 189, 189));
DefineGlobalColor("bearo", CreateColor(49, 121, 245));
DefineGlobalColor("conso", CreateColor(216, 131, 5));

#// Functions ? ?
#cc(Series, Period) =>  // Correlation Cycle Function
script cc {
    input Series = close;
    input Perd = 14;
    def PIx2 = 4.0 * ASin(1.0);
    def len = Max(2, Perd);
    def period = len;
    def Rx = fold i = 0 to period with p do
              p + Series[i];
    def Ix = Rx;
    def Rxx = fold i1 = 0 to period with p1 do
              p1 + (Series[i1] * Series[i1]);
    def Ixx = Rxx;
    def Rxy = fold i2 = 0 to period with p2 do
              p2 + (Series[i2] * Cos(PIx2 * i2 / period));
    def Ixy = fold i3 = 0 to period with p3 do
              p3 + (Series[i3] * -Sin(PIx2 * i3 / period));
    def Ryy = fold i4 = 0 to period with p4 do
              p4 + (Cos(PIx2 * i4 / period) *  Cos(PIx2 * i4 / period));
    def Iyy = fold i5 = 0 to period with p5 do
              p5 + (-Sin(PIx2 * i5 / period) * -Sin(PIx2 * i5 / period));
    def Ry  = fold i6 = 0 to period with p6 do
              p6 + (Cos(PIx2 * i6 / period));
    def Iy  = fold i7 = 0 to period with p7 do
              p7 + (-Sin(PIx2 * i7 / period));
    def real_1 = period * Rxx - Rx * Rx;
    def real_2 = period * Ryy - Ry * Ry;
    def real_3 = period * Rxy - Rx * Ry;
    def realPart = if real_1 > 0.0 and real_2 > 0.0 then
                   (real_3) / Sqrt(real_1 * real_2) else realPart[1];
    def imag_1 = period * Ixx - Ix * Ix;
    def imag_2 = period * Iyy - Iy * Iy;
    def imag_3 = period * Ixy - Ix * Iy;
    def imagPart = if imag_1 > 0.0 and imag_2 > 0.0 then
                  (imag_3) / Sqrt(imag_1 * imag_2) else imagPart[1];
    plot real = realPart;
    plot imag = imagPart;
}
#cap(RealPart, ImaginaryPart) =>  // Correlation Angle Phasor Function
script cap {
    input RealPart = 0;
    input ImaginaryPart = 0;
    def HALF_OF_PI = ASin(1.0);
    def Rad = ATan(RealPart / ImaginaryPart) + HALF_OF_PI;
    def DEG_IN_1_RADIAN = 90.0 / HALF_OF_PI;
    def tempAngle = Rad * DEG_IN_1_RADIAN;
    def angle;
    def angle0 = if ImaginaryPart == 0.0 then 0.0 else tempAngle;
    def angle1;
if ImaginaryPart > 0.0 {
    angle1 = angle0 - 180.0;
    } else {
    angle1 = angle0;
}
        angle = if angle1[1] > angle1 and angle1[1] - angle1 < 270.0 then
                angle1[1] else angle1;
    plot out = angle;
}
#mstate(Angle, Degrees) =>  // Market State Function
script mstate {
    input Angle = close;
    input Degrees = 45;
    def thresholdInDegrees = Degrees;
    def temp = AbsValue(Angle - Angle[1]) < thresholdInDegrees;
    def state;
    if Angle >= 0.0 and temp {
        state = 1;
    } else
    if Angle < 0.0 and temp {
        state = -1;
    } else {
        state = 0;#state[1];
    }
    plot out = state;
}
#// Correlation Cycle ? ?

def real1      = cc(source, SlowPeriod).real;
def imaginary1 = cc(source, SlowPeriod).imag;
def real2      = cc(source, MediumPeriod).real;
def imaginary2 = cc(source, MediumPeriod).imag;
def real3      = cc(source, FastPeriod).real;
def imaginary3 = cc(source, FastPeriod).imag;

#// Correlation Angle/Phasor ?
def angle1 = cap(real1, imaginary1);
def angle2 = cap(real2, imaginary2);
def angle3 = cap(real3, imaginary3);

#// Market State ?

def state1 = mstate(angle1, SlowThreshold);
def state2 = mstate(angle2, MediumThreshold);
def state3 = mstate(angle3, FastThreshold);

def colorMS1 = if state1 >  0.5 then up_ else
               if state1 < -0.5 then dn_ else na;
def colorMS2 = if state2 >  0.5 then up_ else
               if state2 < -0.5 then dn_ else na;
def colorMS3 = if state3 >  0.5 then up_ else
               if state3 < -0.5 then dn_ else na;
def color1 = if state1 >  0.5 then up_ else
             if state1 < -0.5 then dn_ else color1[1];
def color2 = if state2 >  0.5 then up_ else
             if state2 < -0.5 then dn_ else color2[1];
def color3 = if state3 >  0.5 then up_ else
             if state3 < -0.5 then dn_ else color3[1];

AddCloud(if colorMS1==1 then state1 else na, -state1,
            GlobalColor("dbullc"), GlobalColor("dbullc"));
AddCloud(if colorMS1==2 then state1 else na, -state1,
            GlobalColor("bulla"), GlobalColor("bulla"));
AddCloud(if colorMS1==3 then state1 else na, -state1,
            GlobalColor("bullo"), GlobalColor("bullo"));
AddCloud(if colorMS1==-1 then state1 else na, -state1,
            GlobalColor("dbearc"), GlobalColor("dbearc"));
AddCloud(if colorMS1==-2 then state1 else na, -state1,
            GlobalColor("beara"), GlobalColor("beara"));
AddCloud(if colorMS1==-3 then state1 else na, -state1,
            GlobalColor("bearo"), GlobalColor("bearo"));

plot Medium = if !last then -state2 else na;  # 'State Medium'
plot Fast   = if !last and state3 then -state3 else na;    # 'State Fast'
Fast.SetLineWeight(2);
Medium.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Fast.SetPaintingStrategy(PaintingStrategy.POINTS);
Medium.AssignValueColor(if color2==1 then GlobalColor("bullc") else
                        if color2==2 then GlobalColor("bulla") else
                        if color2==3 then GlobalColor("bullo") else
                        if color2==-1 then GlobalColor("bearc") else
                        if color2==-2 then GlobalColor("beara") else
                        if color2==-3 then GlobalColor("bearo") else Color.GRAY);
Fast.AssignValueColor(if color3==1 then Color.LIGHT_GREEN else
                      if color3==2 then GlobalColor("bulla") else
                      if color3==3 then GlobalColor("bullo") else
                      if color3==-1 then Color.PINK else
                      if color3==-2 then GlobalColor("beara") else
                      if color3==-3 then GlobalColor("bearo") else Color.GRAY);

plot SlowUp = if !last then state1 else na;    # 'State Slow'
plot SlowDn = if !last then -state1 else na;   # 'State Slow'
SlowUp.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
SlowDn.AssignValueColor(if color1==1 then GlobalColor("bullc") else
                        if color1==2 then GlobalColor("bulla") else
                        if color1==3 then GlobalColor("bullo") else
                        if color1==-1 then GlobalColor("bearc") else
                        if color1==-2 then GlobalColor("beara") else
                        if color1==-3 then GlobalColor("bearo") else Color.GRAY);
#// Bar Gates ?

def trend_slo = if state1 > 0.5 then 1 else
                if state1 < -0.5 then -1 else
                if state1 < 0.5 and state1 > -0.5 then 2 else trend_slo[1];
def trend_med = if state2 > 0.5 then 1 else
                if state2 < -0.5 then -1 else
                if state2 < 0.5 and state2 > -0.5 then 2 else trend_med[1];
def trend_fas = if state3 > 0.5 then 1 else
                if state3 < -0.5 then -1 else
                if state3 < 0.5 and state3 > -0.5 then 2 else trend_fas[1];
def trend_double = if trend_slo == 1 and trend_med == 1 or
                      trend_slo == 1 and trend_fas == 1 or
                      trend_med == 1 and trend_fas == 1 then 1 else
                   if trend_slo == -1 and trend_med == -1 or
                      trend_slo == -1 and trend_fas == -1 or
                      trend_med == -1 and trend_fas == -1 then -1 else trend_double[1];
#/ Parliament arrays ?
def sig_up = (if trend_slo == 1 then 1 else 0) +
             (if trend_med == 1 then 1 else 0) +
             (if trend_fas == 1 then 1 else 0) +          
             (if trend_double == 1 and RecentTrendWeighting then 1 else 0);
def sigSum_up   = sig_up;

def sig_dn = (if trend_slo == -1 then  1 else 0) +
             (if trend_med == -1 then  1 else 0) +
             (if trend_fas == -1 then  1 else 0) +                    
             (if trend_double == -1 and RecentTrendWeighting then 1 else 0);
def sigSum_dn   = sig_dn;
def mid_signal = sigSum_up == sigSum_dn;

plot Retracement = if mid_signal then 0 else na;    # 'Retracement'
Retracement.SetLineWeight(2);
Retracement.SetPaintingStrategy(PaintingStrategy.POINTS);
Retracement.AssignValueColor(if md_==11 then GlobalColor("consc") else
                        if md_==22 then GlobalColor("consa") else
                        if md_==33 then GlobalColor("conso") else Color.GRAY);
def bar_col = if sigSum_up > sigSum_dn then up_ else
              if sigSum_up < sigSum_dn then dn_ else md_;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if bar_col==11 then GlobalColor("consc") else
                 if bar_col==22 then GlobalColor("consa") else
                 if bar_col==33 then GlobalColor("conso") else
                 if bar_col==1 then GlobalColor("bullc") else
                 if bar_col==2 then GlobalColor("bulla") else
                 if bar_col==3 then GlobalColor("bullo") else
                 if bar_col==-1 then GlobalColor("bearc") else
                 if bar_col==-2 then GlobalColor("beara") else
                 if bar_col==-3 then GlobalColor("bearo") else Color.GRAY);
#-- END of CODE
Thank You!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
387 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