Divergence
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DevLucem
//  (                   (                            
//  )\ )                )\ )                         
// (()/(     (    )    (()/(   (          (     )    
//  /(_))   ))\  /((    /(_)) ))\   (    ))\   (     
// (_))_   /((_)(_))\  (_))  /((_)  )\  /((_)  )\  ' 
//  |   \ (_))  _)((_) | |  (_))(  ((_)(_))  _((_))  
//  | |) |/ -_) \ V /  | |__| || |/ _| / -_)| '  \() 
//  |___/ \___|  \_/   |____|\_,_|\__| \___||_|_|_|  
//@version=5

// @description Calculates a divergence between 2 series
library("Divergence", overlay=true)

// @function Calculates bullish divergence
// @param _src Main series
// @param _low Comparison series (`low` is used if no argument is supplied)
// @param depth Fractal Depth (`2` is used if no argument is supplied)
// @returns 2 boolean values for regular and hidden divergence
export bullish(float _src, float _low=low, int depth=2) =>
	bot_fractal = ta.lowest(_src, depth * 2 + 1) == _src[depth]
    prev_bot_fractal = ta.valuewhen(bot_fractal, _src[depth], 0)[depth]
    prev_low = ta.valuewhen(bot_fractal, _low[depth], 0)[depth]
    regular_bullish_div = bot_fractal and _low[depth] < prev_low and _src[depth] > prev_bot_fractal
    hidden_bullish_div = bot_fractal and _low[depth] > prev_low and _src[depth] < prev_bot_fractal
    [regular_bullish_div, hidden_bullish_div]

// @function Calculates bearish divergence
// @param _src Main series
// @param _high Comparison series (`high` is used if no argument is supplied)
// @param depth Fractal Depth (`2` is used if no argument is supplied)
// @returns 2 boolean values for regular and hidden divergence
export bearish(float _src, float _high=high, int depth=2) =>
    top_fractal = ta.highest(_src, depth * 2 + 1) == _src[depth]
    prev_top_fractal = ta.valuewhen(top_fractal, _src[depth], 0)[depth]
    prev_high = ta.valuewhen(top_fractal, _high[depth], 0)[depth]
    regular_bearish_div = top_fractal and _high[depth] > prev_high and _src[depth] < prev_top_fractal
    hidden_bearish_div = top_fractal and _high[depth] < prev_high and _src[depth] > prev_top_fractal
    [regular_bearish_div, hidden_bearish_div]


stoch = ta.sma(ta.stoch(close, high, low, 14), 3)

[regular_bullish_div, hidden_bullish_div] = bullish(stoch)
plotshape(regular_bullish_div or hidden_bullish_div, 'spot bottom', shape.diamond, location.belowbar, color.new(color.lime, 0), size=size.small, offset=-2)

[regular_bearish_div, hidden_bearish_div] = bearish(stoch)
plotshape(regular_bearish_div or hidden_bearish_div, 'spot top', shape.diamond, location.abovebar, color.new(color.red, 0), size=size.small, offset=-2)


//  (                   (                            
//  )\ )                )\ )                         
// (()/(     (    )    (()/(   (          (     )    
//  /(_))   ))\  /((    /(_)) ))\   (    ))\   (     
// (_))_   /((_)(_))\  (_))  /((_)  )\  /((_)  )\  ' 
//  |   \ (_))  _)((_) | |  (_))(  ((_)(_))  _((_))  
//  | |) |/ -_) \ V /  | |__| || |/ _| / -_)| '  \() 
//  |___/ \___|  \_/   |____|\_,_|\__| \___||_|_|_|  