Libre
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Schwehng
//@version=5
library("Libre")

rng_size(x, qty, n)=> 
    wper      = (n*2) - 1
    avrng     = ta.ema(math.abs(x - x[1]), n)
    AC = ta.ema(avrng, wper)*qty
    rng_size = AC
rng_filt(x, rng_, n)=>
    r          = rng_
    var rfilt  = array.new_float(2, x)
    array.set(rfilt, 1, array.get(rfilt, 0))
    if x - r > array.get(rfilt, 1)
        array.set(rfilt, 0, x - r)
    if x + r < array.get(rfilt, 1)
        array.set(rfilt, 0, x + r)
    rng_filt1 = array.get(rfilt, 0)
    hi_band   = rng_filt1 + r
    lo_band   = rng_filt1 - r
    rng_filt  = rng_filt1
    [hi_band, lo_band, rng_filt]

donchian(int len) => math.avg(ta.lowest(len), ta.highest(len))

variant1(src) =>
    a1 = math.exp(-1.414*3.14159 / 8)
    b1 = 2*a1*math.cos(1.414*3.14159 / 8)
    c3 = (-a1)*a1
    v12 = 0.0
    v12 := (1 - b1 - c3)*(src + nz(src[1])) / 2 + b1*nz(v12[1]) + c3*nz(v12[2])
    v12
variant2(src) =>
    vsrc=ta.sma(src, 8)
    v7 = 0.0
    v7 := na(v7[1]) ? vsrc : (v7[1] * (8 - 1) + src) / 8
    v7

