CHOPORSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hussarya

//@version=5
indicator(title="Hussarya CHOPORSI v5.0", shorttitle="CHOPORSI v5.0 ", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

//                              CHOP ORYGINAL
length = input.int(14, title="CHOP Length",group="CHOP Settings", minval=1)
ci = 100 * math.log10(math.sum(ta.atr(1), length) / (ta.highest(length) - ta.lowest(length))) / math.log10(length)
offset = input.int(0, "Offset",  minval = -500, maxval = 500)


//                              RSI  ORYGINAL 
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//                              DMI  ORYGINAL 

lensig = input.int(14, title="ADX Smoothing",group="DMI Settings", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")
up1 = ta.change(high)
down1 = -ta.change(low)
plusDM = na(up1) ? na : (up1 > down1 and up1 > 0 ? up1 : 0)
minusDM = na(down) ? na : (down1 > up1 and down1 > 0 ? down1 : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)


//                              STOCH RSI ORYGINAL

smoothK = input.int(3, "K", group="STOCH Settings", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)


// RSI nowy tym z MA 

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "HMA" => ta.hma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
maTypeInput = input.string("HMA", title="MA Type", options=["SMA", "EMA", "HMA", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(8, title="MA Length", group="MA Settings")
rsiMA = ma(rsi, maLengthInput, maTypeInput)

buy = rsiMA*2-30
cs = (ci + k)/2
rs = (rsi + k)/2
price = ( rs  + cs)-40
sell = (minus +adx )*2 -50

top_band = input.float(105,"Top Band", minval = 0, maxval = 120, group="BANDS Settings")
bottom_band = input.float(15,"Bottom Band", minval = 0, maxval = 120, group = "BANDS Settings")
//a= plot(line_a)

poziom_rsi_buy =24
poziom_rsi_sell= 65
rsi2 = rsi -5



//                                  RYSOWANIE SYGNAŁÓW


//Lines
plot(price, "CHOP RSI Price action" , color= color.blue)

plot(sell, title="Sell power",  color=color.red)

plot(buy, "Buy Powwer", color= color.green)


//Bands
plot(top_band, "Top Band", color=color.red)
plot(bottom_band, "Bottom Band", color= color.green)


// cross
plot(ta.crossover(buy, sell) ? buy : na, "Cross up",color=color.green, style = plot.style_cross, linewidth = 3) 
plot(ta.crossunder( buy, sell ) ? buy : na,"Cross down", color=#FF0000, style = plot.style_cross, linewidth = 3) 



// Alarmy

pricetop = ta.crossover(price,top_band)
pricelow = ta.crossunder(price,bottom_band)

selltop = ta.crossover(sell,top_band)
selllow = ta.crossunder(sell,bottom_band)

perfectsell = if pricetop and selllow
    true
else
    false

perfectbuy = if selltop and pricelow
    true
else
    false


alertcondition(pricelow,"Buy ready?","Price  cross under lower band")

alertcondition(pricetop,"Sell ready?","Price  cross over top band")

alertcondition(perfectbuy,"Optimal buy condition?","Oversold condition")

alertcondition(perfectsell,"optimal sell condition?","Overbought condition")
