:: Magic Osc 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bluetuga
//@version=5
indicator(":: Magic Pro C")
hline(0)
gray1 = #abbad3 , gray2 = #8993a5 , blue1 = #5f85c4 , blue2 = #52679c , blue3 = #1976d2 , red1=#ef5350, red2=#d32f2f, green1=#66bb6a, green2=#009688
histA_up = #26A69A40, histA_dn = #B2DFDB40, histB_up = #FF525240, histB_dn = #FFCDD240

// =========================================================  T R E N D 
trendLenght   = input.int(50, title='» EMA Lenght for trend defenition', group='Data', inline='1')
PriceAvg    = ta.ema(close, trendLenght)
DownTrend   = close < PriceAvg
UpTrend     = close > PriceAvg
plotshape(DownTrend, "Downtrend", color=color.new(color.red,50), style=shape.diamond, location=location.top)
plotshape(UpTrend, "Uptrend", color=color.new(color.green,50), style=shape.diamond, location=location.bottom)

// =========================================================  R S I
rsiLenght   = input.int(34, title='» RSI Lenght', group='Data', inline='1')
rsiMaLenght = input.int(34, title=' - EMA Lenght', group='Data', inline='1')
rsi         = ta.rsi(close,rsiLenght)
smooth_rsi  = rsi + (0.5* (rsi[1]-rsi))
rsi_ma      = ta.ema(rsi,rsiMaLenght)
rsi_log     = math.log(rsi/rsi_ma)

// RSI signal
rsi_up      = rsi_log > 0
rsi_dn      = rsi_log < 0

// Var Statements for histogram Color Change
var rsiA_IsUp   = false
var rsiA_IsDown = false
var rsiB_IsDown = false
var rsiB_IsUp   = false

rsiA_IsUp   := rsi_log == rsi_log[1] ? rsiA_IsUp[1]    : rsi_log > rsi_log[1] and rsi_log > 0
rsiA_IsDown := rsi_log == rsi_log[1] ? rsiA_IsDown[1]  : rsi_log < rsi_log[1] and rsi_log > 0
rsiB_IsDown := rsi_log == rsi_log[1] ? rsiB_IsDown[1]  : rsi_log < rsi_log[1] and rsi_log <= 0
rsiB_IsUp   := rsi_log == rsi_log[1] ? rsiB_IsUp[1]    : rsi_log > rsi_log[1] and rsi_log <= 0

rsi_color = rsiA_IsUp ? #26A69A40 : rsiA_IsDown ? #B2DFDB40 : rsiB_IsDown ? #FF525240 : rsiB_IsUp ? #FFCDD240 : color.silver
//rsi_color    = rsi_up ? color.new(color.green,70) : rsi_dn ? color.new(color.green,70) : color.new(color.silver,70)
plot(rsi_log, title='RSI', color = rsi_color, style=plot.style_columns)

// =========================================================  F I L T E R S
// –––––– Filter 1 (Sohrt EMA)
f1Lenght    = input.int(34, title='» Ema1 Lenght', group='Data', inline='2')
filter1     = ta.ema(close,f1Lenght)
f1_long     = close > filter1
f1_short    = close < filter1

f1_signal   = math.log(close/filter1)*2
f1_color    = f1_long ? color.new(color.green,50) : f1_short ? color.new(color.red,50) : color.new(color.orange,50)
plot(f1_signal, title='MA Filter', color=f1_color, style=plot.style_area)

// –––––– Filter 2 (Long SMA)
smaLongLenght   = input.int(128, title='» SMA Long Vision Length', group='Data', inline='3', tooltip="Testar entre 50/128/200")
smaf            = ta.sma(ohlc4,smaLongLenght) + (0.5* (ta.sma(ohlc4,smaLongLenght)[1]-ta.sma(ohlc4,smaLongLenght)))       

smaf_signal   = math.log(close/smaf)*2
smaf_long       = close > smaf
smaf_short      = close < smaf
smaf_color    = smaf_long ? color.new(color.green,30) : smaf_short ? color.new(color.red,30) : color.new(color.gray,0)
plot(smaf_signal, title='MA Long Vision', color=smaf_color, style=plot.style_circles, linewidth=2)

// –––––– Long vision signal
emaLup      = ta.crossover(smaf_signal,0)
emaLdn      = ta.crossunder(smaf_signal,0)

// =========================================================   S Q U E E Z E
// –––––– Calculate BB for SQUEEZE
sqzbasis    = ta.sma(close, 20)
sqzdev      = 1.5 * ta.stdev(close, 20)
upperBB     = sqzbasis + sqzdev
lowerBB     = sqzbasis - sqzdev

// –––––– Calculate KC
sqzma   = ta.sma(close, 20)
rangema = ta.sma(ta.tr, 20)
upperKC = sqzma + rangema * 1.2 //1.5
lowerKC = sqzma - rangema * 1.2 //1.5

sqzOn   = lowerBB > lowerKC and upperBB < upperKC
sqzOff  = lowerBB < lowerKC and upperBB > upperKC
noSqz   = sqzOn == false and sqzOff == false 

val     = ta.linreg(close - math.avg(math.avg(ta.highest(high, 20), ta.lowest(low, 20)), ta.sma(close, 20)), 20, 0)

sqzcolor= noSqz ? color.new(color.blue,50) : sqzOn ? color.new(color.orange,20) : color.new(color.gray,100)
plot(0, title='Squeeze points', color=sqzcolor, style=plot.style_circles, linewidth=2)
sqzbgcolor = noSqz ? color.new(blue2,50) : sqzOn ? color.new(blue3,95) : color.new(gray2,100)
bgcolor(sqzbgcolor, title='Squeeze BG')

// - - - - - - - -- -  OBV
_obv = ta.sma(ta.obv,1)
_obv_ma = ta.sma(_obv,20)
_obv_view = math.log(_obv_ma/_obv)*2 * -1
plot(timeframe.period == '15' ? 
     request.security(syminfo.tickerid, "60", _obv_view, gaps = barmerge.gaps_on) : 
     timeframe.period == '30' ? 
     request.security(syminfo.tickerid, "60", _obv_view, gaps = barmerge.gaps_on) : 
     timeframe.period == '45' ? 
     request.security(syminfo.tickerid, "60", _obv_view, gaps = barmerge.gaps_on) : 
     timeframe.period == '60' ? 
     request.security(syminfo.tickerid, "120", _obv_view, gaps = barmerge.gaps_on) : 
     timeframe.period == '120' ? 
     request.security(syminfo.tickerid, "120", _obv_view, gaps = barmerge.gaps_on) : 
     timeframe.period == '240' ? 
     request.security(syminfo.tickerid, "240", _obv_view, gaps = barmerge.gaps_on) : 
     na, 'OBV Line', color=color.blue, style=plot.style_line, linewidth=1)


// =========================================================   S I G N A L S 
// –––––– Conditions
longCondition  = emaLup and rsi > rsi_ma and f1_long //and sqzOff //rsi > 50 and and not sqzOff[1]
shortCondition = emaLdn and rsi < rsi_ma and f1_short //and sqzOff //rsi < 50 and and not sqzOff[1]

long = longCondition and not longCondition[1]
short = shortCondition and not shortCondition[1]

plotshape(long, style=shape.triangleup, location=location.bottom, color=color.new(green1,25), size=size.tiny, title='Signal Long')
plotshape(short, style=shape.triangledown, location=location.top,  color=color.new(red1,25), size=size.tiny, title='Signal Short')


