fix this python script : async def twin_range_filter(symbol, timeframe):
    if not mt5.initialize():
        print("initialize() failed, error code =", mt5.last_error())
        return None
    
    # Ambil data candlestick dari MetaTrader
    candles = mt5.copy_rates_from_pos(symbol, timeframe, 0, 100)
    # Buat DataFrame dari data candlestick
    df = pd.DataFrame(candles)
    close = df['close']

    def smoothrng(x, t, m):
        avrng = talib.EMA(np.abs(x.diff()), timeperiod=t)
        smoothrng = talib.EMA(avrng, timeperiod=t) * m
        return smoothrng.fillna(0)

    per1 = 27
    mult1 = 1.6
    per2 = 55
    mult2 = 2.0

    smrng1 = smoothrng(close, per1, mult1)
    smrng2 = smoothrng(close, per2, mult2)
    smrng = (smrng1 + smrng2) / 2

    def rngfilt(x, r):
        rngfilt = x.copy()
        rngfilt = rngfilt.ffill()
        for i in range(1, len(x)):
            prev_val = rngfilt.iloc[i-1]
            if x.iloc[i] > prev_val:
                rngfilt.iloc[i] = max(prev_val, x.iloc[i] - r.iloc[i])
            else:
                rngfilt.iloc[i] = min(prev_val, x.iloc[i] + r.iloc[i])
        return rngfilt


    filt = rngfilt(close, smrng)

    STR = filt + smrng
    STS = filt - smrng

    FUB = np.zeros(len(df))
    FLB = np.zeros(len(df))

    FUB[0] = STR.iloc[0]
    FLB[0] = STS.iloc[0]

    for i in range(1, len(df)):
        FUB[i] = np.where((STR[i] < STR[i-1]) | (close[i-1] > FUB[i-1]), STR[i], FUB[i-1])
        FLB[i] = np.where((STS[i] > STS[i-1]) | (close[i-1] < FLB[i-1]), STS[i], FLB[i-1])

    TRF = np.zeros(len(df))
    TRF[0] = FUB[0]  # Initialize TRF with the first value of FUB

    for i in range(1, len(df)):
        if (np.roll(TRF, 1)[i] == FUB[i]) and (close[i] <= FUB[i]):
            TRF[i] = FUB[i]
        elif (np.roll(TRF, 1)[i] == FUB[i]) and (close[i] >= FUB[i]):
            TRF[i] = FLB[i]
        elif (np.roll(TRF, 1)[i] == FLB[i]) and (close[i] >= FLB[i]):
            TRF[i] = FLB[i]
        elif (np.roll(TRF, 1)[i] == FLB[i]) and (close[i] <= FLB[i]):
            TRF[i] = FUB[i]
        else:
            TRF[i] = FUB[i]  # Default to FUB if none of the conditions match

    long_signal = (close > np.roll(TRF, 1))
    short_signal = (close < np.roll(TRF, 1))

    df['TRF'] = TRF
    df['long_signal'] = long_signal
    df['short_signal'] = short_signal

    # Filtering signals to print:
    if df.iloc[-1]['long_signal']:
        print("Current signal: BUY")
    elif df.iloc[-1]['short_signal']:
        print("Current signal: SELL")
    else:
        print("No clear signal"),,,,,, bcs the signal generate from this script not same with pine script from trading view indicator, this script : // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator(title='Twin Range Filter', overlay=true, timeframe='')
source = input(defval=close, title='Source')
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
per1 = input.int(defval=27, minval=1, title='Fast period')
mult1 = input.float(defval=1.6, minval=0.1, title='Fast range')
per2 = input.int(defval=55, minval=1, title='Slow period')
mult2 = input.float(defval=2, minval=0.1, title='Slow range')
smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), t)
    smoothrng = ta.ema(avrng, wper) * m
    smoothrng
smrng1 = smoothrng(source, per1, mult1)
smrng2 = smoothrng(source, per2, mult2)
smrng = (smrng1 + smrng2) / 2
rngfilt(x, r) =>
    rngfilt = x
    rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
    rngfilt
filt = rngfilt(source, smrng)
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
STR = filt + smrng
STS = filt - smrng
FUB = 0.0
FUB := STR < nz(FUB[1]) or close[1] > nz(FUB[1]) ? STR : nz(FUB[1])
FLB = 0.0
FLB := STS > nz(FLB[1]) or close[1] < nz(FLB[1]) ? STS : nz(FLB[1])
TRF = 0.0
TRF := nz(TRF[1]) == FUB[1] and close <= FUB ? FUB : nz(TRF[1]) == FUB[1] and close >= FUB ? FLB : nz(TRF[1]) == FLB[1] and close >= FLB ? FLB : nz(TRF[1]) == FLB[1] and close <= FLB ? FUB : FUB
long = ta.crossover(close, TRF)
short = ta.crossunder(close, TRF)
plotshape(showsignals and long, title='Long', text='BUY', style=shape.labelup, textcolor=color.white, size=size.tiny, location=location.belowbar, color=color.rgb(0, 19, 230))
plotshape(showsignals and short, title='Short', text='SELL', style=shape.labeldown, textcolor=color.white, size=size.tiny, location=location.abovebar, color=color.rgb(0, 19, 230))
alertcondition(long, title='Long', message='Long')
alertcondition(short, title='Short', message='Short')
Trfff = plot(TRF)
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
longFillColor = close > TRF ? color.green : na
shortFillColor = close < TRF ? color.red : na
fill(mPlot, Trfff, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, Trfff, title='DownTrend Highligter', color=shortFillColor, transp=90)