Twin Range Filter For ThinkOrSwim

check it out. I added option to show the band as well.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
 
Last edited by a moderator:
check it out. I added option to show the band as well.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
wow great job like always @samer800 . two favor. is its possible,
1. price colors
2. scan.
i hope you can help me with this two request . thank you in advance
 
wow great job like always @samer800 . two favor. is its possible,
1. price colors
2. scan.
i hope you can help me with this two request . thank you in advance
pls provide more details on price color. You want to color the price based on what?
for scan, use the code below. Change the number of look back bars as you wish.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Scan
plot scan = (long or short) within 2 bars;

#---END Code
 
Hi gangs, here is a very nice system: https://www.prorealcode.com/prorealtime-indicators/twin-range-filter/

i hope one of the greatest coder we got here can help me with this. @samer800 if you got any time available i will really appreciate. thank you in advance

Code:
//PRC_Twing Range Filter | indicator
//28.03.2023
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge
//author: colinmck

// --- settings
per1 = 27 //"Fast period"
mult1 = 1.6 //"Fast range"
per2 = 55 //"Slow period"
mult2 = 2 //"Slow range"
// --- end of settings

source = customclose

once wper1 = per1 * 2 - 1
avrng1 = average[per1,1](abs(source - source[1]))
smrng1 = average[wper1,1](avrng1) * mult1
once wper2 = per2 * 2 - 1
avrng2 = average[per2,1](abs(source - source[1]))
smrng2 = average[wper2,1](avrng2) * mult2
smrng = (smrng1 + smrng2) / 2

r = smrng
if source>rngfilt[1] then
if (source-r)<rngfilt[1] then
rngfilt=rngfilt[1]
else
rngfilt=source-r
endif
elsif (source+r)>rngfilt[1] then
rngfilt=rngfilt[1]
else
rngfilt=source+r
endif

if rngfilt > rngfilt[1] then
upward=upward+1
downward=0
endif
if rngfilt < rngfilt[1] then
upward=0
downward=downward+1
endif

longCond = (source > rngfilt and source > source[1] and upward > 0) or (source > rngfilt and source < source[1] and upward > 0)
shortCond = (source < rngfilt and source < source[1] and downward > 0) or (source < rngfilt and source > source[1] and downward > 0)

if longcond then
colorr=0
colorg=255
elsif shortcond then
colorr=255
colorg=0
endif

if colorr=0 and colorr[1]=255 then
drawarrowup(barindex,min(low,rngfilt)-averagetruerange[14]/2) coloured("green")
endif
if colorr=255 and colorr[1]=0 then
drawarrowdown(barindex,max(high,rngfilt)+averagetruerange[14]/2) coloured("crimson")
endif

return rngfilt coloured(colorr,colorg,0) style(line,2)


@samer800 please check this out when you get a change! thank you in advance
check this.

https://usethinkscript.com/threads/twin-range-filter-for-thinkorswim.12831/#post-110151
 
imho @FOTM_8888 range filter/s typically results in some signal(s) as shown by the bubbles. However, if you so choose, you could set the ShowBand to yes (as added by the relentless and prolific coder @samer800!!!) but that would be a side show
 
I've added this indicator to my TOS, both as an upper overlay, and as a lower indicator. There are no "Buy" and "Sell" bubbles. Also it appears to only work when the indicator goes from 0 to 1. I'd expect that when the indicator goes from 0 to 1, that would trigger a buy and when it goes from 1 to 0, that would trigger a sell. When using the indicator in the price chart, sutup as boolean, an arrow is applied to every candle that where the value is 1. Again, I feel this should only be the first candle when the indicator changes from 0 to 1 and 1 to 0.
 
I've added this indicator to my TOS, both as an upper overlay, and as a lower indicator. There are no "Buy" and "Sell" bubbles. Also it appears to only work when the indicator goes from 0 to 1. I'd expect that when the indicator goes from 0 to 1, that would trigger a buy and when it goes from 1 to 0, that would trigger a sell. When using the indicator in the price chart, sutup as boolean, an arrow is applied to every candle that where the value is 1. Again, I feel this should only be the first candle when the indicator changes from 0 to 1 and 1 to 0.
try this if works with you

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

#script nz {
#    input data  = close;
#    input repl  = 0;
#    def ret_val = if isNaN(data) then repl else data;
#    plot return = ret_val;
#}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > rngfilt[1] then
              CompoundValue(1, if x - r < rngfilt[1] then rngfilt[1] else x - r, x)
              else if x + r > rngfilt[1] then rngfilt[1] else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def longCond; def shortCond; def CondIni;
def upward = if filt > filt[1] then upward[1] + 1 else if filt < filt[1] then 0 else upward[1];
def downward = if filt < filt[1] then downward[1] + 1 else if filt > filt[1] then 0 else downward[1];
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
 
check it out. I added option to show the band as well.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
Hello @samer800 can i ask you a favor please. can you add an arrow instead of the label? so that way i think the chart look more clean. thank you in advance.
 
Hello @samer800 can i ask you a favor please. can you add an arrow instead of the label? so that way i think the chart look more clean. thank you in advance.
find below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input signalStyle = {Default Arrows, Bubbles};
input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

def na = Double.NaN;
def arrow = signalStyle==signalStyle.Arrows;
#script nz {
#    input data  = close;
#    input repl  = 0;
#    def ret_val = if isNaN(data) then repl else data;
#    plot return = ret_val;
#}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > rngfilt[1] then
              CompoundValue(1, if x - r < rngfilt[1] then rngfilt[1] else x - r, x)
              else if x + r > rngfilt[1] then rngfilt[1] else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def longCond; def shortCond; def CondIni;
