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

//@version=5

// @description TODO: add library description here
library("Library_RICH")

// @function TODO: add function description here
// @param x TODO: add parameter x description here
// @returns TODO: add what function returns
export sum(float x, float y) =>
    a = x + y
    
// @function: ckeck if there are buy reversal conditions (divergences) in rsi
//            insure that the bar count since the last pivot low is within the specified range (min- and max range)
//            ckeck if there is a higher-low in rsi and lower-low in the price
//            (doubel) ckeck if buy reversal condition is true 
// @param   : rsi value, pivot left lenght, pivot right lenght, min. and max. range
// @returns : two values: bar-reversal-condition (true) and pivot low if is true
export checkBuyReversal(float _rsiValue, int _pivotLeft, int _pivotRight, int _minRange, int _maxRange) =>
    bool pivotLowTrue = na(ta.pivotlow(_rsiValue, _pivotLeft, _pivotRight)) ? false : true 
    int barsCount = ta.barssince(pivotLowTrue[1] == true)
    bool inRange = _minRange <= barsCount and barsCount <= _maxRange
    bool RSI_HL_check = _rsiValue[_pivotRight] > ta.valuewhen(pivotLowTrue, _rsiValue[_pivotRight], 1) and inRange
    bool price_LL_check = close[_pivotRight] < ta.valuewhen(pivotLowTrue, close[_pivotRight], 1)
    bool buyReversalCondition = price_LL_check and RSI_HL_check and pivotLowTrue //buy condition is true
    [buyReversalCondition, pivotLowTrue]
//[buyCondition, pivot_low_true] = checkBuyReversal(rsi_value, pivot_left, pivot_right)  <- how to call the function with multiple results

// @function: ckeck if there are buy cintinuation conditions (hidden divergences) in rsi
//            insure that the bar count since the last pivot low is within the specified range (min- and max range)
//            ckeck if there is a lower-low in rsi and higher-low in the price
//            (doubel) ckeck if buy continuation condition is true 
// @param   : rsi value, pivot left lenght, pivot right lenght, min. and max. range
// @returns : two results: bar-reversal-condition (true) and pivot low, if is true
export checkBuyContinuation(float _rsiValue, int _pivotLeft, int _pivotRight, int _minRange, int _maxRange) =>
    bool pivotLowTrue = na(ta.pivotlow(_rsiValue, _pivotLeft, _pivotRight)) ? false : true 
    int barsCount = ta.barssince(pivotLowTrue[1] == true)
    bool inRange = _minRange <= barsCount and barsCount <= _maxRange
    bool RSI_LL_check = _rsiValue[_pivotRight] < ta.valuewhen(pivotLowTrue, _rsiValue[_pivotRight], 1) and inRange
    bool price_HL_check = close[_pivotRight] > ta.valuewhen(pivotLowTrue, close[_pivotRight], 1)
    bool buyContinuationCondition = price_HL_check and RSI_LL_check and pivotLowTrue //buy condition is true
    [buyContinuationCondition, pivotLowTrue]
    
    
// @function: ckeck if there are sell reversal conditions (divergences) in rsi
//            insure that the bar count since the last pivot high is within the specified range (min- and max range)
//            ckeck if there is a lower-high in rsi and higher-high in the price
//            (doubel) ckeck if sell reversal condition is true 
// @param   : rsi value, pivot left lenght, pivot right lenght, min. and max. range
// @returns : two results: sell-reversal-condition (true) and pivot high, if is true
export checkSellReversal(float _rsiValue, int _pivotLeft, int _pivotRight, int _minRange, int _maxRange) =>
    bool pivotHighTrue = na(ta.pivothigh(_rsiValue, _pivotLeft, _pivotRight)) ? false : true 
    int barsCount = ta.barssince(pivotHighTrue[1] == true)
    bool inRange = _minRange <= barsCount and barsCount <= _maxRange
    bool RSI_LH_check = _rsiValue[_pivotRight] < ta.valuewhen(pivotHighTrue, _rsiValue[_pivotRight], 1) and inRange
    bool price_HH_check = close[_pivotRight] > ta.valuewhen(pivotHighTrue, close[_pivotRight], 1)
    bool sellReversalCondition = price_HH_check and RSI_LH_check and pivotHighTrue //buy condition is true
    [sellReversalCondition, pivotHighTrue]
    
// @function: ckeck if there are sell cointunuation conditions (divergences) in rsi
//            insure that the bar count since the last pivot high is within the specified range (min- and max range)
//            ckeck if there is a higher-high in rsi and lower-high in the price
//            (doubel) ckeck if sell continuation condition is true 
// @param   : rsi value, pivot left lenght, pivot right lenght, min. and max. range
// @returns : two results: sell-continuation-condition (true) and pivot high, if is true
export checkSellContinuation(float _rsiValue, int _pivotLeft, int _pivotRight, int _minRange, int _maxRange) =>
    bool pivotHighTrue = na(ta.pivothigh(_rsiValue, _pivotLeft, _pivotRight)) ? false : true 
    int barsCount = ta.barssince(pivotHighTrue[1] == true)
    bool inRange = _minRange <= barsCount and barsCount <= _maxRange
    bool RSI_HH_check = _rsiValue[_pivotRight] > ta.valuewhen(pivotHighTrue, _rsiValue[_pivotRight], 1) and inRange
    bool price_LH_check = close[_pivotRight] < ta.valuewhen(pivotHighTrue, close[_pivotRight], 1)
    bool sellContinuationCondition = price_LH_check and RSI_HH_check and pivotHighTrue //buy condition is true
    [sellContinuationCondition, pivotHighTrue]
    
    
    
    
    
    
    
    
    
    
    
    
    