Search code examples
pine-scriptpine-script-v5algorithmic-trading

Find the Lowest between 2 Conditions


I want to find the highest value between the previous inside_bar and longCondition.

inside_bar = high <= high[1] and low >= low[1] 

longCondition = ta.crossover(a, b)

The logic is when longCondition happens, I want to find the previous inside_bar (which formed before longCondition) and find the highest bar value between them.

Here's a screenshot for better understanding: https://prnt.sc/qtrgEnl6lHx6


Solution

  • You can do that with a var variables. Just reset it when it is an inside bar and check its value when your condition is true.

    var highest_val = high
    
    if (inside_bar)
        highest_val := high      // Reset
    else
        if (high > highest_val)  // Update
            highest_val := high
    
    // Check the value of highest_val when longCondition is true