Twin Range Filter Visualized For ThinkOrSwim

Arun85

Member
VIP
Author states: On the previous version of Twin Range Filter found here:
https://usethinkscript.com/threads/twin-range-filter-for-thinkorswim.12831/
You can only see Long and Short signals on the chart.
But in this version of TRF, users can visually see the BUY and SELL signals on the chart with an added line of TRF.

TRF is an average of two smoothed Exponential Moving Averages, fast one has 27 bars of length and the slow one has 55 bars.

The purpose is to obtain two ranges that price fluctuates between (upper and lower range) and have LONG AND SHORT SIGNALS when close price crosses above the upper range and conversely crosses below lower range.

-BUY when price is higher or equal to the upper range level and the indicator line turns to draw the lower range to follow the price just under the bars as a trailing stop loss indicator like SuperTrend.

-SELL when price is lower or equal to the lower range levelline under the bars and then the indicator line turns to draw the upper range to follow the price just over the bars in that same trailing stop loss logic.

YfCR8fZ.png


Original Tradingview code found:
https://www.tradingview.com/script/bCCLOhFE-Twin-Range-Filter-Visualized/

Must scroll down to the next post for the new ThinkOrSwim code
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Author states: Visulaized version of colinmck's Twin Range Filter version on TradingView.
On colinmck's Twin Range Filter version, you can only see Long and Short signals on the chart.
But in this version of TRF, users can visually see the BUY and SELL signals on the chart with an added line of TRF.

@samer800 I know you have already converted the original Twin Range Filter and i would really appreciate it if you could convert this newer version as well.
https://www.tradingview.com/script/bCCLOhFE-Twin-Range-Filter-Visualized/
check the below:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#//@KivancOzbilgic
#indicator(title='Twin Range Filter', overlay=true)
# Converted by Sam4Cok@Samer800     - 11/2024


input timeframe = AggregationPeriod.MIN;
input source = FundamentalType.CLOSE; #(defval=close, title='Source')
input showSignals = yes; #(title='Show Buy/Sell Signals ?', defval=true)
input FastPeriod = 27; #, minval=1, title='Fast period')
input FastRangeMulti = 1.6; #, minval=0.1, title='Fast range')
input SlowPeriod = 55; #, minval=1, title='Slow period')
input SlowRangeMulti = 2.0; #, minval=0.1, title='Slow range')

def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def src = if !last then Fundamental(FundamentalType = source, Period = tf) else src[1];
def src1 = if !last then Fundamental(FundamentalType = source, Period = tf)[1] else src1[1];
def dif = AbsValue(src - src1);
def ohlc = ohlc4;

