Confirmation Candles Indicator For ThinkorSwim

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

Hi @Christopher84,
Thank you for all the indicators you created and shared with us. I am using scalper and its helping me a lot to ride the trend but loosing and struggeling to determine side ways or choppy market..
Could you please provide some tips or tricks when not to get into a trade in short time frame (1/3/5 mins) on /ES or SPY?
Thanks!

If you like, I'll share a few things that helped me along the way.

1- Trading on timeframes of 5 min or less can be very difficult. Usually I load a grid with 5, 10, and 15 min charts. If those charts are showing a mix of long and short, I stay out. If they are all trending in the same direction, then I look at 1 or 2 min charts for an entry in the direction that the higher timeframes are showing. Typically, I don't trade in a direction opposite of the daily time frame trend, but your risk tolerance may be different. If you load a 10 min chart, you may find that the signals look better to your eye than a 2 min chart. There is no shame in trading on a higher timeframe!

2- Another indicator I like is the VWAP. I load the daily value onto the chart. If price is below VWAP, I look for shorts. If above VWAP, I look for longs.

3- Another thing you can try is loading the 50 and 200 EMA. If stacked negative, look only for short signals. If stacked positive, look only for longs.

4- Know the times of day your strategy works best. For example, I trade some instruments on the Euro session only. Others I trade during specific hours of the US session.

5- Keep a trading log/journal of what is working and what isn't. It can help fine tune your strategy.

Don't know if that helped, but wish you the best. Happy trading!
 
Hey @Falout!
It appears that the default time frames have to be changed in the code to get the floating PL study to work. I have put together 3 different versions. Please be cautious that these strategies can repaint until all time frames in the calculation have closed. Here's the Scalper strategy for 3 min chart.
Code:
#Scalper Upper v2 Strategy Created 03/16/2022 by Christopher84

declare upper;

input price = close;
input length = 10;
input length2 = 35;
input agperiod1 = { "1 min", "2 min", default "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod2 = {"1 min", "2 min", "3 min",default "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod3 = {"1 min", "2 min", "3 min", "5 min", default "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod4 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", default "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod5 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", default "4 hours", "Day", "Week", "Month"};
input agperiod6 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week", "Month"};
def displace = 0;
input paintCandles = yes;
input show_ema_cloud = yes;

#Current Period
plot AvgExp = ExpAverage(price[-displace], length);
AvgExp.SetStyle(Curve.SHORT_DASH);
def UPC1 = AvgExp > AvgExp[1];
def DNC1 = AvgExp < AvgExp[1];

plot AvgExp2 = ExpAverage(price[-displace], length2);
AvgExp2.SetStyle(Curve.SHORT_DASH);
def UPC2 = AvgExp2 > AvgExp2[1];
def DNC2 = AvgExp2 < AvgExp2[1];

def Below = AvgExp < AvgExp2;
def Spark = UPC1 + UPC2 + Below;

def UPEMA = AvgExp[1] < AvgExp;
def DOWNEMA = AvgExp[1] > AvgExp;
AvgExp.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp2[1] < AvgExp2;
def DOWNEMA2 = AvgExp2[1] > AvgExp2;
AvgExp2.AssignValueColor(if UPEMA2 then Color.LIGHT_GREEN else if DOWNEMA2 then Color.RED else Color.YELLOW);

AddCloud(if show_ema_cloud and (AvgExp2 > AvgExp) then AvgExp2 else Double.NaN, AvgExp, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExp > AvgExp2) then AvgExp else Double.NaN, AvgExp2, Color.LIGHT_GREEN, Color.CURRENT);

#Agperiod1
def avg = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];

def avg2 = ExpAverage(close(period = agperiod1), length2);
def height2 = avg2 - avg2[length2];

def UP = avg > avg2;
def DOWN = avg < avg2;

def R1UP = avg > avg[1];
def R1DN = avg < avg[1];
def R2UP = avg2 > avg2[1];
def R2DN = avg2 < avg2[1];

#Agperiod2
def avg3 = ExpAverage(close(period = agperiod2), length);
def height3 = avg3 - avg3[length];

def avg4 = ExpAverage(close(period = agperiod2), length2);
def height4 = avg4 - avg4[length2];

def UP2 = avg3 > avg4;
def DOWN2 = avg3 < avg4;

def R3UP = avg3 > avg3[1];
def R3DN = avg3 < avg3[1];
def R4UP = avg4 > avg4[1];
def R4DN = avg4 < avg4[1];

#Agperiod3
def avg5 = ExpAverage(close(period = agperiod3), length);
def height5 = avg5 - avg5[length];

def avg6 = ExpAverage(close(period = agperiod3), length2);
def height6 = avg6 - avg6[length2];

def UP3 = avg5 > avg6;
def DOWN3 = avg5 < avg6;

def R5UP = avg5 > avg5[1];
def R5DN = avg5 < avg5[1];
def R6UP = avg6 > avg6[1];
def R6DN = avg6 < avg6[1];

#Agperiod4
def avg7 = ExpAverage(close(period = agperiod4), length);
def height7 = avg7 - avg7[length];

def avg8 = ExpAverage(close(period = agperiod4), length2);
def height8 = avg8 - avg8[length2];

def UP4 = avg7 > avg8;
def DOWN4 = avg7 < avg8;

def R7UP = avg7 > avg7[1];
def R7DN = avg7 < avg7[1];
def R8UP = avg8 > avg8[1];
def R8DN = avg8 < avg8[1];

#Agperiod5
def avg9 = ExpAverage(close(period = agperiod5), length);
def height9 = avg9 - avg9[length];

def avg10 = ExpAverage(close(period = agperiod5), length2);
def height10 = avg10 - avg10[length2];

def UP5 = avg9 > avg10;
def DOWN5 = avg9 < avg10;

def R9UP = avg9 > avg9[1];
def R9DN = avg9 < avg9[1];
def R10UP = avg10 > avg10[1];
def R10DN = avg10 < avg10[1];

#Agperiod6
def avg11 = ExpAverage(close(period = agperiod6), length);
def height11 = avg11 - avg11[length];

def avg12 = ExpAverage(close(period = agperiod6), length2);
def height12 = avg12 - avg12[length2];

def UP6 = avg11 > avg12;
def DOWN6 = avg11 < avg12;

def R11UP = avg11 > avg11[1];
def R11DN = avg11 < avg11[1];
def R12UP = avg12 > avg12[1];
def R12DN = avg12 < avg12[1];

def Long_Only = UP + UP2 + UP3 + UP4 + UP5 + UP6;
def Short_Only = DOWN + DOWN2 + DOWN3 + DOWN4 + DOWN5 + DOWN6;
def Consensus_Bias = Long_Only - Short_Only;

def RUP = UPC1 + UPC2 + R1UP + R2UP + R3UP + R4UP + R5UP + R6UP + R7UP + R8UP + R9UP + R10UP + R11UP + R12UP;
def RDN = DNC1 + DNC2 + R1DN + R2DN + R3DN + R4DN + R5DN + R6DN + R7DN + R8DN + R9DN + R10DN + R11DN + R12DN;
def ConsensusR = RUP - RDN;

script WMA_Smooth {
    input price = hl2;
    plot smooth = (4 * price
+ 3 * price[1]
+ 2 * price[2]
+ price[3]) / 10;
}

script Phase_Accumulation {
# This is Ehler's Phase Accumulation code. It has a full cycle delay.
# However, it computes the correction factor to a very high degree.
#
    input price = hl2;

    rec Smooth;
    rec Detrender;
    rec Period;
    rec Q1;
    rec I1;
    rec I1p;
    rec Q1p;
    rec Phase1;
    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase1;
    rec InstPeriod1;
    rec InstPeriod;
    def CorrectionFactor;

    if BarNumber() <= 5
    then {
        Period = 0;
        Smooth = 0;
        Detrender = 0;
        CorrectionFactor = 0;
        Q1 = 0;
        I1 = 0;
        Q1p = 0;
        I1p = 0;
        Phase = 0;
        Phase1 = 0;
        DeltaPhase1 = 0;
        DeltaPhase = 0;
        InstPeriod = 0;
        InstPeriod1 = 0;
    } else {
        CorrectionFactor = 0.075 * Period[1] + 0.54;

# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

# Compute Quadrature and Phase of Detrended signal:
        Q1p = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1p = Detrender[3];

# Smooth out Quadrature and Phase:
        I1 = 0.15 * I1p + 0.85 * I1p[1];
        Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

# Determine Phase
        if I1 != 0
        then {
# Normally, ATAN gives results from -pi/2 to pi/2.
# We need to map this to circular coordinates 0 to 2pi

            if Q1 >= 0 and I1 > 0
            then { # Quarant 1
                Phase1 = ATan(AbsValue(Q1 / I1));
            } else if Q1 >= 0 and I1 < 0
            then { # Quadrant 2
                Phase1 = Double.Pi - ATan(AbsValue(Q1 / I1));
            } else if Q1 < 0 and I1 < 0
            then { # Quadrant 3
                Phase1 = Double.Pi + ATan(AbsValue(Q1 / I1));
            } else { # Quadrant 4
                Phase1 = 2 * Double.Pi - ATan(AbsValue(Q1 / I1));
            }
        } else if Q1 > 0
        then { # I1 == 0, Q1 is positive
            Phase1 = Double.Pi / 2;
        } else if Q1 < 0
        then { # I1 == 0, Q1 is negative
            Phase1 = 3 * Double.Pi / 2;
        } else { # I1 and Q1 == 0
            Phase1 = 0;
        }

# Convert phase to degrees
        Phase = Phase1 * 180 / Double.Pi;

        if Phase[1] < 90 and Phase > 270
        then {
# This occurs when there is a big jump from 360-0
            DeltaPhase1 = 360 + Phase[1] - Phase;
        } else {
            DeltaPhase1 = Phase[1] - Phase;
        }

# Limit our delta phases between 7 and 60
        if DeltaPhase1 < 7
        then {
            DeltaPhase = 7;
        } else if DeltaPhase1 > 60
        then {
            DeltaPhase = 60;
        } else {
            DeltaPhase = DeltaPhase1;
        }

# Determine Instantaneous period:
        InstPeriod1 =
-1 * (fold i = 0 to 40 with v=0 do
if v < 0 then
v
else if v > 360 then
-i
else
v + GetValue(DeltaPhase, i, 41)
);

        if InstPeriod1 <= 0
        then {
            InstPeriod = InstPeriod[1];
        } else {
            InstPeriod = InstPeriod1;
        }

        Period = 0.25 * InstPeriod + 0.75 * Period[1];
    }
    plot DC = Period;
}

script Ehler_MAMA {
    input price = hl2;
    input FastLimit = 0.5;
    input SlowLimit = 0.05;


    rec Period;
    rec Period_raw;
    rec Period_cap;
    rec Period_lim;

    rec Smooth;
    rec Detrender;
    rec I1;
    rec Q1;
    rec jI;
    rec jQ;
    rec I2;
    rec Q2;
    rec I2_raw;
    rec Q2_raw;

    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase_raw;
    rec alpha;
    rec alpha_raw;

    rec Re;
    rec Im;
    rec Re_raw;
    rec Im_raw;

    rec SmoothPeriod;
    rec vmama;
    rec vfama;

    def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

    if BarNumber() <= 5
    then {
        Smooth = 0;
        Detrender = 0;

        Period = 0;
        Period_raw = 0;
        Period_cap = 0;
        Period_lim = 0;
        I1 = 0;
        Q1 = 0;
        I2 = 0;
        Q2 = 0;
        jI = 0;
        jQ = 0;
        I2_raw = 0;
        Q2_raw = 0;
        Re = 0;
        Im = 0;
        Re_raw = 0;
        Im_raw = 0;
        SmoothPeriod = 0;
        Phase = 0;
        DeltaPhase = 0;
        DeltaPhase_raw = 0;
        alpha = 0;
        alpha_raw = 0;
        vmama = 0;
        vfama = 0;
    } else {
# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

        Q1 = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1 = Detrender[3];

        jI = ( 0.0962 * I1
+ 0.5769 * I1[2]
- 0.5769 * I1[4]
- 0.0962 * I1[6] ) * CorrectionFactor;

        jQ = ( 0.0962 * Q1
+ 0.5769 * Q1[2]
- 0.5769 * Q1[4]
- 0.0962 * Q1[6] ) * CorrectionFactor;

# This is the complex conjugate
        I2_raw = I1 - jQ;
        Q2_raw = Q1 + jI;

        I2 = 0.2 * I2_raw + 0.8 * I2_raw[1];
        Q2 = 0.2 * Q2_raw + 0.8 * Q2_raw[1];

        Re_raw = I2 * I2[1] + Q2 * Q2[1];
        Im_raw = I2 * Q2[1] - Q2 * I2[1];

        Re = 0.2 * Re_raw + 0.8 * Re_raw[1];
        Im = 0.2 * Im_raw + 0.8 * Im_raw[1];

# Compute the phase
        if Re != 0 and Im != 0
        then {
            Period_raw = 2 * Double.Pi / ATan(Im / Re);
        } else {
            Period_raw = 0;
        }

        if Period_raw > 1.5 * Period_raw[1]
        then {
            Period_cap = 1.5 * Period_raw[1];
        } else if Period_raw < 0.67 * Period_raw[1] {
            Period_cap = 0.67 * Period_raw[1];
        } else {
            Period_cap = Period_raw;
        }

        if Period_cap < 6
        then {
            Period_lim = 6;
        } else if Period_cap > 50
        then {
            Period_lim = 50;
        } else {
            Period_lim = Period_cap;
        }

        Period = 0.2 * Period_lim + 0.8 * Period_lim[1];
        SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[1];

        if I1 != 0
        then {
            Phase = ATan(Q1 / I1);
        } else if Q1 > 0
        then { # Quadrant 1:
            Phase = Double.Pi / 2;
        } else if Q1 < 0
        then { # Quadrant 4:
            Phase = -Double.Pi / 2;
        } else { # Both numerator and denominator are 0.
            Phase = 0;
        }

        DeltaPhase_raw = Phase[1] - Phase;
        if DeltaPhase_raw < 1
        then {
            DeltaPhase = 1;
        } else {
            DeltaPhase = DeltaPhase_raw;
        }

        alpha_raw = FastLimit / DeltaPhase;
        if alpha_raw < SlowLimit
        then {
            alpha = SlowLimit;
        } else {
            alpha = alpha_raw;
        }
        vmama = alpha * price + (1 - alpha) * vmama[1];
        vfama = 0.5 * alpha * vmama + (1 - 0.5 * alpha) * vfama[1];
    }

    plot MAMA = vmama;
    plot FAMA = vfama;
}


input price2 = hl2;
input FastLimit = 0.5;
input SlowLimit = 0.05;

def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;

def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),

if MAMA > FAMA then Color.GREEN else Color.RED);

##################################
plot C3_MF_Line = (MAMA + FAMA) / 2;
C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
C3_MF_Line.SetLineWeight(3);

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;
C3_MF_Line.AssignValueColor(if paintCandles and ((direction == 1) and (price > C3_MF_Line)) then Color.GREEN else if paintCandles and ((direction == -1) and (price < C3_MF_Line)) then Color.RED else Color.GRAY);


plot buy = AvgExp crosses above AvgExp2;#direction crosses above 0;
buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.SetDefaultColor(Color.WHITE);

plot sell = AvgExp crosses below AvgExp2;#direction crosses below 0;
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN );
sell.SetDefaultColor(Color.WHITE);

AssignPriceColor(if paintCandles then if direction == 1 then Color.GREEN else if direction == -1 then Color.RED else Color.GRAY else Color.CURRENT);

AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.YELLOW else if ((Spark == 2) and (AvgExp > AvgExp2)) then Color.GREEN else if (Spark == 0) then Color.RED else Color.GRAY);

AddLabel(yes, if ((UP6 == 1) and (Consensus_Bias > 0)) then " SCALP_LONG " else if ((DOWN6) and (Consensus_Bias < 0)) then " SCALP_SHORT " else " CHOP ", if ((Consensus_Bias > 0) and (UP6 == 1)) then Color.GREEN else if ((Consensus_Bias < 0) and (DOWN6 == 1)) then Color.RED else Color.GRAY);

AddLabel(yes, if (ConsensusR > 0) then " LONG BIAS = %" + Round((ConsensusR / 14) * 100, 1) + " " else if (ConsensusR < 0) then  " SHORT BIAS = %" + Round(((ConsensusR * -1) / 14) * 100, 1) + " " else " CHOP =" + Round((ConsensusR / 14) * 100, 1) + " ", if (ConsensusR > 0) then Color.GREEN else if (ConsensusR < 0) then Color.RED else Color.GRAY);

Alert(direction crosses above 0, "long", Alert.BAR, Sound.DING);
Alert(direction crosses below 0, "short", Alert.BAR, Sound.DING);

