top of page

(PineScript) Range Filter

설명

"Larry Williams"의 변동성돌파 전략 Based Idea입니다.

평균 변동성(디폴트)으로 밴드를 구축하고, 그 밴드를 벗어난 방향으로 중앙선의 색이 전환됩니다.

사용방법

선의 색에 따라 추세우위를 판단합니다.

변동성이 기준이 되었기에 밴드와 중앙선은 지지 및 저항의 역할이 가능해 박스권 매매에 활용할 수도 있습니다.

Source Code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © RiskReturn


//@version=4

study(title="RANGE FILTER", overlay=true)


Cond_EMA(x, cond, n)=>

var val = array.new_float(0)

var ema_val = array.new_float(1)

if cond

array.push(val, x)

if array.size(val) > 1

array.remove(val, 0)

if na(array.get(ema_val, 0))

array.fill(ema_val, array.get(val, 0))

array.set(ema_val, 0, (array.get(val, 0) - array.get(ema_val, 0))*(2/(n + 1)) + array.get(ema_val, 0))

EMA = array.get(ema_val, 0)

EMA


Cond_SMA(x, cond, n)=>

var vals = array.new_float(0)

if cond

array.push(vals, x)

if array.size(vals) > n

array.remove(vals, 0)

SMA = array.avg(vals)

SMA


Stdev(x, n)=>

sqrt(Cond_SMA(pow(x, 2), 1, n) - pow(Cond_SMA(x, 1, n), 2))


//Range Size Function

rng_size(x, scale, qty, n)=>

ATR = Cond_EMA(tr(true), 1, n)

AC = Cond_EMA(abs(x - x[1]), 1, n)

SD = Stdev(x, n)

rng_size = scale=="Pips" ? qty*0.0001 : scale=="Points" ? qty*syminfo.pointvalue : scale=="% of Price" ? close*qty/100 : scale=="ATR" ? qty*ATR :

scale=="Average Change" ? qty*AC : scale=="Standard Deviation" ? qty*SD : scale=="Ticks" ? qty*syminfo.mintick : qty


rng_filt(h, l, rng_, n, type, smooth, sn, av_rf, av_n)=>

rng_smooth = Cond_EMA(rng_, 1, sn)

r = smooth ? rng_smooth : rng_

var rfilt = array.new_float(2, (h + l)/2)

array.set(rfilt, 1, array.get(rfilt, 0))

if type=="Type 1"

if h - r > array.get(rfilt, 1)

array.set(rfilt, 0, h - r)

if l + r < array.get(rfilt, 1)

array.set(rfilt, 0, l + r)

if type=="Type 2"

if h >= array.get(rfilt, 1) + r

array.set(rfilt, 0, array.get(rfilt, 1) + floor(abs(h - array.get(rfilt, 1))/r)*r)

if l <= array.get(rfilt, 1) - r

array.set(rfilt, 0, array.get(rfilt, 1) - floor(abs(l - array.get(rfilt, 1))/r)*r)

rng_filt1 = array.get(rfilt, 0)

hi_band1 = rng_filt1 + r

lo_band1 = rng_filt1 - r

rng_filt2 = Cond_EMA(rng_filt1, rng_filt1 != rng_filt1[1], av_n)

hi_band2 = Cond_EMA(hi_band1, rng_filt1 != rng_filt1[1], av_n)

lo_band2 = Cond_EMA(lo_band1, rng_filt1 != rng_filt1[1], av_n)

rng_filt = av_rf ? rng_filt2 : rng_filt1

hi_band = av_rf ? hi_band2 : hi_band1

lo_band = av_rf ? lo_band2 : lo_band1

[hi_band, lo_band, rng_filt]


f_type = input(defval="Type 1", options=["Type 1", "Type 2"], title="Filter Type")


mov_src = input(defval="Close", options=["Wicks", "Close"], title="Movement Source")


rng_qty = input(defval=2.618, minval=0.0000001, title="Range Size")

rng_scale = input(defval="Average Change", options=["Points", "Pips", "Ticks", "% of Price", "ATR", "Average Change", "Standard Deviation", "Absolute"], title="Range Scale")


rng_per = input(defval=14, minval=1, title="Range Period (for ATR, Average Change, and Standard Deviation)")


smooth_range = input(defval=false, title="Smooth Range")

smooth_per = input(defval=27, minval=1, title="Smoothing Period")


av_vals = input(defval=false, title="Average Filter Changes")

av_samples = input(defval=2, minval=1, title="Number Of Changes To Average")


h_val = mov_src=="Wicks" ? high : close

l_val = mov_src=="Wicks" ? low : close


[h_band, l_band, filt] = rng_filt(h_val, l_val, rng_size((h_val + l_val)/2, rng_scale, rng_qty, rng_per), rng_per, f_type, smooth_range, smooth_per, av_vals, av_samples)


var fdir = 0.0

fdir := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir

upward = fdir==1 ? 1 : 0

downward = fdir==-1 ? 1 : 0


filt_color = upward ? #05ff9b : downward ? #ff0583 : #cccccc

bar_color = upward and (close > filt) ? (close > close[1] ? #05ff9b : #00b36b) :

downward and (close < filt) ? (close < close[1] ? #ff0583 : #b8005d) : #cccccc


filt_plot = plot(filt, color=filt_color, linewidth=3, transp=0, title="Filter")


h_band_plot = plot(h_band, color=#05ff9b, transp=100, title="High Band")

l_band_plot = plot(l_band, color=#ff0583, transp=100, title="Low Band")


fill(h_band_plot, filt_plot, color=#00b36b, transp=85, title="High Band Fill")

fill(l_band_plot, filt_plot, color=#b8005d, transp=85, title="Low Band Fill")


barcolor(bar_color)


plot(fdir, transp=100, editable=false, display=display.none, title="External Output - Trend Signal")

최근 게시물

전체 보기

Comments


Since 2020

rrlogo.jpg
Copyright by Risk&amp;Return © All Rights Reserved
bottom of page