Help me in solving BRACQUER problem

My issue

Help me solve this problem

My code

def longest_correct_bracket_substring(s, l, r):
    stack = []            # Indent this line using four spaces
    balance = 0
    max_length = 0

    for i in range(l, r + 1):
        if s[i] == '(':
            stack.append(i)
            balance += 1
        elif s[i] == ')':
            if balance > 0:
                stack.pop()
                balance -= 1
            else:
                stack = []

        if balance == 0:
            max_length = max(max_length, i - stack[-1] + 1)

    return max_length


Learning course: Python with Data structures
Problem Link: Practice Problem in - CodeChef