Squeeze Momentum Indicator

Discover the power of the Squeeze Momentum Indicator in technical analysis. Learn how to effectively use the Squeeze Momentum Indicator to identify periods of low volatility and potential breakouts. Explore its application on popular trading platforms like TradingView. Enhance your trading decisions by combining this versatile indicator with other analysis techniques and risk management strategies. Master the art of spotting trading opportunities and fine-tune your trading strategy with the SQM. Start making informed trades and maximize your profits in various market conditions.

Understanding the Squeeze Momentum Indicator

The Squeeze combines three essential components: the Bollinger Bands, the Keltner Channels, and the Momentum1. The Bollinger Bands measure price volatility, while the Keltner Channels track average true range (ATR). The Momentum indicates the speed of price movements. When the Bollinger Bands move inside the Keltner Channels, it indicates a squeeze, signifying a period of low volatility.

The SQM plots dots above and below the price chart to visually signify the squeeze condition. As the dots move closer together, it indicates an increase in market compression, which often precedes a significant price move.

How to Use the Squeeze Momentum Indicator

1. Identifying the Squeeze

Step 1: To start using the SQM, first, you need to add it to your chart. Most trading platforms offer the SQM as a built-in option. If not, you can find custom scripts or coding to add the indicator manually.

Step 2: Once the indicator is applied to your chart, look for instances where the Bollinger Bands move inside the Keltner Channels. This is the hallmark of the squeeze, indicating low volatility in the market.

Step 3: Observe the dots plotted above and below the price chart. When the dots are close together, it confirms the squeeze condition.

Discover the power of the Squeeze Momentum Indicator in technical analysis. Learn how to effectively use the Squeeze Momentum Indicator to identify periods of low volatility and potential breakouts. Explore its application on popular trading platforms like TradingView. Enhance your trading decisions by combining this versatile indicator with other analysis techniques and risk management strategies. Master the art of spotting trading opportunities and fine-tune your trading strategy with the Squeeze Momentum Indicator. Start making informed trades and maximize your profits in various market conditions.

2. Understanding Squeeze Breakouts

Step 1: As a trader, you should be vigilant when the dots transition from being close together to moving apart. This signals the end of the squeeze and potentially precedes a breakout.

Step 2: A squeeze breakout occurs when the price moves above the upper Bollinger Band, indicating a potential bullish momentum, or below the lower Bollinger Band, indicating a potential bearish momentum.

Step 3: Combine the breakout signal with other technical indicators or chart patterns to validate the potential momentum shift. For example, you can use Moving Averages or MACD to strengthen your analysis.

3. How to Use the Squeeze Momentum Indicator TradingView

TradingView is a widely used platform for technical analysis, and it offers the SQM as a popular option. Here’s how you can access and use the indicator on TradingView:

1: Open the TradingView platform and select your preferred trading chart.

2: Click on the “Indicators” button, and in the search bar, type “SQM.”

3: Select the indicator from the list, and it will be automatically applied to your chart.

4: Interpret the indicator as explained in the previous sections to identify potential trading opportunities.

4. Fine-tuning Your Trading Strategy with the SQM

While the SQM is a powerful tool on its own, it’s essential to combine it with other analysis techniques to fine-tune your trading strategy. Here are some tips to enhance your trading decisions using the Squeeze:

a. Use Volume Confirmation: To improve the accuracy of your trades, consider analyzing trading volume alongside the squeeze conditions. High volume during a squeeze breakout can provide additional confirmation of a potential price movement.

b. Implement Stop Loss Orders: As with any trading strategy, risk management is crucial. Always use stop-loss orders to protect your capital in case the market moves against your position.

c. Practice on Different Time Frames: The Squeeze M can be used effectively on various time frames, from intraday to longer-term charts. Experiment and find the time frame that best suits your trading style and risk tolerance.

d. Avoid Overtrading: While the Squeeze can generate exciting trading opportunities, avoid overtrading based solely on indicator signals. Always consider the broader market context and perform thorough analysis before making trading decisions.

Effectiveness of the Squeeze Momentum Indicator with ADX and Other Indicators

Indicator CombinationEffectivenessDescription
Squeeze Momentum Indicator + ADXHighCombining the Squeeze Momentum Indicator with ADX can provide strong confirmation of potential trend reversals.
Squeeze Momentum Indicator + RSIMediumUsing the Squeeze Momentum Indicator with RSI can enhance trend confirmation and identify overbought/oversold levels.
Squeeze Momentum Indicator + MACDHighThe combination of the Squeeze Momentum Indicator with MACD offers robust signals for trend changes and momentum.
Squeeze Momentum Indicator + SMAMediumIntegrating the Squeeze Momentum Indicator with Simple Moving Averages can help validate trend direction.
Squeeze Momentum Indicator + EMAHighCombining the Squeeze Momentum Indicator with Exponential Moving Averages can provide strong signals for trend shifts.

By combining the Squeeze with the ADX and other technical indicators, traders can obtain a comprehensive view of market conditions and make more informed trading decisions. The table above demonstrates the effectiveness of using the SQM in conjunction with various indicators. Depending on your trading style and risk tolerance, experimenting with different indicator combinations can lead to improved accuracy and profitability.

Remember that while these indicator combinations can be powerful, there is no one-size-fits-all approach in trading. Always conduct thorough research, backtest your strategies, and consider risk management principles to develop a trading plan that suits your individual needs and preferences.

Using tables in articles can provide readers with easily digestible information and can be valuable for SEO, as search engines often favor well-structured and informative content. By providing valuable insights through tables and combining multiple indicators, your article can become a valuable resource for traders seeking to enhance their technical analysis skills and optimize their trading strategies.

Strategy code Squeeze

Squeeze Momentum Indicator Squeeze Momentum Indicator how to use Squeeze Momentum Indicator TradingView
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lordeleal

//@version=5

strategy(shorttitle="SQZMOM [+]", title="Squeeze Momentum [Plus]", overlay=false)
// Function to select the type of source

tiempo45  = input.timeframe('15', "Tiempo val 15", options=['3','5','15','30','45','60','120','240','D', 'W', 'M'])


get_src(Type) =>
    if Type == "VWAP"
        ta.vwap
    else if Type == "Close"
        close
    else if Type == "Open"
        open
    else if Type == "HL2"
        hl2
    else if Type == "HLC3"
        hlc3
    else if Type == "OHLC4"
        ohlc4
    else if Type == "HLCC4"
        hlcc4
    else if Type == "Volume"
        nz(volume) * (high - low)
    else if Type == "High"
        high
    else if Type == "Low"
        low
    else if Type == "vwap(Close)"
        ta.vwap(close)
    else if Type == "vwap(Open)"
        ta.vwap(open)
    else if Type == "vwap(High)"
        ta.vwap(high)
    else if Type == "vwap(Low)"
        ta.vwap(low)
    else if Type == "AVG(vwap(H,L))"
        math.avg(ta.vwap(high), ta.vwap(low))
    else if Type == "AVG(vwap(O,C))"
        math.avg(ta.vwap(open), ta.vwap(close))
    else if Type == "OBV" // On Balance Volume
        ta.obv
    else if Type == "AccDist" // Accumulation Distribution
        ta.accdist
    else if Type == "PVT" // Price Volume Trend
        ta.pvt

// Kaufman's Adaptive Moving Average - Fast and Slow Ends
fastK = 0.666 // KAMA Fast End
slowK = 0.0645 // KAMA Slow End
kama(x, t)=>
    dist   = math.abs(x[0] - x[1])
    signal = math.abs(x - x[t])
    noise  = math.sum(dist, t)
    effr   = noise != 0 ? signal/noise : 1
    sc     = math.pow(effr*(fastK - slowK) + slowK,2)
    KAMA   = x
    KAMA  := nz(KAMA[1]) + sc*(x - nz(KAMA[1]))
    KAMA

