Range Filter Buy and Sell 5min

Forexbros22 · study · 117 行 · 点赞 1,845 · TradingView 原页

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

Pine Script

//@version=5
indicator(title="Range Filter Buy and Sell 5min", shorttitle="Range Filter", overlay=true)
 
// Original Script > @DonovanWall
// Adapted Version > @guikroth
// Updated PineScript to version 5 > @tvenn
// Optimized for Efficiency > [Your Name]
 
// Color variables
upColor   = color.white
midColor  = #90bff9
downColor = color.blue
 
// Source
src = input(defval=close, title="Source")
 
// Sampling Period Settings
// Optimized for 5min chart, BTCUSDC. For other coins or timeframes, adjust parameters or use auto-adjustment.
auto_per = input.bool(defval=true, title="Auto-adjust Sampling Period based on timeframe")
per_input = input.int(defval=100, minval=1, title="Sampling Period (if not auto)")
base_timeframe = 5  // Base timeframe in minutes (5min chart)
base_per = 100      // Base sampling period for 5min chart
per = auto_per and timeframe.isintraday ? math.max(1, math.round(base_per * (base_timeframe / timeframe.multiplier))) : per_input
// Note: If Auto-adjust is enabled, per scales relative to a 5min chart (e.g., 15min -> ~33, 1min -> 500). For non-intraday charts, uses manual input.
 
// Range Multiplier
mult = input.float(defval=3.0, minval=0.1, title="Range Multiplier")
// Tip: Adjust mult (e.g., 2.0-4.0) based on asset volatility for optimal signal accuracy.
 
// Smooth Average 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
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 ? upColor : downward > 0 ? downColor : midColor
barcolor = src > filt and src > src[1] and upward > 0 ? upColor :
   src > filt and src < src[1] and upward > 0 ? upColor : 
   src < filt and src < src[1] and downward > 0 ? downColor : 
   src < filt and src > src[1] and downward > 0 ? downColor : midColor
 
filtplot = plot(filt, color=filtcolor, linewidth=2, title="Range Filter")
 
// Target Bands
hbandplot = plot(hband, color=color.new(upColor, 70), title="High Target")
lbandplot = plot(lband, color=color.new(downColor, 70), title="Low Target")
 
// Fills
fill(hbandplot, filtplot, color=color.new(upColor, 90), title="High Target Range")
fill(lbandplot, filtplot, color=color.new(downColor, 90), 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.small, location=location.belowbar, color=color.new(#aaaaaa, 20))
plotshape(shortCondition, title="Sell Signal", text="Sell", textcolor=color.white, style=shape.labeldown, size=size.small, location=location.abovebar, color=color.new(downColor, 20))
 
alertcondition(longCondition, title="Buy alert on Range Filter", message="Buy alert on Range Filter")
alertcondition(shortCondition, title="Sell alert on Range Filter", message="Sell alert on Range Filter")
alertcondition(longCondition or shortCondition, title="Buy and Sell alert on Range Filter", message="Buy and Sell alert on Range Filter")
 
// For use as a Strategy
// ___________________________________________________________________________________________________________
// 1. Replace "indicator" with "strategy" on line 2
// 2. Uncomment the code below by highlighting it and pressing Ctrl + '/'
// Settings for 5min BTCUSDT. For other timeframes/assets, adjust parameters.
// ___________________________________________________________________________________________________________
 
// grp_STRAT   = "Strategy settings"
// timeInput   = input.time(timestamp("1 Nov 2022 00:00 +0000"), title="Start date", group=grp_STRAT)
// tpInPips    = input.int(400, title="TP (in pips)", group=grp_STRAT)
// slInPips    = input.int(100, title="SL (in pips)", group=grp_STRAT)
 
// timePeriod  = time >= timeInput
// notInTrade  = strategy.position_size <= 0
 
// if(longCondition and timePeriod and notInTrade)
//     strategy.entry("Long", strategy.long)
//     strategy.exit("Exit long", "Long", loss=slInPips, profit=tpInPips)
// if(shortCondition and timePeriod and notInTrade)
//     strategy.entry("Short", strategy.short)
//     strategy.exit("Exit short", "Short", loss=slInPips, profit=tpInPips)

← 返回列表