Range Detector [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
UonMxgF.png


Author Message:
The Range Detector indicator aims to detect and highlight intervals where prices are ranging. The extremities of the ranges are highlighted in real-time, with breakouts being indicated by the color changes of the extremities.

More Details : https://www.tradingview.com/v/QOuZIuvH/

CODE:

CSS:
#https://www.tradingview.com/v/QOuZIuvH/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International
#// © LuxAlgo
#indicator("Range Detector [LuxAlgo]", "LuxAlgo - Range Detector", overlay = true, m
# Converted by Sam4Cok@Samer800    - 10/2023 - Not Typical

#//Settings
input highlightRangeStart = yes;
input RangeDetectMethod = {Default "Simple Average", "Median Average"};
input source = close;
input MinimumRangeLength = 20;
input RangeWidth   = 1.0;         # 'Range Width'
input atrLength = 500;            # 'ATR Length'

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def len = MinimumRangeLength;
def last = isNaN(close);
def sma = RangeDetectMethod==RangeDetectMethod."Simple Average";

def sMTF = source;
def hMTF = high;
def lMTF = low;
def cMTF = close;

#-- Colors
DefineGlobalColor("up", CreateColor(0,165,255));
DefineGlobalColor("dn", CreateColor(255,0,148));
DefineGlobalColor("dup", CreateColor(0, 89, 137));
DefineGlobalColor("ddn", CreateColor(137, 0, 80));
DefineGlobalColor("No", CreateColor(254,165,0));
DefineGlobalColor("dNo", CreateColor(136,89,0));
#//Detect and highlight ranges
#//Ranges drawings
def bxTop;# = na
def bxBot;
def lvl;# = na
def max;# = na
def min;# = na
def detect_css;
def os;# = 0
def os1;
def bxRight;

def n   = BarNumber();
def tr  = TrueRange(hMTF, cMTF, lMTF);
def nATR = WildersAverage(tr, atrLength) * RangeWidth;
def ma1 = Average(sMTF, len);
def ma2  = Median(sMTF, len);
def MovAvg = if sma then ma1 else ma2;
def ma = MovAvg;

def count = fold i = 0 to len with p do
            p + (if AbsValue(sMTF[i] - ma) > nATR then 1 else 0);

if count == 0 and count != count[1] {
if (n - len) <= bxRight[1] {
    max = max(ma + nATR, max[1]);
    min = min(ma - nATR, if(min[1], min[1], ma));
    lvl = (max + min) / 2;
    bxRight = bxRight[1];
    bxTop = max;
    bxBot = min;
    os = 0;
    detect_css = na;
    } else {
    max = ma + nATR;
    min = ma - nATR;
    lvl = ma;
    bxRight = n;
    bxTop = max;
    bxBot = min;
    detect_css = 1;
    os = 0;
    }
} else {
    max = max[1];
    min = min[1];
    lvl = lvl[1];
    bxRight = if count == 0 then n else bxRight[1];
    bxTop = max;
    bxBot = min;
    os = os1[1];
    detect_css = na;
}
#//Set color
if sMTF > bxTop {
    os1 = 1;
    } else if sMTF < bxBot {
    os1 = -1;
    } else {
    os1 = os;
}
def Dir = os1[-len];
def Dir1 = Dir[-len];
def Rng = !isNaN(detect_css);
def RngStart = Rng and !Rng[1];

def bardetect = if RngStart then n+1  else bardetect[1];
def barEnd = bardetect + len;
def condEnd = n < barEnd;

plot UpLine = if !last and bxTop then bxTop else na;
plot LoLine = if !last and bxBot then bxBot else na;

UpLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LoLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

UpLine.AssignValueColor(if os1 > 0 then GlobalColor("up") else
                        if os1 < 0 then GlobalColor("dn") else GlobalColor("No"));
LoLine.AssignValueColor(if os1 > 0 then GlobalColor("up") else
                        if os1 < 0 then GlobalColor("dn") else GlobalColor("No"));

plot avg = if !last and lvl then lvl else na;
Avg.SetStyle(Curve.SHORT_DASH);
avg.SeTPaintingStrategy(PaintingStrategy.HORIZONTAL);
avg.AssignValueColor(if os1 > 0 then GlobalColor("dup") else
                     if os1 < 0 then GlobalColor("ddn") else GlobalColor("dNo"));
plot PreAvg = if !last and lvl[-len] then lvl[-len] else na;
PreAvg.SetStyle(Curve.POINTS);
PreAvg.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PreAvg.AssignValueColor(if Dir1 > 0 then GlobalColor("dup") else
                        if Dir1 < 0 then GlobalColor("ddn") else GlobalColor("dNo"));

def PreHi = if last[len] then na else bxTop;
def PreLo = if last[len] then na else bxBot;

AddCloud(if Dir1>0 then PreHi[-len] else na,
         if condEnd[-len] then PreLo[-len] else na, GlobalColor("dup"));
AddCloud(if Dir1<0 then PreHi[-len] else na,
         if condEnd[-len] then PreLo[-len] else na, GlobalColor("ddn"));
AddCloud(if Dir1== 0 then PreHi[-len] else na,
         if condEnd[-len] then PreLo[-len] else na, GlobalColor("dNo"));

def bgCond = if !highlightRangeStart then 0 else
             if RngStart then 1 else
             if RngStart[-1] then 1 else 0;

AddCloud(if bgCond then pos else na, neg, Color.GRAY);

#-- END of CODE
 

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
399 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