#Strategy
def Long_Entry =  (ConsensusR crosses above Consensus_Bias);
def Long_Exit =  (ConsensusR crosses below Consensus_Bias);
AddOrder(OrderType.BUY_AUTO, condition = Long_Entry, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME, name = "LE");
AddOrder(OrderType.SELL_AUTO, condition = Long_Exit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME, name = "SE");

Alert(Long_Entry, "long", Alert.BAR, Sound.DING);
Alert(Long_Exit, "short", Alert.BAR, Sound.DING);

Here's the strategy for the 1 min chart.
Code:
#Scalper Upper v2 Strategy Created 03/16/2022 by Christopher84

declare upper;

input price = close;
input length = 10;
input length2 = 35;
input agperiod1 = { "1 min", default "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod2 = {"1 min", "2 min", default "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod3 = {"1 min", "2 min", "3 min", "5 min", default "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod4 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", default "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod5 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", default "4 hours", "Day", "Week", "Month"};
input agperiod6 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week", "Month"};
def displace = 0;
input paintCandles = yes;
input show_ema_cloud = yes;

#Current Period
plot AvgExp = ExpAverage(price[-displace], length);
AvgExp.SetStyle(Curve.SHORT_DASH);
def UPC1 = AvgExp > AvgExp[1];
def DNC1 = AvgExp < AvgExp[1];

plot AvgExp2 = ExpAverage(price[-displace], length2);
AvgExp2.SetStyle(Curve.SHORT_DASH);
def UPC2 = AvgExp2 > AvgExp2[1];
def DNC2 = AvgExp2 < AvgExp2[1];

def Below = AvgExp < AvgExp2;
def Spark = UPC1 + UPC2 + Below;

def UPEMA = AvgExp[1] < AvgExp;
def DOWNEMA = AvgExp[1] > AvgExp;
AvgExp.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp2[1] < AvgExp2;
def DOWNEMA2 = AvgExp2[1] > AvgExp2;
AvgExp2.AssignValueColor(if UPEMA2 then Color.LIGHT_GREEN else if DOWNEMA2 then Color.RED else Color.YELLOW);

AddCloud(if show_ema_cloud and (AvgExp2 > AvgExp) then AvgExp2 else Double.NaN, AvgExp, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExp > AvgExp2) then AvgExp else Double.NaN, AvgExp2, Color.LIGHT_GREEN, Color.CURRENT);

#Agperiod1
def avg = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];

def avg2 = ExpAverage(close(period = agperiod1), length2);
def height2 = avg2 - avg2[length2];

def UP = avg > avg2;
def DOWN = avg < avg2;

def R1UP = avg > avg[1];
def R1DN = avg < avg[1];
def R2UP = avg2 > avg2[1];
def R2DN = avg2 < avg2[1];

#Agperiod2
def avg3 = ExpAverage(close(period = agperiod2), length);
def height3 = avg3 - avg3[length];

def avg4 = ExpAverage(close(period = agperiod2), length2);
def height4 = avg4 - avg4[length2];

def UP2 = avg3 > avg4;
def DOWN2 = avg3 < avg4;

def R3UP = avg3 > avg3[1];
def R3DN = avg3 < avg3[1];
def R4UP = avg4 > avg4[1];
def R4DN = avg4 < avg4[1];

#Agperiod3
def avg5 = ExpAverage(close(period = agperiod3), length);
def height5 = avg5 - avg5[length];

def avg6 = ExpAverage(close(period = agperiod3), length2);
def height6 = avg6 - avg6[length2];

def UP3 = avg5 > avg6;
def DOWN3 = avg5 < avg6;

def R5UP = avg5 > avg5[1];
def R5DN = avg5 < avg5[1];
def R6UP = avg6 > avg6[1];
def R6DN = avg6 < avg6[1];

#Agperiod4
def avg7 = ExpAverage(close(period = agperiod4), length);
def height7 = avg7 - avg7[length];

def avg8 = ExpAverage(close(period = agperiod4), length2);
def height8 = avg8 - avg8[length2];

def UP4 = avg7 > avg8;
def DOWN4 = avg7 < avg8;

def R7UP = avg7 > avg7[1];
def R7DN = avg7 < avg7[1];
def R8UP = avg8 > avg8[1];
def R8DN = avg8 < avg8[1];

#Agperiod5
def avg9 = ExpAverage(close(period = agperiod5), length);
def height9 = avg9 - avg9[length];

def avg10 = ExpAverage(close(period = agperiod5), length2);
def height10 = avg10 - avg10[length2];

def UP5 = avg9 > avg10;
def DOWN5 = avg9 < avg10;

def R9UP = avg9 > avg9[1];
def R9DN = avg9 < avg9[1];
def R10UP = avg10 > avg10[1];
def R10DN = avg10 < avg10[1];

#Agperiod6
def avg11 = ExpAverage(close(period = agperiod6), length);
def height11 = avg11 - avg11[length];

def avg12 = ExpAverage(close(period = agperiod6), length2);
def height12 = avg12 - avg12[length2];

def UP6 = avg11 > avg12;
def DOWN6 = avg11 < avg12;

def R11UP = avg11 > avg11[1];
def R11DN = avg11 < avg11[1];
def R12UP = avg12 > avg12[1];
def R12DN = avg12 < avg12[1];

def Long_Only = UP + UP2 + UP3 + UP4 + UP5 + UP6;
def Short_Only = DOWN + DOWN2 + DOWN3 + DOWN4 + DOWN5 + DOWN6;
def Consensus_Bias = Long_Only - Short_Only;

def RUP = UPC1 + UPC2 + R1UP + R2UP + R3UP + R4UP + R5UP + R6UP + R7UP + R8UP + R9UP + R10UP + R11UP + R12UP;
def RDN = DNC1 + DNC2 + R1DN + R2DN + R3DN + R4DN + R5DN + R6DN + R7DN + R8DN + R9DN + R10DN + R11DN + R12DN;
def ConsensusR = RUP - RDN;

script WMA_Smooth {
    input price = hl2;
    plot smooth = (4 * price
+ 3 * price[1]
+ 2 * price[2]
+ price[3]) / 10;
}

script Phase_Accumulation {
# This is Ehler's Phase Accumulation code. It has a full cycle delay.
# However, it computes the correction factor to a very high degree.
#
    input price = hl2;

    rec Smooth;
    rec Detrender;
    rec Period;
    rec Q1;
    rec I1;
    rec I1p;
    rec Q1p;
    rec Phase1;
    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase1;
    rec InstPeriod1;
    rec InstPeriod;
    def CorrectionFactor;

    if BarNumber() <= 5
    then {
        Period = 0;
        Smooth = 0;
        Detrender = 0;
        CorrectionFactor = 0;
        Q1 = 0;
        I1 = 0;
        Q1p = 0;
        I1p = 0;
        Phase = 0;
        Phase1 = 0;
        DeltaPhase1 = 0;
        DeltaPhase = 0;
        InstPeriod = 0;
        InstPeriod1 = 0;
    } else {
        CorrectionFactor = 0.075 * Period[1] + 0.54;

# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

# Compute Quadrature and Phase of Detrended signal:
        Q1p = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1p = Detrender[3];

# Smooth out Quadrature and Phase:
        I1 = 0.15 * I1p + 0.85 * I1p[1];
        Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

# Determine Phase
        if I1 != 0
        then {
# Normally, ATAN gives results from -pi/2 to pi/2.
# We need to map this to circular coordinates 0 to 2pi

            if Q1 >= 0 and I1 > 0
            then { # Quarant 1
                Phase1 = ATan(AbsValue(Q1 / I1));
            } else if Q1 >= 0 and I1 < 0
            then { # Quadrant 2
                Phase1 = Double.Pi - ATan(AbsValue(Q1 / I1));
            } else if Q1 < 0 and I1 < 0
            then { # Quadrant 3
                Phase1 = Double.Pi + ATan(AbsValue(Q1 / I1));
            } else { # Quadrant 4
                Phase1 = 2 * Double.Pi - ATan(AbsValue(Q1 / I1));
            }
        } else if Q1 > 0
        then { # I1 == 0, Q1 is positive
            Phase1 = Double.Pi / 2;
        } else if Q1 < 0
        then { # I1 == 0, Q1 is negative
            Phase1 = 3 * Double.Pi / 2;
        } else { # I1 and Q1 == 0
            Phase1 = 0;
        }

# Convert phase to degrees
        Phase = Phase1 * 180 / Double.Pi;

        if Phase[1] < 90 and Phase > 270
        then {
# This occurs when there is a big jump from 360-0
            DeltaPhase1 = 360 + Phase[1] - Phase;
        } else {
            DeltaPhase1 = Phase[1] - Phase;
        }

# Limit our delta phases between 7 and 60
        if DeltaPhase1 < 7
        then {
            DeltaPhase = 7;
        } else if DeltaPhase1 > 60
        then {
            DeltaPhase = 60;
        } else {
            DeltaPhase = DeltaPhase1;
        }

# Determine Instantaneous period:
        InstPeriod1 =
-1 * (fold i = 0 to 40 with v=0 do
if v < 0 then
v
else if v > 360 then
-i
else
v + GetValue(DeltaPhase, i, 41)
);

        if InstPeriod1 <= 0
        then {
            InstPeriod = InstPeriod[1];
        } else {
            InstPeriod = InstPeriod1;
        }

        Period = 0.25 * InstPeriod + 0.75 * Period[1];
    }
    plot DC = Period;
}

script Ehler_MAMA {
    input price = hl2;
    input FastLimit = 0.5;
    input SlowLimit = 0.05;


    rec Period;
    rec Period_raw;
    rec Period_cap;
    rec Period_lim;

    rec Smooth;
    rec Detrender;
    rec I1;
    rec Q1;
    rec jI;
    rec jQ;
    rec I2;
    rec Q2;
    rec I2_raw;
    rec Q2_raw;

    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase_raw;
    rec alpha;
    rec alpha_raw;

    rec Re;
    rec Im;
    rec Re_raw;
    rec Im_raw;

    rec SmoothPeriod;
    rec vmama;
    rec vfama;

    def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

    if BarNumber() <= 5
    then {
        Smooth = 0;
        Detrender = 0;

        Period = 0;
        Period_raw = 0;
        Period_cap = 0;
        Period_lim = 0;
        I1 = 0;
        Q1 = 0;
        I2 = 0;
        Q2 = 0;
        jI = 0;
        jQ = 0;
        I2_raw = 0;
        Q2_raw = 0;
        Re = 0;
        Im = 0;
        Re_raw = 0;
        Im_raw = 0;
        SmoothPeriod = 0;
        Phase = 0;
        DeltaPhase = 0;
        DeltaPhase_raw = 0;
        alpha = 0;
        alpha_raw = 0;
        vmama = 0;
        vfama = 0;
    } else {
# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

        Q1 = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1 = Detrender[3];

        jI = ( 0.0962 * I1
+ 0.5769 * I1[2]
- 0.5769 * I1[4]
- 0.0962 * I1[6] ) * CorrectionFactor;

        jQ = ( 0.0962 * Q1
+ 0.5769 * Q1[2]
- 0.5769 * Q1[4]
- 0.0962 * Q1[6] ) * CorrectionFactor;

# This is the complex conjugate
        I2_raw = I1 - jQ;
        Q2_raw = Q1 + jI;

        I2 = 0.2 * I2_raw + 0.8 * I2_raw[1];
        Q2 = 0.2 * Q2_raw + 0.8 * Q2_raw[1];

        Re_raw = I2 * I2[1] + Q2 * Q2[1];
        Im_raw = I2 * Q2[1] - Q2 * I2[1];

        Re = 0.2 * Re_raw + 0.8 * Re_raw[1];
        Im = 0.2 * Im_raw + 0.8 * Im_raw[1];

# Compute the phase
        if Re != 0 and Im != 0
        then {
            Period_raw = 2 * Double.Pi / ATan(Im / Re);
        } else {
            Period_raw = 0;
        }

        if Period_raw > 1.5 * Period_raw[1]
        then {
            Period_cap = 1.5 * Period_raw[1];
        } else if Period_raw < 0.67 * Period_raw[1] {
            Period_cap = 0.67 * Period_raw[1];
        } else {
            Period_cap = Period_raw;
        }

        if Period_cap < 6
        then {
            Period_lim = 6;
        } else if Period_cap > 50
        then {
            Period_lim = 50;
        } else {
            Period_lim = Period_cap;
        }

        Period = 0.2 * Period_lim + 0.8 * Period_lim[1];
        SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[1];

        if I1 != 0
        then {
            Phase = ATan(Q1 / I1);
        } else if Q1 > 0
        then { # Quadrant 1:
            Phase = Double.Pi / 2;
        } else if Q1 < 0
        then { # Quadrant 4:
            Phase = -Double.Pi / 2;
        } else { # Both numerator and denominator are 0.
            Phase = 0;
        }

        DeltaPhase_raw = Phase[1] - Phase;
        if DeltaPhase_raw < 1
        then {
            DeltaPhase = 1;
        } else {
            DeltaPhase = DeltaPhase_raw;
        }

        alpha_raw = FastLimit / DeltaPhase;
        if alpha_raw < SlowLimit
        then {
            alpha = SlowLimit;
        } else {
            alpha = alpha_raw;
        }
        vmama = alpha * price + (1 - alpha) * vmama[1];
        vfama = 0.5 * alpha * vmama + (1 - 0.5 * alpha) * vfama[1];
    }

    plot MAMA = vmama;
    plot FAMA = vfama;
}


input price2 = hl2;
input FastLimit = 0.5;
input SlowLimit = 0.05;

def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;

def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),

if MAMA > FAMA then Color.GREEN else Color.RED);

##################################
plot C3_MF_Line = (MAMA + FAMA) / 2;
C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
C3_MF_Line.SetLineWeight(3);

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;
C3_MF_Line.AssignValueColor(if paintCandles and ((direction == 1) and (price > C3_MF_Line)) then Color.GREEN else if paintCandles and ((direction == -1) and (price < C3_MF_Line)) then Color.RED else Color.GRAY);


plot buy = AvgExp crosses above AvgExp2;#direction crosses above 0;
buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.SetDefaultColor(Color.WHITE);

plot sell = AvgExp crosses below AvgExp2;#direction crosses below 0;
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN );
sell.SetDefaultColor(Color.WHITE);

AssignPriceColor(if paintCandles then if direction == 1 then Color.GREEN else if direction == -1 then Color.RED else Color.GRAY else Color.CURRENT);

AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.YELLOW else if ((Spark == 2) and (AvgExp > AvgExp2)) then Color.GREEN else if (Spark == 0) then Color.RED else Color.GRAY);

AddLabel(yes, if ((UP6 == 1) and (Consensus_Bias > 0)) then " SCALP_LONG " else if ((DOWN6) and (Consensus_Bias < 0)) then " SCALP_SHORT " else " CHOP ", if ((Consensus_Bias > 0) and (UP6 == 1)) then Color.GREEN else if ((Consensus_Bias < 0) and (DOWN6 == 1)) then Color.RED else Color.GRAY);

AddLabel(yes, if (ConsensusR > 0) then " LONG BIAS = %" + Round((ConsensusR / 14) * 100, 1) + " " else if (ConsensusR < 0) then  " SHORT BIAS = %" + Round(((ConsensusR * -1) / 14) * 100, 1) + " " else " CHOP =" + Round((ConsensusR / 14) * 100, 1) + " ", if (ConsensusR > 0) then Color.GREEN else if (ConsensusR < 0) then Color.RED else Color.GRAY);

Alert(direction crosses above 0, "long", Alert.BAR, Sound.DING);
Alert(direction crosses below 0, "short", Alert.BAR, Sound.DING);

#Strategy
def Long_Entry =  (ConsensusR crosses above Consensus_Bias);
def Long_Exit =  (ConsensusR crosses below Consensus_Bias);
AddOrder(OrderType.BUY_AUTO, condition = Long_Entry, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME, name = "LE");
AddOrder(OrderType.SELL_AUTO, condition = Long_Exit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME, name = "SE");

Alert(Long_Entry, "long", Alert.BAR, Sound.DING);
Alert(Long_Exit, "short", Alert.BAR, Sound.DING);

And finally, here's the code for 1 hour charts. Hope you find these useful!
Code:
#Scalper Upper v2 Strategy Created 03/16/2022 by Christopher84

declare upper;

input price = close;
input length = 10;
input length2 = 35;
input agperiod1 = { "1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", default "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod2 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", default "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod3 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", default "4 hours", "Day", "Week", "Month"};
input agperiod4 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week", "Month"};
input agperiod5 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", default "Week", "Month"};
input agperiod6 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", default "Month"};
def displace = 0;
input paintCandles = yes;
input show_ema_cloud = yes;

#Current Period
plot AvgExp = ExpAverage(price[-displace], length);
AvgExp.SetStyle(Curve.SHORT_DASH);
def UPC1 = AvgExp > AvgExp[1];
def DNC1 = AvgExp < AvgExp[1];

