EMArea
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © csirisomboonrat

//@version=5
indicator("Short indicator", overlay=true)


src = close
// input for RSI differest Timeframe
rsi = ta.rsi(src, 7)

// ********************************************************************************************** //
// ***********************  Storchastihttps://www.tradingview.com/chart/PTzN6SrG/#cs RSI SECTION ******************************************** //
// ********************************************************************************************** //

smoothK = 3
smoothD = 3
lengthRSI = 14
lengthStoch = 14
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
// ------------------------------ End section STO RSI  ---------------------------------- //

// ********************************************************************************************** //
// ************************************  ADX SECTION ******************************************** //
// ********************************************************************************************** //

len = 14

TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)
Dtrend = math.abs(DIPlus) - math.abs(DIMinus) + 20  // If Dtrend > 0 => Uptrend And Dtrend <0 => Downtrend


// ------------------------------ End section ADX  ---------------------------------- //

normalize_bb(float upper, float lower, int days) =>
    // BolingerBand Ranking
    
    normalize_bb = 0.0
    
    mult = 2.0
    basis = ta.sma(upper, days)
    dev = mult * ta.stdev(upper, days)                                               // Expand deviation to 1.1X from original Bollinger Band
    // upper = basis + dev                                                             // Get upper band 
    // secondupper = basis + (1.5* dev)                                                // Crete new band that higher than original upperband call "secondupper" (1.3X * 1.1X of original)
    // thirdupper = basis + (1.8* dev)
    fourthupper = basis + (2.1* dev)
    
    if upper > basis
        dd = (upper - basis) / (fourthupper - basis)
        normalize_bb := math.pow(dd, 0.33) * 50 + 50
        
    
    basis_l = ta.sma(lower, days)
    dev_l = mult * ta.stdev(lower, days)         
    fourthlower = basis_l - (2.1* dev_l)
    
    if lower < basis_l
        dd_l = (basis_l - lower) / (basis_l - fourthlower)
        normalize_bb := 50 - math.pow(dd_l, 0.33) * 50
        
    normalize_bb

em_area(w,x,y,z) =>
    ema100 = ta.ema(close,w)
    ema50 = ta.ema(close, x)
    ema21 = ta.ema(close, y)
    ema13 = ta.ema(close, z)
    
    ema1d = ema100 - ema21
    ema2d = ema50 - ema13

    em_area = math.abs(ema1d) * math.abs(ema2d) / math.pow((math.abs(ema1d) + math.abs(ema2d))/2,2)
    
    if (ema100 < ema50 and ema50 < ema21)
        em_area := 50 + em_area * 50
    else if ema100 > ema50 and ema50 > ema21
        em_area := 50 - (em_area * 50)
    else
        em_area := 50

    em_area


// Getting inputs
fast_length = 12
slow_length = 26

signal_length = 9
sma_source = "EMA"
sma_signal = "EMA"
// Plot colors

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

color=(hist>=0 ? (hist[1] < hist ? 2 : 1) : (hist[1] < hist ? -1 : -2))

test_md = color
for i = 1 to 6
    if color[i] > 0 
        test_md := test_md + color[i]
    else
        test_md := test_md + 2
        
emarea = em_area(100,50,21,14)

// long = 0
// if emarea <= 5 and nbb <= 12 and rsii <= 25
    //long := 1

//if long == 1 and ta.sma(long, 10) == 0.4
    //my_label = label.new(bar_index, open, text="Long", yloc=yloc.belowbar, style=label.style_label_up)

// plot(long, color=color.green)
plot(test_md)
kdcross = ta.crossunder(k,d)

short = 0
if rsi > 70 and k > d and k > 85
    short := 1

if short == 1 and ta.sma(short, 10) == 0.4 and test_md <= 10 and emarea == 50
    short_label = label.new(bar_index, close, text="Short", yloc=yloc.abovebar, style=label.style_label_down, color=color.red)

