0% found this document useful (0 votes)
94 views3 pages

Range Filter Script for Trading Signals

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views3 pages

Range Filter Script for Trading Signals

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
[Link]

// Credits to the original Script - Range Filter DonovanWall


[Link]
// This version is the old version of the Range Filter with less settings to tinker
with

//@version=4
study(title="Range Filter - B&S Signals", shorttitle="RF - B&S Signals",
overlay=true)

//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
//Functions
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------

//Range Size Function


rng_size(x, qty, n)=>
// AC = Cond_EMA(abs(x - x[1]), 1, n)
wper = (n*2) - 1
avrng = ema(abs(x - x[1]), n)
AC = ema(avrng, wper)*qty
rng_size = AC

//Range Filter Function


rng_filt(x, rng_, n)=>
r = rng_
var rfilt = array.new_float(2, x)
[Link](rfilt, 1, [Link](rfilt, 0))
if x - r > [Link](rfilt, 1)
[Link](rfilt, 0, x - r)
if x + r < [Link](rfilt, 1)
[Link](rfilt, 0, x + r)
rng_filt1 = [Link](rfilt, 0)

hi_band = rng_filt1 + r
lo_band = rng_filt1 - r
rng_filt = rng_filt1
[hi_band, lo_band, rng_filt]

//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
//Inputs
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------

//Range Source
rng_src = input(defval=close, type=[Link], title="Swing Source")

//Range Period
rng_per = input(defval=20, minval=1, title="Swing Period")

//Range Size Inputs


rng_qty = input(defval=3.5, minval=0.0000001, title="Swing Multiplier")

//Bar Colors
use_barcolor = input(defval=false, type=[Link], title="Bar Colors On/Off")
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
//Definitions
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------

//Range Filter Values


[h_band, l_band, filt] = rng_filt(rng_src, rng_size(rng_src, rng_qty, rng_per),
rng_per)

//Direction Conditions
var fdir = 0.0
fdir := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir
upward = fdir==1 ? 1 : 0
downward = fdir==-1 ? 1 : 0

//Trading Condition
longCond = rng_src > filt and rng_src > rng_src[1] and upward > 0 or rng_src > filt
and rng_src < rng_src[1] and upward > 0
shortCond = rng_src < filt and rng_src < rng_src[1] and downward > 0 or rng_src <
filt and rng_src > rng_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

//Colors
filt_color = upward ? #05ff9b : downward ? #ff0583 : #cccccc
bar_color = upward and (rng_src > filt) ? (rng_src > rng_src[1] ? #05ff9b :
#00b36b) :
downward and (rng_src < filt) ? (rng_src < rng_src[1] ? #ff0583 :
#b8005d) : #cccccc

//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
//Outputs
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------

//Filter Plot
filt_plot = plot(filt, color=filt_color, transp=67, linewidth=3, title="Filter")

//Band Plots
h_band_plot = plot(h_band, color=[Link](#05ff9b, 100), title="High Band")
l_band_plot = plot(l_band, color=[Link](#ff0583, 100), title="Low Band")

//Band Fills
fill(h_band_plot, filt_plot, color=[Link](#00b36b, 92), title="High Band Fill")
fill(l_band_plot, filt_plot, color=[Link](#b8005d, 92), title="Low Band Fill")

//Bar Color
barcolor(use_barcolor ? bar_color : na)

//Plot Buy and Sell Labels


plotshape(longCondition, title = "Buy Signal", text ="BUY", textcolor =
[Link], style=[Link], size = [Link], location=[Link],
color = [Link]([Link], 0))
plotshape(shortCondition, title = "Sell Signal", text ="SELL", textcolor =
[Link], style=[Link], size = [Link], location=[Link],
color = [Link]([Link], 0))

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

Common questions

Powered by AI

The 'high band' (hi_band) and 'low band' (lo_band) components act as dynamic thresholds around the filtered price value (rng_filt). They are calculated by adding and subtracting the range value (r) from rng_filt, respectively. These bands serve as markers for potential breakout points and facilitate the identification of overbought or oversold conditions, allowing traders to make tactical decisions on entry and exit points, thus enhancing the strategy's effectiveness in capturing market opportunities .

A 'Long' signal is triggered when the range source value (rng_src) is greater than the filter value (filt) and also greater than the previous range source value, with the condition that the market direction is upward (fdir=1). Alternatively, a 'Long' condition may be satisfied if the range source is greater than filt and less than its previous value, still with upward direction, indicating a market subject to short pullbacks within an uptrend .

'Buy' and 'Sell' alerts in the script are pivotal in automating trade notifications, streamlining the trading process. By defining conditions that trigger these alerts, such as when the long or short signals are activated, traders can receive real-time updates directly from their trading platforms. This reduces the need for constant market monitoring, enhances response times to market changes, and potentially improves trading efficiency .

The Range Filter function ensures adaptability by allowing users to adjust key parameters such as the range source (rng_src), range period (rng_per), and swing multiplier (rng_qty). These inputs enable users to configure the sensitivity and responsiveness of the filter to different market dynamics, such as volatility or trend length, making it versatile for various trading strategies and market conditions .

The 'ema' function is utilized to calculate an exponentially-weighted moving average of price changes over a defined period, enhancing the sensitivity of the analysis to recent changes while smoothing out volatility over time. By giving more weight to recent price movements, the ema function helps filter out noise, providing a clearer understanding of the true market trend, and aids traders in adjusting their strategies more effectively to recent market developments .

Disabling bar colors would result in the loss of immediate and intuitive visual cues that signal market trends and conditions, such as upward and downward movements. Without color differentiation, traders would need to rely more heavily on numerical data or other visual elements, which may reduce efficiency in quickly interpreting market direction and could potentially lead to slower decision-making in fast-moving trading environments .

The Color logic determines the visual representation of market conditions by changing the color of the filter and bars based on directional movements. For instance, when the market is moving upward, colors transition to greens (#05ff9b), while downward movement shifts colors to shades of red (#ff0583). This visual feedback helps traders quickly assess market sentiment and engage in more informed decision-making processes by clearly distinguishing between market phases .

The 'CondIni' condition acts as a transitional logic checkpoint that records the previous state of the market condition, whether long or short. It adjusts its value based on the satisfaction of either the long or short conditions during the previous period. By maintaining this historical state, the script provides a clearer picture of transitioning trends, ensuring signals are not prematurely generated when the market state hasn't definitively changed direction, thus reducing false trading signals .

The Range Size Function calculates the average range (avrng) by applying an exponential moving average (EMA) to the absolute difference between the current value and its previous value, parameterized by n. This value is then scaled by a calculated weighting period (wper = (n*2) - 1). The adjusted range size (rng_size) is determined by multiplying the EMA value by a given quantity parameter (qty).

The 'filt_plot' output visually represents the filtered price on the chart with transparency and a specified line width. This creates a clear, visual reference for traders to observe how the real-time market price behaves relative to the filtered value. The transparency and color transitions reflect the market's directional tendencies, helping traders determine whether to maintain or alter their trading positions based on trend confirmation or a possible reversal .

You might also like