plot AvgExp2 = ExpAverage(price[-displace], length2);
AvgExp2.SetStyle(Curve.SHORT_DASH);
def UPC2 = AvgExp2 > AvgExp2[1];
def DNC2 = AvgExp2 < AvgExp2[1];

def Below = AvgExp < AvgExp2;
def Spark = UPC1 + UPC2 + Below;

def UPEMA = AvgExp[1] < AvgExp;
def DOWNEMA = AvgExp[1] > AvgExp;
AvgExp.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp2[1] < AvgExp2;
def DOWNEMA2 = AvgExp2[1] > AvgExp2;
AvgExp2.AssignValueColor(if UPEMA2 then Color.LIGHT_GREEN else if DOWNEMA2 then Color.RED else Color.YELLOW);

AddCloud(if show_ema_cloud and (AvgExp2 > AvgExp) then AvgExp2 else Double.NaN, AvgExp, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExp > AvgExp2) then AvgExp else Double.NaN, AvgExp2, Color.LIGHT_GREEN, Color.CURRENT);

#Agperiod1
def avg = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];

def avg2 = ExpAverage(close(period = agperiod1), length2);
def height2 = avg2 - avg2[length2];

def UP = avg > avg2;
def DOWN = avg < avg2;

def R1UP = avg > avg[1];
def R1DN = avg < avg[1];
def R2UP = avg2 > avg2[1];
def R2DN = avg2 < avg2[1];

#Agperiod2
def avg3 = ExpAverage(close(period = agperiod2), length);
def height3 = avg3 - avg3[length];

def avg4 = ExpAverage(close(period = agperiod2), length2);
def height4 = avg4 - avg4[length2];

def UP2 = avg3 > avg4;
def DOWN2 = avg3 < avg4;

def R3UP = avg3 > avg3[1];
def R3DN = avg3 < avg3[1];
def R4UP = avg4 > avg4[1];
def R4DN = avg4 < avg4[1];

#Agperiod3
def avg5 = ExpAverage(close(period = agperiod3), length);
def height5 = avg5 - avg5[length];

def avg6 = ExpAverage(close(period = agperiod3), length2);
def height6 = avg6 - avg6[length2];

def UP3 = avg5 > avg6;
def DOWN3 = avg5 < avg6;

def R5UP = avg5 > avg5[1];
def R5DN = avg5 < avg5[1];
def R6UP = avg6 > avg6[1];
def R6DN = avg6 < avg6[1];

#Agperiod4
def avg7 = ExpAverage(close(period = agperiod4), length);
def height7 = avg7 - avg7[length];

def avg8 = ExpAverage(close(period = agperiod4), length2);
def height8 = avg8 - avg8[length2];

def UP4 = avg7 > avg8;
def DOWN4 = avg7 < avg8;

def R7UP = avg7 > avg7[1];
def R7DN = avg7 < avg7[1];
def R8UP = avg8 > avg8[1];
def R8DN = avg8 < avg8[1];

#Agperiod5
def avg9 = ExpAverage(close(period = agperiod5), length);
def height9 = avg9 - avg9[length];

def avg10 = ExpAverage(close(period = agperiod5), length2);
def height10 = avg10 - avg10[length2];

def UP5 = avg9 > avg10;
def DOWN5 = avg9 < avg10;

def R9UP = avg9 > avg9[1];
def R9DN = avg9 < avg9[1];
def R10UP = avg10 > avg10[1];
def R10DN = avg10 < avg10[1];

#Agperiod6
def avg11 = ExpAverage(close(period = agperiod6), length);
def height11 = avg11 - avg11[length];

def avg12 = ExpAverage(close(period = agperiod6), length2);
def height12 = avg12 - avg12[length2];

def UP6 = avg11 > avg12;
def DOWN6 = avg11 < avg12;

def R11UP = avg11 > avg11[1];
def R11DN = avg11 < avg11[1];
def R12UP = avg12 > avg12[1];
def R12DN = avg12 < avg12[1];

def Long_Only = UP + UP2 + UP3 + UP4 + UP5 + UP6;
def Short_Only = DOWN + DOWN2 + DOWN3 + DOWN4 + DOWN5 + DOWN6;
def Consensus_Bias = Long_Only - Short_Only;

def RUP = UPC1 + UPC2 + R1UP + R2UP + R3UP + R4UP + R5UP + R6UP + R7UP + R8UP + R9UP + R10UP + R11UP + R12UP;
def RDN = DNC1 + DNC2 + R1DN + R2DN + R3DN + R4DN + R5DN + R6DN + R7DN + R8DN + R9DN + R10DN + R11DN + R12DN;
def ConsensusR = RUP - RDN;

script WMA_Smooth {
    input price = hl2;
    plot smooth = (4 * price
+ 3 * price[1]
+ 2 * price[2]
+ price[3]) / 10;
}

script Phase_Accumulation {
# This is Ehler's Phase Accumulation code. It has a full cycle delay.
# However, it computes the correction factor to a very high degree.
#
    input price = hl2;

    rec Smooth;
    rec Detrender;
    rec Period;
    rec Q1;
    rec I1;
    rec I1p;
    rec Q1p;
    rec Phase1;
    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase1;
    rec InstPeriod1;
    rec InstPeriod;
    def CorrectionFactor;

    if BarNumber() <= 5
    then {
        Period = 0;
        Smooth = 0;
        Detrender = 0;
        CorrectionFactor = 0;
        Q1 = 0;
        I1 = 0;
        Q1p = 0;
        I1p = 0;
        Phase = 0;
        Phase1 = 0;
        DeltaPhase1 = 0;
        DeltaPhase = 0;
        InstPeriod = 0;
        InstPeriod1 = 0;
    } else {
        CorrectionFactor = 0.075 * Period[1] + 0.54;

# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

# Compute Quadrature and Phase of Detrended signal:
        Q1p = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1p = Detrender[3];

# Smooth out Quadrature and Phase:
        I1 = 0.15 * I1p + 0.85 * I1p[1];
        Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

# Determine Phase
        if I1 != 0
        then {
# Normally, ATAN gives results from -pi/2 to pi/2.
# We need to map this to circular coordinates 0 to 2pi

            if Q1 >= 0 and I1 > 0
            then { # Quarant 1
                Phase1 = ATan(AbsValue(Q1 / I1));
            } else if Q1 >= 0 and I1 < 0
            then { # Quadrant 2
                Phase1 = Double.Pi - ATan(AbsValue(Q1 / I1));
            } else if Q1 < 0 and I1 < 0
            then { # Quadrant 3
                Phase1 = Double.Pi + ATan(AbsValue(Q1 / I1));
            } else { # Quadrant 4
                Phase1 = 2 * Double.Pi - ATan(AbsValue(Q1 / I1));
            }
        } else if Q1 > 0
        then { # I1 == 0, Q1 is positive
            Phase1 = Double.Pi / 2;
        } else if Q1 < 0
        then { # I1 == 0, Q1 is negative
            Phase1 = 3 * Double.Pi / 2;
        } else { # I1 and Q1 == 0
            Phase1 = 0;
        }

# Convert phase to degrees
        Phase = Phase1 * 180 / Double.Pi;

        if Phase[1] < 90 and Phase > 270
        then {
# This occurs when there is a big jump from 360-0
            DeltaPhase1 = 360 + Phase[1] - Phase;
        } else {
            DeltaPhase1 = Phase[1] - Phase;
        }

# Limit our delta phases between 7 and 60
        if DeltaPhase1 < 7
        then {
            DeltaPhase = 7;
        } else if DeltaPhase1 > 60
        then {
            DeltaPhase = 60;
        } else {
            DeltaPhase = DeltaPhase1;
        }

# Determine Instantaneous period:
        InstPeriod1 =
-1 * (fold i = 0 to 40 with v=0 do
if v < 0 then
v
else if v > 360 then
-i
else
v + GetValue(DeltaPhase, i, 41)
);

        if InstPeriod1 <= 0
        then {
            InstPeriod = InstPeriod[1];
        } else {
            InstPeriod = InstPeriod1;
        }

        Period = 0.25 * InstPeriod + 0.75 * Period[1];
    }
    plot DC = Period;
}

script Ehler_MAMA {
    input price = hl2;
    input FastLimit = 0.5;
    input SlowLimit = 0.05;


    rec Period;
    rec Period_raw;
    rec Period_cap;
    rec Period_lim;

    rec Smooth;
    rec Detrender;
    rec I1;
    rec Q1;
    rec jI;
    rec jQ;
    rec I2;
    rec Q2;
    rec I2_raw;
    rec Q2_raw;

    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase_raw;
    rec alpha;
    rec alpha_raw;

    rec Re;
    rec Im;
    rec Re_raw;
    rec Im_raw;

    rec SmoothPeriod;
    rec vmama;
    rec vfama;

    def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

    if BarNumber() <= 5
    then {
        Smooth = 0;
        Detrender = 0;

        Period = 0;
        Period_raw = 0;
        Period_cap = 0;
        Period_lim = 0;
        I1 = 0;
        Q1 = 0;
        I2 = 0;
        Q2 = 0;
        jI = 0;
        jQ = 0;
        I2_raw = 0;
        Q2_raw = 0;
        Re = 0;
        Im = 0;
        Re_raw = 0;
        Im_raw = 0;
        SmoothPeriod = 0;
        Phase = 0;
        DeltaPhase = 0;
        DeltaPhase_raw = 0;
        alpha = 0;
        alpha_raw = 0;
        vmama = 0;
        vfama = 0;
    } else {
# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

        Q1 = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1 = Detrender[3];

        jI = ( 0.0962 * I1
+ 0.5769 * I1[2]
- 0.5769 * I1[4]
- 0.0962 * I1[6] ) * CorrectionFactor;

        jQ = ( 0.0962 * Q1
+ 0.5769 * Q1[2]
- 0.5769 * Q1[4]
- 0.0962 * Q1[6] ) * CorrectionFactor;

# This is the complex conjugate
        I2_raw = I1 - jQ;
        Q2_raw = Q1 + jI;

        I2 = 0.2 * I2_raw + 0.8 * I2_raw[1];
        Q2 = 0.2 * Q2_raw + 0.8 * Q2_raw[1];

        Re_raw = I2 * I2[1] + Q2 * Q2[1];
        Im_raw = I2 * Q2[1] - Q2 * I2[1];

        Re = 0.2 * Re_raw + 0.8 * Re_raw[1];
        Im = 0.2 * Im_raw + 0.8 * Im_raw[1];

# Compute the phase
        if Re != 0 and Im != 0
        then {
            Period_raw = 2 * Double.Pi / ATan(Im / Re);
        } else {
            Period_raw = 0;
        }

        if Period_raw > 1.5 * Period_raw[1]
        then {
            Period_cap = 1.5 * Period_raw[1];
        } else if Period_raw < 0.67 * Period_raw[1] {
            Period_cap = 0.67 * Period_raw[1];
        } else {
            Period_cap = Period_raw;
        }

        if Period_cap < 6
        then {
            Period_lim = 6;
        } else if Period_cap > 50
        then {
            Period_lim = 50;
        } else {
            Period_lim = Period_cap;
        }

        Period = 0.2 * Period_lim + 0.8 * Period_lim[1];
        SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[1];

        if I1 != 0
        then {
            Phase = ATan(Q1 / I1);
        } else if Q1 > 0
        then { # Quadrant 1:
            Phase = Double.Pi / 2;
        } else if Q1 < 0
        then { # Quadrant 4:
            Phase = -Double.Pi / 2;
        } else { # Both numerator and denominator are 0.
            Phase = 0;
        }

        DeltaPhase_raw = Phase[1] - Phase;
        if DeltaPhase_raw < 1
        then {
            DeltaPhase = 1;
        } else {
            DeltaPhase = DeltaPhase_raw;
        }

        alpha_raw = FastLimit / DeltaPhase;
        if alpha_raw < SlowLimit
        then {
            alpha = SlowLimit;
        } else {
            alpha = alpha_raw;
        }
        vmama = alpha * price + (1 - alpha) * vmama[1];
        vfama = 0.5 * alpha * vmama + (1 - 0.5 * alpha) * vfama[1];
    }

    plot MAMA = vmama;
    plot FAMA = vfama;
}


input price2 = hl2;
input FastLimit = 0.5;
input SlowLimit = 0.05;

def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;

def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),

if MAMA > FAMA then Color.GREEN else Color.RED);

##################################
plot C3_MF_Line = (MAMA + FAMA) / 2;
C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
C3_MF_Line.SetLineWeight(3);

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;
C3_MF_Line.AssignValueColor(if paintCandles and ((direction == 1) and (price > C3_MF_Line)) then Color.GREEN else if paintCandles and ((direction == -1) and (price < C3_MF_Line)) then Color.RED else Color.GRAY);


plot buy = AvgExp crosses above AvgExp2;#direction crosses above 0;
buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.SetDefaultColor(Color.WHITE);

plot sell = AvgExp crosses below AvgExp2;#direction crosses below 0;
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN );
sell.SetDefaultColor(Color.WHITE);

AssignPriceColor(if paintCandles then if direction == 1 then Color.GREEN else if direction == -1 then Color.RED else Color.GRAY else Color.CURRENT);

AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.YELLOW else if ((Spark == 2) and (AvgExp > AvgExp2)) then Color.GREEN else if (Spark == 0) then Color.RED else Color.GRAY);

AddLabel(yes, if ((UP6 == 1) and (Consensus_Bias > 0)) then " SCALP_LONG " else if ((DOWN6) and (Consensus_Bias < 0)) then " SCALP_SHORT " else " CHOP ", if ((Consensus_Bias > 0) and (UP6 == 1)) then Color.GREEN else if ((Consensus_Bias < 0) and (DOWN6 == 1)) then Color.RED else Color.GRAY);

AddLabel(yes, if (ConsensusR > 0) then " LONG BIAS = %" + Round((ConsensusR / 14) * 100, 1) + " " else if (ConsensusR < 0) then  " SHORT BIAS = %" + Round(((ConsensusR * -1) / 14) * 100, 1) + " " else " CHOP =" + Round((ConsensusR / 14) * 100, 1) + " ", if (ConsensusR > 0) then Color.GREEN else if (ConsensusR < 0) then Color.RED else Color.GRAY);

Alert(direction crosses above 0, "long", Alert.BAR, Sound.DING);
Alert(direction crosses below 0, "short", Alert.BAR, Sound.DING);

#Strategy
def Long_Entry =  (ConsensusR crosses above Consensus_Bias);
def Long_Exit =  (ConsensusR crosses below Consensus_Bias);
AddOrder(OrderType.BUY_AUTO, condition = Long_Entry, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME, name = "LE");
AddOrder(OrderType.SELL_AUTO, condition = Long_Exit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME, name = "SE");

Alert(Long_Entry, "long", Alert.BAR, Sound.DING);
Alert(Long_Exit, "short", Alert.BAR, Sound.DING);
Hi @Christopher84. Been studying and watching the performance of these 3 separate time frames. They all appear to be doing great!

Would it be possible to have a 5m version like the 1m, 3m and 60m version or should I adjust the settings on the 3m version? Not sure what is best, but I think a pure 5m would be a great sweet spot for the tool box!!!

Thanks for all you do!!!
 
Hi @Christopher84. Been studying and watching the performance of these 3 separate time frames. They all appear to be doing great!

Would it be possible to have a 5m version like the 1m, 3m and 60m version or should I adjust the settings on the 3m version? Not sure what is best, but I think a pure 5m would be a great sweet spot for the tool box!!!

Thanks for all you do!!!
Hi @rfb!
You should be able to adjust the settings on the 3 min version. If you have any trouble, please let me know.
 
  • Like
Reactions: rfb
If you like, I'll share a few things that helped me along the way.

1- Trading on timeframes of 5 min or less can be very difficult. Usually I load a grid with 5, 10, and 15 min charts. If those charts are showing a mix of long and short, I stay out. If they are all trending in the same direction, then I look at 1 or 2 min charts for an entry in the direction that the higher timeframes are showing. Typically, I don't trade in a direction opposite of the daily time frame trend, but your risk tolerance may be different. If you load a 10 min chart, you may find that the signals look better to your eye than a 2 min chart. There is no shame in trading on a higher timeframe!

2- Another indicator I like is the VWAP. I load the daily value onto the chart. If price is below VWAP, I look for shorts. If above VWAP, I look for longs.

3- Another thing you can try is loading the 50 and 200 EMA. If stacked negative, look only for short signals. If stacked positive, look only for longs.

4- Know the times of day your strategy works best. For example, I trade some instruments on the Euro session only. Others I trade during specific hours of the US session.

5- Keep a trading log/journal of what is working and what isn't. It can help fine tune your strategy.

Don't know if that helped, but wish you the best. Happy trading!
Thank You @Trader Raider
 
