Range filter v1

RafaelZioni · study · 71 行 · 点赞 470 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
study("Range filter v1",overlay=true)
//Source
src = close
 
//Sampling Period
per = input(defval=25, minval=1, title="Sampling Period")
 
//Range Multiplier
mult = input(defval=2, minval=0, title="Range Multiplier")
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 
//Smooth Average Range
smoothrng(x, t, m)=>
    wper      = (t/3) - 1
    avrng     = sma(abs(x - x[1]), t)
    smoothrng = sma(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 ? lime : downward > 0 ? red : orange
//barcolor  = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green : (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 
//Range Filter
filtplot = plot(filt, color=filtcolor, linewidth=4, title="Range Filter")
// Strategy
 
longCond = na
shortCond = 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 & Signals
 
plotshape(longCondition, title = "Buy Signal", text ="BUY", textcolor = green, style=shape.triangleup, location=location.belowbar, color = lime, transp = 0,offset=-1, size = size.tiny)
plotshape(shortCondition, title = "Sell Signal", text ="SELL", textcolor = maroon, style=shape.triangledown, location=location.abovebar, color = red, transp = 0,offset=-1 ,size = size.tiny)
 
 
alertcondition(longCondition, title="Buy Alert", message = "BUY")
 
alertcondition(shortCondition, title="Sell Alert", message = "SELL")

← 返回列表