// Jurik Moving Average of @everget
jma(src, length, power, phase) => 
    phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2)
    alpha = math.pow(beta, power)
    JMA = 0.0
    e0 = 0.0
    e0 := (1 - alpha) * src + alpha * nz(e0[1])
    e1 = 0.0
    e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
    e2 = 0.0
    e2 := (e0 + phaseRatio * e1 - nz(JMA[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
    JMA := e2 + nz(JMA[1])
    JMA

cti(sm, src, cd) =>
    di = (sm - 1.0) / 2.0 + 1.0
    c1 = 2 / (di + 1.0)
    c2 = 1 - c1
    c3 = 3.0 * (cd * cd + cd * cd * cd)
    c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd)
    c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd
    i1 = 0.0
    i2 = 0.0
    i3 = 0.0
    i4 = 0.0
    i5 = 0.0
    i6 = 0.0
    i1 := c1*src + c2*nz(i1[1])
    i2 := c1*i1 + c2*nz(i2[1])
    i3 := c1*i2 + c2*nz(i3[1])
    i4 := c1*i3 + c2*nz(i4[1])
    i5 := c1*i4 + c2*nz(i5[1])
    i6 := c1*i5 + c2*nz(i6[1])
        
    bfr = -cd*cd*cd*i6 + c3*(i5) + c4*(i4) + c5*(i3)
    bfr
    
a = 0.618
T3ma(src,Len) =>
    e1 = ta.ema(src, Len)
    e2 = ta.ema(e1, Len)
    e3 = ta.ema(e2, Len)
    e4 = ta.ema(e3, Len)
    e5 = ta.ema(e4, Len)
    e6 = ta.ema(e5, Len)
    C1 = -a*a*a
    C2 = 3*a*a+3*a*a*a
    C3 = -6*a*a-3*a-3*a*a*a
    C4 = 1+3*a+a*a*a+3*a*a
    C1*e6+C2*e5+C3*e4+C4*e3

VIDYA(src,Len) =>
    mom     = ta.change(src)
    upSum   = math.sum(math.max(mom, 0), Len)
    downSum = math.sum(-math.min(mom, 0), Len)
    out     = (upSum - downSum) / (upSum + downSum)
    cmo     = math.abs(out)
    alpha   = 2 / (Len + 1)
    vidya   = 0.0
    vidya   := src * alpha * cmo + nz(vidya[1]) * (1 - alpha * cmo)
    vidya

// ZLEMA: Zero Lag
zema(_src, _len) =>
    _alpha = (_len - 1) / 2
    _zlema0 = (_src + (_src - _src[_alpha]))
    _zlemaF = ta.ema(_zlema0, _len)

// ADX Weighted Moving Average of @Duyck 
adx_weighted_ma(_src, _period) =>
    [_diplus, _diminus, _adx] = ta.dmi(17, 14)
    _vol_sum  = 0.0
    _adx_sum  = 0.0
    for i = 0 to _period
        _vol_sum  := _src[i] * _adx[i] + _vol_sum
        _adx_sum  := _adx[i] + _adx_sum 
    _volwma   = _vol_sum / _adx_sum

// COVWMA - Coefficient of Variation Weighted Moving Average of @DonovanWall
covwma(a, b) =>
    cov    = ta.stdev(a, b) / ta.sma(a, b)
    cw     = a*cov
    covwma = math.sum(cw, b) / math.sum(cov, b)

// FRAMA - Fractal Adaptive Moving Average of @DonovanWall
w = -4.6 // "Coefficient (if FRAMA)"
frama(a, b) =>
    frama = 0.0
    n3    = (ta.highest(high, b) - ta.lowest(low, b))/b
    hd2   = ta.highest(high, b/2)
    ld2   = ta.lowest(low, b/2)
    n2    = (hd2 - ld2)/(b/2)
    n1    = (hd2[b/2] - ld2[b/2])/(b/2)
    dim   = (n1 > 0) and (n2 > 0) and (n3 > 0) ? (math.log(n1 + n2) - math.log(n3))/math.log(2) : 0
    alpha = math.exp(w*(dim - 1))
    sc    = (alpha < 0.01 ? 0.01 : (alpha > 1 ? 1 : alpha))
    frama := ta.cum(1)<=2*b ? a : (a*sc) + nz(frama[1])*(1 - sc)
    frama

// EMA RSI Adaptive of @glaz
rsi_ema(src, period) =>
    RsiPeriod = 14
    ema = 0.0
    RSvoltl = math.abs(ta.rsi(close, RsiPeriod)-50)+1.0
    multi   = (5.0+100.0/RsiPeriod) / (0.06+0.92*RSvoltl+0.02*math.pow(RSvoltl,2))
    pdsx    = multi*period
    alpha   = 2.0 /(1.0+pdsx)
    ema     := nz(ema[1])+alpha*(ta.sma(close, period)-nz(ema[1]))
    ema
  
ma(MAType, MASource, MAPeriod) =>
    if MAPeriod > 0
        if MAType == "SMA"
            ta.sma(MASource, MAPeriod)
        else if MAType == "EMA"
            ta.ema(MASource, MAPeriod)
        else if MAType == "WMA"
            ta.wma(MASource, MAPeriod)
        else if MAType == "RMA"
            ta.rma(MASource, MAPeriod)
        else if MAType == "HMA"
            ta.hma(MASource, MAPeriod)
        else if MAType == "DEMA"
            e = ta.ema(MASource, MAPeriod)
            2 * e - ta.ema(e, MAPeriod)
        else if MAType == "TEMA"
            e = ta.ema(MASource, MAPeriod)
            3 * (e - ta.ema(e, MAPeriod)) + ta.ema(ta.ema(e, MAPeriod), MAPeriod)
        else if MAType == "VWMA"
            ta.vwma(MASource, MAPeriod)
        else if MAType == "ALMA"
            ta.alma(MASource, MAPeriod, .85, 6)
        else if MAType == "CTI"
            cti(MAPeriod, MASource, 0)
        else if MAType == "KAMA"
            kama(MASource, MAPeriod)
        else if MAType == "SWMA" 
            ta.swma(MASource)
        else if MAType == "JMA"
            jma(MASource, MAPeriod, 2, 50)
        else if MAType == "LSMA" // Least Squares
            ta.linreg(MASource, MAPeriod, 0) 
        else if MAType == "Wild"
            wild = MASource
            wild := nz(wild[1]) + (MASource - nz(wild[1])) / MAPeriod
        else if MAType == "Tillson T3"
            T3ma(MASource, MAPeriod)
        else if MAType == "VIDYA"
            VIDYA(MASource, MAPeriod)
        else if MAType == "DWMA" // Double Weighted Moving Average
            ta.wma(ta.wma(MASource, MAPeriod), MAPeriod)
        else if MAType == "DVWMA" // Double Volume-Weighted Moving Average
            ta.vwma(ta.vwma(MASource, MAPeriod), MAPeriod)
        else if MAType == "Zero Lag"
            zema(MASource, MAPeriod)
        else if MAType == "Median"
            ta.median(MASource, MAPeriod)
        else if MAType == "RSI EMA"
            rsi_ema(MASource, MAPeriod)
        else if MAType == "ADX MA"
            adx_weighted_ma(MASource, MAPeriod)
        else if MAType == "COVWMA"
            covwma(MASource, MAPeriod)
        else if MAType == "FRAMA"
            frama(MASource, MAPeriod)

// Input - Squeeze Momentum Indicator
show_Momen = input.bool(true,    "▷ Show Momentum ", inline="mom", group="Squeeze Momentum Indicator")
color_M    = input.int(3,        "Color Format", minval=1, maxval=5, inline="mom", group="Squeeze Momentum Indicator")
bgoff      = input.bool(true,    "Background Off", inline="mom", group="Squeeze Momentum Indicator")
lengthM    = input.int(20,       "MOM Length", minval=1, step=1, inline="M", group="Squeeze Momentum Indicator")
srcM       = input.source(close, "Source", inline="M", group="Squeeze Momentum Indicator")
typeMom    = input.string('SMA', "Type", inline = "M", group="Squeeze Momentum Indicator", options=["SMA", "EMA", "WMA", "DWMA", "ALMA", "VWMA", "DVWMA", "HMA", "Wild", "JMA", "KAMA", "Zero Lag", "Tillson T3", "VIDYA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
show_sqz   = input.bool(true,    "Show Squeeze [SQZ]", inline="S", group="Squeeze Momentum Indicator")
length     = input.int(20,       "Length",  minval=1, step=1, inline="S", group="Squeeze Momentum Indicator")
src        = input.source(ohlc4, "Source", inline="S", group="Squeeze Momentum Indicator")
bbmatype   = input.string('SMA', "BB Calculation Type", inline = "bb", group="Squeeze Momentum Indicator", options=["SMA", "EMA", "WMA", "DWMA", "ALMA", "VWMA", "DVWMA", "HMA", "LSMA", "Wild", "JMA", "Zero Lag", "Tillson T3", "VIDYA", "KAMA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
multBB     = input.float(2.0 ,   "BB MultFactor", step=0.25, inline = "bb", group="Squeeze Momentum Indicator") //Bollinger Bands Multiplier
kcmatype   = input.string('EMA', "Keltner Channel Calculation Type", inline = "kc", group="Squeeze Momentum Indicator", options=["SMA", "EMA", "WMA", "DWMA", "ALMA", "VWMA", "DVWMA", "HMA", "LSMA", "Wild", "JMA", "Zero Lag", "Tillson T3", "VIDYA", "KAMA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
useTR      = input.bool(true,    "Use TrueRange (KC)", inline = "kc", group="Squeeze Momentum Indicator")
drdiv      = input.bool(true,    "Draw Divergence", inline="l1", group="Squeeze Momentum Indicator")
zeroSQZ    = input.bool(false,   "SQZ Zero Line", inline="l1", group="Squeeze Momentum Indicator")
darkm      = input.bool(false,   "Gray Background for Dark Mode", inline="l1", group="Squeeze Momentum Indicator")

// Input - ADX and DMI
scale     = input.float(55.0,        "Scala for ADX", inline="dmi0", group="Directional Movement Index")
pos       = input.color(color.white, "Positive slope", inline="dmi0", group="Directional Movement Index") 
neg       = input.color(color.gray,  "Negative slope", inline="dmi0", group="Directional Movement Index") 
show_ADX  = input.bool(true,         "Show ADX", inline="showDMI", group="Directional Movement Index")
show_DMI  = input.bool(true,         "Show Colored DMI", inline="showDMI", group="Directional Movement Index")
hideLabel = input.bool(false,        "▷ Hide DMI Label ◁", inline= "showDMI", group="Directional Movement Index")
adxlen    = input.int(14,            "ADX Smoothing", minval=1, maxval=50, group="Directional Movement Index")
dilen     = input.int(14,            "DI Length", minval=1, group="Directional Movement Index") 
keyLevel  = input.int(23,            "Key level for ADX", group="Directional Movement Index")
weakTrend = input.int(15,            "Weak Trend Theshold", group="Directional Movement Index")
histLabel = input.int(0    ,         "Historical Label Readings", minval=0, group="Directional Movement Index")
keyColor  = input.bool(false,        "KeyLevel with color DMI", inline="k_level", group="Directional Movement Index")
distance  = input.int(-5,            "Distance between KeyLevel and 0", inline="k_level", group="Directional Movement Index") 
detect_bull_dmi = input.bool(false, '◁ Alert: Bullish Trend', inline='alert_dmi', group='Directional Movement Index')
detect_bear_dmi = input.bool(false, '◁ Alert: Bearish Trend', inline='alert_dmi', group='Directional Movement Index')

// Input - Hull Moving Average
show_Hull = input.bool(false,     "Show Hull Signal", inline="hull0", group="Hull Moving Average")
bg_Hull   = input.bool(true,      "Background Hull",  inline="hull0", group="Hull Moving Average")
h_Length  = input.int(55,         "Length", minval=1, step=1, inline = "hull", group="Hull Moving Average")
h_type    = input.string("HMA",   "Type", options=["HMA", "EHMA", "THMA", "AHMA"], inline = "hull", group="Hull Moving Average")
h_src     = input.string("Close", "Source", inline = "hull", group="Hull Moving Average", options=["Close", "Open", "HL2", "HLC3", "OHLC4", "HLCC4", "VWAP", "High", "Low", "vwap(Close)", "vwap(Open)", "vwap(High)", "vwap(Low)", "AVG(vwap(H,L))", "AVG(vwap(O,C))", "OBV", "AccDist", "PVT", "Volume"])

// Input - Williams Vix Fix
sbcFilt  = input.bool(false, "Show Signal For Filtered Entry [▲ FE ]", group="Williams Vix Fix") // Use FILTERED Criteria
sbcAggr  = input.bool(false, "Show Signal For AGGRESSIVE Filtered Entry [▲ AE ]", group="Williams Vix Fix") // Use FILTERED Criteria
sbc      = input.bool(false, "Show Signal if WVF WAS True and IS Now False [▼]", group="Williams Vix Fix") // Use Original Criteria
sbcc     = input.bool(false, "Show Signal if WVF IS True [▼]", group="Williams Vix Fix") // Use Original Criteria
alert_AE = input.bool(false, '◁ Alert: [AGGRESSIVE Entry]', inline='AlertVF', group='Williams Vix Fix')
alert_FE = input.bool(false, '◁ Alert: [Filtered Entry]', inline='AlertVF', group='Williams Vix Fix')
// Inputs Tab Criteria
pd      = input.int(22,    "LookBack Period Standard Deviation High", group="Williams Vix Fix")
bbl     = input.int(20,    "Bolinger Band Length", group="Williams Vix Fix")
mult    = input.float(2.0, "Bollinger Band Standard Devaition Up", minval=1, maxval=5, group="Williams Vix Fix")
lb      = input.int(50,    "Look Back Period Percentile High", group="Williams Vix Fix")
ph      = input.float(.85, "Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%", group="Williams Vix Fix")
ltLB    = input.int(40, minval=25, maxval=99, title="Long-Term Look Back Current Bar Has To Close Below This Value OR Medium Term--Default=40", group="Williams Vix Fix")
mtLB    = input.int(14, minval=10, maxval=20, title="Medium-Term Look Back Current Bar Has To Close Below This Value OR Long Term--Default=14", group="Williams Vix Fix")
str     = input.int(3,  minval=1,  maxval=9,  title="Entry Price Action Strength--Close > X Bars Back---Default=3", group="Williams Vix Fix")

// Input - Elliott Wave Oscillator
show_EWO   = input.bool(false,   "▷ Show EWO Breaking Bands", inline="ewo0", group="Elliott Wave Oscillator")
diver_EWO  = input.bool(true,    "Draw Divergence", inline="ewo0", group="Elliott Wave Oscillator")
bg_EWO     = input.bool(false,   "Background", inline="ewo0", group="Elliott Wave Oscillator")
ma_fast    = input.int(5,        "Fast MA", inline="Fast", group="Elliott Wave Oscillator")
type_fast  = input.string("SMA", "Fast MA Type", inline="Fast", group="Elliott Wave Oscillator", options=["SMA", "EMA", "WMA", "DWMA", "ALMA", "VWMA", "DVWMA", "HMA", "LSMA", "Wild", "JMA", "Zero Lag", "Tillson T3", "VIDYA", "KAMA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
ma_slow    = input.int(34,       "Slow MA", inline="Slow", group="Elliott Wave Oscillator")
type_slow  = input.string("SMA", "Slow MA Type", inline="Slow", group="Elliott Wave Oscillator", options=["SMA", "EMA", "WMA", "ALMA", "DWMA", "VWMA", "DVWMA", "HMA", "LSMA", "Wild", "JMA", "Zero Lag", "Tillson T3", "VIDYA", "KAMA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
SRC        = input.string("HL2", "Source", inline="ewo1", group="Elliott Wave Oscillator", options=["VWAP", "Close", "Open", "HL2", "HLC3", "OHLC4", "HLCC4", "High", "Low", "vwap(Close)", "vwap(Open)", "vwap(High)", "vwap(Low)", "AVG(vwap(H,L))", "AVG(vwap(O,C))", "OBV", "AccDist", "PVT", "Volume"])
show_bands = input.bool(true,    "Show Breaking Bands", inline="ewo1", group="Elliott Wave Oscillator")
signal_ewo = input.bool(true,    "Show Signal(EWO)", inline="ewo2", group="Elliott Wave Oscillator")
Len        = input.int(1,        "Length", minval=1, inline="ewo2", group="Elliott Wave Oscillator") 

// Input - Expert Trend Locator
candles_XTL = input.bool(false,     "Use XTL Bars Color", group="Expert Trend Locator")
period_xtl  = input.int(26,         "Length", minval=2, inline="xtl", group="Expert Trend Locator")
MA_Type2    = input.string("DVWMA", "Type", inline="xtl", group="Expert Trend Locator", options=["SMA", "EMA", "WMA", "DWMA", "ALMA", "VWMA", "DVWMA", "HMA", "LSMA", "Wild", "JMA", "Zero Lag", "Tillson T3", "VIDYA", "KAMA", "CTI", "RMA", "DEMA", "TEMA", "SWMA", "Median", "COVWMA", "FRAMA", "ADX MA", "RSI EMA"])
src_xtl     = input.string("HLC3",  "Source", inline="xtl", group="Expert Trend Locator", options=["VWAP", "Close", "Open", "HL2", "HLC3", "OHLC4", "HLCC4", "High", "Low", "vwap(Close)", "vwap(Open)", "vwap(High)", "vwap(Low)", "AVG(vwap(H,L))", "AVG(vwap(O,C))", "OBV", "AccDist", "PVT", "Volume"])
fixed_Value = input.int(37,         "Threshold Level", minval=10, group="Expert Trend Locator", inline="xtl2")
uTrad       = input.bool(false,     "Use the traditional CCI formula", group="Expert Trend Locator", inline="xtl2")
color_xtlUP = input.color(#00BFFF,  "Trend Up",   group="Expert Trend Locator", inline="ColXTL")
color_xtlNe = input.color(#FFFFFF,  "Neutral",    group="Expert Trend Locator", inline="ColXTL")
color_xtlDN = input.color(#FF4000,  "Trend Down", group="Expert Trend Locator", inline="ColXTL")

// Divergences
group_divergences = "Divergences For Momentum and Elliott Wave Oscillator"
plotBull              = input.bool(true,  "Plot Bullish", inline="bull", group=group_divergences)
plotHiddenBull        = input.bool(false, "Plot Hidden Bullish", inline="bull", group=group_divergences)
plotBear              = input.bool(true,  "Plot Bearish", inline="bear", group=group_divergences)
plotHiddenBear        = input.bool(false, "Plot Hidden Bearish", inline="bear", group=group_divergences)
delay_plot_til_closed = input.bool(true,  "Delay plot until candle is closed (don't repaint)", group=group_divergences)
usex2LB               = input.bool(true,  "Lookback 2 Pivots?", group=group_divergences)
lbR                   = input.int(1,      "Pivot Lookback Right", group=group_divergences)
lbL                   = input.int(2,      "Pivot Lookback Left", group=group_divergences)
rangeLower            = input.int(1,      "Min of Lookback Range", group=group_divergences)
rangeUpper            = input.int(60,     "Max of Lookback Range", group=group_divergences)

//______________________________________________________________________________
// Elliott Wave Oscillator (EWO) Breaking Bands
// https://www.tradingview.com/script/UcBJKm4O/
//______________________________________________________________________________
src_ewo = get_src(SRC)
ewo1    = ma(type_fast, src_ewo, ma_fast) - ma(type_slow, src_ewo, ma_slow) 
AvgEWO  = ta.ema(ewo1, Len)	

UpperBand = ewo1
UpperBand := nz(UpperBand[1])
if ewo1 > 0
    UpperBand := (UpperBand[1] + 0.0555*(ewo1 - UpperBand[1]))

LowerBand = ewo1
LowerBand := nz(LowerBand[1])
if ewo1 < 0
    LowerBand := (LowerBand[1] + 0.0555*(ewo1 - LowerBand[1]))

red_orange = #FF4000
chartreuse = #80FF00
coral      = color.maroon ///#FF8080
lavender   = #8080FF

color_in = ewo1 > LowerBand and ewo1 < 0 ? #7F4040 : ewo1 > 0 and ewo1 < UpperBand ? #40407F : na
background = ewo1 > 0 ? color.new(#00FFFF, 85) : color.new(#FF0000, 85)  

color_ewo  = ewo1 > UpperBand ? lavender : ewo1  < LowerBand ? coral : color_in
plot(show_EWO ? ewo1 : na, title="EWO", color = color_ewo, style = plot.style_columns)

color_sig = AvgEWO > 0 ?  chartreuse : red_orange
plot(show_EWO and signal_ewo ? AvgEWO : na, title = "Signal(EWO)", color = color_sig, linewidth = 2)

plot(show_EWO and show_bands ? UpperBand : na,	title = "Upper Band", color = #EFEFBF)
plot(show_EWO and show_bands ? LowerBand : na,	title = "Lower Band", color = #EFEFBF)
bgcolor(bg_EWO ? background : na, title = "Background EWO")

//_________________________________________________________________________________
// Based on "Squeeze Momentum Indicator" - Author:@LazyBear and @J-Streak          
// https://www.tradingview.com/script/nqQ1DT5a-Squeeze-Momentum-Indicator-LazyBear/
// https://www.tradingview.com/script/557GijRq-JS-Squeeze-Pro-2/                   
//_________________________________________________________________________________
// Bollinger Bands Basis Line
basis = ma(bbmatype, src, length) 

// Keltner Channel Basis Line
basiskc = ma(kcmatype, src, length)
// Keltner Channel Range
range_src = useTR ? ta.tr : (high - low)
range_kc  = ma(kcmatype, range_src, length)
// Keltner Channel Low Multiplier
multlowKC = 2.0
// Keltner Channel Mid Multiplier
multmidKC = 1.5
// Keltner Channel High Multiplier
multhighKC = 1.0

// Bollinger Bands 
dev         = multBB * ta.stdev(src, length)
upperBB     = basis + dev
lowerBB     = basis - dev
// Keltner Channel Bands Low 
upperKCl    = basiskc + range_kc * multlowKC
lowerKCl    = basiskc - range_kc * multlowKC
// Keltner Channel Bands Mid
upperKCm    = basiskc + range_kc * multmidKC
lowerKCm    = basiskc - range_kc * multmidKC
// Keltner Channel Bands High
upperKCh    = basiskc + range_kc * multhighKC
lowerKCh    = basiskc - range_kc * multhighKC

//------------------------{ Squeeze Momentum Basics }------------------------------------
// Momentum
ma_momentum = ma(typeMom, srcM, lengthM)
sz = ta.linreg(srcM - math.avg(math.avg(ta.highest(high, lengthM), ta.lowest(low, lengthM)), ma_momentum), lengthM, 0)

// Momentum Conditions
sc1 = sz >= 0
sc2 = sz < 0
sc3 = sz >= sz[1]
sc4 = sz < sz[1]

// Squeeze On
lowsqz      = lowerBB > lowerKCl and upperBB < upperKCl
lsf         = lowsqz == false
midsqz      = lowerBB > lowerKCm and upperBB < upperKCm
msf         = midsqz == false
highsqz     = lowerBB > lowerKCh and upperBB < upperKCh
hsf         = highsqz == false

//-----------------------{ Color Components }-----------------------------------
// Color Conditions
clr1        = sc1 and sc3 ? #00EEFF : 
   sc1 and sc4 ? #000EFF : sc2 and sc4 ? #FF0000 : sc2 and sc3 ? #FFE500 : color.gray
clr2        = sc1 and sc3 ? #00bcd4 : 
   sc1 and sc4 ? #0D47A1 : sc2 and sc4 ? #BA68C8 : sc2 and sc3 ? #9C27B0 : #673AB7
clr3        = sc1 and sc3 ? #15FF00 : sc1 and sc4 ? #388E3C : 
   sc2 and sc4 ? color.red : sc2 and sc3 ? color.maroon : color.gray
clr4        = sc1 and sc3 ? #fff59d : 
   sc1 and sc4 ? #FFD600 : sc2 and sc4 ? #FFCC80 : sc2 and sc3 ? #FF9800 : #702700
clr5        = sc1 and sc3 ? #2196F3 : 
   sc1 and sc4 ? #0D47A1 : sc2 and sc4 ? #EF9A9A : sc2 and sc3 ? #D32F2F : #CE93D8
choice      = color_M == 1 ? clr1 : color_M == 2 ? clr2 : 
   color_M == 3 ? clr3 : color_M == 4 ? clr4 : color_M == 5 ? clr5 : na

//-----------------{ Indicator Components and Plots }---------------------------
// Squeeze Dot Colors
orange_red     = #FF4000 // Original Squeeze 
golden_yellow  = #FFE500 // High Squeeze
sqzproc  = highsqz ? golden_yellow : midsqz ? orange_red : lowsqz ? color.gray : na
// Squeeze Dot Plot Above 
sqzpro      = zeroSQZ ? na : highsqz ? highsqz : midsqz ? midsqz : lowsqz ? lowsqz : na
plotshape(show_sqz ? sqzpro : na, title="Squeeze Dots Top", style=shape.circle, location=location.top, color=sqzproc)

// Momentum Plot
plot(show_Momen ? sz : na, title="Squeeze Momentum", color=choice, style=plot.style_columns)


//// Plot de senal 

estruzqmUp =  sz > nz(sz[1])  and  sz[2] > nz(sz[1]) and sz < 0

estruzqmDown =  sz < nz(sz[1])  and  sz[2] < nz(sz[1]) and sz > 0

bgcolor(estruzqmUp ? color.new(#008000, 52): na )  //green colores de fondfo 
bgcolor(estruzqmDown ? color.new(#ff5252, 55) : na)  //red colores de fondfo 

///senal 45mnts

Val45mnts = request.security(syminfo.tickerid, tiempo45, sz,  gaps=barmerge.gaps_on)


estruzqmUp45 =  Val45mnts > nz(Val45mnts[1])  and  Val45mnts[2] > nz(Val45mnts[1]) and Val45mnts < 0

estruzqmDown45 =  Val45mnts < nz(Val45mnts[1])  and  Val45mnts[2] < nz(Val45mnts[1]) and Val45mnts > 0


/// Counter SQZ down

AdxcolorEstaenrojo= sz < 0
AdxcolorEstaenrojo45= Val45mnts < 0

/// Counter SQZ Up
var int[] occurrencesLong45 = array.new_int(na)
var nth_most_recent_occurance45 = 0//input.int(0, 'show distance to nth most recent occurrance', minval=0)

f_barssince_occurrenceLong45(_n) =>
    n = nz(_n)
    if array.size(occurrencesLong45) >= n + 1
        distance45 = bar_index - array.get(occurrencesLong45, n)
        distance45
    else
        -1

if AdxcolorEstaenrojo45==true  and  estruzqmUp45    //array.clear() 
    array.unshift(occurrencesLong45, bar_index) 


if AdxcolorEstaenrojo45==false     
    array.clear(occurrencesLong45)


var int[] occurrencesShort45 = array.new_int(na)


f_barssince_occurrenceShort45(_n) =>
    n = nz(_n)
    if array.size(occurrencesShort45) >= n + 1
        distance45 = bar_index - array.get(occurrencesShort45, n)
        distance45
    else
        -1

if AdxcolorEstaenrojo45==false  and  estruzqmDown45    //array.clear() 
    array.unshift(occurrencesShort45, bar_index) 


if AdxcolorEstaenrojo45==true     
    array.clear(occurrencesShort45)    

/////

var int[] occurrencesLong = array.new_int(na)
var nth_most_recent_occurance = 0//input.int(0, 'show distance to nth most recent occurrance', minval=0)

f_barssince_occurrenceLong(_n) =>
    n = nz(_n)
    if array.size(occurrencesLong) >= n + 1
        distance = bar_index - array.get(occurrencesLong, n)
        distance
    else
        -1

if AdxcolorEstaenrojo==true  and  estruzqmUp    //array.clear() 
    array.unshift(occurrencesLong, bar_index) 


if AdxcolorEstaenrojo==false     
    array.clear(occurrencesLong)



/// Counter SQZ down
var int[] occurrencesShort = array.new_int(na)


f_barssince_occurrenceShort(_n) =>
    n = nz(_n)
    if array.size(occurrencesShort) >= n + 1
        distance = bar_index - array.get(occurrencesShort, n)
        distance
    else
        -1

if AdxcolorEstaenrojo==false  and  estruzqmDown    //array.clear() 
    array.unshift(occurrencesShort, bar_index) 


if AdxcolorEstaenrojo==true     
    array.clear(occurrencesShort)


CountSignalGreen=array.size(occurrencesLong)
CountSignalRed=array.size(occurrencesShort)

CountSignalGreen45=array.size(occurrencesLong45)
CountSignalRed45=array.size(occurrencesShort45)


// Plot Zero Line
color_zero =  highsqz ? golden_yellow : midsqz ? orange_red : lowsqz ? color.gray : clr3
plot(zeroSQZ ? 0 : na, title="Squeeze Zero Line", color=color_zero, linewidth=2, style=plot.style_circles)

// Background Conditions
bg1_dark = color.new(color.gray, 95)
bg2_dark = color.new(#673AB7, 95)
bg3_dark = color.new(color.gray, 95)
bg4_dark = color.new(#FFD600, 95)
bg5_dark = color.new(#CE93D8, 95)

bgc_dark = bgoff ? na : color_M == 1 ? bg1_dark : color_M == 2 ? bg2_dark : 
   color_M == 3 ? bg3_dark : color_M == 4 ? bg4_dark : color_M == 5 ? bg5_dark : na

clr1_bg        = sc1 and sc3 ? color.new(#00EEFF, 80) : 
   sc1 and sc4 ? color.new(#000EFF, 80) : sc2 and sc4 ? color.new(#FF0000, 80) : sc2 and sc3 ? color.new(#FFE500, 80) : color.new(color.gray, 80)
clr2_bg        = sc1 and sc3 ? color.new(#00bcd4, 80) : 
   sc1 and sc4 ? color.new(#0D47A1, 80) : sc2 and sc4 ? color.new(#BA68C8, 80) : sc2 and sc3 ? color.new(#9C27B0, 80) : color.new(#673AB7, 80)
clr3_bg        = sc1 and sc3 ? color.new(#15FF00, 80) : sc1 and sc4 ? color.new(#388E3C, 80) : 
   sc2 and sc4 ? color.new(#F44336, 80) : sc2 and sc3 ? color.new(#B71C1C, 80) : color.new(color.gray, 80)
clr4_bg        = sc1 and sc3 ? color.new(#fff59d, 80) : 
   sc1 and sc4 ? color.new(#FFD600, 80) : sc2 and sc4 ? color.new(#FFCC80, 80) : sc2 and sc3 ? color.new(#FF9800, 80) : color.new(#702700, 80)
clr5_bg        = sc1 and sc3 ? color.new(#2196F3, 80) : 
   sc1 and sc4 ? color.new(#0D47A1, 80) : sc2 and sc4 ? color.new(#EF9A9A, 80) : sc2 and sc3 ? color.new(#D32F2F, 80) : color.new(#CE93D8, 80)
choice_bg      = color_M == 1 ? clr1_bg : color_M == 2 ? clr2_bg : 
   color_M == 3 ? clr3_bg : color_M == 4 ? clr4_bg : color_M == 5 ? clr5_bg : na

// Background Colors
bgcolor(darkm ? bgc_dark : na, title = "Gray Background", editable=false)
bgcolor(bgoff ? na       : choice_bg, title = "Momentum Background")

//___________________________________________________________________________________________
// Based on "Better Divergence On Any Indicator" - Author: @DoctaBot
// https://www.tradingview.com/script/tbrsp75I-Better-Divergence-On-Any-Indicator-DoctaBot/
//___________________________________________________________________________________________
osc             = show_Momen and drdiv ? sz : show_EWO and diver_EWO ? ewo1 : na
bearColor       = color.new(color.red, 0)
bullColor       = color.new(color.lime, 0)
hiddenBullColor = color.new(#00945F, 15)
hiddenBearColor = color.new(#990040, 15)
textColor       = color.new(color.white, 0)
textColor2      = color.new(color.black, 0)

repaint = (not(delay_plot_til_closed) or barstate.ishistory or barstate.isconfirmed)

plFound = na(ta.pivotlow(osc, lbL, lbR))  ? false : true
phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true

_inRange(cond, occurence) =>
	bars = (bar_index - ta.valuewhen(cond, bar_index, occurence))
	rangeLower <= bars and bars <= rangeUpper
	
oscPLCur    = ta.valuewhen(plFound, osc[lbR], 0)                    //Current Oscillator Pivot Low
oscPL1      = ta.valuewhen(plFound, osc[lbR], 1)                    //Last Oscillator Pivot Low
oscPL2      = ta.valuewhen(plFound, osc[lbR], 2)                    //2 Back Oscillator Pivot Low

oscPLCurBar = ta.valuewhen(plFound, bar_index - lbR, 0)             //Current Oscillator Pivot Low Bar Index
oscPL1Bar   = ta.valuewhen(plFound, bar_index - lbR, 1)             //Last Oscillator Pivot Low Bar Index
oscPL2Bar   = ta.valuewhen(plFound, bar_index - lbR, 2)             //2 Back Oscillator Pivot Low Bar Index

oscPHCur    = ta.valuewhen(phFound, osc[lbR], 0)                    //Current Oscillator Pivot High
oscPH1      = ta.valuewhen(phFound, osc[lbR], 1)                    //Last Oscillator Pivot High
oscPH2      = ta.valuewhen(phFound, osc[lbR], 2)                    //2 Back Oscillator Pivot High

oscPHCurBar = ta.valuewhen(phFound, bar_index - lbR, 0)             //Current Oscillator Pivot High Bar Index
oscPH1Bar   = ta.valuewhen(phFound, bar_index - lbR, 1)             //Last Oscillator Pivot High Bar Index
oscPH2Bar   = ta.valuewhen(phFound, bar_index - lbR, 2)             //2 Back Oscillator Pivot High Bar Index

oscPL2Slope = (osc[lbR] - oscPL2)/((bar_index - lbR) - oscPL2Bar)   //Slope Between Pivot Lows
oscPL2Lim   = ((oscPL1Bar - oscPL2Bar)*(oscPL2Slope)) + oscPL2      //Pivot Low Oscillator Intersect Limit

oscPH2Slope = (osc[lbR] - oscPH2)/((bar_index - lbR) - oscPH2Bar)   //Slope Between Pivot Highs
oscPH2Lim   = ((oscPH1Bar - oscPH2Bar)*(oscPH2Slope)) + oscPH2      //Pivot High Oscillator Intersect Limit

// Oscilator Pivots
oscHL1 = (osc[lbR] > oscPL1 and _inRange(plFound, 1))                           //Osc. Higher Low compared to previous
oscHL2 = (osc[lbR] > oscPL2 and _inRange(plFound, 2) and oscPL1 > oscPL2Lim)    //Osc. Higher Low compared to 2 back with no intersection
     
oscLL1 = (osc[lbR] < oscPL1 and _inRange(plFound, 1))                           //Osc. Lower Low compared to previous
oscLL2 = (osc[lbR] < oscPL2 and _inRange(plFound, 2) and oscPL1 > oscPL2Lim)    //Osc. Lower Low compared to 2 back with no intersection
        
oscLH1 = (osc[lbR] < oscPH1 and _inRange(phFound, 1))                           //Osc. Lower High compared to previous
oscLH2 = (osc[lbR] < oscPH2 and _inRange(phFound, 2) and oscPH1 < oscPH2Lim)    //Osc. Lower High compared to 2 back with no intersection
        
oscHH1 = (osc[lbR] > oscPH1 and _inRange(phFound, 1))                           //Osc. Higher High compared to previous
oscHH2 = (osc[lbR] > oscPH2 and _inRange(phFound, 2) and oscPH1 < oscPH2Lim)    //Osc. Higher High compared to 2 back with no intersection

// Price Pivots
priceLL1 = ta.lowest(close, lbR + 2) < ta.valuewhen(plFound, ta.lowest(close, lbR + 2), 1)      //Price Lower Low compared to previous
priceLL2 = ta.lowest(close, lbR + 2) < ta.valuewhen(plFound, ta.lowest(close, lbR + 2), 2)      //Price Lower Low compared to 2 back

priceHL1 = ta.lowest(close, lbR + 2) > ta.valuewhen(plFound, ta.lowest(close, lbR + 2), 1)      //Price Higher Low compared to previous
priceHL2 = ta.lowest(close, lbR + 2) > ta.valuewhen(plFound, ta.lowest(close, lbR + 2), 2)      //Price Higher Low compared to 2 back

priceHH1 = ta.highest(close, lbR + 2) > ta.valuewhen(phFound, ta.highest(close, lbR + 2), 1)    //Price Higher High compared to previous
priceHH2 = ta.highest(close, lbR + 2) > ta.valuewhen(phFound, ta.highest(close, lbR + 2), 2)    //Price Higher High compared to 2 back

priceLH1 = ta.highest(close, lbR + 2) < ta.valuewhen(phFound, ta.highest(close, lbR + 2), 1)    //Price Lower High compared to previous
priceLH2 = ta.highest(close, lbR + 2) < ta.valuewhen(phFound, ta.highest(close, lbR + 2), 2)    //Price Lower High compared to 2 back

// Conditions
bullCond1       = plotBull          and plFound and repaint and (priceLL1 and oscHL1) and osc < 0
bullCond2       = plotBull          and plFound and repaint and (usex2LB ? (priceLL2 and oscHL2) : false) and osc < 0

hiddenBullCond1 = plotHiddenBull    and plFound and repaint and (priceHL1 and oscLL1) and osc < 0
hiddenBullCond2 = plotHiddenBull    and plFound and repaint and (usex2LB ? (priceHL2 and oscLL2) : false) and osc < 0

bearCond1       = plotBear          and phFound and repaint and (priceHH1 and oscLH1) and osc > 0
bearCond2       = plotBear          and phFound and repaint and (usex2LB ? (priceHH2 and oscLH2) : false) and osc > 0

hiddenBearCond1 = plotHiddenBear    and phFound and repaint and (priceLH1 and oscHH1) and osc > 0
hiddenBearCond2 = plotHiddenBear    and phFound and repaint and (usex2LB ? (priceLH2 and oscHH2) : false) and osc > 0

f_drawLine(_x1, _x2, _y1, _y2, _color, _width) =>
    line.new(
     x1     = _x1,
     x2     = _x2,
     y1     = _y1,
     y2     = _y2,
     color  = _color,
     width  = _width,
     xloc   = xloc.bar_index
     )
    
// Plot Bull Lines
if plFound
    f_drawLine(
     bullCond1 or hiddenBullCond1 ? oscPL1Bar : bullCond2 or hiddenBullCond2 ? oscPL2Bar : oscPLCurBar, 
     oscPLCurBar, 
     bullCond1 or hiddenBullCond1 ? oscPL1 : bullCond2 or hiddenBullCond2 ? oscPL2 : oscPLCur, 
     oscPLCur, 
     bullCond1 or bullCond2 ? bullColor : hiddenBullColor, 
     2)

// Plot Bear Lines
if phFound
    f_drawLine(
     bearCond1 or hiddenBearCond1 ? oscPH1Bar : bearCond2 or hiddenBearCond2 ? oscPH2Bar : oscPHCurBar, 
     oscPHCurBar, 
     bearCond1 or hiddenBearCond1 ? oscPH1 : bearCond2 or hiddenBearCond2 ? oscPH2 : oscPHCur, 
     oscPHCur, 
     bearCond1 or bearCond2 ? bearColor : hiddenBearColor, 
     2)
     
// Plot Bull Labels
plotshape(
	 bullCond1 or bullCond2 ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" R ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor2
	 )

// Plot Hidden Bull Labels
plotshape(
	 hiddenBullCond1 or hiddenBullCond2 ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=hiddenBullColor,
	 textcolor=textColor
	 )

// Plot Bear Labels
plotshape(
	 bearCond1 or bearCond2 ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" R ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor
	 )

// Plot Hidden Bear Labels
plotshape(
	 hiddenBearCond1 or hiddenBearCond2 ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=hiddenBearColor,
	 textcolor=textColor
	 )

// Alerts
if bullCond1 or bullCond2
    alert("Regular Bull Divergence", alert.freq_once_per_bar_close )

if hiddenBullCond1 or hiddenBullCond2
    alert("Hidden Bull Divergence", alert.freq_once_per_bar_close )
    
if bearCond1 or bearCond2
    alert("Regular Bear Divergence", alert.freq_once_per_bar_close )
    
if hiddenBearCond1 or hiddenBearCond2
    alert("Hidden Bear Divergence", alert.freq_once_per_bar_close )
    
alertcondition(bullCond1 or bullCond2,             title = "Regular Bull Divergence", message = "Regular Bull Divergence")
alertcondition(hiddenBullCond1 or hiddenBullCond2, title = "Hidden Bull Divergence",  message = "Hidden Bull Divergence")
alertcondition(bearCond1 or bearCond2,             title = "Regular Bear Divergence", message = "Regular Bear Divergence")
alertcondition(hiddenBearCond1 or hiddenBearCond2, title = "Hidden Bear Divergence",  message = "Hidden Bear Divergence")

//______________________________________________________________________________
// Based on "Directional Movement Index and ADX"
// https://www.tradingview.com/script/VroqhJmg/
//______________________________________________________________________________
[diplus, diminus, adxValue] = ta.dmi(dilen, adxlen)

biggest(series) =>
    max = 0.0
    max := nz(max[1], series)
    if series > max
        max := series
    max

// Calculate ADX Scale
ni = biggest(sz)
adx_scale = (adxValue - keyLevel + distance) * ni/scale

// J. Welles Wilder, developer of DMI, believed that a DMI reading above 25 indicated a strong trend, 
// while a reading below 20 indicated a weak or non-existent trend.
dmiBull = diplus   >= diminus  and adxValue >= keyLevel
dmiBear = diplus   <  diminus  and adxValue >= keyLevel
dmiWeak = adxValue <  keyLevel and adxValue >  weakTrend

// Colored Directional Movement Index (CDMI) by @dgtrd and modified by @OskarGallard
slope_positive = adxValue > adxValue[1]
diCross = bar_index - ta.valuewhen(ta.cross(diplus, diminus), bar_index, 0)
strong = slope_positive ? "Strong" : " "
weak_up_dn = dmiWeak ? (diplus   >= diminus ? "[uptrend]" : "[downtrend]") : ""

dmiColor = dmiBull ? (slope_positive ? #006400     : color.green) : 
           dmiBear ? (slope_positive ? #910000     : color.red) : 
           dmiWeak ? (slope_positive ? color.black : color.gray) : 
           (slope_positive ? #DAA80A : #FFC40C)  
 
color_ADX = slope_positive ? pos : neg


//// gradiante ADX 

var color C_GOLD   = #CCCC00
var color C_VIOLET = #AA00FF




color Adxcolordown = color.new(sz < 0 ? color.red : color.green, 0)

color Adxcolorup = color.new(sz < 0 ? C_VIOLET : C_GOLD, 0)

color c_signaladx = color.from_gradient(adxValue, 20, 50, Adxcolordown, Adxcolorup)

plot(show_ADX ? adx_scale : na, color = c_signaladx, title = "ADX", linewidth = 2)
plot(zeroSQZ ? na : (show_ADX ? distance* ni/scale : na), color = keyColor ? dmiColor : color_ADX, title = "Key Level", linewidth = 1, style = plot.style_line)

// Label
lbStat  = histLabel > 0 ? "\n\n Timeframe: " + timeframe.period + ", " + str.tostring(histLabel) + " bar(s) earlier historical value.-": 
          "\n\n Timeframe: " + timeframe.period + ", last di cross " + str.tostring(diCross) + " bar(s) before.-"

adxMom  = slope_positive ? " ▲ Growing | Previous ADX: " + str.tostring(adxValue[1], "#.##") + ")\n» " : " ▼ Falling | Previous ADX(" + str.tostring(adxValue[1], "#.##") + ")\n» "

diStat  = diplus >= diminus ? "diPlus(" + str.tostring(diplus, "#.##") + ") >= diMinus(" + str.tostring(diminus, "#.##") + ")" :
          "diPlus(" + str.tostring(diplus, "#.##") + ") < diMinus(" + str.tostring(diminus, "#.##") + ")"

dmiText = dmiBull ? strong + " Bullish\n» ADX(" + str.tostring(adxValue, "#.##") + ")" + adxMom + diStat: 
          dmiBear ? strong + " Bearish\n» ADX(" + str.tostring(adxValue, "#.##") + ")" + adxMom + diStat:
          dmiWeak ? "Weak " + weak_up_dn + "\n» ADX(" + str.tostring(adxValue, "#.##") + ")" + adxMom + diStat:
          "No Trend\n» ADX(" + str.tostring(adxValue, "#.##") + ")" + adxMom + diStat

oneDay = 24 * 60 * 60 * 1000
barsAhead = 3
onward = if timeframe.ismonthly
    barsAhead * oneDay * 30
else if timeframe.isweekly
    barsAhead * oneDay * 7
else if timeframe.isdaily
    barsAhead * oneDay
else if timeframe.isminutes
    barsAhead * oneDay * timeframe.multiplier / 1440
else if timeframe.isseconds
    barsAhead * oneDay * timeframe.multiplier / 86400
else
    0

// Colored DMI
plotshape(show_DMI and diplus >= diminus, " +DI > -DI", shape.triangleup,   location.bottom, dmiColor)
plotshape(show_DMI and diplus <  diminus, " +DI < -DI", shape.triangledown, location.bottom, dmiColor)

alert_dmiBull = detect_bull_dmi and diplus >= diminus and ta.crossover(adxValue, keyLevel)
alert_dmiBear = detect_bear_dmi and diplus < diminus  and ta.crossover(adxValue, keyLevel)

if alert_dmiBull
    alert('Symbol = (' + syminfo.tickerid + ') \n TimeFrame = (' + timeframe.period + ') \n Current Price (' + str.tostring(close) + ') \n Bullish Trend [DMI].', alert.freq_once_per_bar_close)

if alert_dmiBear
    alert('Symbol = (' + syminfo.tickerid + ') \n TimeFrame = (' + timeframe.period + ') \n Current Price (' + str.tostring(close) + ') \n Bearish Trend [DMI].', alert.freq_once_per_bar_close)

// Label
if not hideLabel
    label dmiLabel = label.new(time, 0, text="DMI reading: " + dmiText[histLabel] + lbStat
         ,tooltip="                 ◁ how to read colored dmi ▷\n* triangle shapes:\n  ▲- bullish : diplus >= diminus\n  ▼- bearish : diplus < diminus\n* colors:\n  green - bullish trend : adx >= keyLevel and di+ > di-\n  red - bearish trend : adx >= keyLevel and di+ < di- \n  gray - weak trend : weekTrend < adx < keyLevel\n  yellow - no trend : adx < weekTrend\n* color density:\n  darker : adx growing\n  lighter : adx falling "
         ,color=dmiColor[histLabel], xloc=xloc.bar_time, style=label.style_label_left, textcolor=adxValue[histLabel] < weakTrend ? color.black : color.white, textalign=text.align_left)
    label.set_x(dmiLabel, label.get_x(dmiLabel) + onward)
    label.delete(dmiLabel[1])

//_____________________________________________________________________________________
// Hull Moving Average
//_____________________________________________________________________________________
// EHMA    
EHMA(_src, _length) =>  ta.ema((2 * ta.ema(_src, _length / 2)) - ta.ema(_src, _length), math.round(math.sqrt(_length)))
// THMA    
THMA(_src, _length) =>  ta.wma(ta.wma(_src, _length / 3) * 3 - ta.wma(_src, _length / 2) - ta.wma(_src, _length), _length)
// Adaptive HMA 
AHMA(_src, _length) => ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.floor(math.sqrt(_length))) 

// Hull type
hull_type(type, src, len) =>
    type == "HMA"  ? ta.hma(src, len) : type == "EHMA" ? EHMA(src, len) : type == "THMA" ? THMA(src, len/2) : AHMA(src, len)

xHMA = hull_type(h_type, get_src(h_src), h_Length)
longHMA  = ta.crossover(xHMA[0], xHMA[2])
shortHMA = ta.crossunder(xHMA[0], xHMA[2])
hull_color = xHMA[0] > xHMA[2] ? color.new(#45CEA2, 85) : color.new(#CE4571, 85)
bgcolor(bg_Hull ? hull_color : na, title = "Hull Background")
plotshape(show_Hull and longHMA,  title="Long Cross",  color=color.new(#45CEA2, 10), style=shape.triangleup,   location=location.bottom, size=size.tiny, text='H')
plotshape(show_Hull and shortHMA, title="Short Cross", color=color.new(#CE4571, 10), style=shape.triangledown, location=location.top,    size=size.tiny, text='H')

//______________________________________________________________________________
// The Expert Trend Locator - XTL 
//______________________________________________________________________________
media  = ma(MA_Type2, get_src(src_xtl), period_xtl)
cciNT = (get_src(src_xtl) - media) / (0.015 * ta.dev(get_src(src_xtl), period_xtl))
cciT  = (get_src(src_xtl) - media) / (0.015 * ta.dev(math.abs(get_src(src_xtl) - media), period_xtl))
values = uTrad ? cciT : cciNT

var color color_XTL = na
if (values < -fixed_Value) 
    color_XTL := color_xtlDN // Bear
if (-fixed_Value <= values and values <= fixed_Value)
    color_XTL := color_xtlNe // Neutral
if (values > fixed_Value)
    color_XTL := color_xtlUP // Bull
barcolor(candles_XTL ? color_XTL : na, title="XTL", editable=false) 

//_____________________________________________________________________________________
// Based on "Williams_Vix_Fix_V3_Upper_Text_Plots" - Author: @ChrisMoody
// https://www.tradingview.com/script/1ffumXy5-CM-Williams-Vix-Fix-V3-Upper-Text-Plots/
// https://www.ireallytrade.com/newsletters/VIXFix.pdf
//_____________________________________________________________________________________
// Williams Vix Fix Formula
wvf       = ((ta.highest(close, pd)-low)/(ta.highest(close, pd)))*100
sDev      = mult * ta.stdev(wvf, bbl)
midLine   = ta.sma(wvf, bbl) 
lowerBand = midLine - sDev
upperBand = midLine + sDev
rangeHigh = (ta.highest(wvf, lb)) * ph

// Filtered Criteria
upRange      = low > low[1] and close > high[1]
upRange_Aggr = close > close[1] and close > open[1]
// Filtered Criteria
filtered      = ((wvf[1] >= upperBand[1] or wvf[1] >= rangeHigh[1]) and (wvf < upperBand and wvf < rangeHigh))
filtered_Aggr = (wvf[1] >= upperBand[1] or wvf[1] >= rangeHigh[1]) and not (wvf < upperBand and wvf < rangeHigh)

// Alerts Criteria
alert1  = wvf >= upperBand or wvf >= rangeHigh ? 1 : 0
alert2  = (wvf[1] >= upperBand[1] or wvf[1] >= rangeHigh[1]) and wvf < upperBand and wvf < rangeHigh ? 1 : 0
cond_FE = upRange and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered 
alert3  = cond_FE ? 1 : 0
cond_AE = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr
alert4  = cond_AE ? 1 : 0

plotshape(sbcc and  alert1 ? alert1 : na,   "WVF Is True", color=color.new(#80FF00, 60), style=shape.triangledown, location=location.bottom, size=size.tiny)
plotshape(sbc  and  alert2 ? alert2 : na,   "WVF Was True - Now False", color=color.new(#008080, 60), style=shape.triangledown, location=location.bottom, size=size.tiny)
plotshape(sbcAggr and alert4 ? alert4 : na, "Aggressive Entry", color=color.new(#80FF00, 20), style=shape.triangleup, location=location.bottom, size=size.tiny, text='AE')
plotshape(sbcFilt and alert3 ? alert3 : na, "Filtered Entry", color=color.new(#80FF00, 20), style=shape.triangleup, location=location.bottom, size=size.tiny, text='FE')

if alert_AE and cond_AE
    alert('Symbol = (' + syminfo.tickerid + ') \n TimeFrame = (' + timeframe.period + ') \n Current Price (' + str.tostring(close) + ') \n Aggressive Entry [VixFix].', alert.freq_once_per_bar_close)

if alert_FE and cond_FE
    alert('Symbol = (' + syminfo.tickerid + ') \n TimeFrame = (' + timeframe.period + ') \n Current Price (' + str.tostring(close) + ') \n Filtered Entry [VixFix].', alert.freq_once_per_bar_close)
 
ADXAlcista = adxValue[1] > adxValue[0]  and  adxValue[2] > adxValue[1] 
ADXBajista = adxValue[1] < adxValue[0]  and  adxValue[2] < adxValue[1] 


//// aqui termina 

///////// divergencias Aall count 



lrb = input.int(5, title='Pivot Point Period divergences', minval=1)
mindiv = input.int(8, title='Minimum #Num of Divergence for Alert', minval=1, group='TPSL')
Esperandodiv = input.int(5, title='Wait #Num of Divergence for Alert', minval=1, group='TPSL')
chcut = input(false, title='Check Cut-Through in indicators')

// RSI
rsi = ta.rsi(close, 14)
// MACD
[macd, signal, deltamacd] = ta.macd(close, 12, 26, 9)
// Momentum
moment = ta.mom(close, 10)
// CCI
cci = ta.cci(close, 10)
// OBV
Obv = ta.obv  // cum(change(close) > 0 ? volume : change(close) < 0 ? -volume : 0 * volume)
// Stoch
stk = ta.sma(ta.stoch(close, high, low, 14), 3)
// DIOSC
DI = ta.change(high) - -ta.change(low)
trur = ta.rma(ta.tr, 14)
diosc = fixnan(100 * ta.rma(DI, 14) / trur)
// volume weighted macd
maFast = ta.vwma(close, 12)
maSlow = ta.vwma(close, 26)
vwmacd = maFast - maSlow
// Chaikin money flow
Cmfm = (close - low - (high - close)) / (high - low)
Cmfv = Cmfm * volume
cmf = ta.sma(Cmfv, 21) / ta.sma(volume, 21)
// Moneyt Flow Index
Mfi = ta.mfi(close, 14)

float top1a = na
float bot = na
top1a := ta.pivothigh(lrb, lrb)
bot := ta.pivotlow(lrb, lrb)

topc = 0
botc = 0
topc := top1a ? lrb : nz(topc[1]) + 1
botc := bot ? lrb : nz(botc[1]) + 1

// Negative Divergence (checking possible higher highs(lb=0))
newtop = ta.pivothigh(lrb, 0)  // check only left side
emptyh = true
if not na(newtop) and newtop > high[topc]  // there must not close price higher than the line between last PH and current high
    diff = (newtop - high[topc]) / topc
    hline = newtop - diff  // virtual line to check there is no close price higher than it
    for x = 1 to topc - 1 by 1
        if close[x] > hline
            emptyh := false
            break
        hline -= diff
        hline
else
    emptyh := false
    emptyh

// check cut-through in indicators
nocut1(indi, len) =>
    _ret = true
    diff = (indi - nz(indi[len])) / len
    ln = indi - diff
    for x = 1 to len - 1 by 1
        if nz(indi[x]) > ln
            _ret := false
            break
        ln -= diff
        ln
    _ret

rsiokn = nocut1(rsi, topc)
macdokn = nocut1(macd, topc)
deltamacdokn = nocut1(deltamacd, topc)
momentokn = nocut1(moment, topc)
cciokn = nocut1(cci, topc)
obvokn = nocut1(ta.obv, topc)
stkokn = nocut1(stk, topc)
dioscokn = nocut1(diosc, topc)
vwmacdokn = nocut1(vwmacd, topc)
cmfokn = nocut1(cmf, topc)
mfiokn = nocut1(Mfi, topc)

negadiv(indi, isok) =>
    _ret = emptyh and not na(newtop) and indi[topc] > indi and (not chcut or isok)
    _ret

// Positive Divergence (checking possible Lower lows(lb=0))
newbot = ta.pivotlow(lrb, 0)  // check only left side
emptyl = true
if not na(newbot) and newbot < low[botc]  // there must not close price lower than the line between last PL and current low
    diff = (newbot - low[botc]) / botc
    lline = newbot - diff  // virtual line to check there is no close price lower than it
    for x = 1 to botc - 1 by 1
        if close[x] < lline
            emptyl := false
            break
        lline -= diff
        lline
else
    emptyl := false
    emptyl

// check cut-through in indicators
nocut2(indi, len) =>
    _ret = true
    diff = (indi - nz(indi[len])) / len
    ln = indi - diff
    for x = 1 to len - 1 by 1
        if nz(indi[x]) < ln
            _ret := false
            break
        ln -= diff
        ln
    _ret



rsiokp = nocut2(rsi, botc)
macdokp = nocut2(macd, botc)
deltamacdokp = nocut2(deltamacd, botc)
momentokp = nocut2(moment, botc)
cciokp = nocut2(cci, botc)
obvokp = nocut2(ta.obv, botc)
stkokp = nocut2(stk, botc)
dioscokp = nocut2(diosc, botc)
vwmacdokp = nocut2(vwmacd, botc)
cmfokp = nocut2(cmf, botc)
mfiokp = nocut2(Mfi, botc)

posidiv(indi, isok) =>
    _ret = emptyl and not na(newbot) and indi[botc] < indi and (not chcut or isok)
    _ret

posidiv_1 = posidiv(rsi, rsiokp)
negadiv_1 = negadiv(rsi, rsiokn)
iff_1 = negadiv_1 ? -1 : 0
indi1 = posidiv_1 ? 1 : iff_1
posidiv_2 = posidiv(macd, macdokp)
negadiv_2 = negadiv(macd, macdokn)
iff_2 = negadiv_2 ? -1 : 0
indi2 = posidiv_2 ? 1 : iff_2
posidiv_3 = posidiv(deltamacd, deltamacdokp)
negadiv_3 = negadiv(deltamacd, deltamacdokn)
iff_3 = negadiv_3 ? -1 : 0
indi3 = posidiv_3 ? 1 : iff_3
posidiv_4 = posidiv(moment, momentokp)
negadiv_4 = negadiv(moment, momentokn)
iff_4 = negadiv_4 ? -1 : 0
indi4 = posidiv_4 ? 1 : iff_4
posidiv_5 = posidiv(cci, cciokp)
negadiv_5 = negadiv(cci, cciokn)
iff_5 = negadiv_5 ? -1 : 0
indi5 = posidiv_5 ? 1 : iff_5
posidiv_6 = posidiv(Obv, obvokp)
negadiv_6 = negadiv(Obv, obvokn)
iff_6 = negadiv_6 ? -1 : 0
indi6 = posidiv_6 ? 1 : iff_6
posidiv_7 = posidiv(stk, stkokp)
negadiv_7 = negadiv(stk, stkokn)
iff_7 = negadiv_7 ? -1 : 0
indi7 = posidiv_7 ? 1 : iff_7
posidiv_8 = posidiv(diosc, dioscokp)
negadiv_8 = negadiv(diosc, dioscokn)
iff_8 = negadiv_8 ? -1 : 0
indi8 = posidiv_8 ? 1 : iff_8
posidiv_9 = posidiv(vwmacd, vwmacdokp)
negadiv_9 = negadiv(vwmacd, vwmacdokn)
iff_9 = negadiv_9 ? -1 : 0
indi9 = posidiv_9 ? 1 : iff_9
posidiv_10 = posidiv(cmf, cmfokp)
negadiv_10 = negadiv(cmf, cmfokn)
iff_10 = negadiv_10 ? -1 : 0
indi10 = posidiv_10 ? 1 : iff_10 
posidiv_11 = posidiv(Mfi, mfiokp)
negadiv_11 = negadiv(Mfi, mfiokn)
iff_11 = negadiv_11 ? -1 : 0
indi11 = posidiv_11 ? 1 : iff_11

totaldiv = indi1 + indi2 + indi3 + indi4 + indi5 + indi6 + indi7 + indi8 + indi9 + indi10 + indi11

//hline(0, color=color.gray, linestyle=hline.style_dotted)
//plot(math.abs(totaldiv), color=totaldiv > 0 ? color.new(color.lime, 0) : totaldiv < 0 ? color.new(color.red, 0) : na, style=plot.style_columns, linewidth=6)
//if totaldiv != 0
   // label.new(x=bar_index, y=math.abs(totaldiv), text=str.tostring(math.abs(totaldiv)), color=color.new(color.white, 100), textcolor=totaldiv < 0 ? color.red : color.lime)

//alertcondition(totaldiv >= mindiv, title='Positive Divergence', message='Positive Divergence')
//alertcondition(totaldiv <= -mindiv, title='Negative Divergence', message='Negative Divergence')

//plotshape(totaldiv >= mindiv, style=shape.triangledown, text='Div', color=color.new(color.green, 0), location=location.abovebar)
//plotshape(totaldiv <= -mindiv, style=shape.triangledown, text='Div', color=color.new(#ff2e2e, 0), location=location.belowbar)

Divergencianum= totaldiv >= 5 or totaldiv <= -5




///////////////Strategy Gooo/////
//////

/////strategy 

///////////////////////////////////////////////




Cuidatrade=false


IsLong() =>
    strategy.position_size > 0

IsShort() =>
    strategy.position_size < 0



BarsSinceFirstEntry() =>
    bar_index - strategy.opentrades.entry_bar_index(0)




    
//TP1Perc = input.float(6,title='TP1 Take Profit (%)', minval=0.0, step=0.1, defval=2, group='TPSL')
//TP2Perc = input.float(6,title='TP2 Take Profit (%)', minval=0.0, step=0.1, defval=4, group='TPSL')
//SLPerc = input.float(1,title='Stop Loss (%)', minval=0.0, step=0.1, defval=2, group='TPSL')


stoploss = input.float( 6 , "Stop Loss percentage" , minval= 0 , group='TPSL')
takeprofit = input.float( 2 , "Take Profit percentage" , minval= 0 ,group='TPSL')
trailingstop = input(true , "Trailing Stop")
trail_p = input.float( 0.05 , "Trailing Stop percentage (of entry price)" , minval= 0,  step=0.01,group='TPSL')


//TP1_Ratio = input.float(title='Sell Postion Size % @ TP1', defval=100, step=1, group='TPSL', tooltip='Example: 50 closing 50% of the position once TP1 is reached') / 100
boltakeprofit = input.bool(true,title='Bool take care profit',  defval=true, group='TPSL')
tkprofit = input.float(0.5,title='Take care of a take profit ', minval=0.1, step=0.1, defval=0.5, group='TPSL')
NumberbarsOpen = input.int(4,title='Number of bars take care strategy the strategy ', minval=1, defval=2, group='TPSL')


percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)

percentAsPrice(pcnt) =>
    strategy.position_size != 0 ? (pcnt / 100.0 + 1.0) * strategy.position_avg_price : float(na)

current_position_size = math.abs(strategy.position_size)
initial_position_size = math.abs(ta.valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))

//TP1 = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
//TP2 = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
//SL = strategy.position_avg_price - percentAsPoints(SLPerc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)




detect_exit = strategy.position_size == 0 and strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index
IsFlat() =>
    strategy.position_size == 0


longNow = (strategy.position_size[1] <= 0) and (strategy.position_size > 0)

shortNow = (strategy.position_size[1] >= 0) and (strategy.position_size < 0)



//DiscountPriceConfirm45= ta.crossover(DiscountZonePrice45,low ) //or ta.crossover(DiscountZonePrice[1],low [1] ) //or ta.crossover(DiscountZonePrice[2],low[2] )
//PremiumPriceConfirm45= ta.crossunder(PremiumZonePrice45,high ) 


////ver si existe cruce en rojo cercano 
//CruzoenverdeContando = ta.barssince(Ema14>Ema21)
//CruzoenrojoContando = ta.barssince(Ema14<Ema21)

Condbull= bullCond1 or bullCond2 
CondBear= bearCond1 or bearCond2 

 
LongConfirm=   Condbull   and ADXBajista //and posidiv_1 //and  indi1 //and CountSignalGreen>5 //and Condbull 
ShortConfirm=   CondBear  and  ADXAlcista //and negadiv_1 // and iff_1  //and CountSignalRed>5 //and CondBear



bgcolor(  Condbull   and ADXBajista and  indi1 ? color.new(#fffc32, 22)  : na)
bgcolor( CondBear  and  ADXAlcista  and iff_1 ? color.new(#ff2de6, 29)  : na)

plotshape(negadiv_1, "Div Rsi", color=color.new(#ff0000, 0), style=shape.triangledown, location=location.top, size=size.tiny, textcolor=color.white, text='RSI')
plotshape(posidiv_1, "Div Rsi", color=color.new(#aaff00, 0), style=shape.triangleup, location=location.bottom, size=size.tiny, textcolor=color.white, text='RSI')
//LongConfirm=   Condbull and  indi1  and ADXBajista //and CountSignalGreen>5 //and Condbull 
//ShortConfirm=   CondBear and iff_1 and  ADXAlcista //and CountSignalRed>5 //and 



//strategy.entry("long", strategy.long, CantidadContratos, when =  Entrandolong and rsiValue<30 or perfectlong )  ///  perfectlong perfectShort
if IsFlat()==true and  IsLong() ==false and IsShort() ==false and   LongConfirm
    strategy.entry("long", strategy.long,  comment="Buy" )  
    alert(": Long on :" +syminfo.basecurrency+ ":SL :" + str.tostring(stoploss) + ":TP :" +  str.tostring(takeprofit), alert.freq_once_per_bar_close)
if  IsFlat()==true and  IsLong() ==false and  IsShort() ==false and  ShortConfirm
    strategy.entry("short", strategy.short,  comment="Short")
    alert(": Short on :" +syminfo.basecurrency+ ":SL :" + str.tostring(stoploss) + ":TP :" +  str.tostring(takeprofit), alert.freq_once_per_bar_close)
bgcolor( IsFlat()==true and LongConfirm ? color.new(#ffffff, 31)  : na)
bgcolor( IsFlat()==true and ShortConfirm ? color.new(#000000, 23)  : na)

  



curProfitInPts() =>
    if strategy.position_size > 0
        (high - strategy.position_avg_price) 
    else if strategy.position_size < 0
        (strategy.position_avg_price - low) 
    else
        0
        
calcStopLossPrice(OffsetPts) =>
    if strategy.position_size > 0
        strategy.position_avg_price - OffsetPts * syminfo.mintick
    else if strategy.position_size < 0
        strategy.position_avg_price + OffsetPts * syminfo.mintick
    else
        0




av_p = strategy.position_avg_price

if trailingstop 
    
    //if strategy.position_size > 0 and strategy.position_size[1] <= 0

    //   mes_pa = "   start trail: "+str.tostring(   av_p + (av_p *  takeprofit/100)    ) +"\n trail offset: "+ str.tostring( av_p *(trail_p/100)   ) +"\n SL: "+ str.tostring( av_p - (av_p *  stoploss/100)    ) 

        //label.new(bar_index, low - low *(trail_p/100) , mes_pa , style = label.style_label_up , color = color.white ,textcolor = color.black, size = size.normal)

  //  else if  strategy.position_size < 0 and strategy.position_size[1] >= 0

    //    mes_pa = "   start trail: "+str.tostring(   av_p - (av_p *  takeprofit/100)    ) +"\n trail offset: "+ str.tostring(   av_p *(trail_p/100)     )  +"\n SL: "+ str.tostring( av_p + (av_p *  stoploss/100)    ) 
        
        //label.new(bar_index, high + high *(trail_p/100) , mes_pa , style = label.style_label_down  , color = color.white ,textcolor = color.black, size = size.normal)





    
    strategy.exit("exit_sell", "short" ,  stop  =  av_p + (av_p *  stoploss/100),  trail_price = av_p - (av_p *  takeprofit/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick  , comment = "exit_sell", alert_message = "exit_sell", alert_loss="exit_sell Alert Loss", alert_profit ="exit_sell", alert_trailing ="exit_sell") 
    
    strategy.exit("exit_buy", "long" ,  stop  =  av_p - (av_p *  stoploss/100)  ,  trail_price = av_p + (av_p *  takeprofit/100) , trail_offset = av_p *(trail_p/100) *1/syminfo.mintick ,comment = "exit_buy", alert_message = "exit_buy", alert_loss="exit_buy Alert Loss", alert_profit ="exit_buy", alert_trailing ="exit_buy" )

    

else
    strategy.exit("exit_buy", "long" ,  when= strategy.position_size > 0 ,  stop  =  av_p - (av_p *  stoploss/100)  ,  limit  =  av_p + (av_p *  takeprofit/100) , comment = "stakeprofit buy" ) 
    
    strategy.exit("exit_sell", "short" ,when= strategy.position_size < 0 ,  stop  =  av_p + (av_p *  stoploss/100)   ,  limit  =  av_p - (av_p *  takeprofit/100) , comment = "sttakeprofit sell") 

//strategy.exit('TP1', from_entry='long', qty=initial_position_size * TP1_Ratio, limit=TP1, stop=SL)

//strategy.exit('TP2', from_entry='long', limit=TP2, stop=SL)

//strategy.exit('Stop Lost Short', from_entry='short', limit=TP2, stop=SL)
//strategy.exit('Stop Lost Long', from_entry='long', limit=TP2, stop=SL)


// Plot take profit values for confirmation

//plot(series=strategy.position_size > 0 ? TP1 : na, color=color.new(color.green, 0), style=plot.style_circles, linewidth=1, title='Take Profit 1')
//plot(series=strategy.position_size > 0 ? TP2 : na, color=color.new(color.green, 0), style=plot.style_circles, linewidth=1, title=' Take Profit 2')
//plot(series=strategy.position_size > 0 ? SL : na, color=color.new(color.red, 0), style=plot.style_circles, linewidth=1, title='Stop Loss')


//strategy.

if strategy.openprofit>=tkprofit and  BarsSinceFirstEntry()>NumberbarsOpen
    Cuidatrade:=true 


CirraYaEnprofit= Cuidatrade==true and strategy.openprofit<=1

///Cuidaba el profit pero no es viable ya por el traling
if boltakeprofit and IsFlat()==false and Cuidatrade and  IsShort()==true
    strategy.close("short", qty_percent = 100, comment = "Tp Short stop%")
    alert('TP Short Stop %', alert.freq_once_per_bar_close)
    
if boltakeprofit and IsFlat()==false and Cuidatrade  and IsLong()==true
    strategy.close("long", qty_percent = 100, comment = "Tp Long stop")
    alert('TP Long Stop%', alert.freq_once_per_bar_close)




//if IsFlat()==false and bull_choch_alert and BarsSinceFirstEntry()>1
 //   strategy.close("short", qty_percent = 100, comment = "Miedo Short stop")
  //  alert('Stop Lost Short Miedo ', alert.freq_once_per_bar_close)
    
//if IsFlat()==false and bear_choch_alert  and BarsSinceFirstEntry()>1
 //   strategy.close("long", qty_percent = 100, comment = "Miedo Long stop")
  //  alert('Stop Lost Long Miedo', alert.freq_once_per_bar_close)
    

//bgcolor( detect_exit==true and ADXAlcista and AdxcolorEstaenrojo==false ? color.new(#9fff32, 31)  : na)
//bgcolor( detect_exit==true and ADXBajista and AdxcolorEstaenrojo==true ? color.new(#ff3333, 23)  : na)



//if ADXAlcista and AdxcolorEstaenrojo==false
//    strategy.close("short", qty_percent = 100, comment = "close buy entry for 100%")
    
//if ADXBajista and AdxcolorEstaenrojo==true
//    strategy.close("long", qty_percent = 100, comment = "close buy entry for 100%")











 // ------
// Table
gr50        = 'Table'
showTable   = input(true, 'Show Quotes Table', group=gr50)
quote       = input('Keep Spirit 😗', 'Drop Your Quotes Here', group=gr50)
tabPosI_    = input.string('Top', 'Table Position', ['Top', 'Middle', 'Bot'], group=gr50)
tabPos_     = tabPosI_=='Top'? position.top_right : tabPosI_=='Bot'? position.bottom_right : position.middle_right
tabColor    = input.color( color.new(color.black,70) , 'Background', group=gr50)
borderCol   = input.color(color.black , 'Border', group=gr50)
tabTextCol  = input.color(color.white, 'Text', group=gr50)
texttendencia= 'No se lee'






saTable     = table.new(tabPos_, 10, 10, tabColor, borderCol, 1, borderCol, 1)
if showTable
    table.cell(saTable, 0, 0, 'Val Adx ' +str.tostring(adxValue)  +str.tostring(adxValue) , text_color=tabTextCol, text_size=size.small)
    table.cell(saTable, 0, 1, ' Array Long  ' +str.tostring(CountSignalGreen ) + ' Condition Long  ' +str.tostring(CountSignalGreen>3 ), text_color=tabTextCol, text_size=size.small)
    table.cell(saTable, 0, 2, ' Array Short  ' +str.tostring(CountSignalRed )+ ' Condition Short  ' +str.tostring(CountSignalRed>3 ), text_color=tabTextCol, text_size=size.small)
   // table.cell(saTable, 0, 3, 'Val inversa ' +str.tostring(iff_1inversa), text_color=tabTextCol, text_size=size.small)
    //table.cell(saTable, 0, 4, ' SL: ' +str.tostring(SL)  +' TP1: ' +str.tostring(TP1)+ ' TP2: ' +str.tostring(TP2), text_color=tabTextCol, text_size=size.small)
    //table.cell(saTable, 0, 5, ' val normal: ' +str.tostring(val)+ ' val 45: ' +str.tostring(Val45mnts), text_color=tabTextCol, text_size=size.small)
    table.cell(saTable, 0, 3, ' Array Long 45  ' +str.tostring(CountSignalGreen45 ) + ' Array Short 45 ' +str.tostring(CountSignalRed45 ), text_color=tabTextCol, text_size=size.small)
   /// table.cell(saTable, 0, 6, ' Currency: ' +str.tostring(syminfo.basecurrency), text_color=tabTextCol, text_size=size.small)