Did you load the code on the strategy tab? Yes, he does update the version number and replaces older code with newest revisions.

Welcome, best wishes, and happy trading!
@Trader Raider Thanks for the reply. Two quick questions, does this forum have direct messages I couldn't find it to ask you this while trying not to post here and clog the thread. 2. Does saving the code in the Strategy area differ from the Studies area? If so how are the two different? Thanks. Also I just thought Chris might've updated his C3_Max_v2 script since 12/14/21 as that's the date on it but if that's the latest then cool.
 
. 2. Does saving the code in the Strategy area differ from the Studies area? If so how are the two different? Thanks.

@macattacktos, sorry, don't have time to post an image right now. Hope these instructions make sense.

1- Click the beaker icon (or studies > edit studies from the drop down menu) on the chart.
2- In the little window that pops up, there are tabs at the top left for studies, strategies, and sets. Click on the strategies tab.
3- Paste the code, name the strategy, and save.

Hope that helps.
 
@macattacktos, sorry, don't have time to post an image right now. Hope these instructions make sense.

1- Click the beaker icon (or studies > edit studies from the drop down menu) on the chart.
2- In the little window that pops up, there are tabs at the top left for studies, strategies, and sets. Click on the strategies tab.
3- Paste the code, name the strategy, and save.

Hope that helps.
@Trader Raider Thanks, I know how to insert code into studies and strategies, I was wondering what is the difference between saving and using the C3_Max or other code in the Studies tab vs the Strategies tab? I've read that somehow when a signal comes you're able to preview a possible order or review the signal, and do P/L charts, are there more differences? I'm running the code under the studies tab, the only issue I've had so far is the chart data is squished until I pick the correct time frame or do a select zoom in so the data will expand.
 
Last edited:
I was wondering what is the difference between saving and using the C3_Max or other code in the Studies tab vs the Strategies tab? I've read that somehow when a signal comes you're able to preview a possible order or review the signal, and do P/L charts, are there more differences?

Apologies for misunderstanding and for being unable to help you further. Perhaps someone else will chime in. TOS isn't my primary trading platform so I am not as familiar with it as others here.
 
I doubt you need to worry about indicators from @Christopher84 :) I shorted a number of instruments this week using the Scalper and C3 Max and feel confident that they are great tools for markets that are not rallying. And for sideways markets, Christopher included squeeze dots, support/resistance zones, clouds, labels (even a CHOP label!) ... everything but the kitchen sink ... to alert the trader to situations when it may be more profitable to stay flat than jump in just because an arrow painted. Personally, I believe the purpose of an indicator is to confirm the trader's reading of price action NOT to substitute for it.

Anyway, welcome aboard. Best Wishes and Happy Trading! Not trying to be critical of your post. Just chiming in to say Christopher has been around the block and designs his indicators such that they respond well to a variety of market conditions. Plot a few and watch them for awhile and you'll probably see what I mean :)
Yes, absolutely 100% agree with you. I should have reworded - Christopher's work is doing amazing in market conditions. Easily one of the most useful tools on all of T/S
 
A couple charts today using my scalper settings...

As you can see here, not all but many times alerts start coming before the breakout.

Every so often they come after it's already run a bit but most of the time, I see good entries.

I also use the Hr B4

CC Max / Scalper one of the Best Indicators I've come across

glrTghI.png





8KA4ZZ0.png
 
Last edited:
A couple charts today using my scalper settings...

As you can see here, not all but many times alerts start coming before the breakout.

Every so often they come after it's already run a bit but most of the time, I see good entries.

I also use the Hr B4

CC Max / Scalper one of the Best Indicators I've come across

glrTghI.png





8KA4ZZ0.png

did you update your labels? also what settings did you disable. i'm trying to make it as clean like your screenshot.
 
did you update your labels? also what settings did you disable. i'm trying to make it as clean like your screenshot.
Here's the code I'm using..

5 agg periods instead of 6

You're right, does look like the % label is gone....:unsure:

There is no "X_ing" signal

You'll get continuous arrows as long as the conditions are true.

If the chart Bias is "Scalp Long", you'll never see a short signal and vice versa

The conditions may fade but the fuel has already been added, if you get my drift

I use the hour B4 to help stay out of bad setups

and yes, I changed the label colors...at least the "Green" ones

Code:
#Scalper Upper v2 Created 02/01/2022 by Christopher84
#Sound Alerts added by Barbaros
#Some changes by RedtoGreen

declare upper;

input price = close;
input length = 10;
input length2 = 35;
input agperiod1 = { "1 min", default "2 min", "3 min", "5 min", "10 min", "15 min", "20 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod2 = {"1 min", "2 min", default "3 min", "5 min", "10 min", "15 min", "20 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod3 = {"1 min", "2 min", "3 min", "5 min", default "10 min", "15 min", "20 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod4 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", default "20 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod5 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "20 min", "1 hour", "2 hours", default "4 hours", "Day", "Week", "Month"};

def displace = 0;
input paintCandles = yes;
input show_ema_cloud = yes;

#Current Period
plot AvgExp = ExpAverage(price[-displace], length);
AvgExp.SetStyle(Curve.SHORT_DASH);
def UPC1 = AvgExp > AvgExp[1];
def DNC1 = AvgExp < AvgExp[1];

plot AvgExp2 = ExpAverage(price[-displace], length2);
AvgExp2.SetStyle(Curve.SHORT_DASH);
def UPC2 = AvgExp2 > AvgExp2[1];
def DNC2 = AvgExp2 < AvgExp2[1];

def Below = AvgExp < AvgExp2;
def Spark = UPC1 + UPC2 + Below;

def UPEMA = AvgExp[1] < AvgExp;
def DOWNEMA = AvgExp[1] > AvgExp;
AvgExp.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp2[1] < AvgExp2;
def DOWNEMA2 = AvgExp2[1] > AvgExp2;
AvgExp2.AssignValueColor(if UPEMA2 then Color.LIGHT_GREEN else if DOWNEMA2 then Color.RED else Color.YELLOW);

AddCloud(if show_ema_cloud and (AvgExp2 > AvgExp) then AvgExp2 else Double.NaN, AvgExp, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExp > AvgExp2) then AvgExp else Double.NaN, AvgExp2, Color.LIGHT_GREEN, Color.CURRENT);

#Agperiod1
def avg = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];

def avg2 = ExpAverage(close(period = agperiod1), length2);
def height2 = avg2 - avg2[length2];

def UP = avg > avg2;
def DOWN = avg < avg2;

def R1UP = avg > avg[1];
def R1DN = avg < avg[1];
def R2UP = avg2 > avg2[1];
def R2DN = avg2 < avg2[1];

#Agperiod2
def avg3 = ExpAverage(close(period = agperiod2), length);
def height3 = avg3 - avg3[length];

def avg4 = ExpAverage(close(period = agperiod2), length2);
def height4 = avg4 - avg4[length2];

def UP2 = avg3 > avg4;
def DOWN2 = avg3 < avg4;

def R3UP = avg3 > avg3[1];
def R3DN = avg3 < avg3[1];
def R4UP = avg4 > avg4[1];
def R4DN = avg4 < avg4[1];

#Agperiod3
def avg5 = ExpAverage(close(period = agperiod3), length);
def height5 = avg5 - avg5[length];

def avg6 = ExpAverage(close(period = agperiod3), length2);
def height6 = avg6 - avg6[length2];

def UP3 = avg5 > avg6;
def DOWN3 = avg5 < avg6;

def R5UP = avg5 > avg5[1];
def R5DN = avg5 < avg5[1];
def R6UP = avg6 > avg6[1];
def R6DN = avg6 < avg6[1];

#Agperiod4
def avg7 = ExpAverage(close(period = agperiod4), length);
def height7 = avg7 - avg7[length];

def avg8 = ExpAverage(close(period = agperiod4), length2);
def height8 = avg8 - avg8[length2];

def UP4 = avg7 > avg8;
def DOWN4 = avg7 < avg8;

def R7UP = avg7 > avg7[1];
def R7DN = avg7 < avg7[1];
def R8UP = avg8 > avg8[1];
def R8DN = avg8 < avg8[1];

#Agperiod5
def avg9 = ExpAverage(close(period = agperiod5), length);
def height9 = avg9 - avg9[length];

def avg10 = ExpAverage(close(period = agperiod5), length2);
def height10 = avg10 - avg10[length2];

def UP5 = avg9 > avg10;
def DOWN5 = avg9 < avg10;

def R9UP = avg9 > avg9[1];
def R9DN = avg9 < avg9[1];
def R10UP = avg10 > avg10[1];
def R10DN = avg10 < avg10[1];

def Long_Only = UP + UP2 + UP3 + UP4 + UP5;
def Short_Only = DOWN + DOWN2 + DOWN3 + DOWN4 + DOWN5;
def Consensus_Bias = Long_Only - Short_Only;

def RUP = UPC1 + UPC2 + R1UP + R2UP + R3UP + R4UP + R5UP + R6UP + R7UP + R8UP + R9UP + R10UP;
def RDN = DNC1 + DNC2 + R1DN + R2DN + R3DN + R4DN + R5DN + R6DN + R7DN + R8DN + R9DN + R10DN;
def ConsensusR = RUP - RDN;

script WMA_Smooth {
    input price = hl2;
    plot smooth = (4 * price
+ 3 * price[1]
+ 2 * price[2]
+ price[3]) / 10;
}

script Phase_Accumulation {
# This is Ehler's Phase Accumulation code. It has a full cycle delay.
# However, it computes the correction factor to a very high degree.
#
    input price = hl2;

    rec Smooth;
    rec Detrender;
    rec Period;
    rec Q1;
    rec I1;
    rec I1p;
    rec Q1p;
    rec Phase1;
    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase1;
    rec InstPeriod1;
    rec InstPeriod;
    def CorrectionFactor;

    if BarNumber() <= 5
    then {
        Period = 0;
        Smooth = 0;
        Detrender = 0;
        CorrectionFactor = 0;
        Q1 = 0;
        I1 = 0;
        Q1p = 0;
        I1p = 0;
        Phase = 0;
        Phase1 = 0;
        DeltaPhase1 = 0;
        DeltaPhase = 0;
        InstPeriod = 0;
        InstPeriod1 = 0;
    } else {
        CorrectionFactor = 0.075 * Period[1] + 0.54;

# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

# Compute Quadrature and Phase of Detrended signal:
        Q1p = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1p = Detrender[3];

# Smooth out Quadrature and Phase:
        I1 = 0.15 * I1p + 0.85 * I1p[1];
        Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

# Determine Phase
        if I1 != 0
        then {
# Normally, ATAN gives results from -pi/2 to pi/2.
# We need to map this to circular coordinates 0 to 2pi

            if Q1 >= 0 and I1 > 0
            then { # Quarant 1
                Phase1 = ATan(AbsValue(Q1 / I1));
            } else if Q1 >= 0 and I1 < 0
            then { # Quadrant 2
                Phase1 = Double.Pi - ATan(AbsValue(Q1 / I1));
            } else if Q1 < 0 and I1 < 0
            then { # Quadrant 3
                Phase1 = Double.Pi + ATan(AbsValue(Q1 / I1));
            } else { # Quadrant 4
                Phase1 = 2 * Double.Pi - ATan(AbsValue(Q1 / I1));
            }
        } else if Q1 > 0
        then { # I1 == 0, Q1 is positive
            Phase1 = Double.Pi / 2;
        } else if Q1 < 0
        then { # I1 == 0, Q1 is negative
            Phase1 = 3 * Double.Pi / 2;
        } else { # I1 and Q1 == 0
            Phase1 = 0;
        }

# Convert phase to degrees
        Phase = Phase1 * 180 / Double.Pi;

        if Phase[1] < 90 and Phase > 270
        then {
# This occurs when there is a big jump from 360-0
            DeltaPhase1 = 360 + Phase[1] - Phase;
        } else {
            DeltaPhase1 = Phase[1] - Phase;
        }

# Limit our delta phases between 7 and 60
        if DeltaPhase1 < 7
        then {
            DeltaPhase = 7;
        } else if DeltaPhase1 > 60
        then {
            DeltaPhase = 60;
        } else {
            DeltaPhase = DeltaPhase1;
        }

# Determine Instantaneous period:
        InstPeriod1 =
-1 * (fold i = 0 to 40 with v=0 do
if v < 0 then
v
else if v > 360 then
-i
else
v + GetValue(DeltaPhase, i, 41)
);

        if InstPeriod1 <= 0
        then {
            InstPeriod = InstPeriod[1];
        } else {
            InstPeriod = InstPeriod1;
        }

        Period = 0.25 * InstPeriod + 0.75 * Period[1];
    }
    plot DC = Period;
}

script Ehler_MAMA {
    input price = hl2;
    input FastLimit = 0.5;
    input SlowLimit = 0.05;


    rec Period;
    rec Period_raw;
    rec Period_cap;
    rec Period_lim;

    rec Smooth;
    rec Detrender;
    rec I1;
    rec Q1;
    rec jI;
    rec jQ;
    rec I2;
    rec Q2;
    rec I2_raw;
    rec Q2_raw;

    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase_raw;
    rec alpha;
    rec alpha_raw;

    rec Re;
    rec Im;
    rec Re_raw;
    rec Im_raw;

    rec SmoothPeriod;
    rec vmama;
    rec vfama;

    def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

    if BarNumber() <= 5
    then {
        Smooth = 0;
        Detrender = 0;

        Period = 0;
        Period_raw = 0;
        Period_cap = 0;
        Period_lim = 0;
        I1 = 0;
        Q1 = 0;
        I2 = 0;
        Q2 = 0;
        jI = 0;
        jQ = 0;
        I2_raw = 0;
        Q2_raw = 0;
        Re = 0;
        Im = 0;
        Re_raw = 0;
        Im_raw = 0;
        SmoothPeriod = 0;
        Phase = 0;
        DeltaPhase = 0;
        DeltaPhase_raw = 0;
        alpha = 0;
        alpha_raw = 0;
        vmama = 0;
        vfama = 0;
    } else {
# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

        Q1 = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1 = Detrender[3];

        jI = ( 0.0962 * I1
+ 0.5769 * I1[2]
- 0.5769 * I1[4]
- 0.0962 * I1[6] ) * CorrectionFactor;

        jQ = ( 0.0962 * Q1
+ 0.5769 * Q1[2]
- 0.5769 * Q1[4]
- 0.0962 * Q1[6] ) * CorrectionFactor;

# This is the complex conjugate
        I2_raw = I1 - jQ;
        Q2_raw = Q1 + jI;

        I2 = 0.2 * I2_raw + 0.8 * I2_raw[1];
        Q2 = 0.2 * Q2_raw + 0.8 * Q2_raw[1];

        Re_raw = I2 * I2[1] + Q2 * Q2[1];
        Im_raw = I2 * Q2[1] - Q2 * I2[1];

        Re = 0.2 * Re_raw + 0.8 * Re_raw[1];
        Im = 0.2 * Im_raw + 0.8 * Im_raw[1];

# Compute the phase
        if Re != 0 and Im != 0
        then {
            Period_raw = 2 * Double.Pi / ATan(Im / Re);
        } else {
            Period_raw = 0;
        }

        if Period_raw > 1.5 * Period_raw[1]
        then {
            Period_cap = 1.5 * Period_raw[1];
        } else if Period_raw < 0.67 * Period_raw[1] {
            Period_cap = 0.67 * Period_raw[1];
        } else {
            Period_cap = Period_raw;
        }

        if Period_cap < 6
        then {
            Period_lim = 6;
        } else if Period_cap > 50
        then {
            Period_lim = 50;
        } else {
            Period_lim = Period_cap;
        }

        Period = 0.2 * Period_lim + 0.8 * Period_lim[1];
        SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[1];

        if I1 != 0
        then {
            Phase = ATan(Q1 / I1);
        } else if Q1 > 0
        then { # Quadrant 1:
            Phase = Double.Pi / 2;
        } else if Q1 < 0
        then { # Quadrant 4:
            Phase = -Double.Pi / 2;
        } else { # Both numerator and denominator are 0.
            Phase = 0;
        }

        DeltaPhase_raw = Phase[1] - Phase;
        if DeltaPhase_raw < 1
        then {
            DeltaPhase = 1;
        } else {
            DeltaPhase = DeltaPhase_raw;
        }

        alpha_raw = FastLimit / DeltaPhase;
        if alpha_raw < SlowLimit
        then {
            alpha = SlowLimit;
        } else {
            alpha = alpha_raw;
        }
        vmama = alpha * price + (1 - alpha) * vmama[1];
        vfama = 0.5 * alpha * vmama + (1 - 0.5 * alpha) * vfama[1];
    }

    plot MAMA = vmama;
    plot FAMA = vfama;
}


input price2 = hl2;
input FastLimit = 0.5;
input SlowLimit = 0.05;

