BTC buy/sell effectiveness
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wzkkzw12345

//@version=4
study(title="BTC move easiness", resolution="60")
res = input("1", type=input.resolution)
length = input(12  , title="EMA length") 

length2 = input(72, title="Smoothing length")
show_lines = input(true, title="show high/low levels (for 1H 1m)")


f_intrabarBTCdata(res,_chartTf) =>
    // string _chartTf: chart TF in `timeframe.period` format. It is used to detect the first intrabar.
    var float up = 0.
    var float dn = 0.
    var float up_v = 0. 
    var float dn_v = 0.
    var float v = 0.
    var int   up_intrabars = na
    var int   dn_intrabars = na
    total_volume = volume + sign(change(close))*(security("BINANCE:BTCBUSD", res, sign(change(close))*volume)+security("KUCOIN:BTCUSDT", res, sign(change(close))*volume)+security("COINBASE:BTCUSD", res, sign(change(close))*volume)+security("FTX:BTCUSDT",res, sign(change(close))*volume)/hlc3+security("FTX:BTCUSD",res, sign(change(close))*volume)/hlc3 +security("BITFINEX:BTCUSD",res,  sign(change(close))*volume))
    abs_low = min(close[1], low)
    abs_high = max(close[1], high)
    if change(time(_chartTf))
        // First intrabar detected: reset data. 
        up_intrabars := 0
        dn_intrabars := 0
        v := total_volume
        dn_v := 0
        up_v := 0
        up := 0 
        dn := 0 
        if close>close[1]
            up_intrabars += 1
            up := log(close/close[1])//*total_volume
            up_v := total_volume*(abs_high-close[1])/(abs_high-abs_low)
        else
            if close< close[1]
                dn_intrabars += 1
                dn := log(close[1]/close)//*total_volume
                dn_v :=total_volume*(close[1]-abs_low)/(abs_high-abs_low)
    else
        v += total_volume
        if close>close[1]
            up_intrabars += 1
            up += log(close/close[1])//*total_volume
            up_v += total_volume*(abs_high-close[1])/(abs_high-abs_low)
        else
            if close< close[1]
                dn_intrabars += 1
                dn += log(close[1]/close)//*total_volume
                dn_v += total_volume*(close[1]-abs_low)/(abs_high-abs_low)
    [up, up_v, dn, dn_v, v, up_intrabars, dn_intrabars]


_f_intrabarBTCMove(_chartTf) =>      
    security("BINANCE:BTCUSDT", res,  f_intrabarBTCdata(res,_chartTf))

//mean = security(syminfo.tickerid, "1", sma(log(close/open), 1440))
[up, up_v, dn, dn_v, v, up_intrabars, dn_intrabars] = if timeframe.period == "M"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("M")
else if timeframe.period == "W"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("W")
else if timeframe.period == "D"
    [a,b, c, d, e,f,g] =_f_intrabarBTCMove("D")
else if timeframe.period == "1440"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("1440")
else if timeframe.period == "720"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("720")
else if timeframe.period == "360"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("360")
else if timeframe.period == "240"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("240")
else if timeframe.period == "180"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("180")
else if timeframe.period == "120"
    [a,b, c, d, e,f,g] =_f_intrabarBTCMove("120")
else if timeframe.period == "60"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("60")
else if timeframe.period == "30"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("30")
else if timeframe.period == "15"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("15")
else if timeframe.period == "10"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("10")
else if timeframe.period == "5"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("5")
else if timeframe.period == "3"
    [a,b, c, d, e,f,g] = _f_intrabarBTCMove("3")
else
    [float(na), float(na), float(na), float(na), float(na), int(1), int(1)]

//up_ema = ema(up*100000 , length)
//up_v_ema = ema(up_v, length)
//dn_ema = ema(dn*100000, length)
//dn_v_ema = ema(dn_v, length)

//plot(up_ema/up_v_ema,color=color.green)
//plot(dn_ema/dn_v_ema,color=color.red)
float up_result = na
float dn_result = na
up_reliability = sqrt(float(up_intrabars))
dn_reliability = sqrt(float(dn_intrabars))
up_reliability_ema = ema(up_reliability, length)
dn_reliability_ema = ema(dn_reliability, length)
up_result := up_v>0.?up*1000/up_v*(v):up_result[1]
dn_result := dn_v>0.?dn*1000/dn_v*(v):dn_result[1]
up_eff = ema(up_result*up_reliability, length)/ up_reliability_ema
dn_eff = ema(dn_result*dn_reliability, length)/ dn_reliability_ema
volume_ema=ema(v,length)/1000

weighted_up_eff = up_eff/volume_ema
weighted_dn_eff = dn_eff/volume_ema

plot(weighted_up_eff,color=color.green, linewidth=2)
plot(weighted_dn_eff,color=color.red, linewidth=2)

range_high = highest(max(weighted_dn_eff, weighted_up_eff),length2)
range_low = lowest(min(weighted_dn_eff, weighted_up_eff),length2)
relative_width = sqrt(sqrt(sqrt(range_low/range_high)))
plot(range_high /relative_width,color=color.red)

plot(range_low *relative_width,color=color.green)

volume_ema2 = ema(v, length2)
volume_times_log_weighted_up = log(weighted_up_eff)*v
volume_times_log_weighted_dn = log(weighted_dn_eff)*v
up_ema = exp(ema(volume_times_log_weighted_up,length2)/volume_ema2) //*hlc3
dn_ema = exp(ema(volume_times_log_weighted_dn,length2)/volume_ema2)

plot((up_intrabars!=0. and dn_intrabars!=0.)? up_ema:na,color=color.new(color.green,30))
plot((up_intrabars!=0. and dn_intrabars!=0.)? dn_ema:na,color=color.new(color.red,30))

cross_UP = up_ema>dn_ema and up_ema[1]<dn_ema[1]
cross_DN = up_ema<dn_ema and up_ema[1]>dn_ema[1]
plot(cross_UP ? up_ema : na, title="Dots", color=color.new(color.green ,30), style=plot.style_circles, linewidth=4, editable=false)
plot(cross_DN ? up_ema : na, title="Dots", color=color.new(color.red ,30), style=plot.style_circles, linewidth=4, editable=false)


//plot(ema(ema((up*100000/up_v*volume-dn*100000/dn_v*volume), length)*(up_v-dn_v)/volume_ema,length),color=color.orange, linewidth=2)

//hline(0.)
hline(show_lines?13.87:na, color=color.purple)
hline(show_lines?6.31:na, color=color.purple)
//hline(2.1)

