twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.2k stars 1.01k forks source link

Range Filter 5min indicator request #612

Open kaanguven opened 1 year ago

kaanguven commented 1 year ago

According to my experience its great indicator for scalping traders, i tried to convert it to python but my values are wrong.

//@version=4

//Original Script > @DonovanWall

// Actual Version > @guikroth

//////////////////////////////////////////////////////////////////////////
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
//////////////////////////////////////////////////////////////////////////

study(title="Range Filter  5min", overlay=true)

// Source

src = input(defval=close, title="Source")

// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters

per = input(defval=100, minval=1, title="Sampling Period")

// Range Multiplier

mult = input(defval=3.0, minval=0.1, title="Range Multiplier")

// Smooth Average Range

smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ema(abs(x - x[1]), t)
    smoothrng = ema(avrng, wper) * m
    smoothrng
smrng = smoothrng(src, per, mult)

// Range Filter

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(src, smrng)

// Filter Direction

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])

// Target Bands

hband = filt + smrng
lband = filt - smrng

// Colors

filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange
barcolor = src > filt and src > src[1] and upward > 0 ? color.lime : 
   src > filt and src < src[1] and upward > 0 ? color.green : 
   src < filt and src < src[1] and downward > 0 ? color.red : 
   src < filt and src > src[1] and downward > 0 ? color.maroon : color.orange

filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")

// Target

hbandplot = plot(hband, color=color.aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=color.fuchsia, transp=100, title="Low Target")

// Fills

fill(hbandplot, filtplot, color=color.aqua, title="High Target Range")
fill(lbandplot, filtplot, color=color.fuchsia, title="Low Target Range")

// Bar Color

barcolor(barcolor)

// Break Outs

longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src[1] and upward > 0 or 
   src > filt and src < src[1] and upward > 0
shortCond := src < filt and src < src[1] and downward > 0 or 
   src < filt and src > src[1] and downward > 0

CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1

//Alerts

plotshape(longCondition, title="Buy Signal", text="BUY", textcolor=color.white, style=shape.labelup, size=size.normal, location=location.belowbar, color=color.green, transp=0)
plotshape(shortCondition, title="Sell Signal", text="SELL", textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.red, transp=0)

alertcondition(longCondition, title="Buy Alert", message="BUY")
alertcondition(shortCondition, title="Sell Alert", message="SELL")

//For use like Strategy, 

//1. Change the word  "study" for "strategy" at the top
//2.  Remove the "//" below

//strategy.entry( id = "Long", long = true, when = longCondition )
//strategy.close( id = "Long", when = shortCondition )

Can you translate this to python or we can do this conversion with my code:

My code below :

src = dfLB["close"]
per = 100
mult = 3
def smoothrng(x, t, m) :
                wper = t * 2 - 1
                avrng = ta.ema((np.absolute(x - x.shift())), t)
                smoothrng = ta.ema(avrng, wper) * m
                return smoothrng
smrng = smoothrng(src, 100, 3)
def rngfilt(x, r):
                rngfilt = x
                rngfilt = np.where(x > rngfilt.shift(),np.where((x-r) < rngfilt.shift(),rngfilt.shift(),x-r),np.where((x+r) > rngfilt.shift(),rngfilt.shift(),x+r))
                return rngfilt
dfLB["filt"] = rngfilt(src, smrng)
dfLB["upward"] = 0.0
dfLB["upward"] = np.where((dfLB["filt"] > dfLB["filt"].shift()), dfLB["upward"].shift() + 1,np.where(dfLB["filt"] < dfLB["filt"].shift(), 0, dfLB["upward"].shift()))
dfLB["downward"] = 0.0
dfLB["downward"] = np.where((dfLB["filt"] < dfLB["filt"].shift()), dfLB["downward"].shift() + 1,np.where(dfLB["filt"] > dfLB["filt"].shift(), 0, dfLB["downward"].shift()))
hband = dfLB["filt"] + smrng
lband = dfLB["filt"] - smrng
longCond = np.where((((src > dfLB["filt"]) & (src > src.shift()) & (dfLB["upward"] > 0)) | ((src > dfLB["filt"]) & (src < src.shift()) & (dfLB["upward"] > 0))),1,0)
shortCond = np.where((((src < dfLB["filt"]) & (src < src.shift()) & (dfLB["downward"] > 0)) | ((src < dfLB["filt"]) & (src > src.shift()) & (dfLB["downward"] > 0))),1,0)

dfLB["CondIni"] = 0
dfLB["CondIni"]  = np.where((longCond == 1), 1 , np.where((shortCond==1), -1 , dfLB["CondIni"].shift()))
longCondition = np.where(((longCond==1) & (dfLB["CondIni"].shift() == -1)),1,0)
shortCondition = np.where(((shortCond==1) & (dfLB["CondIni"].shift()== 1)),1,0) 

you can check hband and lband values in tradingview ( https://tr.tradingview.com/chart/mLWdxhy9/?symbol=BITSTAMP%3AXRPUSD)

hband = blue values lband = purple values

If you can translate this code to python I would be really grateful. Thank you.

twopirllc commented 1 year ago

Hello @kaanguven,

With all the outstanding issues and currently pending PRs and indicator requests, hopefully someone can make a PR with workable code. 😎

Kind Regards KJ

abhishekmittal15 commented 1 year ago

Hi @kaanguven, From what I understand, rngfilt[i] depends on rngfilt[i-1] but in your python code the rngfilt variable doesnt take that into account when you directly use np.where. I think thats where the error arises.

I can attempt to implement it. But I need some data which includes the right values of the indicator, so that I can test my code against it. Let me know if you have any sources( the tv chart link is broken)

Thanks

tkeshfa commented 1 year ago

Hey All, Any plan to include the range filter in pandas-ta library ? or any success to make the above code working with matching result with Tradingview? Thanks!