def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;

def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),

if MAMA > FAMA then createColor(33,145,72) else Color.RED);

##################################
plot C3_MF_Line = (MAMA + FAMA) / 2;
C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
C3_MF_Line.SetLineWeight(3);

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;
C3_MF_Line.AssignValueColor(if paintCandles and ((direction == 1) and (price > C3_MF_Line)) then Color.GREEN else if paintCandles and ((direction == -1) and (price < C3_MF_Line)) then Color.RED else Color.GRAY);


# ====================MTF ===================
# ============================================================
def Price2Ag1 = hl2(period = agperiod1);
def Price2Ag2 = hl2(period = agperiod2);
def Price2Ag3 = hl2(period = agperiod3);
def Price2Ag4 = hl2(period = agperiod4);
def Price2Ag5 = hl2(period = agperiod5);


def MAMAagg1 = Ehler_MAMA(Price2Ag1, FastLimit, SlowLimit).MAMA;
def FAMAagg1 = Ehler_MAMA(Price2Ag1, FastLimit, SlowLimit).FAMA;
def MAMAagg2 = Ehler_MAMA(Price2Ag2, FastLimit, SlowLimit).MAMA;
def FAMAagg2 = Ehler_MAMA(Price2Ag2, FastLimit, SlowLimit).FAMA;
def MAMAagg3 = Ehler_MAMA(Price2Ag3, FastLimit, SlowLimit).MAMA;
def FAMAagg3 = Ehler_MAMA(Price2Ag3, FastLimit, SlowLimit).FAMA;
def MAMAagg4 = Ehler_MAMA(Price2Ag4, FastLimit, SlowLimit).MAMA;
def FAMAagg4 = Ehler_MAMA(Price2Ag4, FastLimit, SlowLimit).FAMA;
def MAMAagg5 = Ehler_MAMA(Price2Ag5, FastLimit, SlowLimit).MAMA;
def FAMAagg5 = Ehler_MAMA(Price2Ag5, FastLimit, SlowLimit).FAMA;



def C3_MF_LineAgg1 = (MAMAagg1 + FAMAagg1) / 2;
def C3_MF_LineAgg2 = (MAMAagg2 + FAMAagg2) / 2;
def C3_MF_LineAgg3 = (MAMAagg3 + FAMAagg3) / 2;
def C3_MF_LineAgg4 = (MAMAagg4 + FAMAagg4) / 2;
def C3_MF_LineAgg5 = (MAMAagg5 + FAMAagg5) / 2;


def C3_MF_UPAgg1 = C3_MF_LineAgg1 > C3_MF_LineAgg1[1];
def C3_MF_DNAgg1 = C3_MF_LineAgg1 < C3_MF_LineAgg1[1];
def C3_MF_UPAgg2 = C3_MF_LineAgg2 > C3_MF_LineAgg2[1];
def C3_MF_DNAgg2 = C3_MF_LineAgg2 < C3_MF_LineAgg2[1];
def C3_MF_UPAgg3 = C3_MF_LineAgg3 > C3_MF_LineAgg3[1];
def C3_MF_DNAgg3 = C3_MF_LineAgg3 < C3_MF_LineAgg3[1];
def C3_MF_UPAgg4 = C3_MF_LineAgg4 > C3_MF_LineAgg4[1];
def C3_MF_DNAgg4 = C3_MF_LineAgg4 < C3_MF_LineAgg4[1];
def C3_MF_UPAgg5 = C3_MF_LineAgg5 > C3_MF_LineAgg5[1];
def C3_MF_DNAgg5 = C3_MF_LineAgg5 < C3_MF_LineAgg5[1];

def MF_UP1 = FAMAagg1 < MAMAagg1;
def MF_DN1 = FAMAagg1 > MAMAagg1;
def MF_UP2 = FAMAagg2 < MAMAagg2;
def MF_DN2 = FAMAagg2 > MAMAagg2;
def MF_UP3 = FAMAagg3 < MAMAagg3;
def MF_DN3 = FAMAagg3 > MAMAagg3;
def MF_UP4 = FAMAagg4 < MAMAagg4;
def MF_DN4 = FAMAagg4 > MAMAagg4;
def MF_UP5 = FAMAagg5 < MAMAagg5;
def MF_DN5 = FAMAagg5 > MAMAagg5;


def MFUP = MF_UP1 and MF_UP2 and MF_UP3 and MF_UP4 and MF_UP5;
def MFDN = MF_DN1 and MF_DN2 and MF_DN3 and MF_DN4 and MF_DN5;



def UP8 = UPEMA and UPEMA2;
def DOWN8 = DOWNEMA and DOWNEMA2;
def priceColor8 = if UP8 then 1
                 else if DOWN8 then -1
                 else 0;

def MaxTimeAvg = ExpAverage(close(period = agperiod5), length);
def MaxTimeAvg2 = ExpAverage(close(period = agperiod5), length2);
def MaxTimeUp = MaxTimeAvg > MaxTimeAvg[1] and MaxTimeAvg2 > MaxTimeAvg2[1];
def MaxTimeDown = MaxTimeAvg < MaxTimeAvg[1] and MaxTimeAvg2 < MaxTimeAvg2[1];

### MACDBB

input MACDBB_FastLength = 12;
input MACDBB_SlowLength = 26;
input MACDBB_BandLength = 15;
input MACDBB_NumDev = 1.0;

def MACDBB_Data = MACD(fastLength = MACDBB_FastLength, slowLength = MACDBB_SlowLength, MACDLength = 5);

def MACDBB_Upper = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).UpperBand;

def MACDBB_Lower = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).Lowerband;


def MACDBB_Midline = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                               Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).MidLine;


def MACDBB_Line = MACDBB_Data;

def B4Long =  MACDBB_Line > MACDBB_Line[1] and MACDBB_Line > MACDBB_Upper
    or MACDBB_Line < MACDBB_Line[1] and MACDBB_Line > MACDBB_Upper;

def B4Short =  MACDBB_Line < MACDBB_Line[1] and MACDBB_Line < MACDBB_Lower
    or MACDBB_Line > MACDBB_Line[1] and MACDBB_Line < MACDBB_Lower;
# ===========================================================
# ===========================================================================
def Longarrow = Long_Only > Short_Only and RUP > RDN and  C3_MF_UPAgg1 and C3_MF_UPAgg2 and C3_MF_UPAgg3 and C3_MF_UPAgg4 and C3_MF_UPAgg5 and  MFUP > MFDN and ((UP5 == 1)) and (Consensus_Bias > 0) AND MaxTimeUp and B4Long;

def ShortArrow =  Long_Only < Short_Only and RUP < RDN and C3_MF_DNAgg1 and C3_MF_DNAgg2 and C3_MF_DNAgg3 and C3_MF_DNAgg4 and C3_MF_DNAgg5 and MFUP < MFDN and ((DOWN5) and (Consensus_Bias < 0) and MaxTimeDown and B4short);
# ========================================================
# =======================================================================
plot buySCA = Longarrow; #AvgExp crosses above AvgExp2 and RUP and MaxTimeUp and long_Only and C3_MF_UPAgg1 and C3_MF_UPAgg2 and C3_MF_UPAgg3 and C3_MF_UPAgg4 and C3_MF_UPAgg5 ; # and Mf_UP1 and MF_UP2 and MF_UP3 and MF_UP4 and MF_UP5 and MF_UP6 and((UP6 == 1) and (Consensus_Bias > 0)) and maxTimeUp ;#highest time expup #direction crosses above 0;
buySCA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySCA.SetDefaultColor(CreateColor(56, 223, 153));
buySCA.SetLineWeight(3);

plot sellSCA = ShortArrow;
 #AvgExp crosses below AvgExp2 and RDN and MaxTimeDown and Short_Only and C3_MF_DNAgg1 and C3_MF_DNAgg2 and C3_MF_DNAgg3 and C3_MF_DNAgg4 and C3_MF_DNAgg5;# and MF_DN1 and MF_DN2 and MF_DN3 and MF_DN4 and MF_DN5 and MF_DN6 and ((DOWN6) and (Consensus_Bias < 0));  #HighestTime frame EMA down #direction crosses below 0;
sellSCA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN );
sellSCA.setdefaultColor(color.RED);
sellSCA.setlineWeight(3);

AssignPriceColor(if paintCandles then if direction == 1 then Color.GREEN else if direction == -1 then Color.RED else Color.GRAY else Color.CURRENT);

AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.YELLOW else if ((Spark == 2) and (AvgExp > AvgExp2)) then createColor(33,145,72) else if (Spark == 0) then Color.RED else Color.GRAY);

AddLabel(yes, if ((UP5 == 1) and (Consensus_Bias > 0)) then " SCALP_LONG " else if ((DOWN5) and (Consensus_Bias < 0)) then " SCALP_SHORT " else " CHOP ", if ((Consensus_Bias > 0) and (UP5 == 1)) then createColor(33,145,72) else if ((Consensus_Bias < 0) and (DOWN5 == 1)) then Color.RED else Color.GRAY);

AddLabel(yes, if (ConsensusR > 0) then " LONG BIAS = %" + Round((ConsensusR / 14) * 100, 1) + " " else if (ConsensusR < 0) then  " SHORT BIAS = %" + Round(((ConsensusR * -1) / 14) * 100, 1) + " " else " CHOP =" + Round((ConsensusR / 14) * 100, 1) + " ", if (ConsensusR > 0) then createColor(33,145,72) else if (ConsensusR < 0) then Color.RED else Color.GRAY);

Alert(Longarrow, "long", Alert.BAR, Sound.DING);
Alert(ShortArrow, "short", Alert.BAR, Sound.DING);
 
Last edited:
The conditions may fade but the fuel has already been added, if you get my drift

I should have said, if enough fuel has been added IE volume etc then the arrows can disappear yet price will still continue to move in the direction.

Follow stops accordingly
 
This is my updated version of the scalper I'm using

I desperately need a WL column that can at least come close to the alerts

mLd4E9V.png


Code:
#C3_Max_v2 Created by Christopher84 12/14/2021  
# Based off of the Confirmation Candles Study. Main difference is that CC Candles weigh factors of positive
# and negative price movement to create the Consensus_Level. The Consensus_Level is considered positive if
# above zero and negative if below zero.

declare upper;


input agperiod1 = { "1 min", default "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod2 = {"1 min", "2 min", default "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod3 = {"1 min", "2 min", "3 min", "5 min", default "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod4 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", default "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};
input agperiod5 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", default "4 hours", "Day", "Week", "Month"};
input agperiod6 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week", "Month"};
input paintCandles = yes;
input price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;
input lengthSCA = 10;
input length2 = 35;

# Momentum Oscillators

def MS = Average(Average(price, ShortLength1) - Average(price, ShortLength2), ShortLength3);
def MS2 = Average(Average(price, LongLength1) - Average(price, LongLength2), LongLength3);
# Wave A
def MSGreens = If (MS >= 0, MS, 0);
def MSReds = If (MS < 0, MS, 0);
# Wave C
def MS2Blues = If (MS2 >= 0, MS2, 0);
def MS2Yellows = If (MS2 < 0, MS2, 0);

def MayhemBullish = MSGreens > MSGreens[1] and  MS2Blues > MS2Blues[1];
def MayhemBearish =  MSReds < MSReds[1] and  MS2Yellows < MS2Yellows[1];

def MS_Pos = MSGreens;
def MS_Neg = MSReds;
def MS2_Pos = MS2Blues;
def MS2_Neg = MS2Yellows;

# Squeeze Indicator
def length = 20;
def nK = 1.5;
def nBB = 2.0;

def BBHalfWidth = StDev(price, length);
def KCHalfWidth = nK * Average(TrueRange(high,  close,  low),  length);
def isSqueezed = nBB * BBHalfWidth / KCHalfWidth < 1;

def BBS_Ind = If(isSqueezed, 0, Double.NaN);

# Bollinger Resolution
def BBSMA = Average(price, length);
def BBSMAL = BBSMA + (-nBB * BBHalfWidth);
def BBSMAU = BBSMA + (nBB * BBHalfWidth);
def PerB = RoundUp((price - BBSMAL) / (BBSMAU - BBSMAL) * 100, 0);
AddLabel(yes, Concat("%B: ", PerB), if PerB < 0 then Color.MAGENTA else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.ORANGE);



####################################################################################################################################################

#OB_OS_Levels_v5

def BarsUsedForRange = 2;
def BarsRequiredToRemainInRange = 2;
def TargetMultiple = 0.5;
def ColorPrice = yes;
def HideTargets = no;
def HideBalance = no;
def HideBoxLines = no;
def HideCloud = no;
def HideLabels = no;

#--------------
#Squeeze Alert
#--------------

#Squeeze Dots Created 04/28/2021 by Christopher84
input ATRPeriod = 5;
input ATRFactor = 2.0;
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
input trailType = {default modified, unmodified};
def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
input averageType = AverageType.SIMPLE;
input firstTrade = {default long, short};
#input averageType = AverageType.WILDERS;####Use Simple instead of Wilders
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close - loss;
        case short:
            state = state.short;
            trail = close + loss;
    }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    } else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    } else {
        state = state.long;
        trail =  close - loss;
    }
}

def TrailingStop = trail;
def H = Highest(TrailingStop, 12);
def L = Lowest(TrailingStop, 12);
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, BulgeLengthPrice);
def IntermSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice);
def sqzTrigger = BandwidthC3 <= IntermSupport2;
def sqzLevel = if !sqzTrigger[1] and sqzTrigger then hl2
               else if !sqzTrigger then Double.NaN
               else sqzLevel[1];

plot Squeeze_Alert = sqzLevel;
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);

#-----------------------------
#Yellow Candle_height (OB_OS)
#-----------------------------
def displace = 0;
def factorK2 = 3.25;
def lengthK2 = 20;
def price1 = open;
def trueRangeAverageType = AverageType.SIMPLE;
def ATR_length = 15;
def SMA_lengthS = 6;
input ATRPeriod2 = 5;
input ATRFactor2 = 1.5;
#input averageType = AverageType.WILDERS;####Use Simple instead of Wilders
def HiLo2 = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef2 = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef2 = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def loss2 = ATRFactor2 * MovingAverage(averageType, trueRange, ATRPeriod2);

def multiplier_factor = 1.25;
def valS = Average(price, SMA_lengthS);
def average_true_range = Average(TrueRange(high, close, low), length = ATR_length);
def Upper_BandS = valS[-displace] + multiplier_factor * average_true_range[-displace];
def Middle_BandS = valS[-displace];
def Lower_BandS = valS[-displace] - multiplier_factor * average_true_range[-displace];

def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK2);
def averageK2 = MovingAverage(averageType, price, lengthK2);
def AvgK2 = averageK2[-displace];
def Upper_BandK2 = averageK2[-displace] + shiftK2[-displace];
def Lower_BandK2 = averageK2[-displace] - shiftK2[-displace];



def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};

def fastEMA = ExpAverage(price, fastLength);
def slowEMA = ExpAverage(price, slowLength);
def Value;
def Avg1;
switch (MACD_AverageType) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg1 = Average(Value, MACDLength);
case EMA:
    Value = fastEMA - slowEMA;
    Avg1 = ExpAverage(Value, MACDLength);
}
def Diff = Value - Avg1;
def MACDLevel = 0.0;
def Level = MACDLevel;

#RSI
def RSI_length = 14;
def RSI_AverageType = AverageType.WILDERS;
def RSI_OB = 70;
def RSI_OS = 30;