isBat(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.382 and xab <= 0.5
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad <= 0.618 and xad <= 1.000    // 0.886
    _xab and _abc and _bcd and _xad and (_mode)
isAntiBat(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.500 and xab <= 0.886    // 0.618
    _abc = abc >= 1.000 and abc <= 2.618    // 1.13 --> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618    // 2.0  --> 2.618
    _xad = xad >= 0.886 and xad <= 1.000    // 1.13
    _xab and _abc and _bcd and _xad and (_mode)
isAltBat(xab,abc,bcd,xad,_mode)=>
    _xab = xab <= 0.382
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.0 and bcd <= 3.618
    _xad = xad <= 1.13
    _xab and _abc and _bcd and _xad and (_mode)
isButterfly(xab,abc,bcd,xad,_mode)=>
    _xab = xab <= 0.786
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad >= 1.27 and xad <= 1.618
    _xab and _abc and _bcd and _xad and (_mode)
isAntiButterfly(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.236 and xab <= 0.886    // 0.382 - 0.618
    _abc = abc >= 1.130 and abc <= 2.618    // 1.130 - 2.618
    _bcd = bcd >= 1.000 and bcd <= 1.382    // 1.27
    _xad = xad >= 0.500 and xad <= 0.886    // 0.618 - 0.786
    _xab and _abc and _bcd and _xad and (_mode)
isABCD(xab,abc,bcd,xad,_mode)=>
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _abc and _bcd and (_mode)
isGartley(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.5 and xab <= 0.618 // 0.618
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _xad = xad >= 0.75 and xad <= 0.875 // 0.786
    _xab and _abc and _bcd and _xad and (_mode)
isAntiGartley(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.500 and xab <= 0.886    // 0.618 -> 0.786
    _abc = abc >= 1.000 and abc <= 2.618    // 1.130 -> 2.618
    _bcd = bcd >= 1.500 and bcd <= 5.000    // 1.618
    _xad = xad >= 1.000 and xad <= 5.000    // 1.272
    _xab and _abc and _bcd and _xad and (_mode)
isCrab(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.500 and xab <= 0.875    // 0.886
    _abc = abc >= 0.382 and abc <= 0.886    
    _bcd = bcd >= 2.000 and bcd <= 5.000    // 3.618
    _xad = xad >= 1.382 and xad <= 5.000    // 1.618
    _xab and _abc and _bcd and _xad and (_mode)
isAntiCrab(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.250 and xab <= 0.500    // 0.276 -> 0.446
    _abc = abc >= 1.130 and abc <= 2.618    // 1.130 -> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618    // 1.618 -> 2.618
    _xad = xad >= 0.500 and xad <= 0.750    // 0.618
    _xab and _abc and _bcd and _xad and (_mode)
isShark(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.500 and xab <= 0.875    // 0.5 --> 0.886
    _abc = abc >= 1.130 and abc <= 1.618    //
    _bcd = bcd >= 1.270 and bcd <= 2.240    //
    _xad = xad >= 0.886 and xad <= 1.130    // 0.886 --> 1.13
    _xab and _abc and _bcd and _xad and (_mode)
isAntiShark(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.382 and xab <= 0.875    // 0.446 --> 0.618
    _abc = abc >= 0.500 and abc <= 1.000    // 0.618 --> 0.886
    _bcd = bcd >= 1.250 and bcd <= 2.618    // 1.618 --> 2.618
    _xad = xad >= 0.500 and xad <= 1.250    // 1.130 --> 1.130
    _xab and _abc and _bcd and _xad and (_mode)
is5o(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 1.13 and xab <= 1.618
    _abc = abc >= 1.618 and abc <= 2.24
    _bcd = bcd >= 0.5 and bcd <= 0.625 // 0.5
    _xad = xad >= 0.0 and xad <= 0.236 // negative?
    _xab and _abc and _bcd and _xad and (_mode)
isWolf(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 1.27 and xab <= 1.618
    _abc = abc >= 0 and abc <= 5
    _bcd = bcd >= 1.27 and bcd <= 1.618
    _xad = xad >= 0.0 and xad <= 5
    _xab and _abc and _bcd and _xad and (_mode)
isHnS(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 2.0 and xab <= 10
    _abc = abc >= 0.90 and abc <= 1.1
    _bcd = bcd >= 0.236 and bcd <= 0.88
    _xad = xad >= 0.90 and xad <= 1.1
    _xab and _abc and _bcd and _xad and (_mode)
isConTria(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 0.382 and xab <= 0.618
    _abc = abc >= 0.382 and abc <= 0.618
    _bcd = bcd >= 0.382 and bcd <= 0.618
    _xad = xad >= 0.236 and xad <= 0.764
    _xab and _abc and _bcd and _xad and (_mode)
isExpTria(xab,abc,bcd,xad,_mode)=>
    _xab = xab >= 1.236 and xab <= 1.618
    _abc = abc >= 1.000 and abc <= 1.618
    _bcd = bcd >= 1.236 and bcd <= 2.000
    _xad = xad >= 2.000 and xad <= 2.236
    _xab and _abc and _bcd and _xad and (_mode)
f_last_fib(_rate,dzz,czz,fib_range)=>dzz > czz ? dzz-(fib_range*_rate):dzz+(fib_range*_rate)


export OOO(simple string tuuw) =>
    bool ixet = na
    var bool lmode = na
    bool lube = na
    barm = bar_index
    var tyme = str.format('{0,date,d-M-YYYY}', time)
    var Sharp1 = tuuw == "Sharp 1"
    var Sharp2 = tuuw == "Sharp 2"
    var Sharp3 = tuuw == "Sharp 3"
    var Sharp4 = tuuw == "Sharp 4"
    var Sharp5 = tuuw == "Sharp 5"
    var Sharp6 = tuuw == "Sharp 6"
    var Oscil1 = tuuw == "Oscil 1"
    var Oscil2 = tuuw == "Oscil 2"
    var Oscil3 = tuuw == "Oscil 3"
    var Oscil4 = tuuw == "Oscil 4"
    var Oscil5 = tuuw == "Oscil 5"
    var Oscil6 = tuuw == "Oscil 6"
    var Trend1 = tuuw == "Trend 1"
    var Trend2 = tuuw == "Trend 2"
    var Trend3 = tuuw == "Trend 3"
    var Trend4 = tuuw == "Trend 4"
    var Trend5 = tuuw == "Trend 5"
    var Trend6 = tuuw == "Trend 6"
    var Border1 = tuuw == "Border 1"
    var Border2 = tuuw == "Border 2"
    var Border3 = tuuw == "Border 3"
    var Border4 = tuuw == "Border 4"
    var Border5 = tuuw == "Border 5"
    var Border6 = tuuw == "Border 6"
    float resulta = 0.0
    var hayay= array.new_float(0)
    var lowow= array.new_float(0)
    var payer= array.new_float(0)
    var gain = array.new_float(0)
    var loss = array.new_float(0)
    var int posak = 0
    var float aver = 0.0
    var float ktpb = 0.0
    var float Pgains = 0.0
    var float DIFF = 0.0
    var float prast = close
    var float sumo = 0.0
    array.push(hayay, high)
    array.push(lowow, low)

    if Sharp1
        resulta := ta.linreg(close - math.avg(math.avg(ta.highest(high, 20), ta.lowest(low, 20)),ta.sma(close,20)), 20,0)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Sharp2
        price=open
        hma1=ta.hma(price, 14)
        hma2=ta.hma(price[1], 14)
        conversionLine = donchian(9)
        baseLine = donchian(26)
        leadLine1 = math.avg(conversionLine, baseLine)
        leadLine2 = donchian(52)
        MACD = ta.hma(price, 12) - ta.hma(price, 26)
        aMACD = ta.hma(MACD, 9)
        resulta := (hma1>hma2 and price>hma2 and leadLine1>leadLine2 and MACD>aMACD) ? 1 :
         (hma1<hma2 and price<hma2 and leadLine1<leadLine2 and MACD<aMACD) ? -1 : 0
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Sharp3
        [h_band, l_band, filt] = rng_filt(close, rng_size(close, 3.5, 20), 20)
        var fdir = 0.0
        fdir    := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir
        upward   = fdir==1 ? 1 : 0
        downward = fdir==-1 ? 1 : 0
        longCond = close > filt and close > close[1] and upward > 0 or close > filt and close < close[1] and upward > 0 
        shortCond = close < filt and close < close[1] and downward > 0 or close < filt and close > close[1] and downward > 0
        CondIni = 0
        CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
        resulta := (longCond and CondIni[1] == -1) ? 1 : (shortCond and CondIni[1] == 1) ? -1 : 0
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Sharp4
        awesome=(ta.sma(hl2,5)-ta.sma(hl2,34))*1000
        k=ta.sma(ta.stoch(close,high,low,14),3)
        d=ta.sma(14,3)
        rsi=ta.rsi(close,10)
        atrvalue=ta.rma(ta.tr,14)
        resulta := (k<20 and rsi<30 and awesome>awesome[1]) ? 1 : (k>80 and rsi>70 and awesome<awesome[1]) ? -1 : 0
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
        // if (LongCondition)
        //     stoploss=low-atrvalue
        //     takeprofit=close+atrvalue
        //     strategy.entry("Long Position", strategy.long)
        //     strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)
        // if (ShortCondition)
        //     stoploss=high+atrvalue
        //     takeprofit=close-atrvalue
        //     strategy.entry("Short Position",strategy.short)
        //     strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)
    else if Sharp5
        Rsi = ta.rsi(close, 14)
        RsiMa = ta.ema(Rsi, 5)
        AtrRsi = math.abs(RsiMa[1] - RsiMa)
        MaAtrRsi = ta.ema(AtrRsi, 27)
        longband = 0.0
        shortband = 0.0
        trend = 0
        DeltaFastAtrRsi = ta.ema(MaAtrRsi, 27) * 4.238
        newshortband = RsiMa + DeltaFastAtrRsi
        newlongband = RsiMa - DeltaFastAtrRsi
        longband := RsiMa[1] > longband[1] and RsiMa > longband[1] ? math.max(longband[1], newlongband) : newlongband
        shortband := RsiMa[1] < shortband[1] and RsiMa < shortband[1] ? math.min(shortband[1], newshortband) : newshortband
        cross_1 = ta.cross(longband[1], RsiMa)
        trend := ta.cross(RsiMa, shortband[1]) ? 1 : cross_1 ? -1 : nz(trend[1], 1)
        FastAtrRsiTL = trend == 1 ? longband : shortband
        QQExlong = 0
        QQExlong := nz(QQExlong[1])
        QQExshort = 0
        QQExshort := nz(QQExshort[1])
        QQExlong := FastAtrRsiTL < RsiMa ? QQExlong + 1 : 0
        QQExshort := FastAtrRsiTL > RsiMa ? QQExshort + 1 : 0
        if QQExlong == 1 ? FastAtrRsiTL[1] - 50 : na
            lube := true
        if QQExshort == 1 ? FastAtrRsiTL[1] - 50 : na
            lube := false
    else if Sharp6
        vrsi = ta.rsi(close, 6)
        [BBmid, BBup, BBlow] = ta.bb(close, 200, 2)
        Bbuy = ta.crossover(close, BBlow)
        Ssell = ta.crossunder(close, BBup)
        Vbuy = ta.crossover(vrsi, 50)
        Vsell = ta.crossunder(vrsi, 50)
        if (not na(vrsi))
            if Vbuy and Bbuy
                lube := true
            if Vsell and Ssell
                lube := false

    else if Oscil1
        TRIX = 10000 * ta.change(ta.ema(ta.ema(ta.ema(math.log(close), 18), 18), 18))
        TSI1 = ta.tsi(close, 25, 13)
        TSI2 = ta.tsi(close, 50, 26)
        resulta += (TRIX>0?1:TRIX<0?-1:0)
        resulta += (TSI1>0?1:TSI1<0?-1:0)
        resulta += (TSI2>0?1:TSI2<0?-1:0)
        if ta.crossover(resulta,1)
            lube := true
        if ta.crossunder(resulta,-1)
            lube := false
    else if Oscil2
        HULL = ta.wma(2 * ta.wma(close,27) - ta.wma(close,55), math.round(math.sqrt(55)))
        HULLx = HULL[2]
        resulta += (HULL>HULLx?1:HULL<HULLx?-1:0)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Oscil3
        HULL = ta.ema(2 * ta.ema(close, 27) - ta.ema(close, 55), math.round(math.sqrt(55)))
        HULLx = HULL[2]
        resulta += (HULL>HULLx?1:HULL<HULLx?-1:0)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Oscil4
        HULL = ta.wma(ta.wma(close,9) * 3 - ta.wma(close, 13) - ta.wma(close, 27), 27)
        HULLx = HULL[2]
        resulta += (HULL>HULLx?1:HULL<HULLx?-1:0)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Oscil5
        ten=ta.sma(hl2,9)
        kij=ta.sma(hl2,26)
        sen=ta.sma(hl2,52)
        sen := ta.change(sen,26)
        bull = ten > kij and close > sen
        bear = ten < kij and close < sen
        resulta += (ten>kij and close>sen?1: ten<kij and close<sen?-1: 0)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Oscil6
        k=ta.sma(ta.stoch(close,high,low,14),3)
        trsi=ta.rsi(close,10)
        awesome=(ta.sma(hl2,5)-ta.sma(hl2,34))*1000
        //d=sma(k,3)
        //atrvalue=ta.rma(ta.tr,14)
        resulta += (k<20 and trsi<30 and awesome>awesome[1]?1:k>80 and trsi>70 and awesome<awesome[1]?-1:0)
        // if (LongCondition)
        //     stoploss=low-atrvalue
        //     takeprofit=close+atrvalue
        //     strategy.entry("Long Position", strategy.long)
        //     strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)
        // if (ShortCondition)
        //     stoploss=high+atrvalue
        //     takeprofit=close-atrvalue
        //     strategy.entry("Short Position",strategy.short)
        //     strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false

    else if Trend1
        [nook,gook] = ta.supertrend(3, 10)
        resulta := -gook
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Trend2
        TRIX = 10000 * ta.change(ta.ema(ta.ema(ta.ema(math.log(ta.vwap), 18), 18), 18))
        TSI1 = ta.tsi(ta.vwap, 25, 13)
        TSI2 = ta.tsi(ta.vwap, 50, 26)
        resulta += (TRIX>0?1:TRIX<0?-1:0)
        resulta += (TSI1>0?1:TSI1<0?-1:0)
        resulta += (TSI2>0?1:TSI2<0?-1:0)
        if ta.crossover(resulta,2)
            lube := true
        if ta.crossunder(resulta,2)
            lube := false
    else if Trend3
        price=ta.vwap
        hma1=ta.hma(price, 14)
        hma2=ta.hma(price[1], 14)
        conversionLine = donchian(9)
        baseLine = donchian(26)
        leadLine1 = math.avg(conversionLine, baseLine)
        leadLine2 = donchian(52)
        MACD = ta.hma(price, 12) - ta.hma(price, 26)
        aMACD = ta.hma(MACD, 9)
        resulta := (hma1>hma2 and price>hma2 and leadLine1>leadLine2 and MACD>aMACD) ? 1 :
         (hma1<hma2 and price<hma2 and leadLine1<leadLine2 and MACD<aMACD) ? -1 : 0
        if ta.crossover(resulta,0)
            lube := true
        if ta.crossunder(resulta,0)
            lube := false
    else if Trend4
        ATR = ta.sma(ta.tr, 14)
        upT = low - ATR
        downT = high + ATR
        AlphaTrend = 0.0
        AlphaTrend := (false ? ta.rsi(close, 14) >= 50 : ta.mfi(hlc3, 14) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT
        if ta.crossover(AlphaTrend, AlphaTrend[2])
            lube := true
        if ta.crossunder(AlphaTrend, AlphaTrend[2])
            lube := false
    else if Trend5
        fastL = ta.highest(20)
        fastLC = ta.lowest(10)
        fastS = ta.lowest(20)
        fastSC = ta.highest(10)
        if high > fastL[1]
            lube := true
        if low < fastS[1]
            lube := false
        ixet := (low<=fastLC[1])?true : (high>=fastSC[1])?false : na
    else if Trend6
        slowL = ta.highest(55)
        slowLC = ta.lowest(20)
        slowS = ta.lowest(55)
        slowSC = ta.highest(20)
        if high > slowL[1]
            lube := true
        if low < slowS[1]
            lube := false
        ixet := (low<=slowLC[1])?true : (high>=slowSC[1])?false : na

    else if Border1
        RSI = ta.rsi(close,14)
        CCI = ta.cci(close,20)
        WERP = ta.wpr(14)
        STOCH = ta.stoch(close,high,low,14)
        rsik = ta.sma(ta.stoch(RSI, RSI, RSI, 14), 3)
        STOCHr = ta.sma(rsik, 3)
        resulta += (RSI>70?-1:RSI<30?1:0)
        resulta += (CCI>100?-1:CCI<-100?1:0)
        resulta += (WERP>-20?-1:WERP<-80?1:0)
        resulta += (STOCH>80?-1:STOCH<20?1:0)
        resulta += (STOCHr>80?-1:STOCHr<20?1:0)
        if ta.crossover(resulta,3)
            lube := true
        if ta.crossunder(resulta,3)
            lube := false
    else if Border2
        k = ta.sma(ta.stoch(close, high, low, 14), 3)
        dd = ta.sma(k, 3)
        vrsi = ta.rsi(close, 14)
        if (not na(k) and not na(dd))
            if (k[1] <= dd and k > dd  and k < 20)
                if (not na(vrsi)) and (vrsi[1] <= 30 and vrsi > 30)
                    lube := true
        if (ta.crossunder(k,dd) and k > 80)
            if (vrsi[1] >= 70 and vrsi < 70)
                lube := false
    else if Border3
        nLoss = ta.atr(10)
        xATRTrailingStop = 0.0
        xATRTrailingStop := (   (close>nz(xATRTrailingStop[1],0) and close[1]>nz(xATRTrailingStop[1],0))  ?  math.max(nz(xATRTrailingStop[1]),close -nLoss) :
             (      (close<nz(xATRTrailingStop[1],0) and close[1]<nz(xATRTrailingStop[1],0)) ? math.min(nz(xATRTrailingStop[1]), (close+nLoss)):
             (close>nz(xATRTrailingStop[1],0)?(close - nLoss):(close + nLoss)))    )
        ema   = ta.ema(close,1)
        above = ta.crossover(ema, xATRTrailingStop)
        below = ta.crossover(xATRTrailingStop, ema)
        if close > xATRTrailingStop and above 
            lube := true
        if close < xATRTrailingStop and below
            lube := false
    else if Border4
        closeSeries     = variant1(close)
        openSeries      = variant1(open)
        if ta.crossover(closeSeries, openSeries)
            lube := true
        if ta.crossunder(closeSeries, openSeries)
            lube := false
    else if Border5
        closeSeries     = variant2(close)
        openSeries      = variant2(open)
        if ta.crossover(closeSeries, openSeries)
            lube := true
        if ta.crossunder(closeSeries, openSeries)
            lube := false
    else if Border6
        _isUp = close >= open
        _isDown = close <= open
        zhz = ta.highest(2)
        zlz = ta.lowest(2)
        int _direction = na
        _direction := _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1])
        sz = _isUp[1] and _isDown and _direction[1] != -1 ? zhz : _isDown[1] and _isUp and _direction[1] != 1 ? zlz : na
        zxz = ta.valuewhen(sz, sz, 4) 
        zaz = ta.valuewhen(sz, sz, 3) 
        zbz = ta.valuewhen(sz, sz, 2) 
        zcz = ta.valuewhen(sz, sz, 1) 
        zdz = ta.valuewhen(sz, sz, 0)
        fib_range = math.abs(zdz-zcz)
        xab = (math.abs(zbz-zaz)/math.abs(zxz-zaz))
        xad = (math.abs(zaz-zdz)/math.abs(zxz-zaz))
        abc = (math.abs(zbz-zcz)/math.abs(zaz-zbz))
        bcd = (math.abs(zcz-zdz)/math.abs(zbz-zcz))
        negat = (zdz > zcz)
        posit = (zdz < zcz)
        buy_patterns_00 = isABCD(xab,abc,bcd,xad,posit) or isBat(xab,abc,bcd,xad,posit) or isAltBat(xab,abc,bcd,xad,posit) or 
         isButterfly(xab,abc,bcd,xad,posit) or isGartley(xab,abc,bcd,xad,posit) or isCrab(xab,abc,bcd,xad,posit) or 
         isShark(xab,abc,bcd,xad,posit) or is5o(xab,abc,bcd,xad,posit) or isWolf(xab,abc,bcd,xad,posit) or 
         isHnS(xab,abc,bcd,xad,posit) or isConTria(xab,abc,bcd,xad,posit) or isExpTria(xab,abc,bcd,xad,posit)
        buy_patterns_01 = isAntiBat(xab,abc,bcd,xad,posit) or isAntiButterfly(xab,abc,bcd,xad,posit) or 
         isAntiGartley(xab,abc,bcd,xad,posit) or isAntiCrab(xab,abc,bcd,xad,posit) or isAntiShark(xab,abc,bcd,xad,posit)
        sel_patterns_00 = isABCD(xab,abc,bcd,xad,negat) or isBat(xab,abc,bcd,xad,negat) or 
         isAltBat(xab,abc,bcd,xad,negat) or isButterfly(xab,abc,bcd,xad,negat) or isGartley(xab,abc,bcd,xad,negat) or 
         isCrab(xab,abc,bcd,xad,negat) or isShark(xab,abc,bcd,xad,negat) or is5o(xab,abc,bcd,xad,negat) or isWolf(xab,abc,bcd,xad,negat) 
         or isHnS(xab,abc,bcd,xad,negat) or isConTria(xab,abc,bcd,xad,negat) or isExpTria(xab,abc,bcd,xad,negat)
        sel_patterns_01 = isAntiBat(xab,abc,bcd,xad,negat) or isAntiButterfly(xab,abc,bcd,xad,negat) or 
         isAntiGartley(xab,abc,bcd,xad,negat) or isAntiCrab(xab,abc,bcd,xad,negat) or isAntiShark(xab,abc,bcd,xad,negat)
        ixet := (high >= f_last_fib(0.618,zdz,zcz,fib_range) or low <= f_last_fib(-0.236,zdz,zcz,fib_range)) ? true : 
         (low <= f_last_fib(0.618,zdz,zcz,fib_range) or high >= f_last_fib(-0.236,zdz,zcz,fib_range)) ? false : na
        if (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(0.236,zdz,zcz,fib_range)
            lube := true
        if (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(0.236,zdz,zcz,fib_range)
            lube := false


    var color cor = na
    bool bloo = na
    var hig = 0.0
    var owl = 0.0
    if lube == true
        cor := color.rgb(33, 149, 243, 66)
        if lmode == false
            hig := array.max(hayay)
            owl := array.min(lowow)
            ssol = -(math.round(((hig - prast) / prast) * 100,2))
            niag = -(math.round(((owl - prast) / prast) * 100,2))
            tpos = -(math.round(((close - prast) / prast) * 100,2))
            array.push(payer, tpos)
            array.push(gain, niag)
            array.push(loss, ssol)
            array.clear(hayay)
            array.clear(lowow)
            prast := close
            posak += 1 
        else if lmode
            hig := array.max(hayay)
            owl := array.min(lowow)
            niag = math.round(((hig - prast) / prast) * 100,2)
            ssol = math.round(((owl - prast) / prast) * 100,2)
            tpos = math.round(((close - prast) / prast) * 100,2)
            array.push(payer, tpos)
            array.push(gain, niag)
            array.push(loss, ssol)
            array.clear(hayay)
            array.clear(lowow)
            prast := close
            posak += 1 
        lmode := true

    else if lube == false
        cor := color.rgb(243, 33, 33, 67)
        if lmode == false
            hig := array.max(hayay)
            owl := array.min(lowow)
            ssol = -(math.round(((hig - prast) / prast) * 100,2))
            niag = -(math.round(((owl - prast) / prast) * 100,2))
            tpos = -(math.round(((close - prast) / prast) * 100,2))
            array.push(payer, tpos)
            array.push(gain, niag)
            array.push(loss, ssol)
            array.clear(hayay)
            array.clear(lowow)
            prast := close
            posak += 1 
        else if lmode
            hig := array.max(hayay)
            owl := array.min(lowow)
            niag = math.round(((hig - prast) / prast) * 100,2)
            ssol = math.round(((owl - prast) / prast) * 100,2)
            tpos = math.round(((close - prast) / prast) * 100,2)
            array.push(payer, tpos)
            array.push(gain, niag)
            array.push(loss, ssol)
            array.clear(hayay)
            array.clear(lowow)
            prast := close
            posak += 1 
        lmode := false


    if barstate.islastconfirmedhistory
        sumo := array.sum(payer)

        aver := sumo/posak
        ktpb := aver / (barm / posak)

        Pgains:= array.sum(gain)
        DIFF := Pgains / sumo
        //REMARK := DIFF < 1.0? str.tostring(DIFF) : "xxxx"

    [tyme,barm,sumo,posak,aver,ktpb,Pgains,DIFF,lube,open,high,low,close,hig,owl,cor,ixet]//,REMARK]




export MMMM(simple string toe,simple bool CLUST1,simple string SYM) => 
    string tex = CLUST1?toe:SYM

export WWWW(simple string oet, simple bool CLUST2, simple string STR) => string tex = CLUST2?oet: STR

export OOOO(simple string inputs, simple string tmp, simple bool CLUST3, simple bool CLUST4, simple string RES, simple string toe1, simple string toe2, simple string toe3, simple string toe4, simple string toe5) => 
    string tex=CLUST3?(CLUST4?(tmp=="1"?toe3: tmp=="5"?toe5: tmp=="15"?toe2: tmp=="30"?toe4: tmp=="60"?toe1:inputs): inputs==""?tmp:inputs): RES==""?tmp: RES

export XXXX(simple float toe) => float tex = toe

export SSSS(simple string toe) => string tex = toe



export HHHH(bool bowl,int bar,float hig,float owl,bool xet) =>
    var bool lmode = na
    var color cor = na
    var float prast = close
    var int galaw = 0
    if bowl
        cor := color.rgb(63, 70, 255)
        if lmode == false
            ssol = -(math.round(((hig - prast) / prast) * 100,2))
            niag = -(math.round(((owl - prast) / prast) * 100,2))
            tpos = -(math.round(((close - prast) / prast) * 100,2))
            box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(niag), 
             text_color=color.white,text_valign=text.align_bottom, bgcolor=na, text_size=size.normal)
            box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(ssol), 
             text_color=color.white,text_valign=text.align_top, bgcolor=color.rgb(91, 82, 255, 49), text_size=size.normal)
            box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
             text_color=color.white, bgcolor=na, text_size=size.normal)
        if lmode == true
            niag = math.round(((hig - prast) / prast) * 100,2)
            ssol = math.round(((owl - prast) / prast) * 100,2)
            tpos = math.round(((close - prast) / prast) * 100,2)
            box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(niag), 
             text_color=color.white,text_valign=text.align_top, bgcolor=na, text_size=size.normal)
            box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(ssol), 
             text_color=color.white,text_valign=text.align_bottom, bgcolor=color.rgb(243, 33, 33, 52), text_size=size.normal)
            box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
             text_color=color.white, bgcolor=na, text_size=size.normal)
        galaw := bar_index
        prast := close
        strategy.entry("long",strategy.long)
        lmode := true
    else if bowl == false
        cor := color.rgb(255, 66, 66)
        if lmode == false
            ssol = -(math.round(((hig - prast) / prast) * 100,2))
            niag = -(math.round(((owl - prast) / prast) * 100,2))
            tpos = -(math.round(((close - prast) / prast) * 100,2))
            box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(niag), 
             text_color=color.white,text_valign=text.align_bottom, bgcolor=na, text_size=size.normal)
            box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(ssol), 
             text_color=color.white,text_valign=text.align_top, bgcolor=color.rgb(91, 82, 255, 49), text_size=size.normal)
            box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
             text_color=color.white, bgcolor=na, text_size=size.normal)
        if lmode == true
            niag = math.round(((hig - prast) / prast) * 100,2)
            ssol = math.round(((owl - prast) / prast) * 100,2)
            tpos = math.round(((close - prast) / prast) * 100,2)
            box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(niag), 
             text_color=color.white,text_valign=text.align_top, bgcolor=na, text_size=size.normal)
            box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(ssol), 
             text_color=color.white,text_valign=text.align_bottom, bgcolor=color.rgb(243, 33, 33, 52), text_size=size.normal)
            box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
             text_color=color.white, bgcolor=na, text_size=size.normal)
        galaw := bar_index
        prast := close
        strategy.entry("short",strategy.short)
        lmode := false

    if xet
        strategy.close("long")
    else if xet==false
        strategy.close("short")

    cor



export DDDD(bool bwol,float num,int bmun,bool xet) =>
    var bool lmode = na
    bar = bar_index
    var hayay= array.new_float(0)
    var lowow= array.new_float(0)
    array.push(hayay, high)
    array.push(lowow, low)
    var float prast = close
    var int galaw = 0
    if bwol
        if num >= bmun
            if (bar - galaw) > 900
                galaw := (bar - 900)
            hig = array.max(hayay)
            owl = array.min(lowow)
            if lmode == false
                ssol = -(math.round(((hig - prast) / prast) * 100,2))
                niag = -(math.round(((owl - prast) / prast) * 100,2))
                tpos = -(math.round(((close - prast) / prast) * 100,2))
                box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(niag), 
                 text_color=color.white,text_valign=text.align_bottom, bgcolor=na, text_size=size.normal)
                box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(ssol), 
                 text_color=color.white,text_valign=text.align_top, bgcolor=color.rgb(91, 82, 255, 49), text_size=size.normal)
                box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
                 text_color=color.white, bgcolor=na, text_size=size.normal)
            if lmode == true
                niag = math.round(((hig - prast) / prast) * 100,2)
                ssol = math.round(((owl - prast) / prast) * 100,2)
                tpos = math.round(((close - prast) / prast) * 100,2)
                box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(niag), 
                 text_color=color.white,text_valign=text.align_top, bgcolor=na, text_size=size.normal)
                box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(ssol), 
                 text_color=color.white,text_valign=text.align_bottom, bgcolor=color.rgb(243, 33, 33, 52), text_size=size.normal)
                box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
                 text_color=color.white, bgcolor=na, text_size=size.normal)
            galaw := bar_index
            prast := close
            array.clear(hayay)
            array.clear(lowow)
            strategy.entry("long",strategy.long)
            lmode := true
    else if bwol == false
        if num <= -bmun
            if (bar - galaw) > 900
                galaw := (bar - 900)
            hig = array.max(hayay)
            owl = array.min(lowow)
            if lmode == false
                ssol = -(math.round(((hig - prast) / prast) * 100,2))
                niag = -(math.round(((owl - prast) / prast) * 100,2))
                tpos = -(math.round(((close - prast) / prast) * 100,2))
                box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(niag), 
                 text_color=color.white,text_valign=text.align_bottom, bgcolor=na, text_size=size.normal)
                box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(ssol), 
                 text_color=color.white,text_valign=text.align_top, bgcolor=color.rgb(91, 82, 255, 49), text_size=size.normal)
                box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
                 text_color=color.white, bgcolor=na, text_size=size.normal)
            if lmode == true
                niag = math.round(((hig - prast) / prast) * 100,2)
                ssol = math.round(((owl - prast) / prast) * 100,2)
                tpos = math.round(((close - prast) / prast) * 100,2)
                box.new(galaw,hig,bar,prast,color.blue,2,text=str.tostring(niag), 
                 text_color=color.white,text_valign=text.align_top, bgcolor=na, text_size=size.normal)
                box.new(galaw,prast,bar,owl,color.red,2,text=str.tostring(ssol), 
                 text_color=color.white,text_valign=text.align_bottom, bgcolor=color.rgb(243, 33, 33, 52), text_size=size.normal)
                box.new(galaw,prast,bar,close,color.yellow,2, text=str.tostring(tpos), 
                 text_color=color.white, bgcolor=na, text_size=size.normal)
            galaw := bar_index
            prast := close
            array.clear(hayay)
            array.clear(lowow)
            strategy.entry("short",strategy.short)
            lmode := false
    if xet
        strategy.close("long")
    else if xet==false
        strategy.close("short")


export IIII(float swap)=>
    median = ta.percentile_nearest_rank(swap, 3, 50)
    atr_ = 2 * ta.atr(14)
    lwr = median - atr_
    gap = ((median + atr_) - lwr) / 15
    [lwr,gap]

export QQQQ(bool bol1,simple float harang1,bool bol2,simple float harang2,bool bol3,simple float harang3,
 bool bol4,simple float harang4,bool bol5,simple float harang5)=>
    var DG = array.new_float(5,0)
    if bol1
        array.set(DG,0,harang1)
    else if bol1 == false
        array.set(DG,0,-harang1)
    if bol2
        array.set(DG,1,harang2)
    else if bol2 == false
        array.set(DG,1,-harang2)
    if bol3
        array.set(DG,2,harang3)
    else if bol3 == false
        array.set(DG,2,-harang3)
    if bol4
        array.set(DG,3,harang4)
    else if bol4 == false
        array.set(DG,3,-harang4)
    if bol5
        array.set(DG,4,harang5)
    else if bol5 == false
        array.set(DG,4,-harang5)
    oist = array.sum(DG)
    oist











 

























