iBagging Multi-Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SkyRockSignals

//@version=4
study(title="iBagging", shorttitle="iB", overlay=true, resolution="")


var int sum_bull=0
var int sum_bear=0
sum_bear:=0
sum_bull:=0

//EMAx2++ params
len_fast = input(9, minval=1, title="Length of fast EMA")
src_fast = input(close, title="Source of fast EMA")
offset_fast = input(title="Offset of fast EMA", type=input.integer, defval=0, minval=-500, maxval=500)

len_slow = input(30, minval=1, title="Length of slow EMA")
src_slow = input(close, title="Source of slow EMA")
offset_slow = input(title="Offset of slow EMA", type=input.integer, defval=0, minval=-500, maxval=500)

len_filter=input(title="Length of Filter", type=input.integer,defval=10)
filter = input(true,title='Filter', type=input.bool)

//divergency params
len = input(title="RSI Period", minval=1, defval=14)
src = input(title="RSI Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
osc = rsi(src, len)

//BB Power Params
len_bb = input(title="EMA Period of BB Power", minval=1, defval=13)

//NATR PARAMS
natr_len=input(title='Normalized ATR Length',minval=1,defval=13)
natr_trashold=input(title="NATR Trashold",minval=0,defval=1)

//EMAx2++
basis = sma(close, 20)
dev = 2.0 * stdev(close, 20)
upper = basis + dev
lower = basis - dev
width = abs(upper-lower)
var float[] width_arr=array.new_float(len_filter)
for i=0 to array.size(width_arr)-1
    array.push(width_arr,width[i])
    array.shift(width_arr)
    
//supp/resis indicator params
n = input(title="Periods of pivot", defval=20, minval=2, type=input.integer)
inacc = input(title = "Inaccuracy", defval = 0.05, type = input.float, step = 0.005)
int i_history = input(200, "Number of fractals")

width_aver = array.avg(width_arr)
out_fast = ema(src_fast, len_fast)
out_slow = ema(src_slow, len_slow)
    
var bool bullish=false
var bool strong_bullish=false
var bool bearish=false
var bool strong_bearish=false

if (filter==true)


    bullish:= crossover(out_fast,out_slow) //and width>width_aver
    strong_bullish:=out_slow<min(close,open) and crossover(out_fast,out_slow) and width>width_aver
    eq=bullish==strong_bullish
    bullish:= not eq

    bearish:= crossunder(out_fast,out_slow)  //and width>width_aver
    strong_bearish:=out_slow>min(open,close) and crossunder(out_fast,out_slow) and width>width_aver
    eq:= bearish==strong_bearish
    bearish:= not eq
else
    bullish:= crossover(out_fast,out_slow)// and width>width_aver
    strong_bullish:=out_slow<min(close,open) and crossover(out_fast,out_slow)
    eq=bullish==strong_bullish
    bullish:= not eq

    bearish:= crossunder(out_fast,out_slow)
    strong_bearish:=out_slow>min(open,close) and crossunder(out_fast,out_slow)
    eq:= bearish==strong_bearish
    bearish:= not eq
    
if (bearish or strong_bearish)
    sum_bear:=sum_bear+1

if (bullish or strong_bullish)
    sum_bull:=sum_bull+1  
    
//divergency--------------------------------------------------------------------
var bool[] bull_div_array=array.new_bool(5000)
var bool[] bear_div_array=array.new_bool(5000)
plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
	bars = barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper


// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
bullCond = priceLL and oscHL and plFound



// Regular Bearish
// Osc: Lower High

oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High

priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
bearCond = priceHH and oscLH and phFound
for i=0 to 3
    if (bearCond[4999-i]==true)
        sum_bear:=sum_bear+1
        break
        
for i=0 to 3
    if (bullCond[4999-i]==true)
        sum_bull:=sum_bull+1
        break
    
//bull_bear power
bull_power=high-ema(close,len_bb)
bear_power=low-ema(close,len_bb)
if (bull_power>0 and bear_power>0)
    sum_bull:=sum_bull+1
if (bull_power<0 and bear_power<0)
    sum_bear:=sum_bear+1
    
//Norm ATR
natr = 100 * atr(natr_len) / close
if (natr>natr_trashold)
    sum_bull:=sum_bull+1
    sum_bear:=sum_bear+1



//enter
var float pivot_less=100000000000000000
var float pivot_more=-1

longCondition = sum_bull>=3
plotshape(longCondition,text="Bullish Signal",style=shape.flag,size=size.small,offset=2,color=color.green,textcolor=color.green,location=location.abovebar)

shortCondition = sum_bear>=3
plotshape(shortCondition,text="Bearish Signal",style=shape.flag,size=size.small,offset=2,color=color.red,textcolor=color.red,location=location.belowbar)
 