def NetChgAvg = MovingAverage(RSI_AverageType, price - price[1], RSI_length);
def TotChgAvg = MovingAverage(RSI_AverageType, AbsValue(price - price[1]), RSI_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def condition2 = (RSI[3] < RSI) is true or (RSI >= 80) is true;
def condition2D = (RSI[3] > RSI) is true or (RSI < 20) is true;
def conditionOB1 = RSI > RSI_OB;
def conditionOS1 = RSI < RSI_OS;

#MFI
def MFI_Length = 14;
def MFIover_Sold = 20;
def MFIover_Bought = 80;
def movingAvgLength = 1;
def MoneyFlowIndex = Average(MoneyFlow(high, close, low, volume, MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;



#Forecast
def na = Double.NaN;
def MidLine = 50;
def Momentum = MarketForecast().Momentum;
def NearT =  MarketForecast().NearTerm;
def Intermed = MarketForecast().Intermediate;
def FOB = 80;
def FOS = 20;
def upperLine = 110;



#Change in Price
def lengthCIP = 5;
def CIP = (price - price[1]);
def AvgCIP = ExpAverage(CIP[-displace], lengthCIP);
def CIP_UP = AvgCIP > AvgCIP[1];
def CIP_DOWN = AvgCIP < AvgCIP[1];



#EMA_1
def EMA_length = 8;
def AvgExp = ExpAverage(price[-displace], EMA_length);



#EMA_2
def EMA_2length = 20;
def displace2 = 0;
def AvgExp2 = ExpAverage(price[-displace2], EMA_2length);



#DMI Oscillator
def DMI_length = 5;#Typically set to 10
input DMI_averageType = AverageType.WILDERS;
def diPlus = DMI(DMI_length, DMI_averageType)."DI+";
def diMinus = DMI(DMI_length, DMI_averageType)."DI-";
def Osc = diPlus - diMinus;
def Hist = Osc;
def ZeroLine = 0;

def condition8 = Osc >= ZeroLine;
def condition8D = Osc < ZeroLine;

#Trend_Periods
def TP_fastLength = 3;#Typically 7
def TP_slowLength = 4;#Typically 15
def Periods = Sign(ExpAverage(close, TP_fastLength) - ExpAverage(close, TP_slowLength));



#Polarized Fractal Efficiency
def PFE_length = 5;#Typically 10
def smoothingLength = 2.5;#Typically 5
def PFE_diff = close - close[PFE_length - 1];
def val = 100 * Sqrt(Sqr(PFE_diff) + Sqr(PFE_length)) / Sum(Sqrt(1 + Sqr(close - close[1])), PFE_length - 1);
def PFE = ExpAverage(if PFE_diff > 0 then val else -val, smoothingLength);
def UpperLevel = 50;
def LowerLevel = -50;



#Bollinger Bands PercentB
input BBPB_averageType = AverageType.SIMPLE;
def BBPB_length = 20;#Typically 20
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.0;
def BBPB_OB = 100;
def BBPB_OS = 0;
def upperBand = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, BBPB_averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, BBPB_averageType).LowerBand;
def PercentB = (price - lowerBand) / (upperBand - lowerBand) * 100;
def HalfLine = 50;
def UnitLine = 100;



#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);


#Projection Oscillator
def ProjectionOsc_length = 30;#Typically 10
def MaxBound = HighestWeighted(high, ProjectionOsc_length, LinearRegressionSlope(price = high, length = ProjectionOsc_length));
def MinBound = LowestWeighted(low, ProjectionOsc_length, LinearRegressionSlope(price = low, length = ProjectionOsc_length));
def ProjectionOsc_diff = MaxBound - MinBound;
def PROSC = if ProjectionOsc_diff != 0 then 100 * (close - MinBound) / ProjectionOsc_diff else 0;
def PROSC_OB = 80;
def PROSC_OS = 20;


#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
input Confirmation_Factor = 7;
#Use for testing conditions individually. Remove # from line below and change Confirmation_Factor to 1.
#def Agreement_Level = condition1;
def Agreement_LevelOB = 12;
def Agreement_LevelOS = 2;

def factorK = 2.0;
def lengthK = 20;
def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK);
def averageK = MovingAverage(averageType, price, lengthK);
def AvgK = averageK[-displace];
def Upper_BandK = averageK[-displace] + shift[-displace];
def Lower_BandK = averageK[-displace] - shift[-displace];
# ============================================================
# ============= CONDITIONS =======================

def condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);
def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;
def condition3 = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;
def condition3D = (MoneyFlowIndex[2] > MoneyFlowIndex) is true or (MoneyFlowIndex < 20) is true;
def conditionOB2 = MoneyFlowIndex > MFIover_Bought;
def conditionOS2 = MoneyFlowIndex < MFIover_Sold;
def condition4 = (Intermed[1] <= Intermed) or (NearT >= MidLine);
def condition4D = (Intermed[1] > Intermed) or (NearT < MidLine);
def conditionOB3 = Intermed > FOB;
def conditionOS3 = Intermed < FOS;
def conditionOB4 = NearT > FOB;
def conditionOS4 = NearT < FOS;
def condition5 = CIP_UP;
def condition5D = CIP_DOWN;
def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);
def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);
def condition9 = Periods > 0;
def condition9D = Periods < 0;
def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;
def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

def condition12 = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);
def condition12D = (Upper_BandS[1] > Upper_BandS) and (Lower_BandS[1] > Lower_BandS);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);
def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

def conditionK1UP = price >= Upper_BandK;
def conditionK2UP = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3DN = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);
def conditionK4DN = price < Lower_BandK;



# ==============
# ==============================
# =======================================================
def Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13 + condition14 + conditionK1UP + conditionK2UP;

def Agreement_LevelD = (condition1D + condition2D + condition3D + condition4D + condition5D + condition6D + condition7D + condition8D + condition9D + condition10D + condition11D + condition12D + condition13D + condition14D + conditionK3DN + conditionK4DN);

def Consensus_Level = Agreement_Level - Agreement_LevelD;

def UP2 = Consensus_Level >= 4;
def DOWN2 = Consensus_Level < -5;

def priceColor2 = if UP2 then 1
                 else if DOWN2 then -1
                 else priceColor2[1];

def Consensus_Level_OB = 14;
def Consensus_Level_OS = -12;

#Super_OB/OS Signal
def OB_Level = conditionOB1 + conditionOB2 + conditionOB3 + conditionOB4 + conditionOB5 + conditionOB6 + conditionOB7;
def OS_Level = conditionOS1 + conditionOS2 + conditionOS3 + conditionOS4 + conditionOS5 + conditionOS6 + conditionOS7;

def Consensus_Line = OB_Level - OS_Level;
def Zero_Line = 0;
def Super_OB = 4;
def Super_OS = -4;

def DOWN_OB = (Agreement_Level > Agreement_LevelOB) and (Consensus_Line > Super_OB) and (Consensus_Level > Consensus_Level_OB);
def UP_OS = (Agreement_Level < Agreement_LevelOS) and (Consensus_Line < Super_OS) and (Consensus_Level < Consensus_Level_OS);

def OS_Buy = UP_OS;
def OB_Sell = DOWN_OB;
def neutral = Consensus_Line < Super_OB and Consensus_Line > Super_OS;



input use_line_limits = yes;#Yes, plots line from/to; No, plot line across entire chart
input linefrom = 100;#Hint linefrom: limits how far line plots in candle area
input lineto   = 12;#Hint lineto: limits how far into expansion the line will plot

def YHOB = if coloredCandlesOn and ((price1 > Upper_BandS) and (condition_BandRevDn)) then high else Double.NaN;
def YHOS = if coloredCandlesOn and ((price1 < Lower_BandS) and (condition_BandRevUp)) then high else Double.NaN;

def YLOB = if coloredCandlesOn and ((price1 > Upper_BandS) and (condition_BandRevDn)) then low else Double.NaN;
def YLOS = if coloredCandlesOn and ((price1 < Lower_BandS) and (condition_BandRevUp)) then low else Double.NaN;

#extend midline of yellow candle
plot YCOB = if !IsNaN(YHOB) then hl2 else Double.NaN;
YCOB.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOB.SetDefaultColor(Color.GREEN);
def YHextOB = if IsNaN(YCOB) then YHextOB[1] else YCOB;
plot YHextlineOB = YHextOB;
YHextlineOB.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOB.SetDefaultColor(color.RED);
YHextlineOB.SetLineWeight(4);

plot YCOS = if !IsNaN(YHOS) then hl2 else Double.NaN;
YCOS.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOS.SetDefaultColor(Color.GREEN);
def YHextOS = if IsNaN(YCOS) then YHextOS[1] else YCOS;
plot YHextlineOS = YHextOS;
YHextlineOS.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOS.SetDefaultColor(Color.LIGHT_GREEN);
YHextlineOS.SetLineWeight(4);

def YC = coloredCandlesOn and priceColor2 == 1 and price1 > Upper_BandS and condition_BandRevDn;

#Additional Signals
input showCloud = yes;
#AddCloud(if showCloud and condition_BandRevUp then Lower_BandK2 else Double.NaN,  Lower_BandS,  Color.LIGHT_GREEN,  Color.CURRENT);
#AddCloud(if showCloud and condition_BandRevDn then Upper_BandS else Double.NaN,  Upper_BandK2,  Color.LIGHT_RED,  Color.CURRENT);

# Identify Consolidation

def HH = Highest(high[1], BarsUsedForRange);
def LL = Lowest(low[1], BarsUsedForRange);

def maxH = Highest(HH, BarsRequiredToRemainInRange);
def maxL = Lowest(LL, BarsRequiredToRemainInRange);

def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];

def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;

def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;
def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;

def ExpH = if BarNumber() == 1 then Double.NaN else
            if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else
            if high <= ExpH[1] then ExpH[1] else Double.NaN;

def ExpL = if BarNumber() == 1 then Double.NaN else
            if CountL[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else
            if low >= ExpL[1] then ExpL[1] else Double.NaN;

# Plot the High and Low of the Box; Paint Cloud
def BoxHigh = if ((DOWN_OB) or (Upper_BandS crosses above Upper_BandK2) or (condition_BandRevDn) and (high > high[1]) and ((price > Upper_BandK2) or (price > Upper_BandS))) then Highest(ExpH) else Double.NaN;

def BoxLow = if (DOWN_OB) or ((Upper_BandS crosses above Upper_BandK2)) then Lowest(low) else Double.NaN;

def BoxHigh2 = if ((UP_OS) or ((Lower_BandS crosses below Lower_BandK2))) then Highest(ExpH) else Double.NaN;

#def BH2 = if !IsNaN(BoxHigh2) then high else Double.NaN;

#def BH2ext = if IsNaN(BH2) then BH2ext[1] else BH2;
#def BH2extline = BH2ext;

#plot H_BH2extline = Lowest(BH2extline, 1);
#H_BH2extline.SetDefaultColor(Color.GREEN);

def BoxLow2 = if ((UP_OS) or (Lower_BandS crosses below Lower_BandK2) or (condition_BandRevUp) and (low < low[1]) and ((price < Lower_BandK2) or (price < Lower_BandS))) or ((UP_OS[1]) and (low < low[1])) then Lowest(low) else Double.NaN;

# extend the current YCHigh line to the right edge of the chart
def BH1 = if !IsNaN(BoxHigh) then high else Double.NaN;

def BH1ext = if IsNaN(BH1) then BH1ext[1] else BH1;
def BH1extline = BH1ext;


def BL1 = if !IsNaN(BoxLow) then low else Double.NaN;
#BL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#BL1.SetDefaultColor(Color.RED);
def BL1ext = if IsNaN(BL1) then BL1ext[1] else BL1;
plot BL1extline = BL1ext;
BL1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL1extline.SetDefaultColor(Color.RED);
BL1extline.SetLineWeight(1);

def BH2 = if !IsNaN(BoxHigh2) then high else Double.NaN;
#BH2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#BH2.SetDefaultColor(Color.GREEN);
def BH2ext = if IsNaN(BH2) then BH2ext[1] else BH2;
def BH2extline = BH2ext;
#BH2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#BH2extline.SetDefaultColor(Color.GREEN);
#BH2extline.SetLineWeight(3);

def BL2 = if !IsNaN(BoxLow2) then low else Double.NaN;
#BL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#BL2.SetDefaultColor(Color.RED);
def BL2ext = if IsNaN(BL2) then BL2ext[1] else BL2;
plot BL2extline = BL2ext;
BL2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL2extline.SetDefaultColor(Color.GREEN);
BL2extline.SetLineWeight(1);

plot H_BH1extline = Highest(BH1extline, 1);
H_BH1extline.SetDefaultColor(Color.RED);
plot L_BL1extline = Highest(BL1extline, 1);
L_BL1extline.SetDefaultColor(Color.RED);

plot H_BH2extline = Lowest(BH2extline, 1);
H_BH2extline.SetDefaultColor(Color.GREEN);
plot L_BL2extline = Lowest(BL2extline, 1);
L_BL2extline.SetDefaultColor(Color.GREEN);

#plot L_BL1extline = Highest(BL1extline, 1);
#     L_BL1extline.SetDefaultColor(Color.Red);

AddCloud(if showCloud and !HideCloud then BH1extline else Double.NaN, BL1extline, Color.RED, Color.GRAY);
AddCloud(if showCloud and !HideCloud then BH2extline else Double.NaN, BL2extline, Color.GREEN, Color.GRAY);

script WMA_Smooth {
    input price = hl2;
    plot smooth = (4 * price
+ 3 * price[1]
+ 2 * price[2]
+ price[3]) / 10;
}

script Phase_Accumulation {

    input price = hl2;

    rec Smooth;
    rec Detrender;
    rec Period;
    rec Q1;
    rec I1;
    rec I1p;
    rec Q1p;
    rec Phase1;
    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase1;
    rec InstPeriod1;
    rec InstPeriod;
    def CorrectionFactor;

    if BarNumber() <= 5
    then {
        Period = 0;
        Smooth = 0;
        Detrender = 0;
        CorrectionFactor = 0;
        Q1 = 0;
        I1 = 0;
        Q1p = 0;
        I1p = 0;
        Phase = 0;
        Phase1 = 0;
        DeltaPhase1 = 0;
        DeltaPhase = 0;
        InstPeriod = 0;
        InstPeriod1 = 0;
    } else {
        CorrectionFactor = 0.075 * Period[1] + 0.54;

# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

# Compute Quadrature and Phase of Detrended signal:
        Q1p = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1p = Detrender[3];

# Smooth out Quadrature and Phase:
        I1 = 0.15 * I1p + 0.85 * I1p[1];
        Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

# Determine Phase
        if I1 != 0
        then {
# Normally, ATAN gives results from -pi/2 to pi/2.
# We need to map this to circular coordinates 0 to 2pi

            if Q1 >= 0 and I1 > 0
            then { # Quarant 1
                Phase1 = ATan(AbsValue(Q1 / I1));
            } else if Q1 >= 0 and I1 < 0
            then { # Quadrant 2
                Phase1 = Double.Pi - ATan(AbsValue(Q1 / I1));
            } else if Q1 < 0 and I1 < 0
            then { # Quadrant 3
                Phase1 = Double.Pi + ATan(AbsValue(Q1 / I1));
            } else { # Quadrant 4
                Phase1 = 2 * Double.Pi - ATan(AbsValue(Q1 / I1));
            }
        } else if Q1 > 0
        then { # I1 == 0, Q1 is positive
            Phase1 = Double.Pi / 2;
        } else if Q1 < 0
        then { # I1 == 0, Q1 is negative
            Phase1 = 3 * Double.Pi / 2;
        } else { # I1 and Q1 == 0
            Phase1 = 0;
        }

# Convert phase to degrees
        Phase = Phase1 * 180 / Double.Pi;

        if Phase[1] < 90 and Phase > 270
        then {
# This occurs when there is a big jump from 360-0
            DeltaPhase1 = 360 + Phase[1] - Phase;
        } else {
            DeltaPhase1 = Phase[1] - Phase;
        }

# Limit our delta phases between 7 and 60
        if DeltaPhase1 < 7
        then {
            DeltaPhase = 7;
        } else if DeltaPhase1 > 60
        then {
            DeltaPhase = 60;
        } else {
            DeltaPhase = DeltaPhase1;
        }

# Determine Instantaneous period:
        InstPeriod1 =
-1 * (fold i = 0 to 40 with v=0 do
if v < 0 then
v
else if v > 360 then
-i
else
v + GetValue(DeltaPhase, i, 41)
);

        if InstPeriod1 <= 0
        then {
            InstPeriod = InstPeriod[1];
        } else {
            InstPeriod = InstPeriod1;
        }

        Period = 0.25 * InstPeriod + 0.75 * Period[1];
    }
    plot DC = Period;
}

script Ehler_MAMA {
    input price = hl2;
    input FastLimit = 0.5;
    input SlowLimit = 0.05;


    rec Period;
    rec Period_raw;
    rec Period_cap;
    rec Period_lim;

    rec Smooth;
    rec Detrender;
    rec I1;
    rec Q1;
    rec jI;
    rec jQ;
    rec I2;
    rec Q2;
    rec I2_raw;
    rec Q2_raw;

    rec Phase;
    rec DeltaPhase;
    rec DeltaPhase_raw;
    rec alpha;
    rec alpha_raw;

    rec Re;
    rec Im;
    rec Re_raw;
    rec Im_raw;

    rec SmoothPeriod;
    rec vmama;
    rec vfama;

    def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

    if BarNumber() <= 5
    then {
        Smooth = 0;
        Detrender = 0;

        Period = 0;
        Period_raw = 0;
        Period_cap = 0;
        Period_lim = 0;
        I1 = 0;
        Q1 = 0;
        I2 = 0;
        Q2 = 0;
        jI = 0;
        jQ = 0;
        I2_raw = 0;
        Q2_raw = 0;
        Re = 0;
        Im = 0;
        Re_raw = 0;
        Im_raw = 0;
        SmoothPeriod = 0;
        Phase = 0;
        DeltaPhase = 0;
        DeltaPhase_raw = 0;
        alpha = 0;
        alpha_raw = 0;
        vmama = 0;
        vfama = 0;
    } else {
# Smooth and detrend my smoothed signal:
        Smooth = WMA_Smooth(price);
        Detrender = ( 0.0962 * Smooth
+ 0.5769 * Smooth[2]
- 0.5769 * Smooth[4]
- 0.0962 * Smooth[6] ) * CorrectionFactor;

        Q1 = ( 0.0962 * Detrender
+ 0.5769 * Detrender[2]
- 0.5769 * Detrender[4]
- 0.0962 * Detrender[6] ) * CorrectionFactor;
        I1 = Detrender[3];

        jI = ( 0.0962 * I1
+ 0.5769 * I1[2]
- 0.5769 * I1[4]
- 0.0962 * I1[6] ) * CorrectionFactor;

        jQ = ( 0.0962 * Q1
+ 0.5769 * Q1[2]
- 0.5769 * Q1[4]
- 0.0962 * Q1[6] ) * CorrectionFactor;

# This is the complex conjugate
        I2_raw = I1 - jQ;
        Q2_raw = Q1 + jI;

        I2 = 0.2 * I2_raw + 0.8 * I2_raw[1];
        Q2 = 0.2 * Q2_raw + 0.8 * Q2_raw[1];

        Re_raw = I2 * I2[1] + Q2 * Q2[1];
        Im_raw = I2 * Q2[1] - Q2 * I2[1];

        Re = 0.2 * Re_raw + 0.8 * Re_raw[1];
        Im = 0.2 * Im_raw + 0.8 * Im_raw[1];

# Compute the phase
        if Re != 0 and Im != 0
        then {
            Period_raw = 2 * Double.Pi / ATan(Im / Re);
        } else {
            Period_raw = 0;
        }

        if Period_raw > 1.5 * Period_raw[1]
        then {
            Period_cap = 1.5 * Period_raw[1];
        } else if Period_raw < 0.67 * Period_raw[1] {
            Period_cap = 0.67 * Period_raw[1];
        } else {
            Period_cap = Period_raw;
        }

        if Period_cap < 6
        then {
            Period_lim = 6;
        } else if Period_cap > 50
        then {
            Period_lim = 50;
        } else {
            Period_lim = Period_cap;
        }

        Period = 0.2 * Period_lim + 0.8 * Period_lim[1];
        SmoothPeriod = 0.33 * Period + 0.67 * SmoothPeriod[1];

        if I1 != 0
        then {
            Phase = ATan(Q1 / I1);
        } else if Q1 > 0
        then { # Quadrant 1:
            Phase = Double.Pi / 2;
        } else if Q1 < 0
        then { # Quadrant 4:
            Phase = -Double.Pi / 2;
        } else { # Both numerator and denominator are 0.
            Phase = 0;
        }

        DeltaPhase_raw = Phase[1] - Phase;
        if DeltaPhase_raw < 1
        then {
            DeltaPhase = 1;
        } else {
            DeltaPhase = DeltaPhase_raw;
        }

        alpha_raw = FastLimit / DeltaPhase;
        if alpha_raw < SlowLimit
        then {
            alpha = SlowLimit;
        } else {
            alpha = alpha_raw;
        }
        vmama = alpha * price + (1 - alpha) * vmama[1];
        vfama = 0.5 * alpha * vmama + (1 - 0.5 * alpha) * vfama[1];
    }

    plot MAMA = vmama;
    plot FAMA = vfama;
}


input price2 = hl2;
input FastLimit = 0.5;
input SlowLimit = 0.05;

def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;



def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);



