convert this pinescript to python script and use talib : // This source code is subject…
- User
convert this pinescript to python script and use talib : // 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)
- Assistant
- User
no need to show the plot , just print the finall signal buy or sell
- Assistant
- User
fix that script again, just print the current signal from given dataframe
- Assistant
To adjust the script to print out the current signal from the given DataFrame without needing iterative comparisons for previous values, a direct approach can be taken. This revised script will focus on the current row to determine if it's a "Buy" or "Sell" signal and simplify the implementation:
- User
why the output still No Clear Signal, in trading view with this script showing buy signal : // 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)
- Assistant
- User
implement that to this 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) df = pd.DataFrame(candles) close = df['close'] def smoothrng(x, t, m): wper = t * 2 - 1 avrng = talib.EMA(abs(x.diff()), timeperiod=t) smoothrng = talib.EMA(avrng, timeperiod=wper) * m return smoothrng 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() 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 # For signals: assuming basic crossover and crossunder logic, replacing complex Pine Script state-dependent logic df['long_signal'] = close > STR df['short_signal'] = close < STS # Filtering signals to print: last_row = df.iloc[-1] if last_row['long_signal']: print("Current signal: BUY") elif last_row['short_signal']: print("Current signal: SELL") else: print("No clear signal") - Assistant
- User
why the output still this : Current signal for XAUUSDm on timeframe 1: No clear signal
- Assistant