RsiLib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hamster-Coder

//@version=5

// @description TODO: add library description here
library("RsiLib", overlay = true)

highestIndex(float value, int length) =>
    temp = value
    result = 0
    for idx = 0 to length - 1
        if (value[idx] > temp)
            temp := value[idx]
            result := idx
    result

lowestIndex(float value, int length) =>
    temp = value
    int result = 0
    for idx = 0 to length - 1
        if (value[idx] < temp)
            temp := value[idx]
            result := idx
    result

export bullishDivergencePower(float rsi, int check_backward_length, int skip_length, float rsi_threshold, float rsi_overload_threshold) =>
    power = 0

	if (close[0] < open[0] and lowestIndex(close, check_backward_length) == 0)
		power += 1
		last_rsi = rsi[0]
		last_price = close[0]

		bool found_lower_rsi = false

		for idx = 1 to check_backward_length - 1
			if (rsi[idx] > rsi_overload_threshold)
				break

			if (rsi[idx] < rsi_threshold)
				found_lower_rsi := true

			if (close[idx] < open[idx] and rsi[idx] <= last_rsi and close[idx] >= last_price)
				if (idx >= skip_length)
					power += 1
				last_price := close[idx]
				last_rsi := rsi[idx]

		if (found_lower_rsi == false)
			power := 0

	power	

export bullishHiddenDivergencePower(float rsi, int check_backward_length, int skip_length, float rsi_threshold) =>
    power = 0

	if (close[0] < open[0] and lowestIndex(rsi, check_backward_length) == 0 and rsi[0] < rsi_threshold)
		power += 1
		last_rsi = rsi[0]
		last_price = close[0]

		for idx = 1 to check_backward_length - 1
			if (close[idx] < open[idx] and rsi[idx] >= last_rsi and close[idx] <= last_price)
				if (idx >= skip_length)
					power += 1
				last_price := close[idx]
				last_rsi := rsi[idx]

	power

export bearishDivergencePower(float rsi, int check_backward_length, int skip_length, float rsi_threshold, float rsi_overload_threshold) =>
    power = 0

	if (close[0] > open[0] and highestIndex(close, check_backward_length) == 0)
		power += 1
		last_rsi = rsi[0]
		last_price = close[0]

		bool match_rsi_threshold = false

		for idx = 1 to check_backward_length - 1
			if (rsi[idx] < rsi_overload_threshold)
				break

			if (rsi[idx] > rsi_threshold)
				match_rsi_threshold := true

			if (close[idx] > open[idx] and rsi[idx] >= last_rsi and close[idx] <= last_price)
				if (idx >= skip_length)
					power += 1
				last_price := close[idx]
				last_rsi := rsi[idx]

		if (match_rsi_threshold == false)
			power := 0

	power	

export bearishHiddenDivergencePower(float rsi, int check_backward_length, int skip_length, float rsi_threshold) =>
    power = 0

	if (close[0] > open[0] and highestIndex(rsi, check_backward_length) == 0 and rsi[0] > rsi_threshold)
		power += 1
		last_rsi = rsi[0]
		last_price = close[0]

		for idx = 1 to check_backward_length - 1
			if (close[idx] > open[idx] and rsi[idx] <= last_rsi and close[idx] >= last_price)
				if (idx >= skip_length)
					power += 1
				last_price := close[idx]
				last_rsi := rsi[idx]

	power