##################################
plot C3_MF_Line = (MAMA + FAMA) / 2;
C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
C3_MF_Line.SetLineWeight(3);
C3_MF_Line.AssignValueColor(if ((priceColor2 == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if ((priceColor2 == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if priceColor2 == -1 then Color.RED  else if (priceColor2 == 1) then Color.GREEN else Color.CURRENT);

def C3_MF_UP = C3_MF_Line > C3_MF_Line[1];
def C3_MF_DN = C3_MF_Line < C3_MF_Line[1];
def priceColor9 = if C3_MF_UP then 1
                 else if C3_MF_DN then -1
                 else priceColor9[1];

def MF_UP = FAMA < MAMA;
def MF_DN = FAMA > MAMA;
def priceColor10 = if MF_UP then 1
                 else if MF_DN then -1
                 else priceColor10[1];

input extension_length_limited_to = 10;
def lastbar = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def inertline = InertiaAll(C3_MF_Line, 2);
def EXT_C3_MF = if !IsNaN(close()) then inertline else EXT_C3_MF[1] + ((EXT_C3_MF[1] - EXT_C3_MF[2]) / (2 - 1));
plot extension = if BarNumber() <= HighestAll(lastbar) + extension_length_limited_to then EXT_C3_MF else Double.NaN;
extension.SetDefaultColor(Color.WHITE);
####################################################################################################################################################

#EMA's
input length8 = 10;
input length9 = 35;
input show_ema_cloud = yes;

plot AvgExp8 = ExpAverage(price[-displace], length8);
plot AvgExp9 = ExpAverage(price[-displace], length9);

def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);
def UPW = AvgExp9[1] < AvgExp9;
AvgExp9.SetStyle(Curve.SHORT_DASH);
#AvgExp9.SetLineWeight(1);

def Below = AvgExp8 < AvgExp9;
def Spark = UPD + UPW + Below;

def UPEMA = AvgExp8[1] < AvgExp8;
def DOWNEMA = AvgExp8[1] > AvgExp8;
def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;

AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);
AvgExp9.AssignValueColor(if UPEMA2 then Color.LIGHT_GREEN else if DOWNEMA2 then Color.RED else Color.YELLOW);

AddCloud(if show_ema_cloud and (AvgExp9 > AvgExp8) then AvgExp9 else Double.NaN, AvgExp8, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExp8 > AvgExp9) then AvgExp8 else Double.NaN, AvgExp9, Color.LIGHT_GREEN, Color.CURRENT);

def UP8 = UPEMA and UPEMA2;
def DOWN8 = DOWNEMA and DOWNEMA2;
def priceColor8 = if UP8 then 1
                 else if DOWN8 then -1
                 else 0;

#=======================

# Parabolic SAR Signal
def accelerationFactor = 0.0275;
def accelerationLimit = 0.2;

def SAR = ParabolicSAR(accelerationFactor = accelerationFactor, accelerationLimit = accelerationLimit);
def bearishCross = Crosses(SAR, price, CrossingDirection.ABOVE);

#plot signalDown = bearishCross and MAMA < FAMA ;#If(bearishCross, 0, Double.NaN);
#signalDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#signalDown.SetLineWeight(3);
#signalDown.AssignValueColor(Color.DOWNTICK);

def bullishCross = Crosses(SAR, price, CrossingDirection.BELOW);

#plot signalUp =  bullishCross and MAMA > FAMA ;#If(bullishCross, 0, Double.NaN);
#signalUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#signalUp.SetLineWeight(3);
#signalUp.AssignValueColor(Color.UPTICK);

def UP = bullishCross;
def DOWN = bearishCross;
def priceColor = if UP then 1
                 else if DOWN then -1
                 else priceColor[1];

#===================

def UpCalc =  (priceColor == 1) + (priceColor2 == 1) + (priceColor8 == 1) + (priceColor10 == 1);

def CandleColor = if (UpCalc >= 3) then 1
                 else if (UpCalc == 0) then -1
                 else if (priceColor2 == 1) then 1
                 else if (priceColor2 == -1) then -1
                 else CandleColor[1];
AssignPriceColor(if coloredCandlesOn and (CandleColor == 1) then createColor(13,165,112) else if coloredCandlesOn and (CandleColor == -1) then createColor(164,4,30) else Color.CURRENT);

#Labels
def Buy = UP_OS;
def Sell = DOWN_OB;
def conditionLTB = (conditionK2UP and (Consensus_Level < 0));
def conditionLTS = (conditionK3DN and (Consensus_Level > 0));
def conditionBO = ((Upper_BandS[1] < Upper_BandS) and (Lower_BandS[1] < Lower_BandS)) and ((Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK));
def conditionBD = ((Upper_BandS[1] > Upper_BandS) and (Lower_BandS[1] > Lower_BandS) and (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK));
def MomentumUP = Consensus_Level[1] < Consensus_Level;
def MomentumDOWN = Consensus_Level[1] > Consensus_Level;

def Squeeze_Signal = !IsNaN(Squeeze_Alert);
def conditionOB = (Consensus_Level >= 12) and (Consensus_Line >= 4);
def conditionOS = (Consensus_Level <= -12) and (Consensus_Line <= -3);



# =============================================================================================================
# =====================================================================================
# ==================== SCALPER ============================

#Scalper Upper v2 Created 02/01/2022 by Christopher84
#Sound Alerts added by Barbaros





#Current Period
plot AvgExpSCA = ExpAverage(price[-displace], lengthSCA);
plot AvgExp2SCA = ExpAverage(price[-displace], length2);

def UPC1 = AvgExpSCA > AvgExpSCA[1];
def DNC1 = AvgExpSCA < AvgExpSCA[1];
def UPC2 = AvgExp2SCA > AvgExp2SCA[1];
def DNC2 = AvgExp2SCA < AvgExp2SCA[1];
def BelowSCA = AvgExpSCA < AvgExp2SCA;
def SparkSCA = UPC1 + UPC2 + BelowSCA;
def UPEMASCA = AvgExpSCA[1] < AvgExpSCA;
def DOWNEMASCA = AvgExpSCA[1] > AvgExpSCA;
def UPEMA2SCA = AvgExp2SCA[1] < AvgExp2SCA;
def DOWNEMA2SCA = AvgExp2SCA[1] > AvgExp2SCA;

AvgExpSCA.SetStyle(Curve.SHORT_DASH);
AvgExpSCA.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);
AvgExp2SCA.SetStyle(Curve.SHORT_DASH);
AvgExp2SCA.AssignValueColor(if UPEMA2SCA then Color.LIGHT_GREEN else if DOWNEMA2SCA then Color.RED else Color.YELLOW);




AddCloud(if show_ema_cloud and (AvgExp2SCA > AvgExpSCA) then AvgExp2SCA else Double.NaN, AvgExpSCA, Color.LIGHT_RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (AvgExpSCA > AvgExp2SCA) then AvgExpSCA else Double.NaN, AvgExp2SCA, Color.LIGHT_GREEN, Color.CURRENT);

# ========= MTF INFO ========================
def MaxTimeAvg = ExpAverage(close(period = agperiod6), length8);
def MaxTimeUp = MaxTimeAvg > MaxTimeAvg[1];
def MaxTimeDown = MaxTimeAvg < MaxTimeAvg[1];

# ============================================================
def Price2Ag1 = hl2(period = agperiod1);
def Price2Ag2 = hl2(period = agperiod2);
def Price2Ag3 = hl2(period = agperiod3);
def Price2Ag4 = hl2(period = agperiod4);
def Price2Ag5 = hl2(period = agperiod5);
def Price2Ag6 = hl2(period = agperiod6);

def MAMAagg1 = Ehler_MAMA(Price2Ag1, FastLimit, SlowLimit).MAMA;
def FAMAagg1 = Ehler_MAMA(Price2Ag1, FastLimit, SlowLimit).FAMA;
def MAMAagg2 = Ehler_MAMA(Price2Ag2, FastLimit, SlowLimit).MAMA;
def FAMAagg2 = Ehler_MAMA(Price2Ag2, FastLimit, SlowLimit).FAMA;
def MAMAagg3 = Ehler_MAMA(Price2Ag3, FastLimit, SlowLimit).MAMA;
def FAMAagg3 = Ehler_MAMA(Price2Ag3, FastLimit, SlowLimit).FAMA;
def MAMAagg4 = Ehler_MAMA(Price2Ag4, FastLimit, SlowLimit).MAMA;
def FAMAagg4 = Ehler_MAMA(Price2Ag4, FastLimit, SlowLimit).FAMA;
def MAMAagg5 = Ehler_MAMA(Price2Ag5, FastLimit, SlowLimit).MAMA;
def FAMAagg5 = Ehler_MAMA(Price2Ag5, FastLimit, SlowLimit).FAMA;
def MAMAagg6 = Ehler_MAMA(Price2Ag6, FastLimit, SlowLimit).MAMA;
def FAMAagg6 = Ehler_MAMA(Price2Ag6, FastLimit, SlowLimit).FAMA;

def MF_UP2 = FAMAagg2 < MAMAagg2;
def MF_DN2 = FAMAagg2 > MAMAagg2;
def MF_UP3 = FAMAagg3 < MAMAagg3;
def MF_DN3 = FAMAagg3 > MAMAagg3;
def MF_UP4 = FAMAagg4 < MAMAagg4;
def MF_DN4 = FAMAagg4 > MAMAagg4;
def MF_UP5 = FAMAagg5 < MAMAagg5;
def MF_DN5 = FAMAagg5 > MAMAagg5;
def MF_UP6 = FAMAagg6 < MAMAagg6;
def MF_DN6 = FAMAagg6 > MAMAagg6;



def MF_UP1 = FAMAagg1 < MAMAagg1;
def MF_DN1 = FAMAagg1 > MAMAagg1;

def C3_MF_LineAgg1 = (MAMAagg1 + FAMAagg1) / 2;
def C3_MF_LineAgg2 = (MAMAagg2 + FAMAagg2) / 2;
def C3_MF_LineAgg3 = (MAMAagg3 + FAMAagg3) / 2;
def C3_MF_LineAgg4 = (MAMAagg4 + FAMAagg4) / 2;
def C3_MF_LineAgg5 = (MAMAagg5 + FAMAagg5) / 2;
def C3_MF_LineAgg6 = (MAMAagg6 + FAMAagg6) / 2;

def C3_MF_UPAgg1 = C3_MF_LineAgg1 > C3_MF_LineAgg1[1];
def C3_MF_DNAgg1 = C3_MF_LineAgg1 < C3_MF_LineAgg1[1];
def C3_MF_UPAgg2 = C3_MF_LineAgg2 > C3_MF_LineAgg2[1];
def C3_MF_DNAgg2 = C3_MF_LineAgg2 < C3_MF_LineAgg2[1];
def C3_MF_UPAgg3 = C3_MF_LineAgg3 > C3_MF_LineAgg3[1];
def C3_MF_DNAgg3 = C3_MF_LineAgg3 < C3_MF_LineAgg3[1];
def C3_MF_UPAgg4 = C3_MF_LineAgg4 > C3_MF_LineAgg4[1];
def C3_MF_DNAgg4 = C3_MF_LineAgg4 < C3_MF_LineAgg4[1];
def C3_MF_UPAgg5 = C3_MF_LineAgg5 > C3_MF_LineAgg5[1];
def C3_MF_DNAgg5 = C3_MF_LineAgg5 < C3_MF_LineAgg5[1];
def C3_MF_UPAgg6 = C3_MF_LineAgg6 > C3_MF_LineAgg6[1];
def C3_MF_DNAgg6 = C3_MF_LineAgg6 < C3_MF_LineAgg6[1];

#def MF_UP = FAMA < MAMA;
#def MF_DN = FAMA > MAMA;

# ============ MTF CANDLE COLOR REFerence ==================


#DEF Agg6CandleColorGRN = CandleColor(period = agperiod6) == 1;
#def UpCalcaGG6 = UPCalc(period = agperiod6);
#def CandleColorAGG6 = UPCalc(period = agperiod6),UPCALC;

# ====================================================
#Agperiod1
def avg = ExpAverage(close(period = agperiod1), lengthSCA);
def avg2 = ExpAverage(close(period = agperiod1), length2);
def avg3 = ExpAverage(close(period = agperiod2), lengthSCA);
def avg4 = ExpAverage(close(period = agperiod2), length2);
def avg5 = ExpAverage(close(period = agperiod3), lengthSCA);
def avg6 = ExpAverage(close(period = agperiod3), length2);
def avg7 = ExpAverage(close(period = agperiod4), lengthSCA);
def avg8 = ExpAverage(close(period = agperiod4), length2);
def avg9 = ExpAverage(close(period = agperiod5), lengthSCA);
def avg10 = ExpAverage(close(period = agperiod5), length2);
def avg11 = ExpAverage(close(period = agperiod6), lengthSCA);
def avg12 = ExpAverage(close(period = agperiod6), length2);

def height = avg - avg[lengthSCA];
def height2 = avg2 - avg2[length2];
def height3 = avg3 - avg3[lengthSCA];
def height4 = avg4 - avg4[length2];
def height5 = avg5 - avg5[lengthSCA];
def height6 = avg6 - avg6[length2];
def height7 = avg7 - avg7[lengthSCA];
def height8 = avg8 - avg8[length2];
def height9 = avg9 - avg9[lengthSCA];
def height10 = avg10 - avg10[length2];
def height11 = avg11 - avg11[lengthSCA];
def height12 = avg12 - avg12[length2];

