-- fuente = https://www.youtube.com/playlist?
list=PLx35UDp49x7r47GlCl7OcbwS55E1PCTY_
instrument {
    name = 'RSI OVB/OVS',
    short_name = 'RSIX',
    icon = 'indicators:RSI',
    overlay = true
}
RSI_period = input(10,"RSI period",input.integer,1,1000,1)
RSI_average = input(1,"RSI average", input.string_selection,averages.titles)
RSI_title = input(1,"RSI title", input.string_selection,inputs.titles)
RSI_OVB1 = input(50,"RSI OVB1",input.double,0.01,100,1,false)
RSI_OVB2 = input(70,"RSI OVB2",input.double,0.01,100,1,false)
RSI_OVS1 = input(50,"RSI OVS1",input.double,0.01,100,1,false)
RSI_OVS2 = input(30,"RSI OVS2",input.double,0.01,100,1,false)
MaFast_period = input(5,"Ma Fast period",input.integer,1,1000,1)
MaFast_average = input(1,"Ma Fast average", input.string_selection,averages.titles)
MaFast_title = input(1,"Ma Fast title", input.string_selection,inputs.titles)
MaSlow_period = input(10,"Ma Slow period",input.integer,1,1000,1)
MaSlow_average = input(2,"Ma Slow average", input.string_selection,averages.titles)
MaSlow_title = input(1,"Ma Slow title", input.string_selection,inputs.titles)
MaTrend_period = input(20,"Ma Trend period",input.integer,1,1000,5)
MaTrend_average = input(2,"Ma Trend average",
input.string_selection,averages.titles)
MaTrend_title = input(1,"Ma Trend title", input.string_selection,inputs.titles)
input_group {
    "Ma Fast Line",
    colorFast = input { default = "#ff56e8", type = input.color },
    widthFast = input { default = 1, type = input.line_width},
    visibleFast = input { default = true, type = input.plot_visibility }
}
input_group {
    "Ma Slow Line",
    colorSlow = input { default = "#2d2af7", type = input.color },
    widthSlow = input { default = 2, type = input.line_width},
    visibleSlow = input { default = true, type = input.plot_visibility }
}
input_group {
    "Ma Trend Line",
    colorTrend = input { default = "#f74200", type = input.color },
    widthTrend = input { default = 3, type = input.line_width},
    visibleTrend = input { default = true, type = input.plot_visibility }
}
input_group {
    "Buy RSI X Out",
    colorBuy = input { default = "lime", type = input.color },
    visibleBuy = input { default = true, type = input.plot_visibility }
}
input_group {
    "Sell RSI X Out",
    colorSell = input { default = "orangered", type = input.color },
    visibleSell = input { default = true, type = input.plot_visibility }
}
input_group {
    "Buy RSI X In",
    colorBuy1 = input { default = "lime", type = input.color },
    visibleBuy1 = input { default = true, type = input.plot_visibility }
}
input_group {
    "Sell RSI X In",
    colorSell1 = input { default = "orangered", type = input.color },
    visibleSell1 = input { default = true, type = input.plot_visibility }
}
local avgFast = averages[MaFast_average]
local titleFast = inputs[MaFast_title]
local avgSlow = averages[MaSlow_average]
local titleSlow = inputs[MaSlow_title]
local avgTrend = averages[MaTrend_average]
local titleTrend = inputs[MaTrend_title]
local avgRSI = averages[RSI_average]
local titleRSI = inputs[RSI_title]
if visibleFast == true then
    plot(avgFast(titleFast,MaFast_period),"Ma Fast",colorFast,widthFast)
end
if visibleSlow == true then
    plot(avgSlow(titleSlow,MaSlow_period),"Ma Slow",colorSlow,widthSlow)
end
if visibleTrend == true then
    plot(avgTrend(titleTrend,MaTrend_period),"Ma Trend",colorTrend,widthTrend)
end
candle_time = {"1s", "5s", "10s", "15s", "30s", "1m", "2m", "5m", "10m", "15m",
"30m", "1H", "2H", "4H", "8H", "12H", "1D", "1W", "1M", "1Y"}
candle_time_res = input(6,"Candle check
resolution",input.string_selection,candle_time)
sec = security (current_ticker_id, candle_time[candle_time_res])
delta = titleRSI - titleRSI[1]
up = avgRSI(max(delta,0), RSI_period)
down = avgRSI(max(-delta,0), RSI_period)
RS = up/down
RES = 100 - 100/(1+ RS)
if (sec ~= nil) and (sec.open_time == open_time) then
    Mafast0 = avgFast(titleFast,MaFast_period) --Ma Fast bar 0
    Mafast1 = Mafast0[1]                       --Ma Fast bar 1
    MaSlow0 = avgSlow(titleSlow,MaSlow_period) --Ma Slow bar 0
    MaSlow1 = MaSlow0[1]                       --Ma Slow bar 1
    MaTrend0 = avgTrend(titleTrend,MaTrend_period) --Ma Trend 0
    Matrend1 = MaTrend0[1]                         --Ma Trend 1
    if(visibleBuy == true) then
         plot_shape(RES[1] < RSI_OVB1 and RES > RSI_OVB1 and close > Mafast0 and
Mafast0 > MaSlow0 and MaSlow0 > MaTrend0,
              "Call",
              shape_style.arrowup,
              shape_size.huge,
              colorBuy,
              shape_location.belowbar,
              0,
              "Buy",
              colorBuy
            )
     end
    if(visibleSell == true) then
        plot_shape(RES[1] > RSI_OVS1 and RES < RSI_OVS1 and close < Mafast0 and
Mafast0 < MaSlow0 and MaSlow0 < MaTrend0,
            "Put",
            shape_style.arrowdown,
            shape_size.huge,
            colorSell,
            shape_location.abovebar,
            0,
            "Sell",
            colorSell
        )
    end
    if(visibleBuy1 == true) then
         plot_shape(RES[1] < RSI_OVS2 and RES > RSI_OVS2,
              "Call1",
              shape_style.arrowup,
              shape_size.huge,
              colorBuy1,
              shape_location.belowbar,
              0,
              "Buy",
              colorBuy1
            )
     end
    if(visibleSell1 == true) then
        plot_shape(RES[1] > RSI_OVB2 and RES < RSI_OVB2 ,
            "Put1",
            shape_style.arrowdown,
            shape_size.huge,
            colorSell1,
            shape_location.abovebar,
            0,
            "Sell",
            colorSell1
        )
    end
end