Script smoothrng {
input dif = close;
input t = 27;
input m = 1.6;
input x1 = close;
    def wper = t * 2 - 1;
#    def dif = AbsValue(x - x[1]);
    def avrng = ExpAverage(dif, t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot out = smoothrng;
}
Script rngfilt {
input x = close;
input r = close;
    def rngfilt;
    def PreFilt = if isNaN(rngfilt[1]) then 0 else rngfilt[1];
    rngfilt = if x > PreFilt then if x - r < PreFilt then PreFilt else x - r else
              if x + r > PreFilt then PreFilt else x + r;
    plot out = if rngfilt then rngfilt else x;
}
def smrng1 = smoothrng(dif, FastPeriod, FastRangeMulti);
def smrng2 = smoothrng(dif, SlowPeriod, SlowRangeMulti);
def smrng = (smrng1 + smrng2) / 2;
def filt = rngfilt(src, smrng);
def upward; # = 0.0
def downward;
def PreUp = if isNaN(upward[1]) then 0 else upward[1];
def PreDn = if isNaN(downward[1]) then 0 else downward[1];
    upward = if filt > filt[1] then PreUp + 1 else
             if filt < filt[1] then 0 else PreUp;
    downward = if filt < filt[1] then PreDn + 1 else
               if filt > filt[1] then 0 else PreDn;
def STR = filt + smrng;
def STS = filt - smrng;
def FUB;
def FLB;
def TRF;
def FUB1 = if isNaN(FUB[1]) then 0 else FUB[1];
def FLB1 = if isNaN(FLB[1]) then 0 else FLB[1];
def TRF1 = if isNaN(TRF[1]) then 0 else TRF[1];
    FUB = if STR < FUB1 or close(Period = tf)[1] > FUB1 then STR else FUB1;
    FLB = if STS > FLB1 or close(Period = tf)[1] < FLB1 then STS else FLB1;
    TRF = if TRF1 == FUB1 and close(Period = tf) <= FUB then FUB else
          if TRF1 == FUB1 and close(Period = tf) >= FUB then FLB else
          if TRF1 == FLB1 and close(Period = tf) >= FLB then FLB else
          if TRF1 == FLB1 and close(Period = tf) <= FLB then FUB else FUB;
def Trfff = if TRF then TRF else na;
def dir = if close(Period = tf) > Trfff then 1 else -1;

#-- plots
plot TRFup = if last then na else if dir>0 then Trfff else na;
plot TRFdn = if last then na else if dir<0 then Trfff else na;
TRFup.SetDefaultColor(Color.GREEN);
TRFdn.SetDefaultColor(Color.RED);

AddCloud(ohlc, TRFup, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(TRFdn, ohlc, Color.DARK_RED, Color.DARK_RED);

#-- Signals
plot long  = if Crosses(close, TRF, CrossingDirection.ABOVE) then Trfff else na;
plot short = if Crosses(close, TRF, CrossingDirection.BELOW) then Trfff else na;
long.SetPaintingStrategy(PaintingStrategy.POINTS);
short.SetPaintingStrategy(PaintingStrategy.POINTS);
long.SetDefaultColor(Color.GREEN);
short.SetDefaultColor(Color.RED);

AddChartBubble(showsignals and long, TRFup, "B", Color.GREEN, no);
AddChartBubble(showsignals and short, TRFdn, "S", Color.RED);

#-- END of CODE
 
check the below:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#//@KivancOzbilgic
#indicator(title='Twin Range Filter', overlay=true)
# Converted by Sam4Cok@Samer800     - 11/2024


input timeframe = AggregationPeriod.MIN;
input source = FundamentalType.CLOSE; #(defval=close, title='Source')
input showSignals = yes; #(title='Show Buy/Sell Signals ?', defval=true)
input FastPeriod = 27; #, minval=1, title='Fast period')
input FastRangeMulti = 1.6; #, minval=0.1, title='Fast range')
input SlowPeriod = 55; #, minval=1, title='Slow period')
input SlowRangeMulti = 2.0; #, minval=0.1, title='Slow range')

def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
def src = if !last then Fundamental(FundamentalType = source, Period = tf) else src[1];
def src1 = if !last then Fundamental(FundamentalType = source, Period = tf)[1] else src1[1];
def dif = AbsValue(src - src1);
def ohlc = ohlc4;

Script smoothrng {
input dif = close;
input t = 27;
input m = 1.6;
input x1 = close;
    def wper = t * 2 - 1;
#    def dif = AbsValue(x - x[1]);
    def avrng = ExpAverage(dif, t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot out = smoothrng;
}
Script rngfilt {
input x = close;
input r = close;
    def rngfilt;
    def PreFilt = if isNaN(rngfilt[1]) then 0 else rngfilt[1];
    rngfilt = if x > PreFilt then if x - r < PreFilt then PreFilt else x - r else
              if x + r > PreFilt then PreFilt else x + r;
    plot out = if rngfilt then rngfilt else x;
}
def smrng1 = smoothrng(dif, FastPeriod, FastRangeMulti);
def smrng2 = smoothrng(dif, SlowPeriod, SlowRangeMulti);
def smrng = (smrng1 + smrng2) / 2;
def filt = rngfilt(src, smrng);
def upward; # = 0.0
def downward;
def PreUp = if isNaN(upward[1]) then 0 else upward[1];
def PreDn = if isNaN(downward[1]) then 0 else downward[1];
    upward = if filt > filt[1] then PreUp + 1 else
             if filt < filt[1] then 0 else PreUp;
    downward = if filt < filt[1] then PreDn + 1 else
               if filt > filt[1] then 0 else PreDn;
def STR = filt + smrng;
def STS = filt - smrng;
def FUB;
def FLB;
def TRF;
def FUB1 = if isNaN(FUB[1]) then 0 else FUB[1];
def FLB1 = if isNaN(FLB[1]) then 0 else FLB[1];
def TRF1 = if isNaN(TRF[1]) then 0 else TRF[1];
    FUB = if STR < FUB1 or close(Period = tf)[1] > FUB1 then STR else FUB1;
    FLB = if STS > FLB1 or close(Period = tf)[1] < FLB1 then STS else FLB1;
    TRF = if TRF1 == FUB1 and close(Period = tf) <= FUB then FUB else
          if TRF1 == FUB1 and close(Period = tf) >= FUB then FLB else
          if TRF1 == FLB1 and close(Period = tf) >= FLB then FLB else
          if TRF1 == FLB1 and close(Period = tf) <= FLB then FUB else FUB;
def Trfff = if TRF then TRF else na;
def dir = if close(Period = tf) > Trfff then 1 else -1;

#-- plots
plot TRFup = if last then na else if dir>0 then Trfff else na;
plot TRFdn = if last then na else if dir<0 then Trfff else na;
TRFup.SetDefaultColor(Color.GREEN);
TRFdn.SetDefaultColor(Color.RED);

AddCloud(ohlc, TRFup, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(TRFdn, ohlc, Color.DARK_RED, Color.DARK_RED);

#-- Signals
plot long  = if Crosses(close, TRF, CrossingDirection.ABOVE) then Trfff else na;
plot short = if Crosses(close, TRF, CrossingDirection.BELOW) then Trfff else na;
long.SetPaintingStrategy(PaintingStrategy.POINTS);
short.SetPaintingStrategy(PaintingStrategy.POINTS);
long.SetDefaultColor(Color.GREEN);
short.SetDefaultColor(Color.RED);

AddChartBubble(showsignals and long, TRFup, "B", Color.GREEN, no);
AddChartBubble(showsignals and short, TRFdn, "S", Color.RED);

#-- END of CODE
U r the best! Thank u!!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
282 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top