def UPSCA = avg > avg2;
def DOWNSCA = avg < avg2;
def UP2SCA = avg3 > avg4;
def DOWN2SCA = avg3 < avg4;
def UP3 = avg5 > avg6;
def DOWN3 = avg5 < avg6;
def UP4 = avg7 > avg8;
def DOWN4 = avg7 < avg8;
def UP5 = avg9 > avg10;
def DOWN5 = avg9 < avg10;
def UP6 = avg11 > avg12;
def DOWN6 = avg11 < avg12;

def R1UP = avg > avg[1];
def R1DN = avg < avg[1];
def R2UP = avg2 > avg2[1];
def R2DN = avg2 < avg2[1];
def R3UP = avg3 > avg3[1];
def R3DN = avg3 < avg3[1];
def R4UP = avg4 > avg4[1];
def R4DN = avg4 < avg4[1];
def R5UP = avg5 > avg5[1];
def R5DN = avg5 < avg5[1];
def R6UP = avg6 > avg6[1];
def R6DN = avg6 < avg6[1];
def R7UP = avg7 > avg7[1];
def R7DN = avg7 < avg7[1];
def R8UP = avg8 > avg8[1];
def R8DN = avg8 < avg8[1];
def R9UP = avg9 > avg9[1];
def R9DN = avg9 < avg9[1];
def R10UP = avg10 > avg10[1];
def R10DN = avg10 < avg10[1];
def R11UP = avg11 > avg11[1];
def R11DN = avg11 < avg11[1];
def R12UP = avg12 > avg12[1];
def R12DN = avg12 < avg12[1];

def Long_Only = UP + UP2 + UP3 + UP4 + UP5 + UP6;
def Short_Only = DOWN + DOWN2 + DOWN3 + DOWN4 + DOWN5 + DOWN6;
def Consensus_Bias = Long_Only - Short_Only;

def RUP = UPC1 + UPC2 + R1UP + R2UP + R3UP + R4UP + R5UP + R6UP + R7UP + R8UP + R9UP + R10UP + R11UP + R12UP;
def RDN = DNC1 + DNC2 + R1DN + R2DN + R3DN + R4DN + R5DN + R6DN + R7DN + R8DN + R9DN + R10DN + R11DN + R12DN;
def ConsensusR = RUP - RDN;

def C3_MFUP = C3_MF_UPAgg1 + C3_MF_UPAgg2 + C3_MF_UPAgg3 + C3_MF_UPAgg4 + C3_MF_UPAgg5 + C3_MF_UPAgg6;
def C3_MFDN = C3_MF_DNAgg1 + C3_MF_DNAgg2 + C3_MF_DNAgg3 + C3_MF_DNAgg4 + C3_MF_DNAgg5 + C3_MF_DNAgg6;

def C3MFConsensusUP = C3_MFUP > C3_MFDN;
def C3MFConsensusDN = C3_MFDN > C3_MFUP;

def MFUP = MF_UP1 and MF_UP2 and MF_UP3 and MF_UP4 and MF_UP5 and MF_UP6;
def MFDN = MF_DN1 and MF_DN2 and MF_DN3 and MF_DN4 and MF_DN5 and MF_DN6;


#input price2 = hl2;
#input FastLimit = 0.5;
#input SlowLimit = 0.05;

#def MAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).MAMA;
#def FAMA = Ehler_MAMA(price2, FastLimit, SlowLimit).FAMA;

#def Crossing = Crosses((MAMA < FAMA), yes);
#Crossing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#def Crossing1 = Crosses((MAMA > FAMA), yes);
#Crossing1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),

if MAMA > FAMA then Color.GREEN else Color.RED);

##################################
#plot C3_MF_Line = (MAMA + FAMA) / 2;
#C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
#C3_MF_Line.SetLineWeight(3);

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;
C3_MF_Line.AssignValueColor(if paintCandles and ((direction == 1) and (price > C3_MF_Line)) then Color.GREEN else if paintCandles and ((direction == -1) and (price < C3_MF_Line)) then Color.RED else Color.DARK_GRAY);

# ================== B4 ==========================

# B4 Indicator
#
# Free for use. Header credits must be included when any form of the code included in this package is used.
# User assumes all risk. Author not responsible for errors or use of tool.
# Copyright (c) 2021 B4 Signals
#
# Get support at: https://b4signals.com
# Join us at: https://discord.gg/kD3pKE2CQd
#
#
# v3.0 - barbros / chuck - official release
# BETA - christoper84    - new squeeze logic
# v3.1 - barbaros        - squeeze integration, added squeeze dots, changed squeeze label text and color


input ShowMarketForecastLabel = yes;        #hint ShowMarketForecastLabel: Show the intermediate Market Forecast label
input ShowMACDBBLabel = yes;                #hint ShowMACDBBLabel: Show the MACDBB based Trend label
input ShowMACDBBCloud = no;                 #hint ShowMACDBBCloud: Show the MACDBB cloud shaded between BB
input ShowRSMCloud = no;                    #hint ShowRSMCloud: Show the vertical cloud based on RSM
input ShowHMALabel = yes;                   #hint ShowHMALabel: Show HUll Moving Average based Divergence label
input ShowSQZSqueezeLabel = yes;            #hint ShowSQZSqueezeLabel: Show Stochastic Scalper based squeeze label
input ShowSQZSqueezeCloud = yes;            #hint ShowSQZSqueezeCloud: Show Stochastic Scalper based squeeze cloud between BB
input ShowBullBearVerticalLines = yes;      #hint ShowBullBearVerticalLines: Show vertical lines for bullish or bearish direction


### Market Forecast



### MACDBB

input MACDBB_FastLength = 12;
input MACDBB_SlowLength = 26;
input MACDBB_BandLength = 15;
input MACDBB_NumDev = 1.0;

def MACDBB_Data = MACD(fastLength = MACDBB_FastLength, slowLength = MACDBB_SlowLength, MACDLength = 5);

def MACDBB_Upper = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).UpperBand;

def MACDBB_Lower = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                             Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).Lowerband;


def MACDBB_Midline = reference BollingerBands(price = MACDBB_Data, length = MACDBB_BandLength,
                                               Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).MidLine;


def MACDBB_Line = MACDBB_Data;








def B4Long =  MACDBB_Line > MACDBB_Line[1] and MACDBB_Line > MACDBB_Upper
    or MACDBB_Line < MACDBB_Line[1] and MACDBB_Line > MACDBB_Upper;

def B4Short =  MACDBB_Line < MACDBB_Line[1] and MACDBB_Line < MACDBB_Lower
    or MACDBB_Line > MACDBB_Line[1] and MACDBB_Line < MACDBB_Lower;
### RSI/STOCASTIC/MACD CONFLUENCE COMBO

def RSM_MACD_Diff = reference MACD("fast length" = 12, "slow length" = 26, "macd length" = 9).Diff;

 

def RSM_MACD_ZeroLine = 0;





### Strategy
### Alerts

#Alert(Strategy_BuySignal, "Long Entry", Alert.BAR, Sound.DING);
#Alert(Strategy_SellSignal, "Short Entry", Alert.BAR, Sound.DING);
#Alert(MACDBB_Line crosses above MACDBB_CrossFromAboveVal, "MACDBB Crossed Up", Alert.BAR, Sound.DING);
#Alert(MACDBB_Line crosses below MACDBB_CrossFromBelowVal, "MACDBB Crossed Down", Alert.BAR, Sound.DING);
#Alert(SQZ_squeeze_signal, "Squeeze Alert", Alert.BAR, Sound.DING);

# ================================================================

plot buySCA = long_Only > Short_Only and RUP > RDN and  C3_MF_UPAgg1 and C3_MF_UPAgg2 and C3_MF_UPAgg3 and C3_MF_UPAgg4 and C3_MF_UPAgg5 and MaxTimeUp and MFUP > MFDN and ((UP6 == 1) and (Consensus_Bias > 0) and B4long ); #AvgExp crosses above AvgExp2 and RUP and MaxTimeUp and long_Only and C3_MF_UPAgg1 and C3_MF_UPAgg2 and C3_MF_UPAgg3 and C3_MF_UPAgg4 and C3_MF_UPAgg5 ; # and Mf_UP1 and MF_UP2 and MF_UP3 and MF_UP4 and MF_UP5 and MF_UP6 and((UP6 == 1) and (Consensus_Bias > 0)) and maxTimeUp ;#highest time expup #direction crosses above 0;
buySCA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySCA.SetDefaultColor(createColor(56,223,153));
buySCA.SetLineWeight(3);

plot sellSCA = long_Only < Short_Only and RUP < RDN and C3_MF_DNAgg1 and C3_MF_DNAgg2 and C3_MF_DNAgg3 and C3_MF_DNAgg4 and C3_MF_DNAgg5 and MaxTimeDown and MFUP < MFDN and ((DOWN6) and (Consensus_Bias < 0) and B4short);
 #AvgExp crosses below AvgExp2 and RDN and MaxTimeDown and Short_Only and C3_MF_DNAgg1 and C3_MF_DNAgg2 and C3_MF_DNAgg3 and C3_MF_DNAgg4 and C3_MF_DNAgg5;# and MF_DN1 and MF_DN2 and MF_DN3 and MF_DN4 and MF_DN5 and MF_DN6 and ((DOWN6) and (Consensus_Bias < 0));  #HighestTime frame EMA down #direction crosses below 0;
sellSCA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN );
sellSCA.SetDefaultColor(createColor(165,51,51));
sellSCA.SetLineWeight(3);
AssignPriceColor(if paintCandles then if direction == 1 then createColor(13,165,112) else if direction == -1 then createColor(164,4,30) else Color.GRAY else Color.CURRENT);

# ============ MAX LABELS ================================
#AddLabel(yes, if conditionLTB then "BULLISH_LOOK_To_BUY" else if conditionLTS then "BEARISH_LOOK_TO_SELL" else if conditionK2UP then "TREND_BULLISH" else if conditionK3DN then "TREND_BEARISH" else "TREND_CONSOLIDATION", if conditionLTB then Color.GREEN else if conditionLTS then Color.RED else if conditionK2UP then Color.GREEN else if conditionK3DN then Color.DARK_GRAY else Color.GRAY);

#AddLabel(yes, if conditionBD then "BREAKDOWN" else if conditionBO then "BREAKOUT" else "NO_BREAK", if conditionBD then Color.RED else if conditionBO then Color.GREEN else Color.GRAY);

#AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.DARK_ORANGE else if (Spark == 2) then Color.GREEN else if (Spark == 0) then Color.RED else Color.GRAY);

#AddLabel(yes, "SQUEEZE ALERT", if Squeeze_Signal then Color.DARK_ORANGE else Color.GRAY);

#AddLabel(yes, if MomentumUP then "Consensus_Increasing = " + Round(Consensus_Level, 1) else if MomentumUP or MomentumDOWN and conditionOB then "Consensus_OVERBOUGHT = " + Round(Consensus_Level, 1) else if MomentumDOWN then  "Consensus_Decreasing = " + Round(Consensus_Level, 1) else if MomentumUP or MomentumDOWN and conditionOS then "Consensus_OVERSOLD = " + Round(Consensus_Level, 1) else "Consensus = " + Round(Consensus_Level, 1), if conditionOB then Color.RED else if conditionOS then Color.GREEN else Color.GRAY);

# ==========================================================


AddLabel(yes, if ((UP6 == 1) and (Consensus_Bias > 0)) then " SCALP_LONG " else if ((DOWN6) and (Consensus_Bias < 0)) then " SCALP_SHORT " else " CHOP ", if ((Consensus_Bias > 0) and (UP6 == 1)) then Color.GREEN else if ((Consensus_Bias < 0) and (DOWN6 == 1)) then Color.RED else Color.GRAY);

AddLabel(yes, if conditionLTB then "BULLISH_LOOK_To_BUY" else if conditionLTS then "BEARISH_LOOK_TO_SELL" else if conditionK2UP then "TREND_BULLISH" else if conditionK3DN then "TREND_BEARISH" else "TREND_CONSOLIDATION", if conditionLTB then Color.GREEN else if conditionLTS then Color.RED else if conditionK2UP then Color.GREEN else if conditionK3DN then Color.DARK_GRAY else Color.GRAY);

AddLabel(yes, if conditionBD then "BREAKDOWN" else if conditionBO then "BREAKOUT" else "NO_BREAK", if conditionBD then Color.RED else if conditionBO then Color.GREEN else Color.GRAY);

AddLabel(yes, if (Spark == 3) then "SPARK UP = " + Round(Spark, 1) else if (Spark == 0) then  "SPARK DOWN = " + Round(Spark, 1) else "SPARK = " + Round(Spark, 1), if (Spark == 3) then Color.DARK_ORANGE else if (Spark == 2) then Color.GREEN else if (Spark == 0) then Color.RED else Color.GRAY);

AddLabel(yes, "SQUEEZE ALERT", if Squeeze_Signal then Color.DARK_ORANGE else Color.GRAY);

AddLabel(yes, if MomentumUP then "Consensus_Increasing = " + Round(Consensus_Level, 1) else if MomentumUP or MomentumDOWN and conditionOB then "Consensus_OVERBOUGHT = " + Round(Consensus_Level, 1) else if MomentumDOWN then  "Consensus_Decreasing = " + Round(Consensus_Level, 1) else if MomentumUP or MomentumDOWN and conditionOS then "Consensus_OVERSOLD = " + Round(Consensus_Level, 1) else "Consensus = " + Round(Consensus_Level, 1), if conditionOB then Color.RED else if conditionOS then Color.GREEN else Color.GRAY);



#AddLabel(yes, if (ConsensusR > 0) then " LONG BIAS = %" + Round((ConsensusR / 14) * 100, 1) + " " else if (ConsensusR < 0) then  " SHORT BIAS = %" + Round(((ConsensusR * -1) / 14) * 100, 1) + " " else " CHOP =" + Round((ConsensusR / 14) * 100, 1) + " ", if (ConsensusR > 0) then Color.GREEN else if (ConsensusR < 0) then Color.RED else Color.GRAY);

Alert(yes,long_Only > Short_Only and RUP > RDN and  C3_MF_UPAgg1 and C3_MF_UPAgg2 and C3_MF_UPAgg3 and C3_MF_UPAgg4 and C3_MF_UPAgg5 and MaxTimeUp and MFUP > MFDN and ((UP6 == 1) and (Consensus_Bias > 0)), Alert.BAR, Sound.Ding);
Alert(yes, long_Only < Short_Only and RUP < RDN and C3_MF_DNAgg1 and C3_MF_DNAgg2 and C3_MF_DNAgg3 and C3_MF_DNAgg4 and C3_MF_DNAgg5 and MaxTimeDown and MFUP < MFDN and ((DOWN6) and (Consensus_Bias < 0)), Alert.BAR, Sound.Chimes);
Very Interesting, is there a video on this?
 
Hi @Christopher84
I watched the YT vid tonight, I've read every thread entry (took around 18 hours :)), taken notes, screenshots, code saves, just Fantastic stuff you are sharing, so I wanted to give major THANK YOU to you!!!

Two questions,
1. Can you talk about the difference between the Wide band style colored S/R cloud found in C3_Max vs the Line style colored S/R found in C3_v6 please & CC_v10 and maybe others? Is the wide band giving you more S/R price range history information and the line type is the average of that range possibly?
Also, in C3_Max, with the wide colored band S/R there are also additional yellow and green horizontal lines, are those pre-market or opening day S/R lines?
Thanks.
 
Last edited:
The color of the candle and the color of the cloud aren't really what I would be looking at. I am looking at the color of the candle with the color of the channel. The channel gives you insight to the trend environment (Red Channel downtrend, Green Channel uptrend). A red candle in a green channel potentially represents weakness in a strong trend and a potential buying opportunity (in general these are the safest entry points). Conversely, a green candle in a red channel potentially represents strength in a weak trend and a potential selling opportunity. Buying in a red channel is aggressive and inherently more risky. The clouds are meant to show short term reversal potential. They can serve as an indication to take profit and reduce risk or to buy the dip. The charts in your image look like aggressive entry positions in a downtrend which can workout well depending your risk tolerance and time horizon. Definitely give the most attention to the candle signals to get in or out of trades. Be cautious of entering trades in red channels unless you are playing the mean reversion or you are shorting. Also, it looks like you may be using an older version of the indicator, I would highly suggest getting the newest version on page 1 of this thread. Thank you for testing it out! Hope this helps!
Hi @Christopher84 , I am very intrigued with these set of indicators and have been using the C3 indicator for the past couple of days. I have been reading this topic (not completed yet). I believe there was a mention to use the 10 min timeframe for scalping and that is what I am starting off with to use the C3 indicator. I understand your strategy for entering and exiting trades from your above explanation.
What about entering a short trade when there is a red candle with a red down arrow in a red channel? And vice versa?
Also, what if there is a red candle with a red down arrow but in a green channel?
Thanks.
 
Last edited:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
334 Online
Create Post

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