def upward = if filt > filt[1] then upward[1] + 1 else if filt < filt[1] then 0 else upward[1];
def downward = if filt < filt[1] then downward[1] + 1 else if filt > filt[1] then 0 else downward[1];
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

plot SigUp = if arrow and long then low else na;
plot SigDn = if arrow and short then high else na;
SigUp.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Up);
SigDn.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);

AddChartBubble(!arrow and long, low, "Long", Color.CYAN, no);
AddChartBubble(!arrow and short, high, "Short", Color.MAGENTA, yes);


#---END Code
 
find below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input signalStyle = {Default Arrows, Bubbles};
input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

def na = Double.NaN;
def arrow = signalStyle==signalStyle.Arrows;
#script nz {
#    input data  = close;
#    input repl  = 0;
#    def ret_val = if isNaN(data) then repl else data;
#    plot return = ret_val;
#}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > rngfilt[1] then
              CompoundValue(1, if x - r < rngfilt[1] then rngfilt[1] else x - r, x)
              else if x + r > rngfilt[1] then rngfilt[1] else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def longCond; def shortCond; def CondIni;
def upward = if filt > filt[1] then upward[1] + 1 else if filt < filt[1] then 0 else upward[1];
def downward = if filt < filt[1] then downward[1] + 1 else if filt > filt[1] then 0 else downward[1];
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

plot SigUp = if arrow and long then low else na;
plot SigDn = if arrow and short then high else na;
SigUp.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Up);
SigDn.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);

AddChartBubble(!arrow and long, low, "Long", Color.CYAN, no);
AddChartBubble(!arrow and short, high, "Short", Color.MAGENTA, yes);


#---END Code
Does it repaint
 
check it out. I added option to show the band as well.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
Thanks for this beautiful conversion. I'm new to this, so is it possible to make this a strategy and add buy/sell orders?
 
check it out. I added option to show the band as well.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r
              else if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def upward; def downward; def longCond; def shortCond; def CondIni;
upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

AddChartBubble(long, low, "Long", Color.CYAN, no);
AddChartBubble(short, high, "Short", Color.MAGENTA, yes);


#---END Code
@samer800 thank you for all your hard work, much appreciated
 
find below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © colinmck
#study(title="Twin Range Filter", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 10/2022 - request from https://usethinkscript.com/ memeber

input signalStyle = {Default Arrows, Bubbles};
input source = close;#(defval=close, title="Source")
input ShowBand = no;
#// Smooth Average Range
input FastPeriod = 27;        # "Fast period"
input FastRange = 1.6;      # "Fast range"

input SlowPeriod = 55;        # "Slow period"
input SlowRange = 2;        # "Slow range"

def na = Double.NaN;
def arrow = signalStyle==signalStyle.Arrows;
#script nz {
#    input data  = close;
#    input repl  = 0;
#    def ret_val = if isNaN(data) then repl else data;
#    plot return = ret_val;
#}
#smoothrng(x, t, m) =>
script smoothrng {
    input x = close;
    input t = 20;
    input m = 1;
    def wper = t * 2 - 1;
    def avrng = ExpAverage(AbsValue(x - x[1]), t);
    def smoothrng = ExpAverage(avrng, wper) * m;
    plot return = smoothrng;
}
def smrng1 = smoothrng(source, FastPeriod, FastRange);
def smrng2 = smoothrng(source, SlowPeriod, SlowRange);
def smrng = (smrng1 + smrng2) / 2;

#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
    input x = close;
    input r = 0;
    def rngfilt;
    rngfilt = if x > rngfilt[1] then
              CompoundValue(1, if x - r < rngfilt[1] then rngfilt[1] else x - r, x)
              else if x + r > rngfilt[1] then rngfilt[1] else x + r;
    plot return = rngfilt;
}
def filt = rngfilt(source, smrng);
#--------------------------------
def longCond; def shortCond; def CondIni;
def upward = if filt > filt[1] then upward[1] + 1 else if filt < filt[1] then 0 else upward[1];
def downward = if filt < filt[1] then downward[1] + 1 else if filt > filt[1] then 0 else downward[1];
def hband = filt + smrng;
def lband = filt - smrng;

longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0;
shortCond = source < filt and source < source[1] and downward > 0 or source < filt and source > source[1] and downward > 0;

CondIni = if longCond then 1 else if shortCond then -1 else CondIni[1];

def long = longCond and CondIni[1] == -1;
def short = shortCond and CondIni[1] == 1;

#// Plotting
plot UpperBand = hband;
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperBand.SetHiding(!ShowBand);
plot LowerBand = lband;
LowerBand.SetDefaultColor(Color.DARK_GREEN);
LowerBand.SetHiding(!ShowBand);

plot SigUp = if arrow and long then low else na;
plot SigDn = if arrow and short then high else na;
SigUp.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Up);
SigDn.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_Down);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);

AddChartBubble(!arrow and long, low, "Long", Color.CYAN, no);
AddChartBubble(!arrow and short, high, "Short", Color.MAGENTA, yes);


#---END Code
@samer800 do you think it is possible to add a candle colors at this script! example if is a long signal pain all the candles green until get a sell signal, then a sell signal paint the candles red until the signal changes to long! thank you in advance!
 
@samer800 do you think it is possible to add a candle colors at this script! example if is a long signal pain all the candles green until get a sell signal, then a sell signal paint the candles red until the signal changes to long! thank you in advance!
add the below at the end of code:

CSS:
input colorBars = yes;

def dir;
if long {
    dir = 1;
    } else if short {
    dir = -1;
    } else {
    dir = dir[1];
}
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if dir > 0 then Color.GREEN else
                 if dir < 0 then Color.RED else Color.GRAY);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
337 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