Confirmation Candles Indicator For ThinkorSwim

Hi Everyone,

What happens if the lower EMAD gives a different signal on different time frames, like it shows differently on the 2-minute and 5-minute chart? Do you just wait to trade until the two signals are in sync?

Edited

The following are indications one will notice overtime using the various indicators in this thread. It is not a rules based system.

If you have trouble understanding all the various indicators THEN JUST GIVE UP! Joking people! Just a joke! But seriously if that is the case and you like to trade short term 1-3min charts just focus on the Bull Bear V5 candles and watch the other indications until you understand them. (You could turn everything off except for Bull Bear V5 candles and Spark Up/Down arrows on only and do well ((I would also add triple exhaustion))

* End edit *


This is generally my method (note: I use a watchlist column for high relative volume with only high liquidity tickers like AAPL TSLA SPY etc):

1. Determine the direction of the hourly trend (before market open) using one or more of the following indications:
A. C3 MF Line color
B. Spark Up / Down arrow
C. EMAD EMA color
D. PLD crossover
E. Price wrapping

- Trending up - take long trades only
- Trending down - take short trades only

** Exceptions to step 1 **

  • Triple Exhaustion indication (dots)
  • Significant levels (supply or demand zone with yellow or light green line inside the cloud of respective zone)if a supply zone with a yellow line inside the zone is acting as support in a bullish trend then the zone moves up and is now used as resistance —> wait for a breakdown or up from this level as it may go sideways at said level

Note
If your using one of the hourly indications that signal the trend change later than the others then be patient and wait for a good entry… if using an indication that alerts trend change rather quickly that goes against the slower indicator (like the upper EMA cloud) that is fine just know that it will likely be a short lived pullback as opposed to a full trend reversal.


2. Determine wether or not the 1Min - 3Min chart agrees with the hourly trend direction (look at both timeframes)

- If the hourly is bearish I’m waiting for a move in the opposite direction to reject the supply zone or for Triple Exhaustion indication and/or bull bear V5 candles turn from green to red to buy puts (make sure you do not trade opposite a zone that is wrapping)

- If the hourly is bearish and 1/3min trends agree I will buy puts if one or more of the following is true:

A. Candles wrapping at open
B. Major zone rejection premarket (near open)
C. PLD crossover reject before open (wicks)
D. Bull bear V5 candles red
E. C3 Line red

3. Exit trade when one of the following occurs:

- Triple Exhaustion indication (usually good to use as a “risk off” point (bought 5 contracts sell 4 let one ride if the trend is not broken)
- Supply / Demand zone moving horizontal (or hold until candle breaks out of the zone)
- Candles move above a zone (depending on price action)
- If price is moving towards a significant zone (zone with yellow or light green line inside) I will expect a bounce and risk off accordingly also if the lower line of the EMAD is stepping up while price is going down I will expect a bounce

** The triple exhaustion in particular shines on strong price action for high liquidity underlyings like TSLA NVDA etc. also I look for the indication on other timeframes as well but works best used on the 1-3min I find **

Test first trade later.


 
Last edited:

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

Hi Everyone,

What happens if the lower EMAD gives a different signal on different time frames, like it shows differently on the 2-minute and 5-minute chart? Do you just wait to trade until the two signals are in sync?
Hi @MichaelR,
Looking for agreement between timeframes is definitely a good idea. I typically use the 1 hour chart with TS_v9 strategy and the Bull Bear Candles and TS_v9 on the 5 min chart https://tos.mx/YiKSDE8 and look for agreement in the strategies. It has been working very well for me. Using the 1 hour chart helps to avoid choppy conditions. You could take a similar approach using EMAD, but I would suggest using a higher timeframe than the 5 min chart to help avoid choppy trade. It's also extremely important to test the strategy before trading it live. I hope this helps!
fORzZL3.png
 
Edited

The following are indications one will notice overtime using the various indicators in this thread. It is not a rules based system.

If you have trouble understanding all the various indicators THEN JUST GIVE UP! Joking people! Just a joke! But seriously if that is the case and you like to trade short term 1-3min charts just focus on the Bull Bear V5 candles and watch the other indications until you understand them. (You could turn everything off except for Bull Bear V5 candles and Spark Up/Down arrows on only and do well ((I would also add triple exhaustion))

* End edit *


This is generally my method (note: I use a watchlist column for high relative volume with only high liquidity tickers like AAPL TSLA SPY etc):

1. Determine the direction of the hourly trend (before market open) using one or more of the following indications:
A. C3 MF Line color
B. Spark Up / Down arrow
C. EMAD EMA color
D. PLD crossover
E. Price wrapping

- Trending up - take long trades only
- Trending down - take short trades only

** Exceptions to step 1 **

  • Triple Exhaustion indication (dots)
  • Significant levels (supply or demand zone with yellow or light green line inside the cloud of respective zone)if a supply zone with a yellow line inside the zone is acting as support in a bullish trend then the zone moves up and is now used as resistance —> wait for a breakdown or up from this level as it may go sideways at said level

Note
If your using one of the hourly indications that signal the trend change later than the others then be patient and wait for a good entry… if using an indication that alerts trend change rather quickly that goes against the slower indicator (like the upper EMA cloud) that is fine just know that it will likely be a short lived pullback as opposed to a full trend reversal.


2. Determine wether or not the 1Min - 3Min chart agrees with the hourly trend direction (look at both timeframes)

- If the hourly is bearish I’m waiting for a move in the opposite direction to reject the supply zone or for Triple Exhaustion indication and/or bull bear V5 candles turn from green to red to buy puts (make sure you do not trade opposite a zone that is wrapping)

- If the hourly is bearish and 1/3min trends agree I will buy puts if one or more of the following is true:

A. Candles wrapping at open
B. Major zone rejection premarket (near open)
C. PLD crossover reject before open (wicks)
D. Bull bear V5 candles red
E. C3 Line red

3. Exit trade when one of the following occurs:

- Triple Exhaustion indication (usually good to use as a “risk off” point (bought 5 contracts sell 4 let one ride if the trend is not broken)
- Supply / Demand zone moving horizontal (or hold until candle breaks out of the zone)
- Candles move above a zone (depending on price action)
- If price is moving towards a significant zone (zone with yellow or light green line inside) I will expect a bounce and risk off accordingly also if the lower line of the EMAD is stepping up while price is going down I will expect a bounce

** The triple exhaustion in particular shines on strong price action for high liquidity underlyings like TSLA NVDA etc. also I look for the indication on other timeframes as well but works best used on the 1-3min I find **

Test first trade later.


Do you mind sharing the Triple Exhaustion Indicator you are using?
 
@Christopher84 If you have the time could you look at this code for breakouts... it works like this: if the wick close or body of a candle cross a level then do the opposite within x bars an arrow is plotted.... which works fine... however my goal would be to have far less arrows so I would like to accomplish an arrow to plot if 3 different crosses (that are already in the code below) happen within x number of bars... I cannot seem to make it work as I do not fully understand the code. Thanks.


Code:
# def support = ... some price level                                       
                                                                                                        
def Y_Line_OS = YHextlineOS;                                                                                                       
def LG_Line_OB = YHextlineOB;                                                                                                       
                                        
def High_BH1_Red = H_BH1EXTLINE;                                                                                                       
def Low_BL1_Red = L_BL1EXTLINE;                                                                                                       
                                        
def High_BH2_Grn = H_BH2EXTLINE;                                                                                                       
def Low_BL2_Grn = L_BL2EXTLINE;                                                                                                       
                                        
############# INPUT FOR TESTING ONLY ##############                                                                                                       
                                        
input support_level = 50;                                                                                                       
                                        
########### CONVERT CONSTANT TO VAR ############                                                                                                                                                               
                                        
input show_ref_line = no;                                                                                                       
                                        
# plot y = if show_ref_line then support else na_1;                                                                                                                                                       
                                        
########### CHOOSE WICK, BODY, or CLOSE ###########                                                                                                       
                                        
input OB_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_OB;                                                                                                       
def Low_OB;                                                                                                       
switch (OB_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_OB = high;                                                                                                       
    Low_OB = low;                                                                                                       
case "body":                                                                                                       
    High_OB = Max(open, close);                                                                                                       
    Low_OB = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_OB = close;                                                                                                       
    Low_OB = close;                                                                                                       
}                                                                                                       
                                        
input OS_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_OS;                                                                                                       
def Low_OS;                                                                                                       
switch (OS_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_OS = high;                                                                                                       
    Low_OS = low;                                                                                                       
case "body":                                                                                                       
    High_OS = Max(open, close);                                                                                                       
    Low_OS = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_OS = close;                                                                                                       
    Low_OS = close;                                                                                                       
}                                                                                                       
                                        
input H_BH1_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_H_BH1;                                                                                                       
def Low_H_BH1;                                                                                                       
switch (H_BH1_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_H_BH1 = high;                                                                                                       
    Low_H_BH1 = low;                                                                                                       
case "body":                                                                                                       
    High_H_BH1 = Max(open, close);                                                                                                       
    Low_H_BH1 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_H_BH1 = close;                                                                                                       
    Low_H_BH1 = close;                                                                                                       
}                                                                                                       
                                        
input H_BH2_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_H_BH2;                                                                                                       
def Low_H_BH2;                                                                                                       
switch (H_BH2_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_H_BH2 = high;                                                                                                       
    Low_H_BH2 = low;                                                                                                       
case "body":                                                                                                       
    High_H_BH2 = Max(open, close);                                                                                                       
    Low_H_BH2 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_H_BH2 = close;                                                                                                       
    Low_H_BH2 = close;                                                                                                       
}                                                                                                       
                                        
input L_BL1_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_L_BL1;                                                                                                       
def Low_L_BL1;                                                                                                       
switch (L_BL1_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_L_BL1 = high;                                                                                                       
    Low_L_BL1 = low;                                                                                                       
case "body":                                                                                                       
    High_L_BL1 = Max(open, close);                                                                                                       
    Low_L_BL1 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_L_BL1 = close;                                                                                                       
    Low_L_BL1 = close;                                                                                                       
}                                                                                                       
                                        
input L_BL2_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_L_BL2;                                                                                                       
def Low_L_BL2;                                                                                                       
switch (L_BL2_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_L_BL2 = high;                                                                                                       
    Low_L_BL2 = low;                                                                                                       
case "body":                                                                                                       
    High_L_BL2 = Max(open, close);                                                                                                       
    Low_L_BL2 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_L_BL2 = close;                                                                                                       
    Low_L_BL2 = close;                                                                                                       
}                                                                                                       
                                        
########### LABELS ############                                                                                       
                                        
#AddLabel(1, "Crossing Levels OS " + OS_Candle , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Bars Back OS" + bars_back_OS , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Crossing Levels OB " + OB_Candle , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Bars Back OB " + bars_back_OB , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Crossing Levels H_BH1 " + H_BH1_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back H_BH1 " + bars_back_H_BH1, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels H_BH2 " + H_BH2_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back H_BH2 " + bars_back_H_BH2, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels L_BL1 " + L_BL1_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back L_BL1 " + bars_back_L_BL1, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels L_BL2 " + L_BL2_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back L_BL2 " + bars_back_L_BL2, Color.YELLOW);                                               
                                                                                
########## CROSSING UP / DOWN ###########                                                                                               
                                        
def OS_Down_Bn = if bn == 1 then 0 else if High_OS[1] > Y_Line_OS[1] and Low_OS < Y_Line_OS then bn else OS_Down_Bn[1];                                                                                                       
def OS_Up_Bn = if bn == 1 then 0 else if Low_OS[1] < Y_Line_OS[1] and High_OS > Y_Line_OS then bn else OS_Up_Bn[1];                                                                                                       
                                        
def OB_Down_Bn = if bn == 1 then 0 else if High_OB[1] > LG_Line_OB[1] and Low_OB < LG_Line_OB then bn else OB_Down_Bn[1];                                                                                                       
def OB_Up_Bn = if bn == 1 then 0 else if Low_OB[1] < LG_Line_OB[1] and High_OB > LG_Line_OB then bn else OB_Up_Bn[1];                                                                                                       
                                        
def H_BH1_Down_Bn = if bn == 1 then 0 else if High_H_BH1[1] > High_BH1_Red[1] and Low_H_BH1 < High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                       
def H_BH1_Up_Bn = if bn == 1 then 0 else if Low_H_BH1[1] < High_BH1_Red[1] and High_H_BH1 > High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                       
                                        
def H_BH2_Down_Bn = if bn == 1 then 0 else if High_H_BH2[1] > High_BH2_Grn[1] and Low_H_BH2 < High_BH2_Grn then bn else H_BH2_Down_Bn[1];                                                                                       
def H_BH2_Up_Bn = if bn == 1 then 0 else if Low_H_BH2[1] < High_BH2_Grn[1] and High_H_BH2 > High_BH2_Grn then bn else H_BH2_Up_Bn[1];                                                                                                       
                                        
def L_BL1_Down_Bn = if bn == 1 then 0 else if High_L_BL1[1] > Low_BL1_Red[1] and Low_L_BL1 < Low_BL1_Red then bn else L_BL1_Down_Bn[1];                                                                                                       
def L_BL1_Up_Bn = if bn == 1 then 0 else if Low_L_BL1[1] < Low_BL1_Red[1] and High_L_BL1 > Low_BL1_Red then bn else L_BL1_Up_Bn[1];                                                                                                       
                                        
def L_BL2_Down_Bn = if bn == 1 then 0 else if High_L_BL2[1] > Low_BL2_Grn[1] and Low_L_BL2 < Low_BL2_Grn then bn else L_BL2_Down_Bn[1];                                                                                                       
def L_BL2_Up_Bn = if bn == 1 then 0 else if Low_L_BL2[1] < Low_BL2_Grn[1] and High_L_BL2 > Low_BL2_Grn then bn else L_BL2_Up_Bn[1];                                                                                                       
                                        
########### GETTIN CROSSED-UP ###########                                                                                                       
                                        
# dip then rise, within len bars                                                                                                        #TEST#
                                        
def OS_Up_0 = if                                                                                                       
bn > bars_back_OS                                                                                                     
and                                                                                                       
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OS_Down_0 = if                                                                                                       
bn > bars_back_OS                                                                                                       
and                                                                                                       
( (OS_Down_Bn - OS_Up_Bn) <= bars_back_OS and (OS_Down_Bn - OS_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Down_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OB_Up_0 = if                                                                                                       
bn > bars_back_OS                                                                                                       
and                                                                                                       
( (OB_Up_Bn - OB_Down_Bn) <= bars_back_OB and (OB_Up_Bn - OB_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OB_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OB_Up_0[1], (bars_back_OB - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OB_Down_0 = if                                                                                                       
bn > bars_back_OB                                                                                                       
and                                                                                                       
( (OB_Down_Bn - OB_Up_Bn) <= bars_back_OB and (OB_Down_Bn - OB_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
OB_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(OB_Down_0[1], (bars_back_OB - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH1_Up_0 = if                                                                                                       
bn > bars_back_H_BH1                                                                                                       
and                                                                                                       
( (H_BH1_Up_Bn - H_BH1_Down_Bn) <= bars_back_H_BH1 and (H_BH1_Up_Bn - H_BH1_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH1_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH1_Up_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                     
then 1 else 0;                                                                                                       
                                        
def H_BH1_Down_0 = if                                                                                                       
bn > bars_back_H_BH1                                                                                                       
and                                                                                                       
( (H_BH1_Down_Bn - H_BH1_Up_Bn) <= bars_back_H_BH1 and (H_BH1_Down_Bn - H_BH1_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH1_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH1_Down_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL1_Up_0 = if                                                                                                       
bn > bars_back_L_BL1                                                                                                     
and                                                                                                       
( (L_BL1_Up_Bn - L_BL1_Down_Bn) <= bars_back_L_BL1 and (L_BL1_Up_Bn - L_BL1_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL1_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL1_Up_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL1_Down_0 = if                                                                                                       
bn > bars_back_L_BL1                                                                                                       
and                                                                                                       
( (L_BL1_Down_Bn - L_BL1_Up_Bn) <= bars_back_L_BL1 and (L_BL1_Down_Bn - L_BL1_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL1_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL1_Down_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH2_Up_0 = if                                                                                                       
bn > bars_back_H_BH2                                                                                                       
and                                                                                                       
( (H_BH2_Up_Bn - H_BH2_Down_Bn) <= bars_back_H_BH2 and (H_BH2_Up_Bn - H_BH2_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH2_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH2_Up_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH2_Down_0 = if                                                                                                       
bn > bars_back_H_BH2                                                                                                       
and                                                                                                       
( (H_BH2_Down_Bn - H_BH2_Up_Bn) <= bars_back_H_BH2 and (H_BH2_Down_Bn - H_BH2_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH2_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH2_Down_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL2_Up_0 = if                                                                                                       
bn > bars_back_L_BL2                                                                                                       
and                                                                                                       
( (L_BL2_Up_Bn - L_BL2_Down_Bn) <= bars_back_L_BL2 and (L_BL2_Up_Bn - L_BL2_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL2_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL2_Up_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                   
                                        
def L_BL2_Down_0 = if                                                                                                       
bn > bars_back_L_BL2                                                                                                       
and                                                                                                       
( (L_BL2_Down_Bn - L_BL2_Up_Bn) <= bars_back_L_BL2 and (L_BL2_Down_Bn - L_BL2_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL2_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL2_Down_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                       
then 1 else 0;

############## --- CROSSOVER TEST(s) --- ###############

def OS_Up_2 = if                                                                                                       
bn > bars_back_OS                                                                                                     
and                                                                                                       
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
and                                                                                                                                                                                                       ( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                   

########### ARROWS ###########                                                                                                       
                                        
input arrow_size = 1;     

plot OS_Up = OS_Up_0;                                                                                                       
OS_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
OS_Up.SetDefaultColor(Color.White);                                                                                                       
OS_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OS_Down = OS_Down_0;                                                                                                       
OS_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
OS_Down.SetDefaultColor(Color.White);                                                                                                       
OS_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OB_Up = OB_Up_0;                                                                                                       
OB_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
OB_Up.SetDefaultColor(Color.White);                                                                                                       
OB_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OB_Down = OB_Down_0;                                                                                                       
OB_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
OB_Down.SetDefaultColor(Color.White);                                                                                                       
OB_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH1_Up = H_BH1_Up_0;                                                                                                       
H_BH1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
H_BH1_Up.SetDefaultColor(Color.White);                                                                                                       
H_BH1_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH1_Down = H_BH1_Down_0;                                                                                                       
H_BH1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
H_BH1_Down.SetDefaultColor(Color.White);                                                                                                       
H_BH1_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL1_Up = L_BL1_Up_0;                                                                                                       
L_BL1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
L_BL1_Up.SetDefaultColor(Color.White);                                                                                                       
L_BL1_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL1_Down = L_BL1_Down_0;                                                                                                       
L_BL1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
L_BL1_Down.SetDefaultColor(Color.White);                                                                                                       
L_BL1_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH2_Up = H_BH2_Up_0;                                                                                                       
H_BH2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
H_BH2_Up.SetDefaultColor(Color.White);                                                                                                       
H_BH2_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH2_Down = H_BH1_Down_0;                                                                                                       
H_BH2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
H_BH2_Down.SetDefaultColor(Color.BLUE);                                                                                                       
H_BH2_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL2_Up = L_BL2_Up_0;                                                                                                       
L_BL2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
L_BL2_Up.SetDefaultColor(Color.LIGHT_GREEN);                                                                                                       
L_BL2_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL2_Down = L_BL2_Down_0;                                                                                                       
L_BL2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
L_BL2_Down.SetDefaultColor(Color.WHITE);                                                                                                       
L_BL2_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
                                        
########### TEST VARIABLES ###########                                                                                                       
                                        
input test_crossing_bubbles = no;                                                                                                       
                                        
AddChartBubble(test_crossing_bubbles and OS_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and OS_Up_Bn == bn, low, bn, Color.GREEN, yes);                                                                                                       
                                        
AddChartBubble(test_crossing_bubbles and OB_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and OB_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and H_BH1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and H_BH1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and H_BH2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and H_BH2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and L_BL1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and L_BL1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and L_BL2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and L_BL2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
##################################################################################
 
@Christopher84 If you have the time could you look at this code for breakouts... it works like this: if the wick close or body of a candle cross a level then do the opposite within x bars an arrow is plotted.... which works fine... however my goal would be to have far less arrows so I would like to accomplish an arrow to plot if 3 different crosses (that are already in the code below) happen within x number of bars... I cannot seem to make it work as I do not fully understand the code. Thanks.


Code:
# def support = ... some price level                                      
                                                                                                       
def Y_Line_OS = YHextlineOS;                                                                                                      
def LG_Line_OB = YHextlineOB;                                                                                                      
                                       
def High_BH1_Red = H_BH1EXTLINE;                                                                                                      
def Low_BL1_Red = L_BL1EXTLINE;                                                                                                      
                                       
def High_BH2_Grn = H_BH2EXTLINE;                                                                                                      
def Low_BL2_Grn = L_BL2EXTLINE;                                                                                                      
                                       
############# INPUT FOR TESTING ONLY ##############                                                                                                      
                                       
input support_level = 50;                                                                                                      
                                       
########### CONVERT CONSTANT TO VAR ############                                                                                                                                                              
                                       
input show_ref_line = no;                                                                                                      
                                       
# plot y = if show_ref_line then support else na_1;                                                                                                                                                      
                                       
########### CHOOSE WICK, BODY, or CLOSE ###########                                                                                                      
                                       
input OB_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_OB;                                                                                                      
def Low_OB;                                                                                                      
switch (OB_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_OB = high;                                                                                                      
    Low_OB = low;                                                                                                      
case "body":                                                                                                      
    High_OB = Max(open, close);                                                                                                      
    Low_OB = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_OB = close;                                                                                                      
    Low_OB = close;                                                                                                      
}                                                                                                      
                                       
input OS_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_OS;                                                                                                      
def Low_OS;                                                                                                      
switch (OS_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_OS = high;                                                                                                      
    Low_OS = low;                                                                                                      
case "body":                                                                                                      
    High_OS = Max(open, close);                                                                                                      
    Low_OS = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_OS = close;                                                                                                      
    Low_OS = close;                                                                                                      
}                                                                                                      
                                       
input H_BH1_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_H_BH1;                                                                                                      
def Low_H_BH1;                                                                                                      
switch (H_BH1_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_H_BH1 = high;                                                                                                      
    Low_H_BH1 = low;                                                                                                      
case "body":                                                                                                      
    High_H_BH1 = Max(open, close);                                                                                                      
    Low_H_BH1 = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_H_BH1 = close;                                                                                                      
    Low_H_BH1 = close;                                                                                                      
}                                                                                                      
                                       
input H_BH2_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_H_BH2;                                                                                                      
def Low_H_BH2;                                                                                                      
switch (H_BH2_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_H_BH2 = high;                                                                                                      
    Low_H_BH2 = low;                                                                                                      
case "body":                                                                                                      
    High_H_BH2 = Max(open, close);                                                                                                      
    Low_H_BH2 = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_H_BH2 = close;                                                                                                      
    Low_H_BH2 = close;                                                                                                      
}                                                                                                      
                                       
input L_BL1_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_L_BL1;                                                                                                      
def Low_L_BL1;                                                                                                      
switch (L_BL1_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_L_BL1 = high;                                                                                                      
    Low_L_BL1 = low;                                                                                                      
case "body":                                                                                                      
    High_L_BL1 = Max(open, close);                                                                                                      
    Low_L_BL1 = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_L_BL1 = close;                                                                                                      
    Low_L_BL1 = close;                                                                                                      
}                                                                                                      
                                       
input L_BL2_Candle = {default "wick" , "body" , "close"};                                                                                                      
def High_L_BL2;                                                                                                      
def Low_L_BL2;                                                                                                      
switch (L_BL2_Candle) {                                                                                                      
case "wick":                                                                                                      
    High_L_BL2 = high;                                                                                                      
    Low_L_BL2 = low;                                                                                                      
case "body":                                                                                                      
    High_L_BL2 = Max(open, close);                                                                                                      
    Low_L_BL2 = Min(open, close);                                                                                                      
case "close":                                                                                                      
    High_L_BL2 = close;                                                                                                      
    Low_L_BL2 = close;                                                                                                      
}                                                                                                      
                                       
########### LABELS ############                                                                                      
                                       
#AddLabel(1, "Crossing Levels OS " + OS_Candle , Color.YELLOW);                                      
                                                                                                       
#AddLabel(1, "Bars Back OS" + bars_back_OS , Color.YELLOW);                                      
                                                                                                       
#AddLabel(1, "Crossing Levels OB " + OB_Candle , Color.YELLOW);                                      
                                                                                                       
#AddLabel(1, "Bars Back OB " + bars_back_OB , Color.YELLOW);                                              
                                                                                               
#AddLabel(1, "Crossing Levels H_BH1 " + H_BH1_Candle , Color.YELLOW);                                              
                                                                                               
#AddLabel(1, "Bars Back H_BH1 " + bars_back_H_BH1, Color.YELLOW);                                              
                                       
#AddLabel(1, "Crossing Levels H_BH2 " + H_BH2_Candle , Color.YELLOW);                                              
                                                                                               
#AddLabel(1, "Bars Back H_BH2 " + bars_back_H_BH2, Color.YELLOW);                                              
                                       
#AddLabel(1, "Crossing Levels L_BL1 " + L_BL1_Candle , Color.YELLOW);                                              
                                                                                               
#AddLabel(1, "Bars Back L_BL1 " + bars_back_L_BL1, Color.YELLOW);                                              
                                       
#AddLabel(1, "Crossing Levels L_BL2 " + L_BL2_Candle , Color.YELLOW);                                              
                                                                                               
#AddLabel(1, "Bars Back L_BL2 " + bars_back_L_BL2, Color.YELLOW);                                              
                                                                               
########## CROSSING UP / DOWN ###########                                                                                              
                                       
def OS_Down_Bn = if bn == 1 then 0 else if High_OS[1] > Y_Line_OS[1] and Low_OS < Y_Line_OS then bn else OS_Down_Bn[1];                                                                                                      
def OS_Up_Bn = if bn == 1 then 0 else if Low_OS[1] < Y_Line_OS[1] and High_OS > Y_Line_OS then bn else OS_Up_Bn[1];                                                                                                      
                                       
def OB_Down_Bn = if bn == 1 then 0 else if High_OB[1] > LG_Line_OB[1] and Low_OB < LG_Line_OB then bn else OB_Down_Bn[1];                                                                                                      
def OB_Up_Bn = if bn == 1 then 0 else if Low_OB[1] < LG_Line_OB[1] and High_OB > LG_Line_OB then bn else OB_Up_Bn[1];                                                                                                      
                                       
def H_BH1_Down_Bn = if bn == 1 then 0 else if High_H_BH1[1] > High_BH1_Red[1] and Low_H_BH1 < High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                      
def H_BH1_Up_Bn = if bn == 1 then 0 else if Low_H_BH1[1] < High_BH1_Red[1] and High_H_BH1 > High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                      
                                       
def H_BH2_Down_Bn = if bn == 1 then 0 else if High_H_BH2[1] > High_BH2_Grn[1] and Low_H_BH2 < High_BH2_Grn then bn else H_BH2_Down_Bn[1];                                                                                      
def H_BH2_Up_Bn = if bn == 1 then 0 else if Low_H_BH2[1] < High_BH2_Grn[1] and High_H_BH2 > High_BH2_Grn then bn else H_BH2_Up_Bn[1];                                                                                                      
                                       
def L_BL1_Down_Bn = if bn == 1 then 0 else if High_L_BL1[1] > Low_BL1_Red[1] and Low_L_BL1 < Low_BL1_Red then bn else L_BL1_Down_Bn[1];                                                                                                      
def L_BL1_Up_Bn = if bn == 1 then 0 else if Low_L_BL1[1] < Low_BL1_Red[1] and High_L_BL1 > Low_BL1_Red then bn else L_BL1_Up_Bn[1];                                                                                                      
                                       
def L_BL2_Down_Bn = if bn == 1 then 0 else if High_L_BL2[1] > Low_BL2_Grn[1] and Low_L_BL2 < Low_BL2_Grn then bn else L_BL2_Down_Bn[1];                                                                                                      
def L_BL2_Up_Bn = if bn == 1 then 0 else if Low_L_BL2[1] < Low_BL2_Grn[1] and High_L_BL2 > Low_BL2_Grn then bn else L_BL2_Up_Bn[1];                                                                                                      
                                       
########### GETTIN CROSSED-UP ###########                                                                                                      
                                       
# dip then rise, within len bars                                                                                                        #TEST#
                                       
def OS_Up_0 = if                                                                                                      
bn > bars_back_OS                                                                                                    
and                                                                                                      
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
OS_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def OS_Down_0 = if                                                                                                      
bn > bars_back_OS                                                                                                      
and                                                                                                      
( (OS_Down_Bn - OS_Up_Bn) <= bars_back_OS and (OS_Down_Bn - OS_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
OS_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(OS_Down_0[1], (bars_back_OS - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def OB_Up_0 = if                                                                                                      
bn > bars_back_OS                                                                                                      
and                                                                                                      
( (OB_Up_Bn - OB_Down_Bn) <= bars_back_OB and (OB_Up_Bn - OB_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
OB_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(OB_Up_0[1], (bars_back_OB - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def OB_Down_0 = if                                                                                                      
bn > bars_back_OB                                                                                                      
and                                                                                                      
( (OB_Down_Bn - OB_Up_Bn) <= bars_back_OB and (OB_Down_Bn - OB_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
OB_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(OB_Down_0[1], (bars_back_OB - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def H_BH1_Up_0 = if                                                                                                      
bn > bars_back_H_BH1                                                                                                      
and                                                                                                      
( (H_BH1_Up_Bn - H_BH1_Down_Bn) <= bars_back_H_BH1 and (H_BH1_Up_Bn - H_BH1_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
H_BH1_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(H_BH1_Up_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                    
then 1 else 0;                                                                                                      
                                       
def H_BH1_Down_0 = if                                                                                                      
bn > bars_back_H_BH1                                                                                                      
and                                                                                                      
( (H_BH1_Down_Bn - H_BH1_Up_Bn) <= bars_back_H_BH1 and (H_BH1_Down_Bn - H_BH1_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
H_BH1_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(H_BH1_Down_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def L_BL1_Up_0 = if                                                                                                      
bn > bars_back_L_BL1                                                                                                    
and                                                                                                      
( (L_BL1_Up_Bn - L_BL1_Down_Bn) <= bars_back_L_BL1 and (L_BL1_Up_Bn - L_BL1_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
L_BL1_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(L_BL1_Up_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def L_BL1_Down_0 = if                                                                                                      
bn > bars_back_L_BL1                                                                                                      
and                                                                                                      
( (L_BL1_Down_Bn - L_BL1_Up_Bn) <= bars_back_L_BL1 and (L_BL1_Down_Bn - L_BL1_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
L_BL1_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(L_BL1_Down_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def H_BH2_Up_0 = if                                                                                                      
bn > bars_back_H_BH2                                                                                                      
and                                                                                                      
( (H_BH2_Up_Bn - H_BH2_Down_Bn) <= bars_back_H_BH2 and (H_BH2_Up_Bn - H_BH2_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
H_BH2_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(H_BH2_Up_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def H_BH2_Down_0 = if                                                                                                      
bn > bars_back_H_BH2                                                                                                      
and                                                                                                      
( (H_BH2_Down_Bn - H_BH2_Up_Bn) <= bars_back_H_BH2 and (H_BH2_Down_Bn - H_BH2_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
H_BH2_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(H_BH2_Down_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                      
                                       
def L_BL2_Up_0 = if                                                                                                      
bn > bars_back_L_BL2                                                                                                      
and                                                                                                      
( (L_BL2_Up_Bn - L_BL2_Down_Bn) <= bars_back_L_BL2 and (L_BL2_Up_Bn - L_BL2_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
L_BL2_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(L_BL2_Up_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                      
then 1 else 0;                                                                                                  
                                       
def L_BL2_Down_0 = if                                                                                                      
bn > bars_back_L_BL2                                                                                                      
and                                                                                                      
( (L_BL2_Down_Bn - L_BL2_Up_Bn) <= bars_back_L_BL2 and (L_BL2_Down_Bn - L_BL2_Up_Bn) > 0 )                                                                                                      
and                                                                                                      
L_BL2_Down_Bn == bn                                                                                                      
and                                                                                                      
Sum(L_BL2_Down_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                      
then 1 else 0;

############## --- CROSSOVER TEST(s) --- ###############

def OS_Up_2 = if                                                                                                      
bn > bars_back_OS                                                                                                    
and                                                                                                      
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
OS_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                      
and                                                                                                                                                                                                       ( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                      
and                                                                                                      
OS_Up_Bn == bn                                                                                                      
and                                                                                                      
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                      
then 1 else 0;                  

########### ARROWS ###########                                                                                                      
                                       
input arrow_size = 1;    

plot OS_Up = OS_Up_0;                                                                                                      
OS_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
OS_Up.SetDefaultColor(Color.White);                                                                                                      
OS_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot OS_Down = OS_Down_0;                                                                                                      
OS_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
OS_Down.SetDefaultColor(Color.White);                                                                                                      
OS_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
plot OB_Up = OB_Up_0;                                                                                                      
OB_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
OB_Up.SetDefaultColor(Color.White);                                                                                                      
OB_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot OB_Down = OB_Down_0;                                                                                                      
OB_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
OB_Down.SetDefaultColor(Color.White);                                                                                                      
OB_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
plot H_BH1_Up = H_BH1_Up_0;                                                                                                      
H_BH1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
H_BH1_Up.SetDefaultColor(Color.White);                                                                                                      
H_BH1_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot H_BH1_Down = H_BH1_Down_0;                                                                                                      
H_BH1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
H_BH1_Down.SetDefaultColor(Color.White);                                                                                                      
H_BH1_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
plot L_BL1_Up = L_BL1_Up_0;                                                                                                      
L_BL1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
L_BL1_Up.SetDefaultColor(Color.White);                                                                                                      
L_BL1_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot L_BL1_Down = L_BL1_Down_0;                                                                                                      
L_BL1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
L_BL1_Down.SetDefaultColor(Color.White);                                                                                                      
L_BL1_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
plot H_BH2_Up = H_BH2_Up_0;                                                                                                      
H_BH2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
H_BH2_Up.SetDefaultColor(Color.White);                                                                                                      
H_BH2_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot H_BH2_Down = H_BH1_Down_0;                                                                                                      
H_BH2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
H_BH2_Down.SetDefaultColor(Color.BLUE);                                                                                                      
H_BH2_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
plot L_BL2_Up = L_BL2_Up_0;                                                                                                      
L_BL2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                      
L_BL2_Up.SetDefaultColor(Color.LIGHT_GREEN);                                                                                                      
L_BL2_Up.SetLineWeight(arrow_size);                                                                                                      
                                       
plot L_BL2_Down = L_BL2_Down_0;                                                                                                      
L_BL2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                      
L_BL2_Down.SetDefaultColor(Color.WHITE);                                                                                                      
L_BL2_Down.SetLineWeight(arrow_size);                                                                                                      
                                       
                                       
########### TEST VARIABLES ###########                                                                                                      
                                       
input test_crossing_bubbles = no;                                                                                                      
                                       
AddChartBubble(test_crossing_bubbles and OS_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and OS_Up_Bn == bn, low, bn, Color.GREEN, yes);                                                                                                      
                                       
AddChartBubble(test_crossing_bubbles and OB_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and OB_Up_Bn == bn, low, bn, Color.GREEN, yes);                                              
                                       
AddChartBubble(test_crossing_bubbles and H_BH1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and H_BH1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                              
                                       
AddChartBubble(test_crossing_bubbles and H_BH2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and H_BH2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                              
                                       
AddChartBubble(test_crossing_bubbles and L_BL1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and L_BL1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                              
                                       
AddChartBubble(test_crossing_bubbles and L_BL2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                      
AddChartBubble(test_crossing_bubbles and L_BL2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                              
                                       
##################################################################################
Code:
#--- C4_MAX_BREAKOUTS_V5 --- #                                                                                                       
#--- C3_MAX_SPARK --- #                                                                                                       
                                        
# Created by Christopher84 06/30/2022                                                                                                           
# 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.                                                                                                       
                                        
# --- HORSERIDER VOLUME --- #                                                                                                       
                                        
# Show total volume in gray.  Buying volume in green.  Sell Volume in red.                                                                                                       
# Volume average is gray line.                                                                                                       
# Specified percent over average volume is cyan triangles.                                                                                                       
# Horserider 12/30/2019 derived from some already existing studies.                                                                                                       
# hiVolume indicator                                                                                                       
# source: http://tinboot.blogspot.com                                                                                                       
# author: allen everhart                                                                                                       
                                        
# --- TRIPLE EXHAUSTION --- #                                                                                                       
                                        
# Requested by @Chence27 from criteria listed here https://usethinkscript.com/threads/triple-exhaustion-indicator.9001/                                                                                                       
# Removing the header Credit credits and description is not permitted, any modification needs to be shared.                                                                                                       
# V 1.0 :    @cos251 - Initial release per request from www.usethinkscript.com forum thread:                                                                                                       
#       :    https://usethinkscript.com/threads/triple-exhaustion-indicator.9001/                                                                                                       
# V 1.1 : @chence27 - modifcations to better approximate original study                                                                                                       
# --- BULL_BEAR V5 --- #

#Bull_Bear_Candles+Label Created by Christopher84
#Created 10/21/2022
# Show SELL and BUY Volume and percentage.
# Referencing credit to Horserider script at https://usethinkscript.com/threads/volume-buy-sell-indicator-with-hot-percent-for-thinkorswim.389/
# Nomak 02/21/2020

declare upper;

input timeframe = AggregationPeriod.Day;
input ColoredCandlesOn = yes;
input BulgeLengthV = 20;
input SqueezeLengthV = 20;
input BulgeLengthV2 = 20;
input SqueezeLengthV2 = 20;

def Vol = volume(period = timeframe);
def at_High = high(period = timeframe);
def at_Open = open(period = timeframe);
def at_Close = close(period = timeframe);
def at_Low = low(period = timeframe);
def Vol1 = volume(period = timeframe);
def at_High1 = high(period = timeframe);
def at_Open1 = open(period = timeframe);
def at_Close1 = close(period = timeframe);
def at_Low1 = low(period = timeframe);

# Buy_Volume forumla is volume * (close_price minus low_price) / (High_price minus low_price)
def Buy_Volume = RoundUp(Vol * (at_Close - at_Low) / (at_High - at_Low));
def Buy_percent = RoundUp((Buy_Volume / Vol) * 100);
#Buy_percent.SetPaintingStrategy(PaintingStrategy.LINE);
#Buy_percent.SetLineWeight(1);
#Buy_percent.SetDefaultColor(Color.GREEN);

#Sell_Volume forumla is  volume * (High_price minus Close_price) / (High_price minus Low_price)
def Sell_Volume = RoundDown(Vol1 * (at_High1 - at_Close1) / (at_High1 - at_Low1));
def Sell_percent = RoundUp((Sell_Volume / Vol1) * 100);
#Sell_percent.SetPaintingStrategy(PaintingStrategy.LINE);
#Sell_percent.SetLineWeight(1);
#Sell_percent.SetDefaultColor(Color.RED);


AddLabel(yes, "BuyVol " + Buy_Volume, Color.GREEN);
AddLabel(yes, "SellVol " + Sell_Volume, Color.RED);
AddLabel(yes, "Buy % " + Buy_percent, Color.GREEN);
AddLabel(yes, "Sell % " + Sell_percent, Color.RED);

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

input price = close;
input length_V5 = 10;
input agperiod1 = {"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 agperiod2 = {"1 min", "2 min", "3 min", "5 min",default "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", "Week", "Month"};

plot avg = ExpAverage(close(period = agperiod1), length_V5);
def height = avg - avg[length_V5];
avg.SetStyle(Curve.SHORT_DASH);
avg.SetLineWeight(1);

def UP_V5 = avg[1] < avg;
def DOWN_V5 = avg[1] > avg;
Avg.AssignValueColor(if UP_V5 then Color.LIGHT_GREEN else if DOWN_V5 then Color.RED else Color.YELLOW);

plot avg2 = ExpAverage(close(period = agperiod2), length_V5);
def height2 = avg2 - avg2[length_V5];
avg2.SetStyle(Curve.SHORT_DASH);
avg2.SetLineWeight(1);

def UP2_V5 = avg2[1] < avg2;
def DOWN2_V5 = avg2[1] > avg2;
Avg2.AssignValueColor(if UP2_V5 then Color.LIGHT_GREEN else if DOWN2_V5 then Color.RED else Color.YELLOW);

#AddCloud(avg2, avg, Color.LIGHT_RED, Color.CURRENT);
#AddCloud(avg, avg2, Color.LIGHT_GREEN, Color.CURRENT);

def Condition1UP = avg > avg2;
def Condition1DN = avg < avg2;

def Condition2UP = Buy_percent > 50;
def Condition2DN = Buy_percent < 50;

def BullUP = Condition1UP + Condition2UP;
def BearDN = Condition1DN + Condition2DN;

def Bull_Bear = if Condition1UP==1 and Condition2UP == 1 then 1 else if Condition1DN == 1 and Condition2DN == 1 then -1 else 0;

def priceColor_V5 = if ((avg[1]<avg) and (avg2[1]<avg2)) then 1
                 else if((avg[1]>avg) and (avg2[1]>avg2)) then -1
                 else priceColor_V5[1];

AssignPriceColor(if ColoredCandlesOn and (priceColor_V5==1) then Color.GREEN else if ColoredCandlesOn and (priceColor_V5==-1) then Color.RED else Color.Gray);                                       
                                        
# --- C3 MAX INPUTS --- #                                                                                                       
                                        
#input Price = CLOSE;                                                                                                       
#input ShortLength1 = 5;                                                                                                       
#input ShortLength2 = 14;                                                                                                       
#input ShortLength3 = 5;                                                                                                       
#input LongLength1 = 12;                                                                                                       
#input LongLength2 = 55;                                                                                                       
#input LongLength3 = 7;                                                                                                       
input Color_Candles = yes;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
def ShortLength1 = 5;                                                                                                       
def ShortLength2 = 14;                                                                                                       
def ShortLength3 = 5;                                                                                                       
def LongLength1 = 12;                                                                                                           
def LongLength2 = 55;                                                                                                       
def LongLength3 = 7;                                                                                                       
                                        
# --- HORSERIDER VOLUME INPUTS ---                                                                                                       
                                        
input Show30DayAvg = no;                                                                                                       
input ShowTodayVolume =  no;                                                                                                       
input ShowPercentOf30DayAvg = yes;                                                                                                       
input UnusualVolumePercent = 200;                                                                                                       
input Show30BarAvg = no;                                                                                                       
input ShowCurrentBar = yes;                                                                                                       
input ShowPercentOf30BarAvg = yes;                                                                                                       
input ShowSellVolumePercent = yes;                                                                                                       
def length_3 = 20;                                                                                                       
input type_HV = { default SMP, EXP } ;                                                                                                       
input length1_3 = 20 ;                                                                                                       
input hotPct = 100.0 ;                                                                                                       
                                        
# --- HORSERIDER DEF ---                                                                                                       
                                        
def O_3 = open;                                                                                                       
def H_3 = high;                                                                                                       
def C_3 = close;                                                                                                       
def L_3 = low;                                                                                                       
def V_3 = volume;                                                                                                       
def buying = V_3 * (C_3 - L_3) / (H_3 - L_3);                                                                                                       
def selling = V_3 * (H_3 - C_3) / (H_3 - L_3);                                                                                                       
def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;                                                                                                       
def today = volume(period = "DAY");                                                                                                       
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);                                                                                                       
def avg30Bars = (volume[1] + volume[2] + volume[3] + volume[4] + volume[5] + volume[6] + volume[7] + volume[8] + volume[9] + volume[10] + volume[11] + volume[12] + volume[13] + volume[14] + volume[15] + volume[16] + volume[17] + volume[18] + volume[19] + volume[20] + volume[21] + volume[22] + volume[23] + volume[24] + volume[25] + volume[26] + volume[27] + volume[28] + volume[29] + volume[30]) / 30;                                                                                                       
def curVolume = volume;                                                                                                       
def percentOf30Bar = Round((curVolume / avg30Bars) * 100, 0);                                                                                                       
def SellVolPercent = Round((selling / volume) * 100, 0);                                                                                                       
def ma = if type_HV == type_HV.SMP then SimpleMovingAvg(volume, length_3) else MovAvgExponential(volume, length_3);                                                                                                       
                                        
# --- HORSERIDER STUDY --- LABELS ---                                                                                                       
                                        
AddLabel(Show30DayAvg, "30 Day Avg: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);                                                                                                       
AddLabel(ShowTodayVolume, "Day: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));                                                                                                       
AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.WHITE) );                                                                                                       
AddLabel(Show30BarAvg, "30 Bar Avg: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);                                                                                                       
AddLabel(ShowCurrentBar, "1 Bar: " + curVolume, (if percentOf30Bar >= UnusualVolumePercent then Color.GREEN else if percentOf30Bar >= 100 then Color.ORANGE else Color.LIGHT_GRAY));                                                                                                       
AddLabel(ShowPercentOf30BarAvg, percentOf30Bar + "%", (if percentOf30Bar >= UnusualVolumePercent then Color.GREEN else if percentOf30Bar >= 100 then Color.ORANGE else Color.WHITE) );                                                                                                       
AddLabel(ShowSellVolumePercent, "1 Bar Sell %: " + SellVolPercent, (if SellVolPercent > 51 then Color.RED else if SellVolPercent < 49 then Color.GREEN else Color.ORANGE));                                                                                                       
                                        
# --- TRIPLE EXHAUSTION STUDY --- INPUTS ---                                                                                                       
                                        
#input over_bought_3x = 80;                                                                                                       
#input over_sold_3x = 20;                                                                                                       
#input KPeriod_3x = 10;                                                                                                       
#input DPeriod_3x = 10;                                                                                                       
#input priceH_3x = high;                                                                                                       
#input priceL_3x = low;                                                                                                       
#input priceC_3x = close;                                                                                                       
input averageType_3x = AverageType.SIMPLE;                                                                                                       
input length_3x = 1000;                                                                                                       
#input paintBars_3x = no;                                                                                                       
#input showLabels_3x = no;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
def over_bought_3x = 80;                                                                                                       
def over_sold_3x = 20;                                                                                                       
def KPeriod_3x = 10;                                                                                                       
def DPeriod_3x = 10;                                                                                                       
def priceH_3x = high;                                                                                                       
def priceL_3x = low;                                                                                                       
def priceC_3x = close;                                                                                                       
                                        
# --- TRIPLE EXHAUSTION STUDY --- DEF --- INDICATORS - StochasticSlow / MACD / MACD StDev / DMI+/-                                                                                                       
                                        
def SlowK_3x = reference StochasticFull(over_bought_3x, over_sold_3x, KPeriod_3x, DPeriod_3x, priceH_3x, priceL_3x, priceC_3x, 3, averageType_3x).FullK;                                                                                                       
def MACD_3x = reference MACD()."Value";                                                                                                       
def priceMean_3x = Average(MACD_3x, length_3x);                                                                                                       
def MACD_stdev_3x =  (MACD_3x - priceMean_3x) / StDev(MACD_3x, length_3x);                                                                                                       
def dPlus_3x = reference DMI()."DI+";                                                                                                       
def dMinus_3x = reference DMI()."DI-";     
                                                                                                  
def sellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;                                                                                                       
def sellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;                                                                                                       
def buyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;                                                                                                       
def buyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;                                                                                                       
                                        
# --- TRIPLE EXHAUSTION STUDY --- ARROWS ---                                                                                                                                                               
                                                                                                                                              
# --- Arrows/Triggers
plot RegularBuy = if sellerRegular[1] and !sellerRegular then low else Double.NaN;
plot ExtremeBuy = if sellerExtreme[1] and !sellerExtreme then low else Double.NaN;
RegularBuy.SetPaintingStrategy(PaintingSTrategy.Points);
ExtremeBuy.SetPaintingSTrategy(paintingSTrategy.Points);
RegularBuy.SetDefaultColor(Color.yellow);
ExtremeBuy.SetDefaultColor(Color.GREEN);

plot RegularSell = if buyerRegular[1] and !buyerRegular then high else Double.NaN;
plot ExtremeSell = if buyerExtreme[1] and !buyerExtreme then high else Double.NaN;
RegularSell.SetPaintingStrategy(PaintingSTrategy.Points);
ExtremeSell.SetPaintingSTrategy(paintingSTrategy.Points);
RegularSell.SetDefaultColor(Color.yellow);
ExtremeSell.SetDefaultColor(Color.RED);

#Addverticalline ((Regularsell),"",Color.red);                                                                                                       
#Addverticalline ((Regularbuy),"", Color.Green);                                                                                                       
                                        
                                        
# --- PLD STUDY --- #                                                                                                       
                                        
# --- PLD STUDY INPUTS ---                                                                                                       
#input price = close;                           
input length_PLD = 5;                                                                                                       
input displace_PLD = -11;                                                                                                       
input showBreakoutSignals_PLD = no;                                                                                                       
input ColoredCandlesOn_PLD = no;                                                                                                       
input BulgeLengthPrice_PLD = 5;                                                                                                       
input SqueezeLengthPrice_PLD = 5;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
#def BulgeLengthPrice_PLD = 5;                                                                                                       
#def SqueezeLengthPrice_PLD = 5;                                                                                                       
                                        
# --- PLD DEF --- #                                                                                                       

                            
plot PLD = ExpAverage(Price[-displace_PLD], length_PLD);                                                                                                       
def UpperBand_PLD = Highest(PLD, BulgeLengthPrice_PLD);                                                                                                       
def LowerBand_PLD = Lowest(PLD, SqueezeLengthPrice_PLD);                                                                                                       
plot UpSignal_PLD = Price crosses above UpperBand_PLD;                                                                                                       
plot DownSignal_PLD = Price crosses below LowerBand_PLD;                                                                                                       
UpSignal_PLD.SetHiding(!showBreakoutSignals_PLD);                                                                                                       
DownSignal_PLD.SetHiding(!showBreakoutSignals_PLD);                                                                                                       
PLD.SetDefaultColor(GetColor(1));                                                                                                       
UpSignal_PLD.SetDefaultColor(Color.UPTICK);                                                                                                       
UpSignal_PLD.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
DownSignal_PLD.SetDefaultColor(Color.DOWNTICK);                                                                                                       
DownSignal_PLD.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
def LongEnter = (Price crosses above UpperBand_PLD);                                                                                                       
def LongExit = (Price crosses below LowerBand_PLD);                                                                                                       
                                        
# --- CONFIRMATION NEAR TERM LEVELS STUDY ---                                                                                                       
                                        
def displace2_2 = 0;                                                                                                       
def factorK2_2 = 2.0;                                                                                                       
def lengthK2_2 = 20;                                                                                                       
input averageType2_2 = AverageType.SIMPLE;                                                                                                       
def trueRangeAverageType2_2 = AverageType.SIMPLE;                                                                                                       
def BulgeLengthPrice2 = 20;                                                                                                       
def SqueezeLengthPrice2 = 20;                                                                                                       
def shift2_2 = factorK2_2  * MovingAverage(trueRangeAverageType2_2 , TrueRange(high, close, low), lengthK2_2 );                                                                                                       
def averageK2_2 = MovingAverage(averageType2_2 , Price , lengthK2_2 );                                                                                                       
def Upper_BandK2_2  = averageK2_2 [-displace2_2 ] + shift2_2 [-displace2_2 ];                                                                                                       
def Lower_BandK2_2  = averageK2_2 [-displace2_2 ] - shift2_2 [-displace2_2 ];                                                                                                       
def conditionK2 = (Upper_BandK2_2 [1] < Upper_BandK2_2 ) and (Lower_BandK2_2 [1] < Lower_BandK2_2 );                                                                                                       
def conditionK3 = (Upper_BandK2_2 [1] > Upper_BandK2_2 ) and (Lower_BandK2_2 [1] > Lower_BandK2_2 );                                                                                                       
                                        
# --- C3 MAX STUDY --- #                                                                                                       
                                        
# 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                                                                                                       
                                        
#input length = 20;                                                                                                       
#input nK = 1.5;                                                                                                       
#input nBB = 2.0;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);                                                                                                       
                                        
# Parabolic SAR Signal                                                                                                       
                                        
input accelerationFactor = 0.0275;                                                                                                       
input accelerationLimit = 0.2;                                                                                                       
                                        
def SAR = ParabolicSAR(accelerationFactor = accelerationFactor, accelerationLimit = accelerationLimit);                                                                                                       
def bearishCross = Crosses(SAR, Price, CrossingDirection.ABOVE);                                                                                                       
def signalDown = bearishCross;#If(bearishCross, 0, Double.NaN);                                                                                                       
def bullishCross = Crosses(SAR, Price, CrossingDirection.BELOW);                                                                                                       
def signalUp =  bullishCross;#If(bullishCross, 0, Double.NaN);                                                                                                       
def UP = bullishCross;                                                                                                       
def DOWN = bearishCross;                                                                                                       
def priceColor = if UP then 1 else if DOWN then -1 else priceColor[1];                                                                                                       
                                        
# OB_OS_Levels_v5 by Christopher84 12/10/2021                                                                                                       
                                        
input BarsUsedForRange = 2;                                                                                                       
input BarsRequiredToRemainInRange = 2;                                                                                                       
input TargetMultiple = 0.5;                                                                                                       
input ColorPrice = yes;                                                                                                       
input HideTargets = no;                                                                                                       
input HideBalance = no;                                                                                                       
input HideBoxLines = no;                                                                                                       
input HideCloud = no;                                                                                                       
input HideLabels = no;                                                                                                       
                                        
#Squeeze Dots Created 04/28/2021 by Christopher84                                                                                                       
                                        
#input ATRPeriod = 5;                                                                                                       
#input ATRFactor = 2.0;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
def ATRPeriod = 5;                                                                                                       
def 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};                                                                                                       
                                        
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;                                                                                                       
                                        
# --- Changed Inputs to "def" so they will not show in settings ---                                                                                                       
                                        
def ATRPeriod2 = 5;                                                                                                       
def ATRFactor2 = 1.5;                                                                                                       
                                        
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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);                                                                                                       
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);                                                                                                       
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;                                                                                                       
def condition1 = Value[1] <= Value;                                                                                                       
def condition1D = Value[1] > Value;                                                                                                       
                                        
#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;                                                                                                       
                                        
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;                                                                                                       
                                        
#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;                                                                                                       
                                        
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;                                                                                                       
                                        
#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];                                                                                                       
def condition5 = CIP_UP;                                                                                                       
def condition5D = CIP_DOWN;                                                                                                       
                                        
#EMA_1                                                                                                       
                                        
def EMA_length = 8;                                                                                                       
def AvgExp = ExpAverage(Price[-displace], EMA_length);                                                                                                       
def condition6 = (Price >= AvgExp) and (AvgExp[2] <= AvgExp);                                                                                                       
def condition6D = (Price < AvgExp) and (AvgExp[2] > AvgExp);                                                                                                       
                                        
#EMA_2                                                                                                       
                                        
def EMA_2length = 20;                                                                                                       
def displace2 = 0;                                                                                                       
def AvgExp2 = ExpAverage(Price[-displace2], EMA_2length);                                                                                                       
def condition7 = (Price >= AvgExp2) and (AvgExp2[2] <= AvgExp);                                                                                                       
def condition7D = (Price < AvgExp2) and (AvgExp2[2] > AvgExp);                                                                                                       
                                        
#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));                                                                                                       
def condition9 = Periods > 0;                                                                                                       
def condition9D = Periods < 0;                                                                                                       
                                        
#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;                                                                                                       
def condition10 = PFE > 0;                                                                                                       
def condition10D = PFE < 0;                                                                                                       
def conditionOB5 = PFE > UpperLevel;                                                                                                       
def conditionOS5 = PFE < LowerLevel;                                                                                                       
                                        
#Bollinger Bands Percent_B                                                                                                       
                                        
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;                                                                                                       
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);                                                                                                       
                                        
#Klinger Histogram                                                                                                       
                                        
def Klinger_Length = 13;                                                                                                       
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;                                                                                                       
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);                                                                                                       
def condition13 = (KVOH > 0);                                                                                                       
def condition13D = (KVOH < 0);                                                                                                       
                                        
#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;                                                                                                       
def condition14 = PROSC > 50;                                                                                                       
def condition14D = PROSC < 50;                                                                                                       
def conditionOB7 = PROSC > PROSC_OB;                                                                                                       
def conditionOS7 = PROSC < PROSC_OS;                                                                                                       
                                        
#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];                                                                                                       
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 >= 10;                                                                                                       
def DOWN2 = Consensus_Level < -10;                                                                                                       
def priceColor2 = if UP2 then 1                                                                                                       
                 else if DOWN2 then -1                                                                                                       
                 else priceColor2[1];                                                                                                       
def Consensus_Level_OB = 10;                                                                                                       
def Consensus_Level_OS = -10;                                                                                                       
                                        
#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 Color_Candles and ((price1 > Upper_BandS) and (condition_BandRevDn)) then high else Double.NaN;                                                                                                       
def YHOS = if Color_Candles and ((price1 < Lower_BandS) and (condition_BandRevUp)) then high else Double.NaN;                                                                                                       
def YLOB = if Color_Candles and ((price1 > Upper_BandS) and (condition_BandRevDn)) then low else Double.NaN;                                                                                                       
def YLOS = if Color_Candles 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.ORANGE);                                                                                                       
YHextlineOB.SetLineWeight(2);                                                                                                       
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(2);                                                                                                       
                                        
def YC = Color_Candles 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;#if (DOWN_OB > 3) then Highest(ExpH) else if (Condition_BandRevDn and (price > AvgExp) and (High > High[1])) then Highest(ExpH) else Double.NaN;                                                                                                       
def BoxLow = if (DOWN_OB) or ((Upper_BandS crosses above Upper_BandK2)) then Lowest(low) else Double.NaN;#if (DOWN_OB crosses above 3) then Lowest(low) else if ((Upper_BandS crosses above Upper_BandK2)) then Lowest(ExpH) else Double.NaN;#if ((DOWN_OB) or (Condition_BandRevDn and (price < price[1]))) then Highest(ExpL) else Double.NaN;                                                                                                       
def BoxHigh2 = if ((UP_OS) or ((Lower_BandS crosses below Lower_BandK2))) then Highest(ExpH) else Double.NaN; #if (UP_OS) then Highest(ExpH) else if ((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;#if (UP_OS) then Lowest(low) else if (Condition_BandRevUp and (price < AvgExp) 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;                                                                                                       
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 BL2 = if !IsNaN(BoxLow2) then low else Double.NaN;                                                                                                       
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 L_BL2extline = Lowest(BL2extline, 1);                                                                                                       
L_BL2extline.SetDefaultColor(Color.GREEN);                                                                                                       
                                        
###################### --- BREAKOUT CODE --- ######################                                                                                       
                                        
# Price crosses level and the opposite crossing happend within x number of bars                                                                                                       
                                        
def bn = BarNumber();                                                                                                       
def na_1 = Double.NaN;                                                                                                       
                                        
input bars_back_OS = 5;                                       
input bars_back_OB = 5;                                       
                                        
input bars_back_H_BH1 = 5;                                       
input bars_back_H_BH2 = 5;                                       
                                        
input bars_back_L_BL1 = 5;                                                                                                       
input bars_back_L_BL2 = 5;                                       
                                        
# def support = ... some price level                                       
                                                                                                        
def Y_Line_OS = YHextlineOS;                                                                                                       
def LG_Line_OB = YHextlineOB;                                                                                                       
                                        
def High_BH1_Red = H_BH1EXTLINE;                                                                                                       
def Low_BL1_Red = L_BL1EXTLINE;                                                                                                       
                                        
def High_BH2_Grn = H_BH2EXTLINE;                                                                                                       
def Low_BL2_Grn = L_BL2EXTLINE;                                                                                                       
                                        
############# INPUT FOR TESTING ONLY ##############                                                                                                       
                                        
input support_level = 50;                                                                                                       
                                        
########### CONVERT CONSTANT TO VAR ############                                                                                                                                                               
                                        
input show_ref_line = no;                                                                                                       
                                        
# plot y = if show_ref_line then support else na_1;                                                                                                                                                       
                                        
########### CHOOSE WICK, BODY, or CLOSE ###########                                                                                                       
                                        
input OB_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_OB;                                                                                                       
def Low_OB;                                                                                                       
switch (OB_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_OB = high;                                                                                                       
    Low_OB = low;                                                                                                       
case "body":                                                                                                       
    High_OB = Max(open, close);                                                                                                       
    Low_OB = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_OB = close;                                                                                                       
    Low_OB = close;                                                                                                       
}                                                                                                       
                                        
input OS_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_OS;                                                                                                       
def Low_OS;                                                                                                       
switch (OS_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_OS = high;                                                                                                       
    Low_OS = low;                                                                                                       
case "body":                                                                                                       
    High_OS = Max(open, close);                                                                                                       
    Low_OS = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_OS = close;                                                                                                       
    Low_OS = close;                                                                                                       
}                                                                                                       
                                        
input H_BH1_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_H_BH1;                                                                                                       
def Low_H_BH1;                                                                                                       
switch (H_BH1_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_H_BH1 = high;                                                                                                       
    Low_H_BH1 = low;                                                                                                       
case "body":                                                                                                       
    High_H_BH1 = Max(open, close);                                                                                                       
    Low_H_BH1 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_H_BH1 = close;                                                                                                       
    Low_H_BH1 = close;                                                                                                       
}                                                                                                       
                                        
input H_BH2_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_H_BH2;                                                                                                       
def Low_H_BH2;                                                                                                       
switch (H_BH2_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_H_BH2 = high;                                                                                                       
    Low_H_BH2 = low;                                                                                                       
case "body":                                                                                                       
    High_H_BH2 = Max(open, close);                                                                                                       
    Low_H_BH2 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_H_BH2 = close;                                                                                                       
    Low_H_BH2 = close;                                                                                                       
}                                                                                                       
                                        
input L_BL1_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_L_BL1;                                                                                                       
def Low_L_BL1;                                                                                                       
switch (L_BL1_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_L_BL1 = high;                                                                                                       
    Low_L_BL1 = low;                                                                                                       
case "body":                                                                                                       
    High_L_BL1 = Max(open, close);                                                                                                       
    Low_L_BL1 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_L_BL1 = close;                                                                                                       
    Low_L_BL1 = close;                                                                                                       
}                                                                                                       
                                        
input L_BL2_Candle = {default "wick" , "body" , "close"};                                                                                                       
def High_L_BL2;                                                                                                       
def Low_L_BL2;                                                                                                       
switch (L_BL2_Candle) {                                                                                                       
case "wick":                                                                                                       
    High_L_BL2 = high;                                                                                                       
    Low_L_BL2 = low;                                                                                                       
case "body":                                                                                                       
    High_L_BL2 = Max(open, close);                                                                                                       
    Low_L_BL2 = Min(open, close);                                                                                                       
case "close":                                                                                                       
    High_L_BL2 = close;                                                                                                       
    Low_L_BL2 = close;                                                                                                       
}                                                                                                       
                                        
########### LABELS ############                                                                                       
                                        
#AddLabel(1, "Crossing Levels OS " + OS_Candle , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Bars Back OS" + bars_back_OS , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Crossing Levels OB " + OB_Candle , Color.YELLOW);                                       
                                                                                                        
#AddLabel(1, "Bars Back OB " + bars_back_OB , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Crossing Levels H_BH1 " + H_BH1_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back H_BH1 " + bars_back_H_BH1, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels H_BH2 " + H_BH2_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back H_BH2 " + bars_back_H_BH2, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels L_BL1 " + L_BL1_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back L_BL1 " + bars_back_L_BL1, Color.YELLOW);                                               
                                        
#AddLabel(1, "Crossing Levels L_BL2 " + L_BL2_Candle , Color.YELLOW);                                               
                                                                                                
#AddLabel(1, "Bars Back L_BL2 " + bars_back_L_BL2, Color.YELLOW);                                               
                                        
                                        
########## CROSSING UP / DOWN ###########                                                                                               
                                        
def OS_Down_Bn = if bn == 1 then 0 else if High_OS[1] > Y_Line_OS[1] and Low_OS < Y_Line_OS then bn else OS_Down_Bn[1];                                                                                                       
def OS_Up_Bn = if bn == 1 then 0 else if Low_OS[1] < Y_Line_OS[1] and High_OS > Y_Line_OS then bn else OS_Up_Bn[1];                                                                                                       
                                        
def OB_Down_Bn = if bn == 1 then 0 else if High_OB[1] > LG_Line_OB[1] and Low_OB < LG_Line_OB then bn else OB_Down_Bn[1];                                                                                                       
def OB_Up_Bn = if bn == 1 then 0 else if Low_OB[1] < LG_Line_OB[1] and High_OB > LG_Line_OB then bn else OB_Up_Bn[1];                                                                                                       
                                        
def H_BH1_Down_Bn = if bn == 1 then 0 else if High_H_BH1[1] > High_BH1_Red[1] and Low_H_BH1 < High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                       
def H_BH1_Up_Bn = if bn == 1 then 0 else if Low_H_BH1[1] < High_BH1_Red[1] and High_H_BH1 > High_BH1_Red then bn else H_BH1_Down_Bn[1];                                                                                                       
                                        
def H_BH2_Down_Bn = if bn == 1 then 0 else if High_H_BH2[1] > High_BH2_Grn[1] and Low_H_BH2 < High_BH2_Grn then bn else H_BH2_Down_Bn[1];                                                                                       
def H_BH2_Up_Bn = if bn == 1 then 0 else if Low_H_BH2[1] < High_BH2_Grn[1] and High_H_BH2 > High_BH2_Grn then bn else H_BH2_Up_Bn[1];                                                                                                       
                                        
def L_BL1_Down_Bn = if bn == 1 then 0 else if High_L_BL1[1] > Low_BL1_Red[1] and Low_L_BL1 < Low_BL1_Red then bn else L_BL1_Down_Bn[1];                                                                                                       
def L_BL1_Up_Bn = if bn == 1 then 0 else if Low_L_BL1[1] < Low_BL1_Red[1] and High_L_BL1 > Low_BL1_Red then bn else L_BL1_Up_Bn[1];                                                                                                       
                                        
def L_BL2_Down_Bn = if bn == 1 then 0 else if High_L_BL2[1] > Low_BL2_Grn[1] and Low_L_BL2 < Low_BL2_Grn then bn else L_BL2_Down_Bn[1];                                                                                                       
def L_BL2_Up_Bn = if bn == 1 then 0 else if Low_L_BL2[1] < Low_BL2_Grn[1] and High_L_BL2 > Low_BL2_Grn then bn else L_BL2_Up_Bn[1];                                                                                                       
                                        
########### GETTIN CROSSED-UP ###########                                                                                                       
                                        
# dip then rise, within len bars                                                                                                        #TEST#
                                        
def OS_Up_0 = if                                                                                                       
bn > bars_back_OS                                                                                                     
and                                                                                                       
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OS_Down_0 = if                                                                                                       
bn > bars_back_OS                                                                                                       
and                                                                                                       
( (OS_Down_Bn - OS_Up_Bn) <= bars_back_OS and (OS_Down_Bn - OS_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Down_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OB_Up_0 = if                                                                                                       
bn > bars_back_OS                                                                                                       
and                                                                                                       
( (OB_Up_Bn - OB_Down_Bn) <= bars_back_OB and (OB_Up_Bn - OB_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OB_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OB_Up_0[1], (bars_back_OB - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def OB_Down_0 = if                                                                                                       
bn > bars_back_OB                                                                                                       
and                                                                                                       
( (OB_Down_Bn - OB_Up_Bn) <= bars_back_OB and (OB_Down_Bn - OB_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
OB_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(OB_Down_0[1], (bars_back_OB - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH1_Up_0 = if                                                                                                       
bn > bars_back_H_BH1                                                                                                       
and                                                                                                       
( (H_BH1_Up_Bn - H_BH1_Down_Bn) <= bars_back_H_BH1 and (H_BH1_Up_Bn - H_BH1_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH1_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH1_Up_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                     
then 1 else 0;                                                                                                       
                                        
def H_BH1_Down_0 = if                                                                                                       
bn > bars_back_H_BH1                                                                                                       
and                                                                                                       
( (H_BH1_Down_Bn - H_BH1_Up_Bn) <= bars_back_H_BH1 and (H_BH1_Down_Bn - H_BH1_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH1_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH1_Down_0[1], (bars_back_H_BH1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL1_Up_0 = if                                                                                                       
bn > bars_back_L_BL1                                                                                                     
and                                                                                                       
( (L_BL1_Up_Bn - L_BL1_Down_Bn) <= bars_back_L_BL1 and (L_BL1_Up_Bn - L_BL1_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL1_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL1_Up_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL1_Down_0 = if                                                                                                       
bn > bars_back_L_BL1                                                                                                       
and                                                                                                       
( (L_BL1_Down_Bn - L_BL1_Up_Bn) <= bars_back_L_BL1 and (L_BL1_Down_Bn - L_BL1_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL1_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL1_Down_0[1], (bars_back_L_BL1 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH2_Up_0 = if                                                                                                       
bn > bars_back_H_BH2                                                                                                       
and                                                                                                       
( (H_BH2_Up_Bn - H_BH2_Down_Bn) <= bars_back_H_BH2 and (H_BH2_Up_Bn - H_BH2_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH2_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH2_Up_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def H_BH2_Down_0 = if                                                                                                       
bn > bars_back_H_BH2                                                                                                       
and                                                                                                       
( (H_BH2_Down_Bn - H_BH2_Up_Bn) <= bars_back_H_BH2 and (H_BH2_Down_Bn - H_BH2_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
H_BH2_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(H_BH2_Down_0[1], (bars_back_H_BH2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                       
                                        
def L_BL2_Up_0 = if                                                                                                       
bn > bars_back_L_BL2                                                                                                       
and                                                                                                       
( (L_BL2_Up_Bn - L_BL2_Down_Bn) <= bars_back_L_BL2 and (L_BL2_Up_Bn - L_BL2_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL2_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL2_Up_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                       
then 1 else 0;                                                                                                   
                                        
def L_BL2_Down_0 = if                                                                                                       
bn > bars_back_L_BL2                                                                                                       
and                                                                                                       
( (L_BL2_Down_Bn - L_BL2_Up_Bn) <= bars_back_L_BL2 and (L_BL2_Down_Bn - L_BL2_Up_Bn) > 0 )                                                                                                       
and                                                                                                       
L_BL2_Down_Bn == bn                                                                                                       
and                                                                                                       
Sum(L_BL2_Down_0[1], (bars_back_L_BL2 - 1)) == 0                                                                                                       
then 1 else 0;

############## --- CROSSOVER TEST(s) --- ###############

def OS_Up_2 = if                                                                                                       
bn > bars_back_OS                                                                                                     
and                                                                                                       
( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
and                                                                                                                                                                                                       ( (OS_Up_Bn - OS_Down_Bn) <= bars_back_OS and (OS_Up_Bn - OS_Down_Bn) > 0 )                                                                                                       
and                                                                                                       
OS_Up_Bn == bn                                                                                                       
and                                                                                                       
Sum(OS_Up_0[1], (bars_back_OS - 1)) == 0                                                                                                       
then 1 else 0;                   



                                                        
                                        
########### ARROWS ###########                                                                                                       
                                        
input arrow_size = 1;     

plot OS_Up_22 = OS_Up_2;                                                                                                       
OS_Up_22.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
OS_Up_22.SetDefaultColor(Color.White);                                                                                                       
OS_Up_22.SetLineWeight(arrow_size); 

plot OS_Up = OS_Up_0;                                                                                                       
OS_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
OS_Up.SetDefaultColor(Color.White);                                                                                                       
OS_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OS_Down = OS_Down_0;                                                                                                       
OS_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
OS_Down.SetDefaultColor(Color.White);                                                                                                       
OS_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OB_Up = OB_Up_0;                                                                                                       
OB_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
OB_Up.SetDefaultColor(Color.White);                                                                                                       
OB_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot OB_Down = OB_Down_0;                                                                                                       
OB_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
OB_Down.SetDefaultColor(Color.White);                                                                                                       
OB_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH1_Up = H_BH1_Up_0;                                                                                                       
H_BH1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
H_BH1_Up.SetDefaultColor(Color.White);                                                                                                       
H_BH1_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH1_Down = H_BH1_Down_0;                                                                                                       
H_BH1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
H_BH1_Down.SetDefaultColor(Color.White);                                                                                                       
H_BH1_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL1_Up = L_BL1_Up_0;                                                                                                       
L_BL1_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
L_BL1_Up.SetDefaultColor(Color.White);                                                                                                       
L_BL1_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL1_Down = L_BL1_Down_0;                                                                                                       
L_BL1_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
L_BL1_Down.SetDefaultColor(Color.White);                                                                                                       
L_BL1_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH2_Up = H_BH2_Up_0;                                                                                                       
H_BH2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
H_BH2_Up.SetDefaultColor(Color.White);                                                                                                       
H_BH2_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot H_BH2_Down = H_BH1_Down_0;                                                                                                       
H_BH2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
H_BH2_Down.SetDefaultColor(Color.BLUE);                                                                                                       
H_BH2_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL2_Up = L_BL2_Up_0;                                                                                                       
L_BL2_Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);                                                                                                       
L_BL2_Up.SetDefaultColor(Color.LIGHT_GREEN);                                                                                                       
L_BL2_Up.SetLineWeight(arrow_size);                                                                                                       
                                        
plot L_BL2_Down = L_BL2_Down_0;                                                                                                       
L_BL2_Down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);                                                                                                       
L_BL2_Down.SetDefaultColor(Color.WHITE);                                                                                                       
L_BL2_Down.SetLineWeight(arrow_size);                                                                                                       
                                        
                                        
########### TEST VARIABLES ###########                                                                                                       
                                        
input test_crossing_bubbles = no;                                                                                                       
                                        
AddChartBubble(test_crossing_bubbles and OS_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and OS_Up_Bn == bn, low, bn, Color.GREEN, yes);                                                                                                       
                                        
AddChartBubble(test_crossing_bubbles and OB_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and OB_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and H_BH1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and H_BH1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and H_BH2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and H_BH2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and L_BL1_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and L_BL1_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
AddChartBubble(test_crossing_bubbles and L_BL2_Down_Bn == bn, high, bn, Color.RED, no);                                                                                                       
AddChartBubble(test_crossing_bubbles and L_BL2_Up_Bn == bn, low, bn, Color.GREEN, yes);                                               
                                        
##################################################################################                                       
                                        
                                        
                                        
                                        
                                        
                                        
##################################################################################                                       
                                        
AddCloud(if !HideCloud then BH1extline else Double.NaN, BL1extline, Color.RED, Color.GRAY);
AddCloud(if !HideCloud then BH2extline else Double.NaN, BL2extline, Color.GREEN, Color.GRAY);

script WMA_Smooth {
    input price_WMA = hl2;
    plot smooth = (4 * price_WMA
+ 3 * price_WMA[1]
+ 2 * price_WMA[2]
+ price_WMA[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_WMA = 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_WMA);
        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_WMA = 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_WMA).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_WMA);
        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_WMA + (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);

# --- C3 MAX --- LABELS ---

AddLabel(yes, Concat("MAMA: ", Concat("",
if MAMA > FAMA then "Bull" else "Bear")),
if MAMA > FAMA then Color.GREEN else Color.RED);

# --- C3 MAX --- C3 MF LINE ---

#plot C3_MF_Line = (MAMA + FAMA) / 2;
#C3_MF_Line.SetPaintingStrategy(PaintingStrategy.LINE);
#C3_MF_Line.SetLineWeight(3);
#C3_MF_Line.AssignValueColor(if Color_Candles and ((priceColor2 == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if Color_Candles and ((priceColor2 == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if Color_Candles and priceColor2 == -1 then Color.RED  else if Color_Candles and (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_Candles
#Created by Christopher84 11/30/2021

input showBreakoutSignals = no;
input length8 = 10;
input length9 = 35;
input show_ema_cloud = yes;

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

def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);

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

def UPW = AvgExp9[1] < AvgExp9;
AvgExp9.SetStyle(Curve.short_dash);
def Below = AvgExp8 < AvgExp9;
def Spark = UPD + UPW + Below;
def UPEMA = AvgExp8[1] < AvgExp8;
def DOWNEMA = AvgExp8[1] > AvgExp8;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);
def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

def UP11 = UPEMA;
def DOWN11 = DOWNEMA;
def priceColor11 = if UP11 then 1
                 else if DOWN11 then -1
                 else 0;
def UP12 = UPEMA2;
def DOWN12 = DOWNEMA2;
def priceColor12 = if UP12 then 1
                 else if DOWN12 then -1
                 else 0;

def UpCalc =  (priceColor == 1) + (priceColor2 == 1) + (priceColor8 == 1) + (priceColor10 == 1);
def StrongUpCalc =  (priceColor == 1) + (priceColor2 == 1)  + (priceColor10 == 1) + (priceColor12 == 1);# + (priceColor12 == 1);
def CandleColor = if (priceColor2 == 1) and (Spark >= 2) then 1
                 else if (priceColor2 == -1) and (Spark < 2) then -1
                 #else if ((PriceColor8[2] == 1) and (PriceColor8 == 1)) then 1
                 #else if ((PriceColor8[2] == -1) and (PriceColor8 == -1))then -1
                 #else if (priceColor2 == 1) then 1
                 #else if (priceColor2 == -1) then -1
                 else 0;
#AssignPriceColor(if Color_Candles and (CandleColor == 1) then Color.GREEN else if Color_Candles and (CandleColor == -1) then Color.RED else Color.GRAY);
plot SparkUP = (Spark == 3) and (CandleColor == 1);
SparkUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SparkUP.AssignValueColor(Color.LIGHT_GREEN);
plot SparkDN = (Spark == 0) and (CandleColor == -1);
SparkDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SparkDN.AssignValueColor(Color.RED);

#--- CONFIRMATION LEVELS --- PLOT ---

plot NearTResistance = Highest(price, BulgeLengthPrice2);
NearTResistance.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
NearTResistance.SetStyle(Curve.SHORT_DASH);
plot NearTSupport = Lowest(price, SqueezeLengthPrice2);
NearTSupport.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
NearTSupport.SetStyle(Curve.SHORT_DASH);

#--- CONFIRMATION LEVELS --- DEF ---

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);

AddLabel(yes, if conditionLTB then "BULLISH LTB" else if conditionLTS then "BEARISH LTS" 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.WHITE 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.YELLOW 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.YELLOW 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);

# --- VERTICLE LINE DAILY ---

AddVerticalLine(( GetDay() <> GetDay()[1]), "", Color.DARK_GRAY, Curve.SHORT_DASH);

#--- END ---

(Full code)
 
@Christopher84 Is there a way to make/allow the C3_MF_Line to be a different timeframe than the chart it is on? For example: I would like the C3_MF_Line to be on a 5min Timeframe but on a 1min chart. Kind of like the Daily SMA in TOS. Is that possible? If so, Would you or someone that knows how to code, help with this? I would like to have 5m,10m,15m,30m, and D options. Thanks.
 
@Christopher84 Is there a way to make/allow the C3_MF_Line to be a different timeframe than the chart it is on? For example: I would like the C3_MF_Line to be on a 5min Timeframe but on a 1min chart. Kind of like the Daily SMA in TOS. Is that possible? If so, Would you or someone that knows how to code, help with this? I would like to have 5m,10m,15m,30m, and D options. Thanks.
hmmm it would have to be converted to non MTF I assume. I have added it as a lower and changed the settings which may mimic a higher timeframe?
 
@Christopher84 Is there a way to make/allow the C3_MF_Line to be a different timeframe than the chart it is on? For example: I would like the C3_MF_Line to be on a 5min Timeframe but on a 1min chart. Kind of like the Daily SMA in TOS. Is that possible? If so, Would you or someone that knows how to code, help with this? I would like to have 5m,10m,15m,30m, and D options. Thanks.
Multi time frame indicators do repaint.
Code:
#C3_MF_Line Created by Christopher84 12/01/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.
# Converted to MTF.

input agperiod = {"1 min", "3 min", "10 min", "15 min", "30 min", "1 hour", "4 hours","1 day","1 week", default "5 min"};


#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close(period = agperiod);
def price1 = open(period = agperiod);
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthPrice3 = 12;
def SqueezeLengthPrice3 = 12;

def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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];

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 BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;

def IntermResistance = Highest(price, BulgeLengthPrice);
#IntermResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
def IntermSupport = Lowest(price, SqueezeLengthPrice);
#IntermSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);

def NearTResistance = Highest(price, BulgeLengthPrice2);
#NearTResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTResistance.SetStyle(Curve.SHORT_DASH);
def NearTSupport = Lowest(price, SqueezeLengthPrice2);
#NearTSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTSupport.SetStyle(Curve.SHORT_DASH);

def NearTResistance1 = Highest(price, BulgeLengthPrice3);
def NearTSupport1 = Lowest(price, SqueezeLengthPrice3);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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(period = agperiod), close(period = agperiod), low(period = agperiod), volume(period = agperiod), MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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(period = agperiod), TP_fastLength) - ExpAverage(close(period = agperiod), TP_slowLength));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

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

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
def multiplier_factor = 1.25;
def valS = Average(price, SMA_lengthS);
def average_true_range = Average(TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

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

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
#input coloredCandlesOn = yes;
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 = 10;
def Agreement_LevelOS = -10;

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 UP = Consensus_Level >= 6;
def DOWN = Consensus_Level < -6;

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

#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

#Consensus_Level.AssignValueColor(
#if Consensus_Level > Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level < 0 then Color.RED else
#if Consensus_Level > Consensus_Level[1] and Consensus_Level < 0 then Color.RED
#else Color.GRAY);

def Zero_Line = 0;


#AddCloud(Consensus_Level, Agreement_LevelOB, Color.LIGHT_RED, Color.CURRENT);
#AddCloud(Consensus_Level, Agreement_LevelOS, Color.CURRENT, Color.LIGHT_GREEN);

#plot BulgeCC = Highest(Consensus_Level, BulgeLengthCC);
#BulgeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot SqueezeCC = Lowest(Consensus_Level, SqueezeLengthCC);
#SqueezeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot BulgeCC2 = Highest(Consensus_Level, BulgeLengthCC2);
#BulgeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#BulgeCC2.SetStyle(Curve.SHORT_DASH);

#plot SqueezeCC2 = Lowest(Consensus_Level, SqueezeLengthCC2);
#SqueezeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#SqueezeCC2.SetStyle(Curve.SHORT_DASH);
##########################################################################################################################

def h12 =  hl2(period = agperiod);
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);
C3_MF_Line.AssignValueColor(if ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if priceColor == -1 then Color.RED  else if (priceColor == 1) then Color.GREEN else Color.CURRENT);

#AssignPriceColor(if coloredCandlesOn and ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if coloredCandlesOn and ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if coloredCandlesOn and (price < C3_MF_Line) and (priceColor == -1) then Color.RED  else if coloredCandlesOn and (price > C3_MF_Line) and (priceColor == 1) then Color.GREEN else Color.GRAY);
 
Last edited:
Multi time frame indicators do repaint.
Code:
#C3_MF_Line Created by Christopher84 12/01/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.
# Converted to MTF.

input agperiod = {"1 min", "3 min", "10 min", "15 min", "30 min", "1 hour", "4 hours","1 day","1 week", default "5 min"};


#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close(period = agperiod);
def price1 = open(period = agperiod);
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthPrice3 = 12;
def SqueezeLengthPrice3 = 12;

def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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];

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 BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;

def IntermResistance = Highest(price, BulgeLengthPrice);
#IntermResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
def IntermSupport = Lowest(price, SqueezeLengthPrice);
#IntermSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);

def NearTResistance = Highest(price, BulgeLengthPrice2);
#NearTResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTResistance.SetStyle(Curve.SHORT_DASH);
def NearTSupport = Lowest(price, SqueezeLengthPrice2);
#NearTSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTSupport.SetStyle(Curve.SHORT_DASH);

def NearTResistance1 = Highest(price, BulgeLengthPrice3);
def NearTSupport1 = Lowest(price, SqueezeLengthPrice3);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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(period = agperiod), close(period = agperiod), low(period = agperiod), volume(period = agperiod), MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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(period = agperiod), TP_fastLength) - ExpAverage(close(period = agperiod), TP_slowLength));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

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

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
def multiplier_factor = 1.25;
def valS = Average(price, SMA_lengthS);
def average_true_range = Average(TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

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

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
#input coloredCandlesOn = yes;
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 = 10;
def Agreement_LevelOS = -10;

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 UP = Consensus_Level >= 6;
def DOWN = Consensus_Level < -6;

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

#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

#Consensus_Level.AssignValueColor(
#if Consensus_Level > Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level < 0 then Color.RED else
#if Consensus_Level > Consensus_Level[1] and Consensus_Level < 0 then Color.RED
#else Color.GRAY);

def Zero_Line = 0;


#AddCloud(Consensus_Level, Agreement_LevelOB, Color.LIGHT_RED, Color.CURRENT);
#AddCloud(Consensus_Level, Agreement_LevelOS, Color.CURRENT, Color.LIGHT_GREEN);

#plot BulgeCC = Highest(Consensus_Level, BulgeLengthCC);
#BulgeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot SqueezeCC = Lowest(Consensus_Level, SqueezeLengthCC);
#SqueezeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot BulgeCC2 = Highest(Consensus_Level, BulgeLengthCC2);
#BulgeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#BulgeCC2.SetStyle(Curve.SHORT_DASH);

#plot SqueezeCC2 = Lowest(Consensus_Level, SqueezeLengthCC2);
#SqueezeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#SqueezeCC2.SetStyle(Curve.SHORT_DASH);
##########################################################################################################################

def h12 =  hl2(period = agperiod);
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);
C3_MF_Line.AssignValueColor(if ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if priceColor == -1 then Color.RED  else if (priceColor == 1) then Color.GREEN else Color.CURRENT);

#AssignPriceColor(if coloredCandlesOn and ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if coloredCandlesOn and ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if coloredCandlesOn and (price < C3_MF_Line) and (priceColor == -1) then Color.RED  else if coloredCandlesOn and (price > C3_MF_Line) and (priceColor == 1) then Color.GREEN else Color.GRAY);
Thank You. I will test this out and give feedback.
 
Multi time frame indicators do repaint.
Code:
#C3_MF_Line Created by Christopher84 12/01/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.
# Converted to MTF.

input agperiod = {"1 min", "3 min", "7 min", "10 min", "15 min", "30 min", "1 hour", "4 hours","1 day","1 week", default "5 min"};


#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close(period = agperiod);
def price1 = open(period = agperiod);
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthPrice3 = 12;
def SqueezeLengthPrice3 = 12;

def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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];

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 BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;

def IntermResistance = Highest(price, BulgeLengthPrice);
#IntermResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
def IntermSupport = Lowest(price, SqueezeLengthPrice);
#IntermSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);

def NearTResistance = Highest(price, BulgeLengthPrice2);
#NearTResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTResistance.SetStyle(Curve.SHORT_DASH);
def NearTSupport = Lowest(price, SqueezeLengthPrice2);
#NearTSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
#NearTSupport.SetStyle(Curve.SHORT_DASH);

def NearTResistance1 = Highest(price, BulgeLengthPrice3);
def NearTSupport1 = Lowest(price, SqueezeLengthPrice3);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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(period = agperiod), close(period = agperiod), low(period = agperiod), volume(period = agperiod), MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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(period = agperiod), TP_fastLength) - ExpAverage(close(period = agperiod), TP_slowLength));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

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

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
def multiplier_factor = 1.25;
def valS = Average(price, SMA_lengthS);
def average_true_range = Average(TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

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

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
#input coloredCandlesOn = yes;
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 = 10;
def Agreement_LevelOS = -10;

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 UP = Consensus_Level >= 6;
def DOWN = Consensus_Level < -6;

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

#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high(period = agperiod), close(period = agperiod), low(period = agperiod)), 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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

#Consensus_Level.AssignValueColor(
#if Consensus_Level > Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
#else if Consensus_Level < Consensus_Level[1] and Consensus_Level < 0 then Color.RED else
#if Consensus_Level > Consensus_Level[1] and Consensus_Level < 0 then Color.RED
#else Color.GRAY);

def Zero_Line = 0;


#AddCloud(Consensus_Level, Agreement_LevelOB, Color.LIGHT_RED, Color.CURRENT);
#AddCloud(Consensus_Level, Agreement_LevelOS, Color.CURRENT, Color.LIGHT_GREEN);

#plot BulgeCC = Highest(Consensus_Level, BulgeLengthCC);
#BulgeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot SqueezeCC = Lowest(Consensus_Level, SqueezeLengthCC);
#SqueezeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#plot BulgeCC2 = Highest(Consensus_Level, BulgeLengthCC2);
#BulgeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#BulgeCC2.SetStyle(Curve.SHORT_DASH);

#plot SqueezeCC2 = Lowest(Consensus_Level, SqueezeLengthCC2);
#SqueezeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
#SqueezeCC2.SetStyle(Curve.SHORT_DASH);
##########################################################################################################################

def h12 =  hl2(period = agperiod);
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);
C3_MF_Line.AssignValueColor(if ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if priceColor == -1 then Color.RED  else if (priceColor == 1) then Color.GREEN else Color.CURRENT);

#AssignPriceColor(if coloredCandlesOn and ((priceColor == 1) and (price1 > Upper_BandS) and (condition_BandRevDn)) then Color.YELLOW else if coloredCandlesOn and ((priceColor == -1) and (price1 < Lower_BandS) and (condition_BandRevUp)) then Color.YELLOW else if coloredCandlesOn and (price < C3_MF_Line) and (priceColor == -1) then Color.RED  else if coloredCandlesOn and (price > C3_MF_Line) and (priceColor == 1) then Color.GREEN else Color.GRAY);
Can you add 7m? I tried but when I select it, It doesn't show up on the chart. I must be missing something else that needs to be added. This is what I added: {"1 min", "3 min", "7 min", "10 min", "15 min", "30 min", "1 hour", "4 hours","1 day","1 week", default "5 min"};
 
Can you add 7m? I tried but when I select it, It doesn't show up on the chart. I must be missing something else that needs to be added. This is what I added: {"1 min", "3 min", "7 min", "10 min", "15 min", "30 min", "1 hour", "4 hours","1 day","1 week", default "5 min"};
7min is not a valid aggregation. The ToS platform makes these aggregations available:
qpxusOH.png
 
do the bull bear candles repaint on the 5min? or change color after they have painted?
Bull bear does re-paint but the limited time aggregation helps limit the re-painting… the highest aggregation is 10min (if you left settings as is) so on the 5 min chart the candles can repaint up to 5 mins later I believe… but it is working well on the 5 for sure
 
Bull bear does re-paint but the limited time aggregation helps limit the re-painting… the highest aggregation is 10min (if you left settings as is) so on the 5 min chart the candles can repaint up to 5 mins later I believe… but it is working well on the 5 for sure
Hi @HODL-Lay-HE-hoo!,
You are right about Bull Bear Candles repainting until the highest aggregation period closes (after that they stay set). That’s why I like grouping it with TS_v9 strategy which will not repaint. They give a nice read when used together especially when using a higher timeframe chart like the 1 hour TS_v9 strategy to determine the controlling trend. I really appreciate your feedback and work that you’ve done with C3_Max. I have been using it primarily on higher timeframes such as the day or month charts to gauge the trend and find the most significant technical levels. It is tremendously helpful. Happy trading!
 
Hi @HODL-Lay-HE-hoo!,
You are right about Bull Bear Candles repainting until the highest aggregation period closes (after that they stay set). That’s why I like grouping it with TS_v9 strategy which will not repaint. They give a nice read when used together especially when using a higher timeframe chart like the 1 hour TS_v9 strategy to determine the controlling trend. I really appreciate your feedback and work that you’ve done with C3_Max. I have been using it primarily on higher timeframes such as the day or month charts to gauge the trend and find the most significant technical levels. It is tremendously helpful. Happy trading!
So since we are in a downtrend on the 1 hour TS_v9 chart, you just wait to get a down signal on the 5 minute Bull Bear chart and ride it until we get an up arrow?
 
@lolreconlol he uses TS_V9 on the one hour chart and the 5 min chart with different settings. He uses Bull Bear V5 to paint the candles on the 5 min along with TS_V9 and C3_MF_Line. As far as entry not many of us (I would imagine) will say we enter and exit a trade based on X indication… though there are some that do. But test it out and see if it ends up being a good entry.

He explains his use of these indicators on both timeframes a few pages back. Search his name and his comments will show up.
 
Hi Everyone!
I have been working on an Idea I am calling confirmation candles. I often times find myself trying to find agreement among the numerous indicators that I use to help guide my decisions. Unfortunately, a lot of the time this creates indicator overload and analysis paralysis. So I have included 15 indicators of trend within this indicator. You can choose how many of the 15 indicators have to be in agreement in order to confirm the trend. I may have gone a bit overboard here, however it makes it adaptable to individual risk tolerance and trading style.

***Please note that I will always post the newest version of these indicators on page 1 of this thread. I am always happy to answer questions for those who are trying to utilize these indicators. However, I ask that you review my post below explaining the various aspects of the indicators. I'll do my best to continue to elaborate to help everyone.

Here is the newest code for C3_Max! Happy trading!!!
iMuHkuC.png


Code:
#C3_Max_v2 Created by Christopher84 12/14/2021
#Last modified 4/11/2022 removed C3_MF_Line_Extension
# 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 price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;

# 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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);

# 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;#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;#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];

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

#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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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;

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#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];

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.ORANGE);
YHextlineOB.SetLineWeight(2);

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(2);

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);

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);
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);
def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

plot AvgExp9 = ExpAverage(price[-displace], length9);
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;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

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 Color.GREEN else if coloredCandlesOn and (CandleColor == -1) then Color.RED else Color.GRAY);

#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);

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.WHITE 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.YELLOW 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.YELLOW 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);

Here is the code for C3_Max_v2_SPX_Forex.
Code:
#C3_Max_v2_SPX_Forex Created by Christopher84 03/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 price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;

# 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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);

# 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;#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;#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];

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

#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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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;

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

#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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

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);

#Klinger Histogram
#def Klinger_Length = 13;
#def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
#def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
#def condition13 = (KVOH > 0);
#def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#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];

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 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition14 + conditionK1UP + conditionK2UP;

def Agreement_LevelD = (condition1D + condition2D + condition4D + condition5D + condition6D + condition7D + condition8D + condition9D + condition10D + condition11D + condition12D + 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 + conditionOB3 + conditionOB4 + conditionOB5 + conditionOB6 + conditionOB7;
def OS_Level = conditionOS1 + 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.ORANGE);
YHextlineOB.SetLineWeight(2);

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(2);

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);

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);
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);
def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

plot AvgExp9 = ExpAverage(price[-displace], length9);
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;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

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 Color.GREEN else if coloredCandlesOn and (CandleColor == -1) then Color.RED else Color.GRAY);

#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);

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.WHITE 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.YELLOW 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.YELLOW 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);

Here is Confirmation Candles v10.
wx6WYDq.png

IATXQlV.png


Code:
#
#Confirmation Candles V.10
#Created 04/15/2021 by Christopher84
#Select the level of agreement among the 15 indicators included.
#Changed 04/19/2021 to V.3 - Removed ChaikinOsc and replaced with STARCBands. Added squeeze alert.
#Changed 04/20/2021 to V.4 - Added Keltner Channel, Labels, and Buy and Sell Zones. Mean Reversion and Breakout Labels added. Reversal_Alert points added.
#Changed 4/22/2021 to V.5 - Removed Buy/Sell clouds. Created new reversal alert buy(gray points) and take profit (red points). Increase factorK.
#Changed 4/23/2021 to V.6 - Refined reversal signals. Fully integrated Super_OB_OS indicator. Fixed candles going yellow if colored_candles is off.
#Changed 4/26/2021 to V.7 - Refined reversal signals and included Keltner Bandwidth. Adjusted Keltner Channel levels.
#Changed 4/27/2021 to V.8 - Improved reversal signals and included support and resistance zones.
#Changed 05/12/2021 to V.9  - dialed in studies to give stronger signals. Removed reversal buy and sell signals with OB/OS signals. Included OB/OS clouds to indicate favorable zones to buy or take profit. Clouds can also indicate nearterm reversals. Cleaned up code.
#Changed 05/20/2021 to V.10 - Removed Pivot Study and replaced with CIP. Reworked Labels to reflect mean reversion Look to Buy/Look to Sell conditions. Removed Mean Reversion Label. Added new label to show the Confirmation_Level and color coded it to show OB/OS conditions.

#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthK = 150;
def SqueezeLengthK = 150;
def BulgeLengthK2 = 40;
def SqueezeLengthK2 = 40;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 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];

def conditionK1 = price >= Upper_BandK;
def conditionK2 = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK2L = (Upper_BandK[2] < Upper_BandK[1]) and (Lower_BandK[2] < Lower_BandK[1]);
def conditionK3L = (Upper_BandK[3] < Upper_BandK[2]) and (Lower_BandK[3] < Lower_BandK[2]);
def conditionK3 = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);
def BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;
def condition_BWKUP = BandwidthK[1] < BandwidthK;
def condition_BWKDOWN = BandwidthK[1] > BandwidthK;
def BulgeK = Highest(BandwidthK, BulgeLengthK);
def SqueezeK = Lowest(BandwidthK, SqueezeLengthK);
def BulgeK2 = Highest(BandwidthK, BulgeLengthK2);
def SqueezeK2 = Lowest(BandwidthK, SqueezeLengthK2);

plot IntermResistance = Highest(price, BulgeLengthPrice);
IntermResistance.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
plot IntermSupport = Lowest(price, SqueezeLengthPrice);
IntermSupport.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

plot NearTResistance = Highest(price, BulgeLengthPrice2);
NearTResistance.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
NearTResistance.SetStyle(Curve.SHORT_DASH);
plot NearTSupport = Lowest(price, SqueezeLengthPrice2);
NearTSupport.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
NearTSupport.SetStyle(Curve.SHORT_DASH);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;

#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 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;

def condition3 = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;
def conditionOB2 = MoneyFlowIndex > MFIover_Bought;
def conditionOS2 = MoneyFlowIndex < 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;

def condition4 =  (Intermed[1] <= Intermed) or (NearT >= MidLine);
def conditionOB3 = Intermed > FOB;
def conditionOS3 = Intermed < FOS;
def conditionOB4 = NearT > FOB;
def conditionOS4 = NearT < FOS;

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

def condition5 = CIP_UP;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp2);

#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;

#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));

def condition9 = Periods > 0;

#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;

def condition10 = PFE > 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 condition12 = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);

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

#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;

def condition14 = (PROSC > 50);# or ((PROSC[1] < PROSC) and PROSC > 40);
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
input coloredCandlesOn = yes;
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 = 3;

def Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13 + condition14 + conditionK1 + conditionK2;

def conditionChannel1 = Upper_BandK > price;
def conditionChannel2 = Lower_BandK < price;

def UP = Agreement_Level >= Confirmation_Factor;
def DOWN = Agreement_Level < Confirmation_Factor;

AssignPriceColor(if coloredCandlesOn and UP then Color.LIGHT_GREEN else if coloredCandlesOn and DOWN then Color.RED else Color.CURRENT);

#Additional Signals

#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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);

#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 = -3;

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

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

#AddVerticalLine (OS_Buy and !OS_Buy[1], close, Color.GREEN, Curve.SHORT_DASH);
#AddVerticalLine (Neutral and !neutral[1], close, Color.Gray, Curve.SHORT_DASH);
#AddVerticalLine (OB_Sell and OB_Sell and !OB_Sell[1], close, Color.RED, Curve.SHORT_DASH);

def Buy_Opportnity = if OS_Buy then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Buy_Opportnity, Neutral, Color.LIGHT_GREEN, Color.LIGHT_RED);
def Sell_Opportnity = if OB_Sell then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Sell_Opportnity, Neutral, Color.LIGHT_RED, Color.LIGHT_RED);

plot OB_Signal = Upper_BandS crosses above IntermResistance;
OB_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OB_Signal.SetLineWeight(3);
OB_Signal.SetDefaultColor(Color.RED);

plot OS_Signal = (condition_BandRevUP) and (Lower_BandS crosses below IntermSupport);
OS_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OS_Signal.SetLineWeight(3);
OS_Signal.SetDefaultColor(Color.GREEN);

#Squeeze Alert
def length = 20;
def BulgeLength = 150;
def SqueezeLength = 150;
def upperBandBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).UpperBand;
def lowerBandBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).LowerBand;
def midLineBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).MidLine;
def Bandwidth = (upperBandBB - lowerBandBB) / midLineBB * 100;
def Bulge = Highest(Bandwidth, BulgeLength);
def Squeeze = Lowest(Bandwidth, SqueezeLength);

plot Squeeze_Alert = Bandwidth <= Squeeze;
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);

#Trend Signals
#Bollinger_Bands2
def lengthBB = 10;
def Num_Dev_DnBB = -0.8;
def Num_Dev_upBB = 0.8;

def price1 = open;
def sDev = StDev(data = price[-displace], length = lengthBB);
def MidLineBB2 = MovingAverage(averageType, data = price[-displace], length = lengthBB);
def LowerBandBB2 = MidLineBB2 + Num_Dev_DnBB * sDev;
def UpperBandBB2 = MidLineBB2 + Num_Dev_upBB * sDev;

plot UPConfirmSignal = Agreement_Level crosses above Confirmation_Factor;
UPConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UPConfirmSignal.SetLineWeight(1);
UPConfirmSignal.SetDefaultColor(Color.GREEN);

plot DOWNConfirmSignal = Agreement_Level crosses below Confirmation_Factor;
DOWNConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DOWNConfirmSignal.SetLineWeight(1);
DOWNConfirmSignal.SetDefaultColor(Color.RED);

#Labels
def Buy = UP_OS;
def Sell = DOWN_OB;
AddLabel(yes, "Look_To_Buy", if (ConditionK2 and (Agreement_Level < Confirmation_Factor)) then Color.GREEN else Color.GRAY);
AddLabel(yes, "Look_To_Sell", if (ConditionK3 and (Agreement_Level > Confirmation_Factor)) then Color.RED else Color.GRAY);

def MomentumUP = Agreement_Level[1] < Agreement_Level;
def MomentumDOWN = Agreement_Level[1] > Agreement_Level;
AddLabel(yes, "Increasing Momentum", if MomentumUP then Color.GREEN else Color.GRAY);
AddLabel(yes, "Decreasing Momentum", if MomentumDOWN then Color.RED else Color.GRAY);

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));
AddLabel(yes, "BREAKOUT", if conditionBO then Color.GREEN else Color.GRAY);

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));
AddLabel(yes, "BREAKDOWN", if conditionBD then Color.RED else Color.GRAY);

def Squeeze_Signal = Squeeze_Alert;
AddLabel(yes, "SQUEEZE ALERT", if Squeeze_Signal then Color.YELLOW else Color.GRAY);

AddLabel(yes, "Confirmation_Level = " + round(Agreement_Level,1), if ((Agreement_Level >= 12) and (Consensus_Line >= 4)) then Color.RED else if ((Agreement_Level <= 3) and (Consensus_Line <= -3)) then Color.Green else color.Gray);

Alert(Agreement_Level crosses above Confirmation_Factor, "long", Alert.BAR, Sound.DING);
Alert(Agreement_Level crosses below Confirmation_Factor, "short", Alert.BAR, Sound.DING);

Here is the Confirmation Candles lower study.
Code:
#Confirmation Candles Lower V.10
#Created 04/15/2021 by Christopher84
#Select the level of agreement among the 14 indicators included.
#Last changed 04/20/2021 to V.3 - Removed ChaikinOsc and replaced with STARCBands. Adjusted levels to match upper study. Added OB/OS levels.
#Changed 05/12/2021 to V.9  - dialed in studies to give stronger signals.
#Changed 05/20/2021 to V.10 - Removed Pivot Study and replaced with CIP.

#Keltner Channel
declare lower;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

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];

def conditionKup = price >= Upper_BandK;
def conditionKdown = price <= Lower_BandK;

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;

#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 conditionRSI_OB = RSI > RSI_OB;
def conditionRSI_OS = 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;

def condition3 = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;
def conditionMFI_OB = MoneyFlowIndex > MFIover_Bought;
def conditionMFI_OS = MoneyFlowIndex < 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;

def condition4 = (Intermed[1] <= Intermed) or (NearT >= MidLine);
def conditionFOB = Intermed > FOB;
def conditionFOS = Intermed < FOS;

#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];

def condition5 = CIP_UP;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp2);

#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;

#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));

def condition9 = Periods > 0;

#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;

def condition10 = PFE > 0;
def conditionPFE_OB = PFE > UpperLevel;
def conditionPFE_OS = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def conditionBBPB_OB = PercentB > BBPB_OB;
def conditionBBPB_OS = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 condition12 = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);

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

#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;

def condition14 = PROSC > 50;
def conditionPROSC_OB = PROSC > PROSC_OB;
def conditionPROSC_OS = PROSC < PROSC_OS;

#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;
plot Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13 + condition14 + conditionKup;

Agreement_Level.AssignValueColor(
if Agreement_Level > Agreement_Level[1] and Agreement_Level >= Confirmation_Factor then Color.LIGHT_GREEN
else if Agreement_Level < Agreement_Level[1] and Agreement_Level >= Confirmation_Factor then Color.LIGHT_GREEN
else if Agreement_Level < Agreement_Level[1] and Agreement_Level < Confirmation_Factor then Color.RED else
if Agreement_Level > Agreement_Level[1] and Agreement_Level < Confirmation_Factor then Color.DARK_RED
else Color.GRAY);

plot Factor_Line = Confirmation_Factor;
Factor_Line.SetStyle(Curve.SHORT_DASH);
Factor_Line.SetLineWeight(1);
Factor_Line.SetDefaultColor(Color.Gray);

plot OB_Level = 12;
OB_Level.SetPaintingStrategy(PaintingStrategy.LINE);
OB_Level.SetLineWeight(1);
OB_Level.SetDefaultColor(Color.RED);

plot OS_Level = 3;
OS_Level.SetPaintingStrategy(PaintingStrategy.LINE);
OS_Level.SetLineWeight(1);
OS_Level.SetDefaultColor(Color.LIGHT_GREEN);

AddCloud(Agreement_Level, OB_Level, Color.RED, Color.CURRENT);
AddCloud(Agreement_Level, OS_Level, Color.CURRENT, Color.LIGHT_GREEN);

(Confirmation Consensus Candles) C3 v5

This is a new candle painting indicator C3, that I have adapted from the original Confirmation Candles. The main difference between the two indicators is that Confirmation Candles confirms only positive factors for upward price movement, and C3 utilizes both positive and negative factors of price movement and weighs them against each other to derive the Consensus Level. There is a histagram style lower study that goes with it. Check it out! Big thanks to everyone trying out my work and giving feedback.
nOD1krz.png

5QygBoC.png

wPdK4Ht.png

Code:
# (Consensus Confirmation Candles) C3 v6
#
# Created 04/28/2021 by Christopher84
# 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.
#
# v2   - 05/11/2021 - dialed in studies to give stronger signals. Removed reversal buy and sell signals with
#                     OB/OS signals. Included OB/OS clouds to indicate favorable zones to buy or take profit.
#                     Clouds can also indicate nearterm reversals. Cleaned up code.
# v3   - 05/20/2021 - Removed Pivot Study and replaced with CIP. Reworked Labels to reflect mean reversion Look
#                     to Buy/Look to Sell conditions. Removed Mean Reversion Label. Added new label to show the
#                     Confirmation_Level and color coded it to show OB/OS conditions.
# BETA - 05/21/2021 - (barbaros) Consensus Level filter set to above 4 and below -4
# v4   - 05/24/2021 - Consensus Level filter changed to above 6 and below -6
# BETA - 05/29/2021 - (barbaros) Bug fixes
# v5   - 06/01/2021 - Consolidated labels. Added new squeeze condition based on NearTSupport and NearTResistance.
# v5   - 06/04/2021 - Included Ichimoku cloud.
# v6   - 06/09/2021 - Added Arrows using Confirmation_Factor

#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthPrice3 = 12;
def SqueezeLengthPrice3 = 12;

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];

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 BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;

plot IntermResistance = Highest(price, BulgeLengthPrice);
IntermResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
plot IntermSupport = Lowest(price, SqueezeLengthPrice);
IntermSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);

plot NearTResistance = Highest(price, BulgeLengthPrice2);
NearTResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
NearTResistance.SetStyle(Curve.SHORT_DASH);
plot NearTSupport = Lowest(price, SqueezeLengthPrice2);
NearTSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
NearTSupport.SetStyle(Curve.SHORT_DASH);

def NearTResistance1 = Highest(price, BulgeLengthPrice3);
def NearTSupport1 = Lowest(price, SqueezeLengthPrice3);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
input coloredCandlesOn = yes;
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 = 10;
def Agreement_LevelOS = -10;

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 UP = Consensus_Level >= 6;
def DOWN = Consensus_Level < -6;

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

AssignPriceColor(if coloredCandlesOn and priceColor == 1 then Color.LIGHT_GREEN else if coloredCandlesOn and priceColor == -1 then Color.RED else Color.CURRENT);

#Additional Signals
#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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);

#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);
def UP_OS = (Agreement_Level < Agreement_LevelOS) and (Consensus_Line < Super_OS);

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

#AddVerticalLine (OS_Buy and !OS_Buy[1], close, Color.GREEN, Curve.SHORT_DASH);
#AddVerticalLine (Neutral and !neutral[1], close, Color.Gray, Curve.SHORT_DASH);
#AddVerticalLine (OB_Sell and OB_Sell and !OB_Sell[1], close, Color.RED, Curve.SHORT_DASH);

def Buy_Opportnity = if OS_Buy then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Buy_Opportnity, Neutral, Color.LIGHT_GREEN, Color.LIGHT_RED);
def Sell_Opportnity = if OB_Sell then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Sell_Opportnity, Neutral, Color.LIGHT_RED, Color.LIGHT_RED);

plot OB_Signal = Upper_BandS crosses above IntermResistance;
OB_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OB_Signal.SetLineWeight(3);
OB_Signal.SetDefaultColor(Color.RED);

plot OS_Signal = (condition_BandRevUp) and (Lower_BandS crosses below IntermSupport);
OS_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OS_Signal.SetLineWeight(3);
OS_Signal.SetDefaultColor(Color.GREEN);

#Squeeze Alert
def BandwidthC3 = (NearTResistance1 - NearTSupport1);
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);

#Trend Signals
plot UPConfirmSignal = Agreement_Level crosses above Confirmation_Factor;
UPConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UPConfirmSignal.SetLineWeight(1);
UPConfirmSignal.SetDefaultColor(Color.GREEN);

plot DOWNConfirmSignal = Agreement_Level crosses below Confirmation_Factor;
DOWNConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DOWNConfirmSignal.SetLineWeight(1);
DOWNConfirmSignal.SetDefaultColor(Color.RED);

#Bollinger_Bands2
def lengthBB = 10;
def Num_Dev_DnBB = -0.8;
def Num_Dev_upBB = 0.8;

def price1 = open;
def sDev = StDev(data = price[-displace], length = lengthBB);
def MidLineBB2 = MovingAverage(averageType, data = price[-displace], length = lengthBB);
def LowerBandBB2 = MidLineBB2 + Num_Dev_DnBB * sDev;
def UpperBandBB2 = MidLineBB2 + Num_Dev_upBB * sDev;

input tenkan_period = 9;
input kijun_period = 26;
input show_Ichimoku_Cloud = yes;

def Tenkan = (Highest(high, tenkan_period) + Lowest(low, tenkan_period)) / 2;
def Kijun = (Highest(high, kijun_period) + Lowest(low, kijun_period)) / 2;
def "Span A" = (Tenkan[kijun_period] + Kijun[kijun_period]) / 2;
def "Span B" = (Highest(high[kijun_period], 2 * kijun_period) + Lowest(low[kijun_period], 2 * kijun_period)) / 2;
def Chikou = close[-kijun_period];

AddCloud(if show_Ichimoku_Cloud and "Span A" then "Span A" else Double.NaN, "Span B",  Color.WHITE,  Color.GRAY);
#AddCloud("Span A", "Span B", color.WHITE, color.GRAY);

#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);

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.WHITE 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, "SQUEEZE ALERT", if Squeeze_Signal then Color.YELLOW 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);

For those of you that trade FOREX or the SPX, here is a modified version that will function on those instruments.
Code:
# (Consensus Confirmation Candles) C3 v5 FOREX & SPX Compatible
#
# Created 04/28/2021 by Christopher84
# 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.
#
# v2   - 05/11/2021 - dialed in studies to give stronger signals. Removed reversal buy and sell signals with
#                     OB/OS signals. Included OB/OS clouds to indicate favorable zones to buy or take profit.
#                     Clouds can also indicate nearterm reversals. Cleaned up code.
# v3   - 05/20/2021 - Removed Pivot Study and replaced with CIP. Reworked Labels to reflect mean reversion Look
#                     to Buy/Look to Sell conditions. Removed Mean Reversion Label. Added new label to show the
#                     Confirmation_Level and color coded it to show OB/OS conditions.
# BETA - 05/21/2021 - (barbaros) Consensus Level filter set to above 4 and below -4
# v4   - 05/24/2021 - Consensus Level filter changed to above 6 and below -6
# BETA - 05/29/2021 - (barbaros) Bug fixes
# v5   - 06/01/2021 - Consolidated labels. Added new squeeze condition based on NearTSupport and NearTResistance.
# BETA - 06/02/2021 - Modified study set to be compatable with FOREX and SPX.
# v6   - 06/09/2021 - Modified to include Confirmation Arrows
#Keltner Channel
declare upper;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthK = 250;
def SqueezeLengthK = 250;
def BulgeLengthK2 = 150;
def SqueezeLengthK2 = 150;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthPrice3 = 12;
def SqueezeLengthPrice3 = 12;

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];

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 BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;
def BandwidthKS = (Bandwidthk[2]+ Bandwidthk[1] + BandwidthK) / 3;
def BulgeK = Highest(BandwidthKS, BulgeLengthK);
def SqueezeK = Lowest(BandwidthKS, SqueezeLengthK);
def BulgeK2 = Highest(BandwidthKS, BulgeLengthK2);
def SqueezeK2 = Lowest(BandwidthKS, SqueezeLengthK2);
def condition_Keltner_Squeeze = BandwidthKS <= SqueezeK;

plot IntermResistance = Highest(price, BulgeLengthPrice);
IntermResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
plot IntermSupport = Lowest(price, SqueezeLengthPrice);
IntermSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);

plot NearTResistance = Highest(price, BulgeLengthPrice2);
NearTResistance.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
NearTResistance.SetStyle(Curve.SHORT_DASH);
plot NearTSupport = Lowest(price, SqueezeLengthPrice2);
NearTSupport.AssignValueColor(if (conditionK2UP) then Color.GREEN else if (conditionK3DN) then Color.RED else Color.GRAY);
NearTSupport.SetStyle(Curve.SHORT_DASH);

def NearTResistance1 = Highest(price, BulgeLengthPrice3);
def NearTSupport1 = Lowest(price, SqueezeLengthPrice3);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

#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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 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);

#Klinger Histogram
#def Klinger_Length = 13;
#def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
#def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
#def condition13 = (KVOH > 0);
#def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
input coloredCandlesOn = yes;
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 = 10;
def Agreement_LevelOS = -10;

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

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

def Consensus_Level = Agreement_Level - Agreement_LevelD;

def UP = Consensus_Level >= 4;
def DOWN = Consensus_Level < -4;

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

AssignPriceColor(if coloredCandlesOn and priceColor == 1 then Color.LIGHT_GREEN else if coloredCandlesOn and priceColor == -1 then Color.RED else Color.CURRENT);

#Additional Signals
#Keltner #2
input showCloud = yes;
def factorK2 = 3.25;
def lengthK2 = 20;

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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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);

#Super_OB/OS Signal
def OB_Level = conditionOB1 + conditionOB3 + conditionOB4 + conditionOB5 + conditionOB6 + conditionOB7;
def OS_Level = conditionOS1 + 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);
def UP_OS = (Agreement_Level < Agreement_LevelOS) and (Consensus_Line < Super_OS);

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

#AddVerticalLine (OS_Buy and !OS_Buy[1], close, Color.GREEN, Curve.SHORT_DASH);
#AddVerticalLine (Neutral and !neutral[1], close, Color.Gray, Curve.SHORT_DASH);
#AddVerticalLine (OB_Sell and OB_Sell and !OB_Sell[1], close, Color.RED, Curve.SHORT_DASH);

def Buy_Opportnity = if OS_Buy then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Buy_Opportnity, Neutral, Color.LIGHT_GREEN, Color.LIGHT_RED);
def Sell_Opportnity = if OB_Sell then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;
#AddCloud(Sell_Opportnity, Neutral, Color.LIGHT_RED, Color.LIGHT_RED);

plot OB_Signal = Upper_BandS crosses above IntermResistance;
OB_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OB_Signal.SetLineWeight(3);
OB_Signal.SetDefaultColor(Color.RED);

plot OS_Signal = (condition_BandRevUp) and (Lower_BandS crosses below IntermSupport);
OS_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
OS_Signal.SetLineWeight(3);
OS_Signal.SetDefaultColor(Color.GREEN);

#Squeeze Alert
def length = 20;
def BulgeLength = 150;
def SqueezeLength = 150;
def upperBandBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).UpperBand;
def lowerBandBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).LowerBand;
def midLineBB = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, averageType).MidLine;
def Bandwidth = (upperBandBB - lowerBandBB) / midLineBB * 100;
def Bulge = Highest(Bandwidth, BulgeLength);
def Squeeze = Lowest(Bandwidth, SqueezeLength);

def BandwidthC3 = (NearTResistance1 - NearTSupport1);

def IntermResistance2 = Highest(BandwidthC3,BulgeLengthPrice);
def IntermSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice);
#def NearTResistance2 = Highest(BandwidthC3, BulgeLengthPrice2);
#def NearTSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice2);

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);

#Trend Signals
plot UPConfirmSignal = Agreement_Level crosses above Confirmation_Factor;
UPConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UPConfirmSignal.SetLineWeight(1);
UPConfirmSignal.SetDefaultColor(Color.GREEN);

plot DOWNConfirmSignal = Agreement_Level crosses below Confirmation_Factor;
DOWNConfirmSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DOWNConfirmSignal.SetLineWeight(1);
DOWNConfirmSignal.SetDefaultColor(Color.RED);
#Bollinger_Bands2
def lengthBB = 10;
def Num_Dev_DnBB = -0.8;
def Num_Dev_upBB = 0.8;

def price1 = open;
def sDev = StDev(data = price[-displace], length = lengthBB);
def MidLineBB2 = MovingAverage(averageType, data = price[-displace], length = lengthBB);
def LowerBandBB2 = MidLineBB2 + Num_Dev_DnBB * sDev;
def UpperBandBB2 = MidLineBB2 + Num_Dev_upBB * sDev;

#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);

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.WHITE 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, "SQUEEZE ALERT", if Squeeze_Signal then Color.YELLOW 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);
Here's the lower study.
gWWYe5M.png

Code:
#CC Candles Lower V.2
#Created 04/28/2021 by Christopher84
#Modified to V.2 05/11/2021 - dialed in studies to give stronger signals. Included OB/OS Clouds and cleaned up code.
#Changed 05/20/2021 to V.3 - Removed Pivot Study and replaced with CIP.

#Keltner Channel
declare lower;
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
def price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthK = 150;
def SqueezeLengthK = 150;
def BulgeLengthK2 = 40;
def SqueezeLengthK2 = 40;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthCC = 40;
def SqueezeLengthCC = 40;
def BulgeLengthCC2 = 8;
def SqueezeLengthCC2 = 8;

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];

def conditionK1 = price >= Upper_BandK;
def conditionK2 = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3D = price < Lower_BandK;
def conditionK4D = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def conditionK2L = (Upper_BandK[2] < Upper_BandK[1]) and (Lower_BandK[2] < Lower_BandK[1]);
def conditionK3L = (Upper_BandK[3] < Upper_BandK[2]) and (Lower_BandK[3] < Lower_BandK[2]);
def conditionK3 = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;
def condition_BWKUP = BandwidthK[1] < BandwidthK;
def condition_BWKDOWN = BandwidthK[1] > BandwidthK;
def BulgeK = Highest(BandwidthK, BulgeLengthK);
def SqueezeK = Lowest(BandwidthK, SqueezeLengthK);
def BulgeK2 = Highest(BandwidthK, BulgeLengthK2);
def SqueezeK2 = Lowest(BandwidthK, SqueezeLengthK2);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;


#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

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

def Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13 + condition14 + conditionK1 + conditionK2;

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

plot Consensus_Level = Agreement_Level - Agreement_LevelD;

def conditionChannel1 = Upper_BandK > price;
def conditionChannel2 = Lower_BandK < price;

def UP = Consensus_Level >= 0;
def DOWN = Consensus_Level < 0;

Consensus_Level.AssignValueColor(
if Consensus_Level > Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
else if Consensus_Level < Consensus_Level[1] and Consensus_Level >= 0 then Color.LIGHT_GREEN
else if Consensus_Level < Consensus_Level[1] and Consensus_Level < 0 then Color.RED else
if Consensus_Level > Consensus_Level[1] and Consensus_Level < 0 then Color.RED
else Color.GRAY);

def Zero_Line = 0;

AddCloud(Consensus_Level, Agreement_LevelOB, Color.LIGHT_RED, Color.CURRENT);
AddCloud(Consensus_Level, Agreement_LevelOS, Color.CURRENT, Color.LIGHT_GREEN);

plot BulgeCC = Highest(Consensus_Level, BulgeLengthCC);
BulgeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

plot SqueezeCC = Lowest(Consensus_Level, SqueezeLengthCC);
SqueezeCC.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

plot BulgeCC2 = Highest(Consensus_Level, BulgeLengthCC2);
BulgeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
BulgeCC2.SetStyle(Curve.SHORT_DASH);

plot SqueezeCC2 = Lowest(Consensus_Level, SqueezeLengthCC2);
SqueezeCC2.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
SqueezeCC2.SetStyle(Curve.SHORT_DASH);

Here is a custom watchlist column for the Confirmation Candles. If you sort the column, it makes it easier to see OB/OS conditions. Especially when grouped with the Super OB/OS custom watchlist column which is also posted below.
oBocGCU.png

Code:
#Confirmation Level WL developed 04/15/2021 by Christopher Wilson
#Select the level of agreement among the 15 indicators included.
#Changed 05/20/21 Include CIP.

#MACD with Price
declare lower;
def price = close;
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

def fastEMA = ExpAverage(price, fastLength);
def slowEMA = ExpAverage(price, slowLength);
def Value;
def Avg;

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

def condition1 = Value[1] <= Value;

#RSI
input RSI_length = 14;
input RSI_AverageType = AverageType.WILDERS;

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;

#MFI
input 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;

def condition3 = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;

#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;

def condition4 = (Intermed[1] <= Intermed) or (NearT >= MidLine);

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

def condition5 = CIP_UP;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp2);

#DMI Oscillator
input DMI_length = 5;
input averageType = AverageType.WILDERS;

def diPlus = DMI(DMI_length, averageType)."DI+";
def diMinus = DMI(DMI_length, averageType)."DI-";

def Osc = diPlus - diMinus;
def Hist = Osc;
def ZeroLine = 0;

def condition8 = Osc >= ZeroLine;

#Trend_Periods
input TP_fastLength = 3;
input TP_slowLength = 4;

def Periods = sign(ExpAverage(close, TP_fastLength) - ExpAverage(close, TP_slowLength));

def condition9 = Periods > 0;

#Polarized Fractal Efficiency
input PFE_length = 5;
input smoothingLength = 2.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;

def condition10 = PFE > ZERoLine;

#Bollinger Bands PercentB
input BBPB_averageType = AverageType.Simple;
input BBPB_length = 20;
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.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;

def condition11 = PercentB > 50;

#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 condition12 = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);

#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;

def condition13 = (PROSC > 50);

#Trend Confirmation
#Confirmation_Factor range 1-13.
input Confirmation_Factor = 7;
#Use for testing conditions individually.
#def Agreement_Level = condition1;
plot Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13;

def Up = Agreement_Level >= Confirmation_Factor;
def Down = Agreement_Level < Confirmation_Factor;

AssignBackgroundColor(if Up then color.DARK_GREEN else if Down then color.LIGHT_RED else color.black);
Here is the Super OB/OS custom watchlist column.
Code:
#Super_OB_OS_WL
#Created by Christopher84 04/22/2021
#Modified 5/12/2021 Adjusted OB/OS levels.

declare lower;
def BulgeLength = 75;
def SqueezeLength = 75;
def BulgeLength2 = 8;
def SqueezeLength2 = 8;

#RSI
def price = close;
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 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 conditionOB2 = MoneyFlowIndex > MFIover_Bought;
def conditionOS2 = MoneyFlowIndex < 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;

def conditionOB3 = Intermed > FOB;
def conditionOS3 = Intermed < FOS;

def conditionOB4 = NearT > FOB;
def conditionOS4 = NearT < FOS;

#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;

def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#Bollinger Bands PercentB
input BBPB_averageType = AverageType.Simple;
def displace = 0;
def BBPB_length = 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;

def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#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;

def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#OB/OS Calculation

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

plot Consensus_Line = OB_Level - OS_Level;

def Zero_Line = 0;
def Super_OB = 4;
def Super_OS = -3;

def OB = Consensus_Line >= Super_OB;
def OS = Consensus_Line <= Super_OS;

AssignBackgroundColor(if OB then color.light_red else if OS then color.dark_green else color.black);

VhkSdLb.png


Here's a code for a cloud reversal WL column. This will show whether an OB/OS cloud is present. OB clouds are light red and OS clouds will show green. I like to group this with the Confirmation Level and SuperOB_OS for additional context.
Code:
#Cloud_Reversal_WL

#Keltner
declare weak_volume_dependency;
input displace = 0;
input factor = 3.25;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);

plot Avg = average[-displace];

def Upper_BandK = average[-displace] + shift[-displace];
def Lower_BandK = average[-displace] - shift[-displace];

#STARC

input ATR_length = 15;
input SMA_length = 6;
input multiplier_factor = 1.25;

def val = Average(price, sma_length);
def average_true_range = Average(TrueRange(high, close, low), length = atr_length);
def Upper_BandS = val[-displace] + multiplier_factor * average_true_range[-displace];
def Lower_BandS = val[-displace] - multiplier_factor * average_true_range[-displace];

def UP = Lower_BandS < Lower_BandK;
def DOWN = Upper_BandS > Upper_BandK;

AssignBackgroundColor(if DOWN then color.LIGHT_RED else if UP then color.dark_green else color.black);

Here is the WL code for the arrows up/down. If the cell is green there is an arrow up, if red there is an arrow down. Adjust the agperiod as desired.
urBhX7D.png

Code:
plot price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input ColoredCandlesOn = yes;

# 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
input length = 20;
input nK = 1.5;
input 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);

# Parabolic SAR Signal

input accelerationFactor = 0.0275;
input accelerationLimit = 0.2;
def SAR = ParabolicSAR(accelerationFactor = accelerationFactor, accelerationLimit = accelerationLimit);
def bearishCross = Crosses(SAR, price, CrossingDirection.ABOVE);
def signalDown = BearishCross;#If(bearishCross, 0, Double.NaN);
def bullishCross = Crosses(SAR, price, CrossingDirection.BELOW);
def signalUp =  BullishCross;#If(bullishCross, 0, Double.NaN);
def UP = BullishCross;
def DOWN = BearishCross;
def priceColor = if UP then 1
                 else if DOWN then -1
                 else priceColor[1];

def UP1 = (UP == 1);
def DN1 = (DOWN == 1);

AssignBackgroundColor(if (DN1 == 1) then color.LIGHT_RED else if (UP1 == 1) then color.dark_green else color.black);

For those of you that are intrested, here is the Super OB/OS lower indicator.
wea6B5x.png

gKaEnqX.png

Code:
#Super_OB_OS_Lower
#Created by Christopher84 04/22/2021
#Modified 5/12/2021 Included dynamic support and resistance. Adjusted OB/OS levels.

declare lower;
def BulgeLength = 75;
def SqueezeLength = 75;
def BulgeLength2 = 8;
def SqueezeLength2 = 8;

#RSI
def price = close;
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 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 conditionOB2 = MoneyFlowIndex > MFIover_Bought;
def conditionOS2 = MoneyFlowIndex < 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;

def conditionOB3 = Intermed > FOB;
def conditionOS3 = Intermed < FOS;

def conditionOB4 = NearT > FOB;
def conditionOS4 = NearT < FOS;

#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;

def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#Bollinger Bands PercentB
input BBPB_averageType = AverageType.Simple;
def displace = 0;
def BBPB_length = 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;

def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#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;

def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#OB/OS Calculation

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

plot Consensus_Line = OB_Level - OS_Level;

def Zero_Line = 0;

plot Bulge = Highest(Consensus_Line, BulgeLength);
bulge.SetPaintingStrategy(PaintingStrategy.LINE);
bulge.SetLineWeight(1);
bulge.SetDefaultColor(Color.RED);

plot Squeeze = Lowest(Consensus_Line, SqueezeLength);
Squeeze.SetPaintingStrategy(PaintingStrategy.LINE);
Squeeze.SetLineWeight(1);
Squeeze.SetDefaultColor(Color.LIGHT_GREEN);

plot Bulge2 = Highest(Consensus_Line, BulgeLength2);
bulge2.SetPaintingStrategy(PaintingStrategy.LINE);
bulge2.SetStyle(Curve.SHORT_DASH);
bulge2.SetLineWeight(1);
bulge2.SetDefaultColor(Color.GRAY);

plot Squeeze2 = Lowest(Consensus_Line, SqueezeLength2);
Squeeze2.SetPaintingStrategy(PaintingStrategy.LINE);
Squeeze2.SetStyle(Curve.SHORT_DASH);
Squeeze2.SetLineWeight(1);
Squeeze2.SetDefaultColor(Color.GRAY);

input Super_OB = 4;
input Super_OS = -3;

Consensus_Line.AssignValueColor(
if Consensus_Line > Consensus_Line[1] and Consensus_Line >= Zero_Line then Color.LIGHT_GREEN
else if Consensus_Line < Consensus_Line[1] and Consensus_Line >= Zero_Line then Color.LIGHT_GREEN
else if Consensus_Line < Consensus_Line[1] and Consensus_Line < Zero_Line then Color.RED else
if Consensus_Line > Consensus_Line[1] and Consensus_Line < Zero_Line then Color.RED
else Color.GRAY);


AddCloud(Consensus_Line, Super_OB, Color.LIGHT_RED, Color.CURRENT);
AddCloud(Consensus_Line, Super_OS, Color.CURRENT, Color.LIGHT_GREEN);
Here is the Super_OB_OS_SPX_Version.
Code:
#Super_OB_OS_Lower_SPX
#Created by Christopher84 04/22/2021
#Modified 5/12/2021 Included dynamic support and resistance. Adjusted OB/OS levels.

declare lower;
def BulgeLength = 75;
def SqueezeLength = 75;
def BulgeLength2 = 8;
def SqueezeLength2 = 8;

#RSI
def price = close;
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 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 conditionOB2 = MoneyFlowIndex > MFIover_Bought;
#def conditionOS2 = MoneyFlowIndex < 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;

def conditionOB3 = Intermed > FOB;
def conditionOS3 = Intermed < FOS;

def conditionOB4 = NearT > FOB;
def conditionOS4 = NearT < FOS;

#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;

def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#Bollinger Bands PercentB
input BBPB_averageType = AverageType.Simple;
def displace = 0;
def BBPB_length = 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;

def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;

#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;

def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#OB/OS Calculation

def OB_Level = conditionOB1 + conditionOB3 + conditionOB4 + conditionOB5 + conditionOB6 + conditionOB7;
def OS_Level = conditionOS1 + conditionOS3 + conditionOS4 + conditionOS5 + conditionOS6 + conditionOS7;

plot Consensus_Line = OB_Level - OS_Level;

def Zero_Line = 0;

plot Bulge = Highest(Consensus_Line, BulgeLength);
bulge.SetPaintingStrategy(PaintingStrategy.LINE);
bulge.SetLineWeight(1);
bulge.SetDefaultColor(Color.RED);

plot Squeeze = Lowest(Consensus_Line, SqueezeLength);
Squeeze.SetPaintingStrategy(PaintingStrategy.LINE);
Squeeze.SetLineWeight(1);
Squeeze.SetDefaultColor(Color.LIGHT_GREEN);

plot Bulge2 = Highest(Consensus_Line, BulgeLength2);
bulge2.SetPaintingStrategy(PaintingStrategy.LINE);
bulge2.SetStyle(Curve.SHORT_DASH);
bulge2.SetLineWeight(1);
bulge2.SetDefaultColor(Color.GRAY);

plot Squeeze2 = Lowest(Consensus_Line, SqueezeLength2);
Squeeze2.SetPaintingStrategy(PaintingStrategy.LINE);
Squeeze2.SetStyle(Curve.SHORT_DASH);
Squeeze2.SetLineWeight(1);
Squeeze2.SetDefaultColor(Color.GRAY);

input Super_OB = 4;
input Super_OS = -4;

Consensus_Line.AssignValueColor(
if Consensus_Line > Consensus_Line[1] and Consensus_Line > Zero_Line then Color.GREEN
else if Consensus_Line < Consensus_Line[1] and Consensus_Line > Zero_Line then Color.GREEN
else if Consensus_Line < Consensus_Line[1] and Consensus_Line <= Zero_Line then Color.RED else
if Consensus_Line > Consensus_Line[1] and Consensus_Line <= Zero_Line then Color.RED
else Color.RED);

### Bar Color
input ColorCandlesON = no;

def UP = Consensus_Line > 2;
def DOWN = Consensus_Line < -2;

def PriceColor = if UP then 1
                 else if DOWN then -1
                 else PriceColor[1];

AssignPriceColor(
    if ColorCandlesOn and PriceColor == 1 then Color.GREEN
else if ColorCandlesOn and PriceColor == -1 then Color.RED
else Color.CURRENT
);

AddCloud(Consensus_Line, Super_OB, Color.LIGHT_RED, Color.CURRENT);
AddCloud(Consensus_Line, Super_OS, Color.CURRENT, Color.GREEN);

So last but not least, I have had several request to share my MTF Cloud Upper and Lower studies. So here they are!
aJLdasc.png

Code:
# MTF Moving Average Upper Created 05/01/2021 by Christopher84

declare upper;

input price = close;
input length = 10;
input agperiod1 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week"};
input agperiod2 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", default "Week"};

plot avg = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];
avg.SetStyle(Curve.SHORT_DASH);
avg.SetLineWeight(1);

def UP = avg[1] < avg;
def DOWN = avg[1] > avg;
Avg.AssignValueColor(if UP then Color.LIGHT_GREEN else if DOWN then Color.RED else Color.YELLOW);

plot avg2 = ExpAverage(close(period = agperiod2), length);
def height2 = avg2 - avg2[length];
avg2.SetStyle(Curve.SHORT_DASH);
avg2.SetLineWeight(1);

def UP2 = avg2[1] < avg2;
def DOWN2 = avg2[1] > avg2;
Avg2.AssignValueColor(if UP2 then Color.LIGHT_GREEN else if DOWN2 then Color.RED else Color.YELLOW);

AddCloud(avg2, avg, Color.LIGHT_RED, Color.CURRENT);
AddCloud(avg, avg2, Color.LIGHT_GREEN, Color.CURRENT);
Here's the lower MTF Cloud study.
Code:
#MTF Moving Average Lower Created by Christopher84 05/01/2021

declare lower;
#Keltner Channel
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
plot price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthK = 150;
def SqueezeLengthK = 150;
def BulgeLengthK2 = 40;
def SqueezeLengthK2 = 40;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthCC = 40;
def SqueezeLengthCC = 40;
def BulgeLengthCC2 = 8;
def SqueezeLengthCC2 = 8;

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];

def conditionK1 = price >= Upper_BandK;
def conditionK2 = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3D = price < Lower_BandK;
def conditionK4D = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def conditionK2L = (Upper_BandK[2] < Upper_BandK[1]) and (Lower_BandK[2] < Lower_BandK[1]);
def conditionK3L = (Upper_BandK[3] < Upper_BandK[2]) and (Lower_BandK[3] < Lower_BandK[2]);
def conditionK3 = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;
def condition_BWKUP = BandwidthK[1] < BandwidthK;
def condition_BWKDOWN = BandwidthK[1] > BandwidthK;
def BulgeK = Highest(BandwidthK, BulgeLengthK);
def SqueezeK = Lowest(BandwidthK, SqueezeLengthK);
def BulgeK2 = Highest(BandwidthK, BulgeLengthK2);
def SqueezeK2 = Lowest(BandwidthK, SqueezeLengthK2);

plot IntermResistance = Highest(price, BulgeLengthPrice);
IntermResistance.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
plot IntermSupport = Lowest(price, SqueezeLengthPrice);
IntermSupport.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;


#Bollinger Bands PercentB
input BBPB_averageType = AverageType.SIMPLE;
def BBPB_length = 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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;


#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0) and (KVOsc[1] <= KVOsc);
def condition13D = (KVOH < 0) and (KVOsc[1] > KVOsc);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
def coloredCandlesOn = no;
def 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 = 3;

def Agreement_Level = condition1 + condition2 + condition3 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition13 + condition14 + conditionK1 + conditionK2;

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

def Consensus_Level = Agreement_Level - Agreement_LevelD;

def conditionChannel1 = Upper_BandK > price;
def conditionChannel2 = Lower_BandK < price;

def UP = Consensus_Level >= 6;

def DOWN = Consensus_Level < -6;


def priceColor = if UP then 1

                 else if DOWN then -1

                 else priceColor[1];

price.AssignValueColor(if priceColor == 1 then Color.LIGHT_GREEN else if priceColor == -1 then Color.RED else Color.CURRENT);

#EMA's
input length = 10;
input agperiod1 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", default "Day", "Week"};
input agperiod2 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", default "Week"};

plot avg1 = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];
avg1.SetStyle(Curve.SHORT_DASH);
avg1.SetLineWeight(1);

def UP1 = avg1[1] < avg1;
def DOWN1 = avg1[1] > avg1;
Avg1.AssignValueColor(if UP1 then Color.LIGHT_GREEN else if DOWN1 then Color.RED else Color.YELLOW);

plot avg2 = ExpAverage(close(period = agperiod2), length);
def height2 = avg2 - avg2[length];
avg2.SetStyle(Curve.SHORT_DASH);
avg2.SetLineWeight(1);

def UP2 = avg2[1] < avg2;
def DOWN2 = avg2[1] > avg2;
Avg2.AssignValueColor(if UP2 then Color.LIGHT_GREEN else if DOWN2 then Color.RED else Color.YELLOW);

AddCloud(avg2, avg1, Color.LIGHT_RED, Color.CURRENT);
AddCloud(avg1, avg2, Color.LIGHT_GREEN, Color.CURRENT);
Here is the code for the MTF_MA_Lower study for SPX and FOREX.
Code:
#MTF Moving Average Lower SPX Forex Created by Christopher84 04/04/2022

declare lower;
#Keltner Channel
def displace = 0;
def factorK = 2.0;
def lengthK = 20;
plot price = close;
input averageType = AverageType.SIMPLE;
def trueRangeAverageType = AverageType.SIMPLE;
def BulgeLengthK = 150;
def SqueezeLengthK = 150;
def BulgeLengthK2 = 40;
def SqueezeLengthK2 = 40;
def BulgeLengthPrice = 75;
def SqueezeLengthPrice = 75;
def BulgeLengthPrice2 = 20;
def SqueezeLengthPrice2 = 20;
def BulgeLengthCC = 40;
def SqueezeLengthCC = 40;
def BulgeLengthCC2 = 8;
def SqueezeLengthCC2 = 8;

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];

def conditionK1 = price >= Upper_BandK;
def conditionK2 = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3D = price < Lower_BandK;
def conditionK4D = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def conditionK2L = (Upper_BandK[2] < Upper_BandK[1]) and (Lower_BandK[2] < Lower_BandK[1]);
def conditionK3L = (Upper_BandK[3] < Upper_BandK[2]) and (Lower_BandK[3] < Lower_BandK[2]);
def conditionK3 = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);

def BandwidthK = (Upper_BandK - Lower_BandK) / AvgK * 100;
def condition_BWKUP = BandwidthK[1] < BandwidthK;
def condition_BWKDOWN = BandwidthK[1] > BandwidthK;
def BulgeK = Highest(BandwidthK, BulgeLengthK);
def SqueezeK = Lowest(BandwidthK, SqueezeLengthK);
def BulgeK2 = Highest(BandwidthK, BulgeLengthK2);
def SqueezeK2 = Lowest(BandwidthK, SqueezeLengthK2);

plot IntermResistance = Highest(price, BulgeLengthPrice);
IntermResistance.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);
plot IntermSupport = Lowest(price, SqueezeLengthPrice);
IntermSupport.AssignValueColor(if (conditionK2) then Color.GREEN else if (conditionK3) then Color.RED else Color.GRAY);

#MACD with Price
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
input MACD_AverageType = {SMA, default EMA};
def MACDLevel = 0.0;

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

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

#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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;


#Bollinger Bands PercentB
input BBPB_averageType = AverageType.SIMPLE;
def BBPB_length = 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;

def condition11 = PercentB > HalfLine;
def condition11D = PercentB < HalfLine;
def conditionOB6 = PercentB > BBPB_OB;
def conditionOS6 = PercentB < BBPB_OS;


#STARC Bands
def ATR_length = 15;
def SMA_lengthS = 6;
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 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);

#Klinger Histogram
#def Klinger_Length = 13;
#def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
#def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
#def condition13 = (KVOH > 0) and (KVOsc[1] <= KVOsc);
#def condition13D = (KVOH < 0) and (KVOsc[1] > KVOsc);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#Trend Confirmation Calculator
#Confirmation_Factor range 1-15.
def coloredCandlesOn = no;
def 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 = 3;

def Agreement_Level = condition1 + condition2 + condition4 + condition5 + condition6 + condition7 + condition8 + condition9 + condition10 + condition11 + condition12 + condition14 + conditionK1 + conditionK2;

def Agreement_LevelD = (condition1D + condition2D + condition4D + condition5D + condition6D + condition7D + condition8D + condition9D + condition10D + condition11D + condition12D + condition14D + conditionK3D + conditionK4D);

def Consensus_Level = Agreement_Level - Agreement_LevelD;

def conditionChannel1 = Upper_BandK > price;
def conditionChannel2 = Lower_BandK < price;

def UP = Consensus_Level >= 6;

def DOWN = Consensus_Level < -6;


def priceColor = if UP then 1

                 else if DOWN then -1

                 else priceColor[1];

price.AssignValueColor(if priceColor == 1 then Color.LIGHT_GREEN else if priceColor == -1 then Color.RED else Color.CURRENT);

#EMA's
input length = 10;
input agperiod1 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour",  "2 hours", "4 hours", default "Day", "Week"};
input agperiod2 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "2 hours", "4 hours", "Day", default "Week"};

plot avg1 = ExpAverage(close(period = agperiod1), length);
def height = avg - avg[length];
avg1.SetStyle(Curve.SHORT_DASH);
avg1.SetLineWeight(1);

def UP1 = avg1[1] < avg1;
def DOWN1 = avg1[1] > avg1;
Avg1.AssignValueColor(if UP1 then Color.LIGHT_GREEN else if DOWN1 then Color.RED else Color.YELLOW);

plot avg2 = ExpAverage(close(period = agperiod2), length);
def height2 = avg2 - avg2[length];
avg2.SetStyle(Curve.SHORT_DASH);
avg2.SetLineWeight(1);

def UP2 = avg2[1] < avg2;
def DOWN2 = avg2[1] > avg2;
Avg2.AssignValueColor(if UP2 then Color.LIGHT_GREEN else if DOWN2 then Color.RED else Color.YELLOW);

AddCloud(avg2, avg1, Color.LIGHT_RED, Color.CURRENT);
AddCloud(avg1, avg2, Color.LIGHT_GREEN, Color.CURRENT);

This is an MTF STARC. Looks great with the MTF MA Cloud for anyone that is interested.
zpf9ntR.png

Code:
#Created 05/26/2021 by Christopher84

declare weak_volume_dependency;

input price = close;
input ATR_length = 15;
input SMA_length = 6;
input displace = 0;
input multiplier_factor = 1.5;
input agperiod1 = {"1 min", "2 min", "5 min", "10 min", "15 min", "30 min", "1 hour", "4 hours", default "Day", "Week"};
def open = open(period = agperiod1);
def high = high(period = agperiod1);
def low = low(period = agperiod1);
def close = close(period = agperiod1);

    def val = Average(close, SMA_length);

    def average_true_range = Average(TrueRange(high, close, low), length = ATR_length);

    plot Upper_Band = val[-displace] + multiplier_factor * average_true_range[-displace];
    def UP = Upper_Band[1] < Upper_Band;
    def DOWN =  Upper_Band[1] > Upper_Band;
    Upper_Band.AssignValueColor(if UP then Color.LIGHT_GREEN else if DOWN then Color.RED else Color.YELLOW);
    Upper_Band.SetStyle(Curve.SHORT_DASH);
    Upper_Band.SetLineWeight(2);

    plot Middle_Band = val[-displace];
    Middle_Band.SetStyle(Curve.SHORT_DASH);
    Middle_Band.SetLineWeight(2);
    Middle_Band.SetDefaultColor(Color.GRAY);

    plot Lower_Band = val[-displace] - multiplier_factor * average_true_range[-displace];
    def UP2 = Lower_Band[1] < Lower_Band;
    def DOWN2 =  Lower_Band[1] > Lower_Band;
    Lower_Band.AssignValueColor(if UP2 then Color.LIGHT_GREEN else if DOWN2 then Color.RED else Color.YELLOW);
    Lower_Band.SetStyle(Curve.SHORT_DASH);
    Lower_Band.SetLineWeight(2);

Here is one more EMA MTF study that can be useful for scalping. It definitely can help to keep you on the right side of the trade. The default settings will only show on a 1 min chart. The settings have be altered to go to different timeframes. One word of caution, this is an MTF study that can repaint. However, through my own experimentation I have found it to be quite useful for scalping 1 min charts and swinging 1 hour charts (setting must be adjusted to the 1 hour timeframe). The labels are intended to give insight to the broader trend. It is preferrable to scalp in the direction of the larger trend. The Bias label is intended to show potential pivots as well as when the lower timeframe trend is in sync with the larger trend.
iVPzuen.png

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

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);
By request, here is Scalper v3 which includes targets based on fib coefficients. Check it out!
tPIdor5.png

Code:
#Scalper Upper v3 Created 06/13/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 ColoredCandlesOn = yes;

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

def AvgExp2 = ExpAverage(price[-displace], length2);
def UPC2 = AvgExp2 > AvgExp2[1];
def DNC2 = AvgExp2 < AvgExp2[1];

#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;

def direction = if ConsensusR > Consensus_Bias then 1 else if ConsensusR < Consensus_Bias then -1 else 0;

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

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


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);

def upsignal = (ConsensusR crosses above Consensus_Bias);

def downsignal = (ConsensusR crosses below Consensus_Bias);

###------------------------------------------------------------------------------------------
# Profit and Loss Labels
#
# Fill in the 0>0 in the Create Signals section below to match your buy and sell signal conditions
#
# When using large amounts of hisorical data, P/L may take time to calculate
###------------------------------------------------------------------------------------------

input showSignals = yes; #hint showSignals: show buy and sell arrows
input LongTrades = yes; #hint LongTrades: perform long trades
input ShortTrades = yes; #hint ShortTrades: perform short trades
input LabelsOn = yes;
input showLabels  = yes; #hint showLabels: show PL labels at top
input showBubbles = yes; #hint showBubbles: show PL bubbles at close of trade
input useStops = no;     #hint useStops: use stop orders
input useAlerts = no;    #hint useAlerts: use alerts on signals
input tradeDaytimeOnly = no; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market


def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
# Only use market hours when using intraday timeframe
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
###------------------------------------------------------------------------------------------

######################################################
##  Create Signals -
##  FILL IN THIS SECTION
##      replace 0>0 with your conditions for signals
######################################################

def PLBuySignal = if  MarketOpen and (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen and (downsignal) then 1 else 0; # insert condition to create short position in place of the 0>0

def PLBuyStop  = if !useStops then 0 else if  (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0>0

def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day


#######################################
##  Maintain the position of trades
#######################################

def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1

if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
} else {
    if CurrentPosition[1] == 0 {            # FLAT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == 1 {      # LONG
        if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal and ShortTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == -1 {     # SHORT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if ((PLSellStop and useStops) or PLMktStop or (PLBuySignal and LongTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else {
        CurrentPosition = CurrentPosition[1];
    }
}


def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;

# If not already long and get a PLBuySignal
#Plot BuySig = if (!isLong[1] and PLBuySignal and showSignals and LongTrades) then 1 else 0;
plot BuySig = if (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(Color.CYAN);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);

Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);

# If not already short and get a PLSellSignal
plot SellSig = if (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(Color.CYAN);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);

Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);

# If long and get a PLBuyStop
plot BuyStpSig = if (PLBuyStop and isLong[1] and showSignals and useStops) or (isLong[1] and PLMktStop) or (isLong[1] and PLSellSignal and !ShortTrades) then 1 else 0;
BuyStpSig.AssignValueColor(Color.LIGHT_GRAY);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);

Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);


# If short and get a PLSellStop
plot SellStpSig = if (PLSellStop and isShort[1] and showSignals and useStops) or (isShort[1] and PLMktStop) or (isShort[1] and PLBuySignal and !LongTrades) then 1 else 0;
SellStpSig.AssignValueColor(Color.LIGHT_GRAY);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);

Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);
Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);


#######################################
##  Orders
#######################################

def isOrder = if ((isFlat[1] and (BuySig and LongTrades) or (SellSig and ShortTrades)) or (isLong[1] and BuyStpSig or (SellSig and ShortTrades)) or (isShort[1] and SellStpSig or (BuySig and LongTrades))) then 1 else 0 ;
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ((BuySig and LongTrades) or (SellSig and ShortTrades))) then close else orderPrice[1];

def orderCount = CompoundValue(1, if IsNaN(isOrder) or BarNumber() == 1 then 0 else if (BuySig or SellSig) then orderCount[1] + 1 else orderCount[1], 0);


#######################################
##  Price and Profit
#######################################

def profitLoss;


if (!isOrder or orderPrice[1] == 0) {
    profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)) {
    profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
    profitLoss = orderPrice[1] - close;
} else {
    profitLoss = 0;
}


# Total Profit or Loss
def profitLossSum = CompoundValue(1, if IsNaN(isOrder)  or BarNumber() == 1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

# How many trades won or lost
def profitWinners = CompoundValue(1, if IsNaN(profitWinners[1]) or BarNumber() == 1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = CompoundValue(1, if IsNaN(profitLosers[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = CompoundValue(1, if IsNaN(profitPush[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);

# Current Open Trade Profit or Loss
def TradePL = if isLong then Round(((close - orderPrice) / TickSize()) * TickValue()) else if isShort then Round(((orderPrice - close) / TickSize()) * TickValue()) else 0;

# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPrice[1] == 0 or IsNaN(orderPrice[1]) then 0 else Round((profitLoss / TickSize()) * TickValue());

# Closed Orders dollar P/L
def dollarPLSum = Round((profitLossSum / TickSize()) * TickValue());


# Split profits or losses by long and short trades
def profitLong = CompoundValue(1, if IsNaN(profitLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then profitLong[1] + dollarProfitLoss else profitLong[1], 0);
def profitShort = CompoundValue(1, if IsNaN(profitShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then profitShort[1] + dollarProfitLoss else profitShort[1], 0);
def countLong = CompoundValue(1, if IsNaN(countLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then countLong[1] + 1 else countLong[1], 0);
def countShort = CompoundValue(1, if IsNaN(countShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then countShort[1] + 1 else countShort[1], 0);

# What was the biggest winning and losing trade
def biggestWin = CompoundValue(1, if IsNaN(biggestWin[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = CompoundValue(1, if IsNaN(biggestLoss[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);

def ClosedTradeCount = if (isLong or isShort) then orderCount - 1 else orderCount;
def OpenTrades = if (isLong or isShort) then 1 else 0;


# What percent were winners
def PCTWin = if (OpenTrades and (TradePL < 0)) then Round((profitWinners / (ClosedTradeCount + 1)) * 100, 2)
else if (OpenTrades and (TradePL > 0)) then Round(((profitWinners + 1) / (ClosedTradeCount + 1)) * 100, 2) else Round(((profitWinners) / (ClosedTradeCount)) * 100, 2) ;

# Average trade
def avgTrade = if (OpenTrades and (TradePL < 0)) then Round(((dollarPLSum - TradePL) / (ClosedTradeCount + 1)), 2)
else if (OpenTrades and (TradePL > 0)) then Round(((dollarPLSum + TradePL) / (ClosedTradeCount + 1)), 2) else Round(((dollarPLSum) / (ClosedTradeCount)), 2) ;


#######################################
##  Create Labels
#######################################


AddLabel(showLabels and isIntraDay, if MarketOpen then "Market Open" else "Market Closed", Color.WHITE);
AddLabel(showLabels, GetSymbol() + " Tick Size: " + TickSize() + " Value: " + TickValue(), Color.WHITE);
AddLabel(showLabels and (LongTrades and ShortTrades), "Long+Short Trades", Color.WHITE);
AddLabel(showLabels and (LongTrades and !ShortTrades), "Long Trades Only", Color.WHITE);
AddLabel(showLabels and (!LongTrades and ShortTrades), "Short Trades Only", Color.WHITE);
AddLabel(showLabels, "Closed Orders: " + ClosedTradeCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: " + AsDollars(TradePL + dollarPLSum), if ((TradePL + dollarPLSum) > 0) then Color.GREEN else if ((TradePL + dollarPLSum) < 0) then Color.RED else Color.GRAY);

AddLabel(showLabels, "Avg per Trade: " + AsDollars(avgTrade), if avgTrade > 0 then Color.GREEN else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: " + PCTWin + "%", if PCTWin > 50 then Color.GREEN else if PCTWin > 40 then Color.YELLOW else Color.GRAY);

AddLabel(showLabels, "MaxUp: " + AsDollars(biggestWin) + " MaxDown: " + AsDollars(biggestLoss), Color.WHITE);
AddLabel(showLabels, "Long Profit: " + AsDollars(profitLong), if profitLong > 0 then Color.GREEN else if profitLong < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Short Profit: " + AsDollars(profitShort), if profitShort > 0 then Color.GREEN else if profitShort < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(CurrentPosition) and showLabels and OpenTrades then 1 else 0, "Open: " + (if isLong then "Bought" else "Sold") + " @ " + orderPrice, Color.WHITE);
AddLabel(if !IsNaN(orderPrice) and showLabels and OpenTrades then 1 else 0, "Open Trade P/L: " + AsDollars(TradePL), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);
AddLabel(showLabels, "Profit Percentile: " + aspercent(TradePL/BiggestWin), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);;
#######################################
##  Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high,  "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 1);

#######################################
#Assign Price Color
#######################################
def Ceiling = biggestWin;
def Floor = biggestLoss;
def MidCAT = (((biggestWin + avgTrade) / 2) + avgTrade) / 2;
def MidFAT = (((biggestLoss + avgTrade) / 2) + avgTrade) / 2;
def AvgProfitWinners = (((profitWinners) / (ClosedTradeCount + 1)));
input mult = 50;

########################################
##Long Stop
########################################
def LongStop = if (BuySig) then low else Double.NaN;
def LongStopext = if (IsNaN(LongStop) and isLong) then LongStopext[1] else LongStop;
plot LongStopextline = LongStopext;
LongStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongStopextline.SetDefaultColor(Color.ORANGE);
LongStopextline.SetLineWeight(2);

########################################
##Long Targets
########################################
plot LongEntry = if isLong then (orderPrice) else Double.NaN;
LongEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(2);

plot AvgProfitLL = if isLong then (orderPrice + ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLL.SetDefaultColor(Color.WHITE);
AvgProfitLL.SetLineWeight(1);

def LT1 = if isLong then (((biggestWin * .13) / mult) + orderPrice) else Double.NaN;
def LT1ext = if (IsNaN(LT1) and isLong) then LT1ext[1] else LT1;
plot LT1extline = LT1ext;
LT1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT1extline.SetDefaultColor(Color.GRAY);
LT1extline.SetLineWeight(1);

def LT2 = if isLong then (((biggestWin * .236) / mult) + orderPrice) else Double.NaN;
def LT2ext = if (IsNaN(LT2) and isLong) then LT2ext[1] else LT2;
plot LT2extline = LT2ext;
LT2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT2extline.SetDefaultColor(Color.GRAY);
LT2extline.SetLineWeight(1);

def LT3 = if isLong then (((biggestWin * .382) / mult) + orderPrice) else Double.NaN;
def LT3ext = if (IsNaN(LT3) and isLong) then LT3ext[1] else LT3;
plot LT3extline = LT3ext;
LT3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT3extline.SetDefaultColor(Color.GRAY);
LT3extline.SetLineWeight(1);

def LT4 = if isLong then (((biggestWin * .5) / mult) + orderPrice) else Double.NaN;
def LT4ext = if (IsNaN(LT4) and isLong) then LT4ext[1] else LT4;
plot LT4extline = LT4ext;
LT4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT4extline.SetDefaultColor(Color.GRAY);
LT4extline.SetLineWeight(1);

def LT5 = if isLong then (((biggestWin * .618) / mult) + orderPrice) else Double.NaN;
def LT5ext = if (IsNaN(LT5) and isLong) then LT5ext[1] else LT5;
plot LT5extline = LT5ext;
LT5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT5extline.SetDefaultColor(Color.GRAY);
LT5extline.SetLineWeight(1);

def LT6 = if isLong then (((biggestWin * .7495) / mult) + orderPrice) else Double.NaN;
def LT6ext = if (IsNaN(LT6) and isLong) then LT6ext[1] else LT6;
plot LT6extline = LT6ext;
LT6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT6extline.SetDefaultColor(Color.GRAY);
LT6extline.SetLineWeight(1);

def LT7 = if isLong then (((biggestWin * .893) / mult) + orderPrice) else Double.NaN;
def LT7ext = if (IsNaN(LT7) and isLong) then LT7ext[1] else LT7;
plot LT7extline = LT7ext;
LT7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT7extline.SetDefaultColor(Color.GRAY);
LT7extline.SetLineWeight(1);
#########################################
##Short Stop
#########################################
def ShortStop = if SellSig then high else Double.NaN;
def ShortStopext = if (IsNaN(ShortStop) and isShort) then ShortStopext[1] else ShortStop;
plot ShortStopextline = ShortStopext;
ShortStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortStopextline.SetDefaultColor(Color.ORANGE);
ShortStopextline.SetLineWeight(2);

########################################
##Short Targets
########################################
plot ShortEntry = if isShort then (orderPrice) else Double.NaN;
;
ShortEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortEntry.SetDefaultColor(Color.RED);
ShortEntry.SetLineWeight(2);

plot AvgProfitLS = if isShort then (orderPrice - ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLS.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLS.SetDefaultColor(Color.WHITE);
AvgProfitLS.SetLineWeight(1);

def ST1 = if isShort then (orderPrice - (biggestWin * .13) / mult) else Double.NaN;
def ST1ext = if (IsNaN(ST1) and isShort) then ST1ext[1] else ST1;
plot ST1extline = ST1ext;
ST1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST1extline.SetDefaultColor(Color.GRAY);
ST1extline.SetLineWeight(1);

def ST2 = if isShort then (orderPrice - (biggestWin * .236) / mult) else Double.NaN;
def ST2ext = if (IsNaN(ST2) and isShort) then ST2ext[1] else ST2;
plot ST2extline = ST2ext;
ST2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST2extline.SetDefaultColor(Color.GRAY);
ST2extline.SetLineWeight(1);

def ST3 = if isShort then (orderPrice - (biggestWin * .382) / mult) else Double.NaN;
def ST3ext = if (IsNaN(ST3) and isShort) then ST3ext[1] else ST3;
plot ST3extline = ST3ext;
ST3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST3extline.SetDefaultColor(Color.GRAY);
ST3extline.SetLineWeight(1);

def ST4 = if isShort then (orderPrice - (biggestWin * .5) / mult) else Double.NaN;
def ST4ext = if (IsNaN(ST4) and isShort) then ST4ext[1] else ST4;
plot ST4extline = ST4ext;
ST4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST4extline.SetDefaultColor(Color.GRAY);
ST4extline.SetLineWeight(1);

def ST5 = if isShort then (orderPrice - (biggestWin * .618) / mult) else Double.NaN;
def ST5ext = if (IsNaN(ST5) and isShort) then ST5ext[1] else ST5;
plot ST5extline = ST5ext;
ST5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST5extline.SetDefaultColor(Color.GRAY);
ST5extline.SetLineWeight(1);

def ST6 = if isShort then (orderPrice - (biggestWin * .7495) / mult) else Double.NaN;
def ST6ext = if (IsNaN(ST6) and isShort) then ST6ext[1] else ST6;
plot ST6extline = ST6ext;
ST6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST6extline.SetDefaultColor(Color.GRAY);
ST6extline.SetLineWeight(1);

def ST7 = if isShort then (orderPrice - (biggestWin * .893) / mult) else Double.NaN;
def ST7ext = if (IsNaN(ST7) and isShort) then ST7ext[1] else ST7;
plot ST7extline = ST7ext;
ST7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST7extline.SetDefaultColor(Color.GRAY);
ST7extline.SetLineWeight(1);
###################################
##Candle Color
###################################
AssignPriceColor(if coloredCandlesOn and (TradePL > (biggestWin * .75)) then Color.YELLOW else if ((ConsensusR > Consensus_Bias)) then Color.GREEN else if coloredCandlesOn and ((ConsensusR < Consensus_Bias)) then Color.RED else Color.GRAY);

###################################
##Line Bubbles
###################################
AddChartBubble(LabelsOn and BuySig, AvgProfitLL, "Average Profit: $" + avgTrade, Color.LiGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT1, "T1: $" + (biggestwin * .13), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT2, "T2: $" + (biggestwin * .236), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT3, "T3: $" + (biggestwin * .382), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT4, "T4: $" + (biggestwin * .5), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT5, "T5: $" + (biggestwin * .618), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT6, "T6: $" + (biggestwin * .7495), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT7, "T7: $" + (biggestwin * .893), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LongStop, "StopLoss: $ -" + (Orderprice - LongStop) * MULT, Color.ORANGE);

AddChartBubble(LabelsOn and SellSig, AvgprofitLS, "Average Profit: $" + AVGTrade, Color.Red);
AddChartBubble(LabelsOn and SellSig, ST1, "T1: $" + (biggestwin * .13), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST2, "T2: $" + (biggestwin * .236), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST3, "T3: $" + (biggestwin * .382), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST4, "T4: $" + (biggestwin * .5), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST5, "T5: $" + (biggestwin * .618), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST6, "T6: $" + (biggestwin * .7495), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST7, "T7: $" + (biggestwin * .893), Color.Red);
AddChartBubble(LabelsOn and SellSig, ShortStop, "StopLoss: $ -" + (ShortStop - Orderprice) * MULT, Color.ORANGE);

**This is a basic strategy that I have put together and will do my best to continue to improve. It is by no means a perfect strategy, however it does show extremely strong performance on some assets in various time periods. Below are some examples (along with the codes). Please do your due dilligence and check the performance before attempting to use this tool in your trading. Feedback is always welcome.
Here is the code for C3_Max_v2_Strategy_LE_SE. This is a basic code that will trade long and short entries. It tends to work best around 5-10 min charts (especially on the /es), with after market hours turned off. ***Please remember to assess the strategy's performance using the Floating P/L study available on ToS before attempting to implement it in your own trading.
zGppUdD.png

Code:
#C3_Max_v2 Strategy LE_SE Created by Christopher84 03/09/2022
#Last modified 4/11/2022 removed C3_MF_Line_Extension
#Note that this is set for long and short entries.
#Remember to add the floatingPL study to backtest the strategy's performance before attempting to implement it.
#Remember to turn off aftermarket trading.
# 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 price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;

# 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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);

# 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;#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;#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];

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

#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);
def pricecolor11 = price > TrailingStop;
#-----------------------------
#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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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;

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;
def UpStrat1 = Value > Avg1;
def DnStrat1 = Value < Avg1;

#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;
def UpStrat2 = RSI > 50;
def DnStrat2 = RSI < 50;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#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];

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.ORANGE);
YHextlineOB.SetLineWeight(2);

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(2);

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);

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);
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);
def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

plot AvgExp9 = ExpAverage(price[-displace], length9);
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;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

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 Color.GREEN else if coloredCandlesOn and (CandleColor == -1) then Color.RED else Color.GRAY);

#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);

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.WHITE 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.YELLOW 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.YELLOW 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);

#Stochastic
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};

def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);

def UPStrat3 = FullK > 50;
def DNStrat3 = FullK < 50;

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

def CandleColor2 = if (UpCalc2 >= 3) then 1
                 else if (UpCalc2 == 0) then -1
                 else if (priceColor2 == 1) then 1
                 else if (priceColor2 == -1) then -1
                 else CandleColor[1];
AssignPriceColor(if coloredCandlesOn and (CandleColor2 == 1) then Color.GREEN else if coloredCandlesOn and (CandleColor2 == -1) then Color.RED else Color.GRAY);

#Strategy
def UPBias = UpStrat1 + UpStrat2 + UpStrat3;
def DNBias =  DnStrat1 + DnStrat2 + DnStrat3;
def Direction = UPBias - DNBias;

def UPConsensus = Direction > 1;
def DOWNConsensus = Direction < 1;

def priceColorTotal = if UPConsensus then 1
                 else if DOWNConsensus then -1
                 else 0;

def Long_Entry =  (UpConsensus);
def Long_Exit =  (DownConsensus);
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(Direction crosses above 1, "long", Alert.BAR, Sound.DING);
Alert(Direction crosses below 1, "short", Alert.BAR, Sound.DING);
Here is the code for C3_Max_v2_Strategy_LE_LX. This is a basic code that will trade long positions only. It tends to work best around 5-10 min charts (especially on the /es), with after market hours turned off. This strategy can perform well on day charts as well. ***Please remember to assess the strategy's performance using the Floating P/L study available on ToS before attempting to implement it in your own trading.
Cl3Le2v.png

Code:
#C3_Max_v2 Strategy LE_LX Created by Christopher84 03/09/2022
#Note that this is set for long positions only.
#Remember to add the floatingPL study to backtest the strategy's performance before attempting to implement it.
#Remember to turn off aftermarket trading.
# 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 price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;

# 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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);

# 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;#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;#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];

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

#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);
def pricecolor11 = price > TrailingStop;
#-----------------------------
#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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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;

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;
def UpStrat1 = Value > Avg1;
def DnStrat1 = Value < Avg1;

#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;
def UpStrat2 = RSI > 50;
def DnStrat2 = RSI < 50;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#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];

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.ORANGE);
YHextlineOB.SetLineWeight(2);

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(2);

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);

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);
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);
def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

plot AvgExp9 = ExpAverage(price[-displace], length9);
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;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

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 Color.GREEN else if coloredCandlesOn and (CandleColor == -1) then Color.RED else Color.GRAY);

#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);

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.WHITE 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.YELLOW 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.YELLOW 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);

#Stochastic
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};

def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);

def UPStrat3 = FullK > 50;
def DNStrat3 = FullK < 50;

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

def CandleColor2 = if (UpCalc2 >= 3) then 1
                 else if (UpCalc2 == 0) then -1
                 else if (priceColor2 == 1) then 1
                 else if (priceColor2 == -1) then -1
                 else CandleColor[1];
AssignPriceColor(if coloredCandlesOn and (CandleColor2 == 1) then Color.GREEN else if coloredCandlesOn and (CandleColor2 == -1) then Color.RED else Color.GRAY);

#Strategy
def UPBias = UpStrat1 + UpStrat2 + UpStrat3;
def DNBias =  DnStrat1 + DnStrat2 + DnStrat3;
def Direction = UPBias - DNBias;

def UPConsensus = Direction > 1;
def DOWNConsensus = Direction < 1;

def priceColorTotal = if UPConsensus then 1
                 else if DOWNConsensus then -1
                 else 0;

def Long_Entry =  (UpConsensus);
def Long_Exit =  (DownConsensus);
AddOrder(OrderType.BUY_TO_OPEN, condition = Long_Entry, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME, name = "LE");
AddOrder(OrderType.SELL_TO_CLOSE, condition = Long_Exit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME, name = "LX");

Here is the code for EMAD_Range. This indicator helps to trader see the trend (below 0 bearish, above 0 bullish). It also helps to see where potential pullbacks may occur by looking at where previous pullbacks have occured.
bX9H7bU.png

Code:
#EMAD_Range Created by Christopher84 01/05/2022

declare lower;
input length8 = 10;
input length9 = 35;
input length10 = 12;
input show_ema_cloud = yes;
input price1 = close;
input coloredCandlesOn = yes;
def showBreakoutSignals = no;
def displace = 0;

def AvgExp8 = ExpAverage(price1[-displace], length8);
def UPD = AvgExp8[1] < AvgExp8;
#AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

def AvgExp9 = ExpAverage(price1[-displace], length9);
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;
#AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

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

def EMAD = (price1 - AvgExp8);
def UPEMAD = EMAD >= EMAD[1];
def DOWNEMAD = EMAD < EMAD[1];
#EMAD.AssignValueColor(if UPEMAD then Color.LIGHT_GREEN else if DOWNEMAD then Color.RED else Color.GRAY);

def EMAD2 = (price1 - AvgExp9);
def UPEMAD2 = EMAD2 >= EMAD2[1];
def DOWNEMAD2 = EMAD2 < EMAD2[1];
#EMAD2.AssignValueColor(if UPEMAD2 then Color.White else if DOWNEMAD2 then Color.BLUE else Color.GRAY);

def EMADAvg = (EMAD + EMAD2) / 2;
def UPEMADAvg = EMADAvg >= EMADAvg[1];
def DOWNEMADAvg = EMADAvg < EMADAvg[1];
#EMADAvg.AssignValueColor(if UPEMADAvg then Color.LIGHT_GREEN else if DOWNEMADAvg then Color.RED else Color.GRAY);

plot EMADSmooth = ExpAverage(EMADAvg[-displace], length10);


#########################################
input length = 14;
input averageType = AverageType.WILDERS;
def price = EMADSmooth;
#def bottom = Min(close[1], low);
input agperiod1 = AggregationPeriod.DAY;

def o = (EMADSmooth + EMADSmooth[1]) / 2;

def h = Max(EMADSmooth, EMADSmooth[1]);

def l = Min(EMADSmooth, EMADSmooth[1]);

def c = EMADSmooth;

#def open = open(period = agperiod1);
#def high = high(period = agperiod1);
#def low = low(period = agperiod1);
#def close = close(period = agperiod1);
def bottom = Min(c[1], l);
def tr = TrueRange(h, c, l);

def ptr = tr / (bottom + tr / 2);

def APTR = MovingAverage(averageType, ptr, length);
#APTR.SetDefaultColor(GetColor(8));
def UpperBand = c[1] + (APTR * o);
#UpperBand.SetDefaultColor(Color.GRAY);

def LowerBand = c[1] - (APTR * o);
#LowerBand.SetDefaultColor(Color.GRAY);

plot MidBand = (UpperBand + LowerBand) / 2;
MidBand.AssignValueColor(if (MidBand > EMADSmooth) then Color.RED else if (MidBand < EMADSmooth) then Color.GREEN else Color.GRAY);
EMADSmooth.AssignValueColor(if (MidBand > EMADSmooth) then Color.RED else if (MidBand < EMADSmooth) then Color.GREEN else Color.GRAY);

AddCloud(if show_ema_cloud and (MidBand > EMADSmooth) then MidBand else Double.NaN, EMADSmooth, Color.RED, Color.CURRENT);
AddCloud(if show_ema_cloud and (EMADSmooth >= MidBand) then EMADSmooth else Double.NaN, MidBand, Color.GREEN, Color.CURRENT);

def BulgeLength = 100;
def SqueezeLength = 100;
def BulgeLength2 = 200;
def SqueezeLength2 = 200;

plot ZeroLine = 0;
ZeroLine.AssignValueColor(if (EMADSmooth > ZeroLine) then Color.GREEN else if (EMADSmooth < ZeroLine) then Color.RED else Color.YELLOW);

def EMADSUp = EMADSmooth > ZeroLine;
def EMADSDown = EMADSmooth < ZeroLine;

AssignPriceColor (if coloredCandlesOn and (MidBand > EMADSmooth) then Color.RED  else if coloredCandlesOn and (MidBand < EMADSmooth) then Color.GREEN else Color.GRAY);

plot Bulge = Highest(MidBand, BulgeLength);
Bulge.SetDefaultColor(Color.WHITE);
plot Squeeze = Lowest(MidBand, SqueezeLength);
Squeeze.SetDefaultColor(Color.WHITE);

Here is the code for the MTF_MOBO. Not the most intracate script, but very effective for showing trend direction and potential support and resistance. The band color reflects the slope of the bands. Hope you all find this useful.
pzErpDO.png

Code:
#MTF MOBO Created 06/10/2021 by Christopher84

declare upper;
input price = close;
input displace = 0;
input length = 10;
input Num_Dev_Dn = -0.8;
input Num_Dev_up = 0.8;
input averageType = AverageType.Simple;
input agperiod1 = {"1 min", "2 min", "3 min", "5 min", "10 min", "15 min", default "30 min", "1 hour", "2 Hours", "4 hours", "Day", "Week", "Month"};
def coloredMoboCloud = Yes;
input paintCandles = Yes;

def sDev = stdev(data = close(period = agperiod1)[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = close(period = agperiod1)[-displace], length = length);
MidLine.SetDefaultColor(Color.Gray);
MidLine.SetStyle(Curve.SHORT_DASH);
MidLine.SetLineWeight(1);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
#LowerBand.SetStyle(Curve.SHORT_DASH);
LowerBand.SetLineWeight(1);

plot UpperBand = MidLine + num_Dev_Up * sDev;
#UpperBand.SetStyle(Curve.SHORT_DASH);
UpperBand.SetLineWeight(1);

def UP = LowerBand[1] < LowerBand;
def DOWN = LowerBand[1] > LowerBand;
LowerBand.AssignValueColor(if UP then Color.GREEN else if DOWN then Color.RED else Color.YELLOW);

def UP2 = UpperBand[1] < UpperBand;
def DOWN2 = UpperBand[1] > UpperBand;
UpperBand.AssignValueColor(if UP2 then Color.GREEN else if DOWN2 then Color.RED else Color.YELLOW);

AssignPriceColor(if paintCandles then if price > UpperBand then Color.GREEN else if price < LowerBand then Color.RED else Color.GRAY else Color.Current);

Here is the code for the new C3_Max_v2_MA_Strategy. The strategy length can be modified to fit the asset and timeframe you wish to trade. The default setting is geared toward the /es 3min chart. Note that the best results are with aftermarket hours off and I would encourage using the floating p/l study to dial in the MA for best results. Adjust the "strategy MA Length" in settings (see image below).
lBdqAcE.png

KPElycB.png

Code:
#C3_Max_v2 MA_Strategy Created by Christopher84 04/14/2022
# 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 price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
input coloredCandlesOn = yes;

# 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.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);

# 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;#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;#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];

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

#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 condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def condition_BandRevUp = (Lower_BandS < Lower_BandK2);

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;

def condition1 = Value[1] <= Value;
def condition1D = Value[1] > Value;

#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;

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;

#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;

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;

#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];

def condition5 = CIP_UP;
def condition5D = CIP_DOWN;

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

def condition6 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D = (price < AvgExp) and (AvgExp[2] > AvgExp);

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

def condition7 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D = (price < AvgExp2) and (AvgExp2[2] > AvgExp);

#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));

def condition9 = Periods > 0;
def condition9D = Periods < 0;

#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;

def condition10 = PFE > 0;
def condition10D = PFE < 0;
def conditionOB5 = PFE > UpperLevel;
def conditionOS5 = PFE < LowerLevel;

#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;

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);

#Klinger Histogram
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13 = (KVOH > 0);
def condition13D = (KVOH < 0);

#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;

def condition14 = PROSC > 50;
def condition14D = PROSC < 50;
def conditionOB7 = PROSC > PROSC_OB;
def conditionOS7 = PROSC < PROSC_OS;

#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];

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.ORANGE);
YHextlineOB.SetLineWeight(2);

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(2);

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);

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);
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 Strategy_MA_Length = 55;
input show_ema_cloud = yes;

plot AvgExp8 = ExpAverage(price[-displace], length8);
def UPD = AvgExp8[1] < AvgExp8;
AvgExp8.SetStyle(Curve.SHORT_DASH);
#AvgExp8.SetLineWeight(1);

plot AvgExp9 = ExpAverage(price[-displace], Strategy_MA_Length);
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;
AvgExp8.AssignValueColor(if UPEMA then Color.LIGHT_GREEN else if DOWNEMA then Color.RED else Color.YELLOW);

def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
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;

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 Color.GREEN else if coloredCandlesOn and (CandleColor == -1) then Color.RED else Color.GRAY);

#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);

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.WHITE 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.YELLOW 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.YELLOW 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);

#Strategy
def Long_Entry =  (price > AvgExp9);
def Long_Exit =  (price crosses below AvgExp9);
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(price crosses above AvgExp9, "long", Alert.BAR, Sound.DING);
Alert(price crosses below AvgExp9, "short", Alert.BAR, Sound.DING);

Here's a strategy based on Ehler's Distant Coefficient Filter. Showing very reasonable results on the /es 3min chart. The candles are colored to match the strategy. The strategy length can be changed to achieve the optimal results for the asset and timeframe being traded.

UHyFOuD.png

Code:
#Ehler's Distant Coefficient Filter Strategy Created by Christopher84 05/17/2022
input length = 34;
input coloredCandlesOn = yes;
def price = (high + low) / 2;

def coeff = length * price * price - 2 * price * sum(price, length)[1] + sum(price * price, length)[1];

plot Ehlers = sum(coeff * price, length) / sum(coeff, length);
Ehlers.SetDefaultColor(GetColor(1));

def UP1 = (Price > Ehlers);
def DN1 = (Price < Ehlers);

#Condition Calculation
def UPBias = UP1;
def DNBias = DN1;
def Direction = UPBias - DNBias;

AssignPriceColor(if coloredCandlesOn and ((Direction > 0)) then Color.GREEN else if coloredCandlesOn and ((Direction < 0)) then Color.RED else Color.GRAY);

def Long_Entry = (Direction > 0);#(price crosses above BuyStop);
def Long_Exit =  (Direction < 0);#(Price crosses below BuyStop);

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");

Here's a strategy based on HiLo study. Showing very reasonable results on the /es 3min chart as well. The candles are colored to match the strategy. The strategy length can be changed to achieve the optimal results for the asset and timeframe being traded.
x6GHOYa.png


Code:
#HiLo Strategy Created by Christopher84 05/17/2022

input lengthHL = 24;
input coloredCandlesOn = yes;
def price = close;
def maHigh = Average(high, lengthHL);
def maLow = Average(low, lengthHL);
def state = {default init, short, long};
if (close > maHigh) {
    state = state.long;
} else if (close < maLow) {
    state = state.short;
} else {
    state = state[1];
}

plot BuyStop = if state == state.short or state != state[1] then maHigh else  if state == state.long or state != state[1] then maLow else Double.NaN;

def UP1 = (Price > BuyStop);
def DN1 = (Price < BuyStop);

#Condition Calculation
def UPBias = UP1;
def DNBias = DN1;
def Direction = UPBias - DNBias;

AssignPriceColor(if coloredCandlesOn and ((Direction > 0)) then Color.GREEN else if coloredCandlesOn and ((Direction < 0)) then Color.RED else Color.GRAY);

def Long_Entry = (Direction > 0);#(price crosses above BuyStop);
def Long_Exit =  (Direction < 0);#(Price crosses below BuyStop);

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");

Here's the newest TS_Strategy. I have it set up for the /ES 15 min chart for the default settings. I like to pair it with the 3 min chart for scalping. For the 3 min chart change the settings to atr period = 10 and atr factor length to 2.3. Enjoy!
T60MLcd.png

Code:
#TS Strategy Created by Christopher84 08/10/2021
#Modified 05/23/2022 to include Chart Bubbles and Labels.

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.0;
input firstTrade = {default long, short};
input averageType = AverageType.SIMPLE;
input price = close;
input coloredCandlesOn = yes;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

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);

def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
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 LongEnter = (price crosses above trailingStop);
def LongExit = (price crosses below trailingStop);

AddOrder(OrderType.BUY_AUTO, condition = LongEnter, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME, name = "LE");
AddOrder(OrderType.SELL_AUTO, condition = LongExit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME, name = "SE");

AssignPriceColor(if coloredCandlesOn and ((price > TrailingStop)) then Color.GREEN else if coloredCandlesOn and ((price < TrailingStop)) then Color.RED else Color.GRAY);
Alert(price crosses above TrailingStop, "long", Alert.BAR, Sound.DING);
Alert(price crosses below TrailingStop, "short", Alert.BAR, Sound.DING);

def upsignal = (price crosses above trailingStop);;
def downsignal = (price crosses below trailingStop);

###------------------------------------------------------------------------------------------
# Profit and Loss Labels
#
# Fill in the 0>0 in the Create Signals section below to match your buy and sell signal conditions
#
# When using large amounts of hisorical data, P/L may take time to calculate
###------------------------------------------------------------------------------------------

input showSignals = yes; #hint showSignals: show buy and sell arrows
input LongTrades = yes; #hint LongTrades: perform long trades
input ShortTrades = yes; #hint ShortTrades: perform short trades
input showLabels  = yes; #hint showLabels: show PL labels at top
input showBubbles = yes; #hint showBubbles: show PL bubbles at close of trade
input useStops = no;     #hint useStops: use stop orders
input useAlerts = no;    #hint useAlerts: use alerts on signals
input tradeDaytimeOnly = no; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market


def Begin = secondsfromTime(OpenTime);
def End = secondsTillTime(closetime);
# Only use market hours when using intraday timeframe
def isIntraDay = if getaggregationperiod() > 14400000 or getaggregationperiod()==0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
###------------------------------------------------------------------------------------------

######################################################
##  Create Signals -
##  FILL IN THIS SECTION
##      replace 0>0 with your conditions for signals
######################################################

def PLBuySignal = if  MarketOpen AND (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen AND (downsignal) then 1 else 0; # insert condition to create short position in place of the 0>0

def PLBuyStop  = if !useStops then 0 else if  (0>0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0>0) then 1 else 0  ; # insert condition to stop in place of the 0>0

def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day


#######################################
##  Maintain the position of trades
#######################################

def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1

if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
}else{
        if CurrentPosition[1] == 0 {            # FLAT
            if (PLBuySignal AND LongTrades) {
                CurrentPosition = 1;
            } else if (PLSellSignal AND ShortTrades){
                CurrentPosition = -1;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else if CurrentPosition[1] == 1 {      # LONG
            if (PLSellSignal AND ShortTrades){
                CurrentPosition = -1;
            } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal AND ShortTrades==0)){
                CurrentPosition = 0;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else if CurrentPosition[1] == -1 {     # SHORT
            if (PLBuySignal AND LongTrades){
                CurrentPosition = 1;
            } else if ((PLSellStop and useStops) or PLMktStop or (PlBuySignal and LongTrades==0)){
                CurrentPosition = 0;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else {
            CurrentPosition = CurrentPosition[1];
       }
}


def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;

# If not already long and get a PLBuySignal
#Plot BuySig = if (!isLong[1] and PLBuySignal and showSignals and LongTrades) then 1 else 0;
Plot BuySig = if (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.cyan);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);

Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);

# If not already short and get a PLSellSignal
Plot SellSig = if (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.cyan);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);

Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);

# If long and get a PLBuyStop
Plot BuyStpSig = if (PLBuyStop and isLong[1] and showSignals and useStops) or (isLong[1] and PLMktStop) or (isLong[1] and PLSellSignal and !ShortTrades) then 1 else 0;
BuyStpSig.AssignValueColor(color.light_gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);

Alert(BuyStpSig and useAlerts, "Buy Stop Signal",Alert.bar,sound.Ding);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal",Alert.bar,sound.Ding);


# If short and get a PLSellStop
Plot SellStpSig = if (PLSellStop and isShort[1] and showSignals and useStops) or (isShort[1] and PLMktStop) or (isShort[1] and PLBuySignal and !LongTrades) then 1 else 0;
SellStpSig.AssignValueColor(color.light_gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);

Alert(SellStpSig and useAlerts, "Sell Stop Signal",Alert.bar,sound.Ding);
Alert(SellStpSig and useAlerts, "Sell Stop Signal",Alert.bar,sound.Ding);


#######################################
##  Orders
#######################################

def isOrder = if ((isFlat[1] and (BuySig and LongTrades) or (SellSig and ShortTrades)) or (isLong[1] and BuyStpSig or (SellSig and ShortTrades)) or (isShort[1] and SellStpSig or (BuySig and LongTrades))) then 1 else 0 ;
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ((BuySig and LongTrades) or (SellSig and ShortTrades))) then close else orderPrice[1];

def orderCount = compoundValue(1,if isNan(isOrder) or barnumber()==1 then 0 else if (BuySig or SellSig) then orderCount[1]+1 else orderCount[1],0);


#######################################
##  Price and Profit
#######################################

def profitLoss;


if (!isOrder or orderPRice[1]==0){
    profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
    profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
    profitLoss = orderPrice[1] - close;
} else {
    profitLoss = 0;
}


# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder)  or barnumber()==1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(profitWinners[1]) or barnumber()==1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(profitLosers[1])  or barnumber()==1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(profitPush[1])  or barnumber()==1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);

# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;

# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());

# Closed Orders dollar P/L
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());


# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(profitLong[1])  or barnumber()==1 then 0 else if isOrder and isLong[1] then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(profitShort[1])  or barnumber()==1 then 0 else if isOrder and isShort[1] then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(countLong[1])  or barnumber()==1 then 0 else if isOrder and isLong[1] then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(countShort[1])  or barnumber()==1 then 0 else if isOrder and isShort[1] then countShort[1]+1 else countShort[1],0);

# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(biggestWin[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(biggestLoss[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);

def ClosedTradeCount = if (isLong or isShort) then orderCount-1 else orderCount;
def OpenTrades = if (isLong or isShort) then 1 else 0;


# What percent were winners
def PCTWin = if (OpenTrades and (TradePL < 0)) then round((profitWinners/(ClosedTradeCount+1))*100,2)
else if (OpenTrades and (TradePL > 0)) then round(((profitWinners+1)/(ClosedTradeCount+1))*100,2) else round(((profitWinners)/(ClosedTradeCount))*100,2) ;

# Average trade
def avgTrade = if (OpenTrades and (TradePL < 0)) then round(((dollarPLSum - TradePL)/(ClosedTradeCount+1)),2)
else if (OpenTrades and (TradePL > 0)) then round(((dollarPLSum + TradePL)/(ClosedTradeCount+1)),2) else round(((dollarPLSum)/(ClosedTradeCount)),2) ;


#######################################
##  Create Labels
#######################################


AddLabel(showLabels and isIntraDay, if MarketOpen then "Market Open" else "Market Closed", color.white);
AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels and (LongTrades and ShortTrades), "Long+Short Trades", color.white);
AddLabel(showLabels and (LongTrades and !ShortTrades),"Long Trades Only", color.white);
AddLabel(showLabels and (!LongTrades and ShortTrades),"Short Trades Only", color.white);
AddLabel(showLabels, "Closed Orders: " + ClosedTradeCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: "+ AsDollars(TradePL+dollarPLSum), if ((TradePL+dollarPLSum) > 0) then color.green else if ((TradePL+dollarPLSum) < 0) then color.red else color.gray);

AddLabel(showLabels, "Avg per Trade: "+ AsDollars(avgTrade), if avgTrade > 0 then Color.Green else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);

AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels and OpenTrades then 1 else 0, "Open: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels and OpenTrades then 1 else 0, "Open Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);



#######################################
##  Chart Bubbles for Profit/Loss
#######################################


AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high,  "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);

Here is the TS_Strategy_v9 with yellow candles for taking profit. The yellow candles appear when the strategy's profitability is peaking, and the trader should be considering taking profit. Also included in this version:
Stoploss = Orange line
Go Short = Red line
Go Long = Green line
Average Profit = White line
Targets = Gray lines (Targets utilize fib coefficient levels)
Target Labels show expected profit at each level.
Stoploss Label shows expected loss.
****Note that the indicator's default settings are geared for the 15 min /es chart. The "mult" (short for multiplier) setting is set to 50 to match the /es multiplier by default. This must be changed to match the asset you are trading (ie: For stocks it should be changed to 1. For the /mes and /btc it would be set to 5.).
GTMIDQi.png

Code:
#TS Strategy_V9 Created by Christopher84 08/10/2021
#Modified 05/23/2022 to include Chart Bubbles and Labels.
#Modified 05/25/2022 to include Targets and Stoplosses.
#Modified 05/26/2022 to include Line Labels by Dcstocks
#Modified 05/27/2022 to include target 7.

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.0;
input firstTrade = {default long, short};
input averageType = AverageType.SIMPLE;
input price = close;
input coloredCandlesOn = yes;
input LabelsOn = yes;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

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);

def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
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 LongEnter = (price crosses above TrailingStop);
def LongExit = (price crosses below TrailingStop);

AddOrder(OrderType.BUY_AUTO, condition = LongEnter, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME);
AddOrder(OrderType.SELL_AUTO, condition = LongExit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME);

#AssignPriceColor(if coloredCandlesOn and ((price > TrailingStop)) then Color.GREEN else if coloredCandlesOn and ((price < TrailingStop)) then Color.RED else Color.GRAY);
Alert(price crosses above TrailingStop, "long", Alert.BAR, Sound.Ding);
Alert(price crosses below TrailingStop, "short", Alert.BAR, Sound.Ding);

def upsignal = (price crosses above TrailingStop);
;
def downsignal = (price crosses below TrailingStop);

###------------------------------------------------------------------------------------------
# Profit and Loss Labels
#
# Fill in the 0>0 in the Create Signals section below to match your buy and sell signal conditions
#
# When using large amounts of hisorical data, P/L may take time to calculate
###------------------------------------------------------------------------------------------

input showSignals = yes; #hint showSignals: show buy and sell arrows
input LongTrades = yes; #hint LongTrades: perform long trades
input ShortTrades = yes; #hint ShortTrades: perform short trades
input showLabels  = yes; #hint showLabels: show PL labels at top
input showBubbles = yes; #hint showBubbles: show PL bubbles at close of trade
input useStops = no;     #hint useStops: use stop orders
input useAlerts = no;    #hint useAlerts: use alerts on signals
input tradeDaytimeOnly = no; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market


def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
# Only use market hours when using intraday timeframe
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
###------------------------------------------------------------------------------------------

######################################################
##  Create Signals -
##  FILL IN THIS SECTION
##      replace 0>0 with your conditions for signals
######################################################

def PLBuySignal = if  MarketOpen and (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen and (downsignal) then 1 else 0; # insert condition to create short position in place of the 0>0

def PLBuyStop  = if !useStops then 0 else if  (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0>0

def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day


#######################################
##  Maintain the position of trades
#######################################

def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1

if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
} else {
    if CurrentPosition[1] == 0 {            # FLAT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == 1 {      # LONG
        if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal and ShortTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == -1 {     # SHORT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if ((PLSellStop and useStops) or PLMktStop or (PLBuySignal and LongTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else {
        CurrentPosition = CurrentPosition[1];
    }
}


def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;

# If not already long and get a PLBuySignal
#Plot BuySig = if (!isLong[1] and PLBuySignal and showSignals and LongTrades) then 1 else 0;
plot BuySig = if (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(Color.CYAN);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);

Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);

# If not already short and get a PLSellSignal
plot SellSig = if (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(Color.CYAN);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);

Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);

# If long and get a PLBuyStop
plot BuyStpSig = if (PLBuyStop and isLong[1] and showSignals and useStops) or (isLong[1] and PLMktStop) or (isLong[1] and PLSellSignal and !ShortTrades) then 1 else 0;
BuyStpSig.AssignValueColor(Color.LIGHT_GRAY);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);

Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);


# If short and get a PLSellStop
plot SellStpSig = if (PLSellStop and isShort[1] and showSignals and useStops) or (isShort[1] and PLMktStop) or (isShort[1] and PLBuySignal and !LongTrades) then 1 else 0;
SellStpSig.AssignValueColor(Color.LIGHT_GRAY);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);

Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);
Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);


#######################################
##  Orders
#######################################

def isOrder = if ((isFlat[1] and (BuySig and LongTrades) or (SellSig and ShortTrades)) or (isLong[1] and BuyStpSig or (SellSig and ShortTrades)) or (isShort[1] and SellStpSig or (BuySig and LongTrades))) then 1 else 0 ;
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ((BuySig and LongTrades) or (SellSig and ShortTrades))) then close else orderPrice[1];

def orderCount = CompoundValue(1, if IsNaN(isOrder) or BarNumber() == 1 then 0 else if (BuySig or SellSig) then orderCount[1] + 1 else orderCount[1], 0);


#######################################
##  Price and Profit
#######################################

def profitLoss;


if (!isOrder or orderPrice[1] == 0) {
    profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)) {
    profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
    profitLoss = orderPrice[1] - close;
} else {
    profitLoss = 0;
}


# Total Profit or Loss
def profitLossSum = CompoundValue(1, if IsNaN(isOrder)  or BarNumber() == 1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

# How many trades won or lost
def profitWinners = CompoundValue(1, if IsNaN(profitWinners[1]) or BarNumber() == 1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = CompoundValue(1, if IsNaN(profitLosers[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = CompoundValue(1, if IsNaN(profitPush[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);

# Current Open Trade Profit or Loss
def TradePL = if isLong then Round(((close - orderPrice) / TickSize()) * TickValue()) else if isShort then Round(((orderPrice - close) / TickSize()) * TickValue()) else 0;

# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPrice[1] == 0 or IsNaN(orderPrice[1]) then 0 else Round((profitLoss / TickSize()) * TickValue());

# Closed Orders dollar P/L
def dollarPLSum = Round((profitLossSum / TickSize()) * TickValue());


# Split profits or losses by long and short trades
def profitLong = CompoundValue(1, if IsNaN(profitLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then profitLong[1] + dollarProfitLoss else profitLong[1], 0);
def profitShort = CompoundValue(1, if IsNaN(profitShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then profitShort[1] + dollarProfitLoss else profitShort[1], 0);
def countLong = CompoundValue(1, if IsNaN(countLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then countLong[1] + 1 else countLong[1], 0);
def countShort = CompoundValue(1, if IsNaN(countShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then countShort[1] + 1 else countShort[1], 0);

# What was the biggest winning and losing trade
def biggestWin = CompoundValue(1, if IsNaN(biggestWin[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = CompoundValue(1, if IsNaN(biggestLoss[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);

def ClosedTradeCount = if (isLong or isShort) then orderCount - 1 else orderCount;
def OpenTrades = if (isLong or isShort) then 1 else 0;


# What percent were winners
def PCTWin = if (OpenTrades and (TradePL < 0)) then Round((profitWinners / (ClosedTradeCount + 1)) * 100, 2)
else if (OpenTrades and (TradePL > 0)) then Round(((profitWinners + 1) / (ClosedTradeCount + 1)) * 100, 2) else Round(((profitWinners) / (ClosedTradeCount)) * 100, 2) ;

# Average trade
def avgTrade = if (OpenTrades and (TradePL < 0)) then Round(((dollarPLSum - TradePL) / (ClosedTradeCount + 1)), 2)
else if (OpenTrades and (TradePL > 0)) then Round(((dollarPLSum + TradePL) / (ClosedTradeCount + 1)), 2) else Round(((dollarPLSum) / (ClosedTradeCount)), 2) ;


#######################################
##  Create Labels
#######################################


AddLabel(showLabels and isIntraDay, if MarketOpen then "Market Open" else "Market Closed", Color.WHITE);
AddLabel(showLabels, GetSymbol() + " Tick Size: " + TickSize() + " Value: " + TickValue(), Color.WHITE);
AddLabel(showLabels and (LongTrades and ShortTrades), "Long+Short Trades", Color.WHITE);
AddLabel(showLabels and (LongTrades and !ShortTrades), "Long Trades Only", Color.WHITE);
AddLabel(showLabels and (!LongTrades and ShortTrades), "Short Trades Only", Color.WHITE);
AddLabel(showLabels, "Closed Orders: " + ClosedTradeCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: " + AsDollars(TradePL + dollarPLSum), if ((TradePL + dollarPLSum) > 0) then Color.GREEN else if ((TradePL + dollarPLSum) < 0) then Color.RED else Color.GRAY);

AddLabel(showLabels, "Avg per Trade: " + AsDollars(avgTrade), if avgTrade > 0 then Color.GREEN else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: " + PCTWin + "%", if PCTWin > 50 then Color.GREEN else if PCTWin > 40 then Color.YELLOW else Color.GRAY);

AddLabel(showLabels, "MaxUp: " + AsDollars(biggestWin) + " MaxDown: " + AsDollars(biggestLoss), Color.WHITE);
AddLabel(showLabels, "Long Profit: " + AsDollars(profitLong), if profitLong > 0 then Color.GREEN else if profitLong < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Short Profit: " + AsDollars(profitShort), if profitShort > 0 then Color.GREEN else if profitShort < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(CurrentPosition) and showLabels and OpenTrades then 1 else 0, "Open: " + (if isLong then "Bought" else "Sold") + " @ " + orderPrice, Color.WHITE);
AddLabel(if !IsNaN(orderPrice) and showLabels and OpenTrades then 1 else 0, "Open Trade P/L: " + AsDollars(TradePL), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);
AddLabel(showLabels, "Profit Percentile: " + aspercent(TradePL/BiggestWin), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);;
#######################################
##  Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high,  "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 1);

#######################################
#Assign Price Color
#######################################
def Ceiling = biggestWin;
def Floor = biggestLoss;
def MidCAT = (((biggestWin + avgTrade) / 2) + avgTrade) / 2;
def MidFAT = (((biggestLoss + avgTrade) / 2) + avgTrade) / 2;
def AvgProfitWinners = (((profitWinners) / (ClosedTradeCount + 1)));
input mult = 50;

########################################
##Long Stop
########################################
def LongStop = if (BuySig) then low else Double.NaN;
def LongStopext = if (IsNaN(LongStop) and isLong) then LongStopext[1] else LongStop;
plot LongStopextline = LongStopext;
LongStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongStopextline.SetDefaultColor(Color.ORANGE);
LongStopextline.SetLineWeight(2);

########################################
##Long Targets
########################################
plot LongEntry = if isLong then (orderPrice) else Double.NaN;
LongEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(2);

plot AvgProfitLL = if isLong then (orderPrice + ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLL.SetDefaultColor(Color.WHITE);
AvgProfitLL.SetLineWeight(1);

def LT1 = if isLong then (((biggestWin * .13) / mult) + orderPrice) else Double.NaN;
def LT1ext = if (IsNaN(LT1) and isLong) then LT1ext[1] else LT1;
plot LT1extline = LT1ext;
LT1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT1extline.SetDefaultColor(Color.GRAY);
LT1extline.SetLineWeight(1);

def LT2 = if isLong then (((biggestWin * .236) / mult) + orderPrice) else Double.NaN;
def LT2ext = if (IsNaN(LT2) and isLong) then LT2ext[1] else LT2;
plot LT2extline = LT2ext;
LT2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT2extline.SetDefaultColor(Color.GRAY);
LT2extline.SetLineWeight(1);

def LT3 = if isLong then (((biggestWin * .382) / mult) + orderPrice) else Double.NaN;
def LT3ext = if (IsNaN(LT3) and isLong) then LT3ext[1] else LT3;
plot LT3extline = LT3ext;
LT3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT3extline.SetDefaultColor(Color.GRAY);
LT3extline.SetLineWeight(1);

def LT4 = if isLong then (((biggestWin * .5) / mult) + orderPrice) else Double.NaN;
def LT4ext = if (IsNaN(LT4) and isLong) then LT4ext[1] else LT4;
plot LT4extline = LT4ext;
LT4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT4extline.SetDefaultColor(Color.GRAY);
LT4extline.SetLineWeight(1);

def LT5 = if isLong then (((biggestWin * .618) / mult) + orderPrice) else Double.NaN;
def LT5ext = if (IsNaN(LT5) and isLong) then LT5ext[1] else LT5;
plot LT5extline = LT5ext;
LT5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT5extline.SetDefaultColor(Color.GRAY);
LT5extline.SetLineWeight(1);

def LT6 = if isLong then (((biggestWin * .7495) / mult) + orderPrice) else Double.NaN;
def LT6ext = if (IsNaN(LT6) and isLong) then LT6ext[1] else LT6;
plot LT6extline = LT6ext;
LT6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT6extline.SetDefaultColor(Color.GRAY);
LT6extline.SetLineWeight(1);

def LT7 = if isLong then (((biggestWin * .893) / mult) + orderPrice) else Double.NaN;
def LT7ext = if (IsNaN(LT7) and isLong) then LT7ext[1] else LT7;
plot LT7extline = LT7ext;
LT7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT7extline.SetDefaultColor(Color.GRAY);
LT7extline.SetLineWeight(1);
#########################################
##Short Stop
#########################################
def ShortStop = if SellSig then high else Double.NaN;
def ShortStopext = if (IsNaN(ShortStop) and isShort) then ShortStopext[1] else ShortStop;
plot ShortStopextline = ShortStopext;
ShortStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortStopextline.SetDefaultColor(Color.ORANGE);
ShortStopextline.SetLineWeight(2);

########################################
##Short Targets
########################################
plot ShortEntry = if isShort then (orderPrice) else Double.NaN;
;
ShortEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortEntry.SetDefaultColor(Color.RED);
ShortEntry.SetLineWeight(2);

plot AvgProfitLS = if isShort then (orderPrice - ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLS.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLS.SetDefaultColor(Color.WHITE);
AvgProfitLS.SetLineWeight(1);

def ST1 = if isShort then (orderPrice - (biggestWin * .13) / mult) else Double.NaN;
def ST1ext = if (IsNaN(ST1) and isShort) then ST1ext[1] else ST1;
plot ST1extline = ST1ext;
ST1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST1extline.SetDefaultColor(Color.GRAY);
ST1extline.SetLineWeight(1);

def ST2 = if isShort then (orderPrice - (biggestWin * .236) / mult) else Double.NaN;
def ST2ext = if (IsNaN(ST2) and isShort) then ST2ext[1] else ST2;
plot ST2extline = ST2ext;
ST2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST2extline.SetDefaultColor(Color.GRAY);
ST2extline.SetLineWeight(1);

def ST3 = if isShort then (orderPrice - (biggestWin * .382) / mult) else Double.NaN;
def ST3ext = if (IsNaN(ST3) and isShort) then ST3ext[1] else ST3;
plot ST3extline = ST3ext;
ST3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST3extline.SetDefaultColor(Color.GRAY);
ST3extline.SetLineWeight(1);

def ST4 = if isShort then (orderPrice - (biggestWin * .5) / mult) else Double.NaN;
def ST4ext = if (IsNaN(ST4) and isShort) then ST4ext[1] else ST4;
plot ST4extline = ST4ext;
ST4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST4extline.SetDefaultColor(Color.GRAY);
ST4extline.SetLineWeight(1);

def ST5 = if isShort then (orderPrice - (biggestWin * .618) / mult) else Double.NaN;
def ST5ext = if (IsNaN(ST5) and isShort) then ST5ext[1] else ST5;
plot ST5extline = ST5ext;
ST5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST5extline.SetDefaultColor(Color.GRAY);
ST5extline.SetLineWeight(1);

def ST6 = if isShort then (orderPrice - (biggestWin * .7495) / mult) else Double.NaN;
def ST6ext = if (IsNaN(ST6) and isShort) then ST6ext[1] else ST6;
plot ST6extline = ST6ext;
ST6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST6extline.SetDefaultColor(Color.GRAY);
ST6extline.SetLineWeight(1);

def ST7 = if isShort then (orderPrice - (biggestWin * .893) / mult) else Double.NaN;
def ST7ext = if (IsNaN(ST7) and isShort) then ST7ext[1] else ST7;
plot ST7extline = ST7ext;
ST7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST7extline.SetDefaultColor(Color.GRAY);
ST7extline.SetLineWeight(1);
###################################
##Candle Color
###################################
AssignPriceColor(if coloredCandlesOn and (TradePL > (biggestWin * .75)) then Color.YELLOW else if ((price > TrailingStop)) then Color.GREEN else if coloredCandlesOn and ((price < TrailingStop)) then Color.RED else Color.GRAY);

###################################
##Line Bubbles
###################################
AddChartBubble(LabelsOn and BuySig, AvgProfitLL, "Average Profit: $" + avgTrade, Color.LiGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT1, "T1: $" + (biggestwin * .13), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT2, "T2: $" + (biggestwin * .236), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT3, "T3: $" + (biggestwin * .382), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT4, "T4: $" + (biggestwin * .5), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT5, "T5: $" + (biggestwin * .618), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT6, "T6: $" + (biggestwin * .7495), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT7, "T7: $" + (biggestwin * .893), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LongStop, "StopLoss: $ -" + (Orderprice - LongStop) * MULT, Color.ORANGE);

AddChartBubble(LabelsOn and SellSig, AvgprofitLS, "Average Profit: $" + AVGTrade, Color.Red);
AddChartBubble(LabelsOn and SellSig, ST1, "T1: $" + (biggestwin * .13), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST2, "T2: $" + (biggestwin * .236), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST3, "T3: $" + (biggestwin * .382), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST4, "T4: $" + (biggestwin * .5), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST5, "T5: $" + (biggestwin * .618), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST6, "T6: $" + (biggestwin * .7495), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST7, "T7: $" + (biggestwin * .893), Color.Red);
AddChartBubble(LabelsOn and SellSig, ShortStop, "StopLoss: $ -" + (ShortStop - Orderprice) * MULT, Color.ORANGE);

Here is the PLD_Bands Strategy for anyone that is interested. In short, this strategy utilizes a displaced average and it's high/low over 5 periods to determine trend. Much like Ichimoku when price action is above or below kumo. It's setup for the 3min /es chart, however the displace is adjustable allowing it to work on a variety of assets.
7GV34vV.png

Code:
#PLD_Bands Strategy Created By Christopher84 07/18/2022

input price = close;
input length = 5;
input displace = -11;
input showBreakoutSignals = yes;
input ColoredCandlesOn = yes;
input LabelsOn = yes;
input BulgeLengthPrice = 5;
input SqueezeLengthPrice = 5;

plot PLD = expAverage(price[-displace],length);
def UpperBand = Highest(PLD, BulgeLengthPrice);
def LowerBand = Lowest(PLD, SqueezeLengthPrice);
plot UpSignal = price crosses above UpperBand;
plot DownSignal = price crosses below LowerBand;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

PLD.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

def LongEnter = (price crosses above UpperBand);
def LongExit = (price crosses below LowerBand);

AddOrder(OrderType.BUY_AUTO, condition = LongEnter, price = open[-1], 1, tickcolor = GetColor(1), arrowcolor = Color.LIME);
AddOrder(OrderType.SELL_AUTO, condition = LongExit, price = open[-1], 1, tickcolor = GetColor(2), arrowcolor = Color.LIME);


###------------------------------------------------------------------------------------------
# Profit and Loss Labels
#
# Fill in the 0>0 in the Create Signals section below to match your buy and sell signal conditions
#
# When using large amounts of hisorical data, P/L may take time to calculate
###------------------------------------------------------------------------------------------

input showSignals = yes; #hint showSignals: show buy and sell arrows
input LongTrades = yes; #hint LongTrades: perform long trades
input ShortTrades = yes; #hint ShortTrades: perform short trades
input showLabels  = yes; #hint showLabels: show PL labels at top
input showBubbles = yes; #hint showBubbles: show PL bubbles at close of trade
input useStops = no;     #hint useStops: use stop orders
input useAlerts = no;    #hint useAlerts: use alerts on signals
input tradeDaytimeOnly = no; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market


def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
# Only use market hours when using intraday timeframe
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
###------------------------------------------------------------------------------------------

######################################################
##  Create Signals -
##  FILL IN THIS SECTION
##      replace 0>0 with your conditions for signals
######################################################

def PLBuySignal = if  MarketOpen and (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen and (downsignal) then 1 else 0; # insert condition to create short position in place of the 0>0

def PLBuyStop  = if !useStops then 0 else if  (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0>0

def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day


#######################################
##  Maintain the position of trades
#######################################

def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1

if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
} else {
    if CurrentPosition[1] == 0 {            # FLAT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == 1 {      # LONG
        if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal and ShortTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == -1 {     # SHORT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if ((PLSellStop and useStops) or PLMktStop or (PLBuySignal and LongTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else {
        CurrentPosition = CurrentPosition[1];
    }
}


def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;

# If not already long and get a PLBuySignal
#Plot BuySig = if (!isLong[1] and PLBuySignal and showSignals and LongTrades) then 1 else 0;
plot BuySig = if (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(Color.CYAN);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);

Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal", Alert.BAR, Sound.Ding);

# If not already short and get a PLSellSignal
plot SellSig = if (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(Color.CYAN);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);

Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal", Alert.BAR, Sound.Ding);

# If long and get a PLBuyStop
plot BuyStpSig = if (PLBuyStop and isLong[1] and showSignals and useStops) or (isLong[1] and PLMktStop) or (isLong[1] and PLSellSignal and !ShortTrades) then 1 else 0;
BuyStpSig.AssignValueColor(Color.LIGHT_GRAY);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);

Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);
Alert(BuyStpSig and useAlerts, "Buy Stop Signal", Alert.BAR, Sound.Ding);


# If short and get a PLSellStop
plot SellStpSig = if (PLSellStop and isShort[1] and showSignals and useStops) or (isShort[1] and PLMktStop) or (isShort[1] and PLBuySignal and !LongTrades) then 1 else 0;
SellStpSig.AssignValueColor(Color.LIGHT_GRAY);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);

Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);
Alert(SellStpSig and useAlerts, "Sell Stop Signal", Alert.BAR, Sound.Ding);


#######################################
##  Orders
#######################################

def isOrder = if ((isFlat[1] and (BuySig and LongTrades) or (SellSig and ShortTrades)) or (isLong[1] and BuyStpSig or (SellSig and ShortTrades)) or (isShort[1] and SellStpSig or (BuySig and LongTrades))) then 1 else 0 ;
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ((BuySig and LongTrades) or (SellSig and ShortTrades))) then close else orderPrice[1];

def orderCount = CompoundValue(1, if IsNaN(isOrder) or BarNumber() == 1 then 0 else if (BuySig or SellSig) then orderCount[1] + 1 else orderCount[1], 0);


#######################################
##  Price and Profit
#######################################

def profitLoss;


if (!isOrder or orderPrice[1] == 0) {
    profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)) {
    profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
    profitLoss = orderPrice[1] - close;
} else {
    profitLoss = 0;
}


# Total Profit or Loss
def profitLossSum = CompoundValue(1, if IsNaN(isOrder)  or BarNumber() == 1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

# How many trades won or lost
def profitWinners = CompoundValue(1, if IsNaN(profitWinners[1]) or BarNumber() == 1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = CompoundValue(1, if IsNaN(profitLosers[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = CompoundValue(1, if IsNaN(profitPush[1])  or BarNumber() == 1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);

# Current Open Trade Profit or Loss
def TradePL = if isLong then Round(((close - orderPrice) / TickSize()) * TickValue()) else if isShort then Round(((orderPrice - close) / TickSize()) * TickValue()) else 0;

# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPrice[1] == 0 or IsNaN(orderPrice[1]) then 0 else Round((profitLoss / TickSize()) * TickValue());

# Closed Orders dollar P/L
def dollarPLSum = Round((profitLossSum / TickSize()) * TickValue());


# Split profits or losses by long and short trades
def profitLong = CompoundValue(1, if IsNaN(profitLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then profitLong[1] + dollarProfitLoss else profitLong[1], 0);
def profitShort = CompoundValue(1, if IsNaN(profitShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then profitShort[1] + dollarProfitLoss else profitShort[1], 0);
def countLong = CompoundValue(1, if IsNaN(countLong[1])  or BarNumber() == 1 then 0 else if isOrder and isLong[1] then countLong[1] + 1 else countLong[1], 0);
def countShort = CompoundValue(1, if IsNaN(countShort[1])  or BarNumber() == 1 then 0 else if isOrder and isShort[1] then countShort[1] + 1 else countShort[1], 0);

# What was the biggest winning and losing trade
def biggestWin = CompoundValue(1, if IsNaN(biggestWin[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = CompoundValue(1, if IsNaN(biggestLoss[1]) or BarNumber() == 1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);

def ClosedTradeCount = if (isLong or isShort) then orderCount - 1 else orderCount;
def OpenTrades = if (isLong or isShort) then 1 else 0;


# What percent were winners
def PCTWin = if (OpenTrades and (TradePL < 0)) then Round((profitWinners / (ClosedTradeCount + 1)) * 100, 2)
else if (OpenTrades and (TradePL > 0)) then Round(((profitWinners + 1) / (ClosedTradeCount + 1)) * 100, 2) else Round(((profitWinners) / (ClosedTradeCount)) * 100, 2) ;

# Average trade
def avgTrade = if (OpenTrades and (TradePL < 0)) then Round(((dollarPLSum - TradePL) / (ClosedTradeCount + 1)), 2)
else if (OpenTrades and (TradePL > 0)) then Round(((dollarPLSum + TradePL) / (ClosedTradeCount + 1)), 2) else Round(((dollarPLSum) / (ClosedTradeCount)), 2) ;


#######################################
##  Create Labels
#######################################


AddLabel(showLabels and isIntraDay, if MarketOpen then "Market Open" else "Market Closed", Color.WHITE);
AddLabel(showLabels, GetSymbol() + " Tick Size: " + TickSize() + " Value: " + TickValue(), Color.WHITE);
AddLabel(showLabels and (LongTrades and ShortTrades), "Long+Short Trades", Color.WHITE);
AddLabel(showLabels and (LongTrades and !ShortTrades), "Long Trades Only", Color.WHITE);
AddLabel(showLabels and (!LongTrades and ShortTrades), "Short Trades Only", Color.WHITE);
AddLabel(showLabels, "Closed Orders: " + ClosedTradeCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: " + AsDollars(TradePL + dollarPLSum), if ((TradePL + dollarPLSum) > 0) then Color.GREEN else if ((TradePL + dollarPLSum) < 0) then Color.RED else Color.GRAY);

AddLabel(showLabels, "Avg per Trade: " + AsDollars(avgTrade), if avgTrade > 0 then Color.GREEN else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: " + PCTWin + "%", if PCTWin > 50 then Color.GREEN else if PCTWin > 40 then Color.YELLOW else Color.GRAY);

AddLabel(showLabels, "MaxUp: " + AsDollars(biggestWin) + " MaxDown: " + AsDollars(biggestLoss), Color.WHITE);
AddLabel(showLabels, "Long Profit: " + AsDollars(profitLong), if profitLong > 0 then Color.GREEN else if profitLong < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Short Profit: " + AsDollars(profitShort), if profitShort > 0 then Color.GREEN else if profitShort < 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNaN(CurrentPosition) and showLabels and OpenTrades then 1 else 0, "Open: " + (if isLong then "Bought" else "Sold") + " @ " + orderPrice, Color.WHITE);
AddLabel(if !IsNaN(orderPrice) and showLabels and OpenTrades then 1 else 0, "Open Trade P/L: " + AsDollars(TradePL), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);
AddLabel(showLabels, "Profit Percentile: " + aspercent(TradePL/BiggestWin), if (TradePL > 0) then Color.GREEN else if (TradePL < 0) then Color.RED else Color.GRAY);;
#######################################
##  Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high,  "$" + dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else Color.RED, 1);

#######################################
#Assign Price Color
#######################################
def Ceiling = biggestWin;
def Floor = biggestLoss;
def MidCAT = (((biggestWin + avgTrade) / 2) + avgTrade) / 2;
def MidFAT = (((biggestLoss + avgTrade) / 2) + avgTrade) / 2;
def AvgProfitWinners = (((profitWinners) / (ClosedTradeCount + 1)));
input mult = 50;

########################################
##Long Stop
########################################
def LongStop = if (BuySig) then low else Double.NaN;
def LongStopext = if (IsNaN(LongStop) and isLong) then LongStopext[1] else LongStop;
plot LongStopextline = LongStopext;
LongStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongStopextline.SetDefaultColor(Color.ORANGE);
LongStopextline.SetLineWeight(2);

########################################
##Long Targets
########################################
plot LongEntry = if isLong then (orderPrice) else Double.NaN;
LongEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LongEntry.SetDefaultColor(Color.GREEN);
LongEntry.SetLineWeight(2);

plot AvgProfitLL = if isLong then (orderPrice + ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLL.SetDefaultColor(Color.WHITE);
AvgProfitLL.SetLineWeight(1);

def LT1 = if isLong then (((biggestWin * .13) / mult) + orderPrice) else Double.NaN;
def LT1ext = if (IsNaN(LT1) and isLong) then LT1ext[1] else LT1;
plot LT1extline = LT1ext;
LT1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT1extline.SetDefaultColor(Color.GRAY);
LT1extline.SetLineWeight(1);

def LT2 = if isLong then (((biggestWin * .236) / mult) + orderPrice) else Double.NaN;
def LT2ext = if (IsNaN(LT2) and isLong) then LT2ext[1] else LT2;
plot LT2extline = LT2ext;
LT2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT2extline.SetDefaultColor(Color.GRAY);
LT2extline.SetLineWeight(1);

def LT3 = if isLong then (((biggestWin * .382) / mult) + orderPrice) else Double.NaN;
def LT3ext = if (IsNaN(LT3) and isLong) then LT3ext[1] else LT3;
plot LT3extline = LT3ext;
LT3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT3extline.SetDefaultColor(Color.GRAY);
LT3extline.SetLineWeight(1);

def LT4 = if isLong then (((biggestWin * .5) / mult) + orderPrice) else Double.NaN;
def LT4ext = if (IsNaN(LT4) and isLong) then LT4ext[1] else LT4;
plot LT4extline = LT4ext;
LT4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT4extline.SetDefaultColor(Color.GRAY);
LT4extline.SetLineWeight(1);

def LT5 = if isLong then (((biggestWin * .618) / mult) + orderPrice) else Double.NaN;
def LT5ext = if (IsNaN(LT5) and isLong) then LT5ext[1] else LT5;
plot LT5extline = LT5ext;
LT5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT5extline.SetDefaultColor(Color.GRAY);
LT5extline.SetLineWeight(1);

def LT6 = if isLong then (((biggestWin * .7495) / mult) + orderPrice) else Double.NaN;
def LT6ext = if (IsNaN(LT6) and isLong) then LT6ext[1] else LT6;
plot LT6extline = LT6ext;
LT6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT6extline.SetDefaultColor(Color.GRAY);
LT6extline.SetLineWeight(1);

def LT7 = if isLong then (((biggestWin * .893) / mult) + orderPrice) else Double.NaN;
def LT7ext = if (IsNaN(LT7) and isLong) then LT7ext[1] else LT7;
plot LT7extline = LT7ext;
LT7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LT7extline.SetDefaultColor(Color.GRAY);
LT7extline.SetLineWeight(1);
#########################################
##Short Stop
#########################################
def ShortStop = if SellSig then high else Double.NaN;
def ShortStopext = if (IsNaN(ShortStop) and isShort) then ShortStopext[1] else ShortStop;
plot ShortStopextline = ShortStopext;
ShortStopextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortStopextline.SetDefaultColor(Color.ORANGE);
ShortStopextline.SetLineWeight(2);

########################################
##Short Targets
########################################
plot ShortEntry = if isShort then (orderPrice) else Double.NaN;
;
ShortEntry.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ShortEntry.SetDefaultColor(Color.RED);
ShortEntry.SetLineWeight(2);

plot AvgProfitLS = if isShort then (orderPrice - ((dollarPLSum) / (ClosedTradeCount) / mult)) else Double.NaN;
AvgProfitLS.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AvgProfitLS.SetDefaultColor(Color.WHITE);
AvgProfitLS.SetLineWeight(1);

def ST1 = if isShort then (orderPrice - (biggestWin * .13) / mult) else Double.NaN;
def ST1ext = if (IsNaN(ST1) and isShort) then ST1ext[1] else ST1;
plot ST1extline = ST1ext;
ST1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST1extline.SetDefaultColor(Color.GRAY);
ST1extline.SetLineWeight(1);

def ST2 = if isShort then (orderPrice - (biggestWin * .236) / mult) else Double.NaN;
def ST2ext = if (IsNaN(ST2) and isShort) then ST2ext[1] else ST2;
plot ST2extline = ST2ext;
ST2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST2extline.SetDefaultColor(Color.GRAY);
ST2extline.SetLineWeight(1);

def ST3 = if isShort then (orderPrice - (biggestWin * .382) / mult) else Double.NaN;
def ST3ext = if (IsNaN(ST3) and isShort) then ST3ext[1] else ST3;
plot ST3extline = ST3ext;
ST3extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST3extline.SetDefaultColor(Color.GRAY);
ST3extline.SetLineWeight(1);

def ST4 = if isShort then (orderPrice - (biggestWin * .5) / mult) else Double.NaN;
def ST4ext = if (IsNaN(ST4) and isShort) then ST4ext[1] else ST4;
plot ST4extline = ST4ext;
ST4extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST4extline.SetDefaultColor(Color.GRAY);
ST4extline.SetLineWeight(1);

def ST5 = if isShort then (orderPrice - (biggestWin * .618) / mult) else Double.NaN;
def ST5ext = if (IsNaN(ST5) and isShort) then ST5ext[1] else ST5;
plot ST5extline = ST5ext;
ST5extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST5extline.SetDefaultColor(Color.GRAY);
ST5extline.SetLineWeight(1);

def ST6 = if isShort then (orderPrice - (biggestWin * .7495) / mult) else Double.NaN;
def ST6ext = if (IsNaN(ST6) and isShort) then ST6ext[1] else ST6;
plot ST6extline = ST6ext;
ST6extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST6extline.SetDefaultColor(Color.GRAY);
ST6extline.SetLineWeight(1);

def ST7 = if isShort then (orderPrice - (biggestWin * .893) / mult) else Double.NaN;
def ST7ext = if (IsNaN(ST7) and isShort) then ST7ext[1] else ST7;
plot ST7extline = ST7ext;
ST7extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ST7extline.SetDefaultColor(Color.GRAY);
ST7extline.SetLineWeight(1);
###################################
##Candle Color
###################################
AssignPriceColor(if coloredCandlesOn and (TradePL > (biggestWin * .75)) then Color.YELLOW else if ((price > UpperBand)) then Color.GREEN else if coloredCandlesOn and ((price < LowerBand)) then Color.RED else Color.GRAY);

###################################
##Line Bubbles
###################################
AddChartBubble(LabelsOn and BuySig, AvgProfitLL, "Average Profit: $" + avgTrade, Color.LiGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT1, "T1: $" + (biggestwin * .13), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT2, "T2: $" + (biggestwin * .236), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT3, "T3: $" + (biggestwin * .382), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT4, "T4: $" + (biggestwin * .5), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT5, "T5: $" + (biggestwin * .618), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT6, "T6: $" + (biggestwin * .7495), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LT7, "T7: $" + (biggestwin * .893), Color.LIGHT_GREEN);
AddChartBubble(LabelsOn and BuySig, LongStop, "StopLoss: $ -" + (Orderprice - LongStop) * MULT, Color.ORANGE);

AddChartBubble(LabelsOn and SellSig, AvgprofitLS, "Average Profit: $" + AVGTrade, Color.Red);
AddChartBubble(LabelsOn and SellSig, ST1, "T1: $" + (biggestwin * .13), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST2, "T2: $" + (biggestwin * .236), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST3, "T3: $" + (biggestwin * .382), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST4, "T4: $" + (biggestwin * .5), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST5, "T5: $" + (biggestwin * .618), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST6, "T6: $" + (biggestwin * .7495), Color.Red);
AddChartBubble(LabelsOn and SellSig, ST7, "T7: $" + (biggestwin * .893), Color.Red);
AddChartBubble(LabelsOn and SellSig, ShortStop, "StopLoss: $ -" + (ShortStop - Orderprice) * MULT, Color.ORANGE);
Hi Christoper84, Just wanted to say thanks a lot, this works very well indeed with Ichimoku, on the intraday on both 3min and 5min. Truly excellent. Is it possible to edit the code such that in the Orders section, enabled for Order 1 and Order 2 is defaulted as off? i find that it is initally on, and this creates a busy visual situation, so that i have to go back in and turn that off. I configure this script so that its mostly about the major up or down arrow points emerging and the way that color is activated (and candles greyed out), in correspondence with Ichimoku 5 lines. As Think or Swim has been bought by Schwab, i have some concern that accounts will be in the next 3 to 12 months moved completly into Schwab and the ToS platform, radically changed or retired. Who knows! Whether that happens or not, I am wondering if you could clarify a bit how the math is active here that makes this script possible? so that it could be cloned in some form in another platform? how is displaced average active exactly and in relation to high/low over 5 periods working ? Thanks again. Excellent script
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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