// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © jeromehorus

//@version=6

indicator("Alladin" ,overlay=true)

range= input.int(33,"peaks")

clos = ta.sma(close,13)

float dm =0

float dd =0

for i = 0 to range

m= clos-clos[1]>0?true:false

diff= math.abs(clos-clos[1])

dm := m? dm+diff:dm

dd := m==false? dd+diff:dd

low = ta.lowest(close,13)

high = ta.highest(close,13)

rsip = input.int(27,"rsi range")

rsivb = input.int(35,"oversold value")

rsivh = input.int(70,"overbought value")

rsiv = ta.rsi(close,rsip)

eml = input.int(1,"length ema")

ema200 = ta.ema(close,eml)

ema666 = ta.ema(close,666)

m6 = ema666 - ema666[1] >0? true:false

plot( ema200, color= m6? color.green:color.red)

m2 = ema200 - ema200[1] >0? true:false

base = ta.lowest(close,33)

high = ta.highest(close,33)

plot( close, color= close> high[13] ? color.green: close< base[13]? color.red: color.yellow,linewidth=5)

of= input.float(10.0, "Minimal rebound in $ to validate the high signal")// peaks are more violent in volatility

ofb= input.float(10.0, "Minimal rebound in $ to validate the low signal")// peaks are more violent in volatility

// Memory of the last low or high detected

// Memory

var float lowDetected = na

var bool waitingForRebound = false

var bool signalDisplayed = false

// Detection of the low

if (close == low and dd > dm and rsiv < rsivb and not waitingForRebound)

lowDetected := close

waitingForRebound := true

signalDisplayed := false

// If waiting and rebound of $200

if (waitingForRebound and not signalDisplayed and close >= lowDetected + of)

//label.new(bar_index, low, "BUY confirmed", style=label.style_label_up, color=color.green, textcolor=color.white)

signalDisplayed := true

waitingForRebound := false

if (close[1]==low[1] and dd[1]>dm[1] and rsiv[1]< rsivb[1] and close[1]+ofb< close)// and m2==false)

label.new(bar_index, low, "b", style=label.style_label_up, color=close>high[13]?color.green:color.rgb(0,122,0), textcolor=color.white)

//alert("The price is low, possibility of purchase? ", alert.freq_once)

alert("LOW SIGNAL",freq=alert.freq_once_per_bar_close)

// allows to eliminate some false signals

// not all because if it hits multiple times or trends upwards, we don't sell (needs advanced algo, I will do it in c)

if (close[1]==high[1] and dm[1]>dd[1] and rsiv[1] > rsivh[1] and close[1]-of > close)// and m2)

label.new(bar_index, high, "h", style=label.style_label_down, color=close<base[13]?color.red:color.rgb(255,55,55), textcolor=color.white)

//alert("The price is high, possibility of sale? ", alert.freq_once)

alert("HIGH SIGNAL",freq=alert.freq_once_per_bar_close)

///////////////// closing signals

rsivd = ta.rsi(close,17)

if (close[1]==low[1] and dd[1]>dm[1] and rsivd[1]< rsivb[1] and close[1]+ofb< close)// and m2==false)

label.new(bar_index, low, "☠", style=label.style_label_up, color=close>high[13]?color.green:color.rgb(0,122,0), textcolor=color.white)

alert("close the short?",freq=alert.freq_once_per_bar_close)

if (close[1]==high[1] and dm[1]>dd[1] and rsivd[1] > rsivh[1] and close[1]-of > close)// and m2)

label.new(bar_index, high, "💀", style=label.style_label_down, color=close<base[13]?color.red:color.rgb(255,55,55), textcolor=color.white)

alert("close the long",freq=alert.freq_once_per_bar_close)

plot(close)