Consecutive Higher/Lower Closings
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rahul_joshi_2
//@version=5
indicator("Custom Buy/Sell Signal", overlay=true)

// Input options
consecutiveHigherValues = input(3, title="Consecutive Higher Values for Buy Signal")
consecutiveLowerValues = input(3, title="Consecutive Lower Values for Sell Signal")
useOpen = input(true, title="Use Open")
useHigh = input(false, title="Use High")
useLow = input(false, title="Use Low")
useClose = input(false, title="Use Close")

// Get the selected value type
value = close
if useOpen
    value := open
else if useHigh
    value := high
else if useLow
    value := low

// Calculate the difference in selected values
valueDiff = value - value[1]

// Function to check for consecutive higher values
consecutiveHigherValuesCheck(count) =>
    higherCount = 0
    for i = 0 to count - 1
        higherCount := valueDiff[i] > 0 ? higherCount + 1 : higherCount
    higherCount >= count

// Function to check for consecutive lower values
consecutiveLowerValuesCheck(count) =>
    lowerCount = 0
    for i = 0 to count - 1
        lowerCount := valueDiff[i] < 0 ? lowerCount + 1 : lowerCount
    lowerCount >= count

// Generate buy and sell signals
buySignal = consecutiveHigherValuesCheck(consecutiveHigherValues)
sellSignal = consecutiveLowerValuesCheck(consecutiveLowerValues)

// Plot buy and sell signals on the chart
plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.triangleup, title="Buy Signal")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.triangledown, title="Sell Signal")
