Ranges and Breakouts [AlgoAlpha] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
2Mljv2U.png


Author Message:
Ranges and Breakouts by AlgoAlpha is a dynamic indicator designed for traders seeking to identify market ranges and capitalize on breakout opportunities. This tool automatically detects ranges based on price action over a specified period, visualizing these ranges with shaded boxes and midlines, making it easy to spot potential breakout scenarios. The indicator includes advanced features such as customizable pivot detection, internal range allowance, and automatic trend color changes for quick market analysis.

CODE:

CSS:
#// Indicator for TOS
#// © AlgoAlpha
# indicator(title="Ranges and Breakouts [AlgoAlpha]", shorttitle="AlgoAlpha - 💥 Ranges/Breakouts", overlay=true
#Hint threshold: The confirmation length for range detection, a higher value will result in lesser ranges.
#Hint PivotDetectionLength: Detection Length for Pivots, the purely aesthetical dots at highs and lows, (they repaint with a delay of the value of this input).
#Hint AllowInternalRanges: Allows ranges to form wihtin larger ranges.

# Converted by Sam4Cok@Samer800    - 08/2024

input extendLines = no;
input colorBreakoutBars = yes;
input length = 30;     # "The main look-back of the indicator"
input threshold = 15;
input AllowInternalRanges = no; # "Allow Internal Ranges"
input showPivotPoints = yes;
input PivotDetectionLength = 7; # "Pivot Detection Length"
input movAvgType = AverageType.HULL;
input movAvgLength = 70;

def na = Double.NaN;
def last = isNaN(close);
def extend = if extendLines then yes else !last;
script Pivot {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
def range_exists1;
def lower =  Lowest(low, length);
def upper =  Highest(high, length);
def basis =  (upper + lower) / 2;

def sinceUp = if (upper != upper[1]) then 0 else sinceUp[1] + 1;
def sinceLo = if (lower != lower[1]) then 0 else sinceLo[1] + 1;

def upper_solid = if sinceUp >= threshold then 1 else 0;
def lower_solid = if sinceLo >= threshold then 1 else 0;

def ranging = upper_solid == 1 and lower_solid == 1;
def range_exists = if ranging then 1 else range_exists1[1];

def pvh = pivot(high[PivotDetectionLength], PivotDetectionLength, PivotDetectionLength, yes);
def pvl = pivot(low[PivotDetectionLength] , PivotDetectionLength, PivotDetectionLength, no);

plot pvtHi = if showPivotPoints and range_exists == 1 and !IsNaN(pvh) then high else na;
plot pvtLo = if showPivotPoints and range_exists == 1 and !IsNaN(pvl) then low  else na;
pvtHi.SetLineWeight(3);
pvtLo.SetLineWeight(3);
pvtHi.SetStyle(Curve.POINTS);
pvtLo.SetStyle(Curve.POINTS);
pvtHi.SetDefaultColor(GetColor(9));
pvtLo.SetDefaultColor(GetColor(9));

def aRngeUp;
def aRngeLo;
def midline;
def aRngeu;
def aRngel;
def cnt;
def longs;
def longs1;
def shorts;
def shorts1;

if ranging[-threshold] and !ranging[-threshold + 1] and (if AllowInternalRanges then yes else range_exists[1] == 0) {
    cnt     = 0;
    aRngeUp = upper;
    aRngeLo = lower;
    midline = basis;
    aRngeu = (basis + upper) / 2;
    aRngel = (basis + lower) / 2;
    longs  = longs1[1];
    shorts = shorts1[1];
    range_exists1 = range_exists;
} else {
    cnt = cnt[1] + 1;
    if  ((close > aRngeUp[1] or close < aRngeLo[1]) and (close[1] > aRngeUp[1] or close[1] < aRngeLo[1])) {
        aRngeUp =  na;
        aRngeLo =  na;
        midline =  na;
        aRngeu  =  na;
        aRngel  =  na;
        range_exists1 = 0;
        longs   = if close > aRngeUp[1] then 1 else longs1[1];
        shorts  = if close > aRngeUp[1] then shorts1[1] else 1;
    } else {
        aRngeUp =  if cnt > 500 then na else aRngeUp[1];
        aRngeLo =  if cnt > 500 then na else aRngeLo[1];
        midline =  if cnt > 500 then na else midline[1];
        aRngeu  =  if cnt > 500 then na else aRngeu[1];
        aRngel  =  if cnt > 500 then na else aRngel[1];
        range_exists1 = range_exists;
        longs   = longs1[1];
        shorts  = shorts1[1];
    }
}

plot hhLine = if extend and aRngeUp then aRngeUp else na;
def hlLine  = if extend and aRngeu  then aRngeu  else na;
plot mdLine = if extend and midline then midline else na;
def lhLine  = if extend and aRngel  then aRngel  else na;
plot llLine = if extend and aRngeLo then aRngeLo else na;

mdLine.SetStyle(Curve.MEDIUM_DASH);
hhLine.SetDefaultColor(Color.RED);
mdLine.SetDefaultColor(Color.GRAY);
llLine.SetDefaultColor(Color.GREEN);

AddCloud(hhLine, hlLine, Color.DARK_RED, Color.DARK_RED);
AddCloud(lhLine, llLine, Color.DARK_GREEN, Color.DARK_GREEN);

# Signals and MA
def ma = MovingAverage(movAvgType, close, movAvgLength);
def ma1 = if ma[1] then ma[1] else ma;
def crossOver  = (ma > ma1) and (ma[1] <= ma1[1]);
def crossUnder = (ma < ma1) and (ma[1] >= ma1[1]);

AddChartBubble(crossUnder and longs > 0, high, "S", Color.RED);
AddChartBubble(crossOver and shorts > 0, low, "B", Color.GREEN, no);

    longs1  = if crossUnder and longs>0 then 0 else longs;
    shorts1 = if crossOver  and shorts> 0 then 0 else shorts;

def col = if shorts1 > 0 and longs1 == 0 then -1 else
          if longs1 > 0 and shorts1 == 0 then  1 else 0;
plot maLine = if longs1 > 0 or shorts1 > 0 then ma else na;
maLine.SetLineWeight(2);
maLine.AssignValueColor(if col<0 then Color.MAGENTA else
                        if col>0 then Color.CYAN else Color.GRAY);

AssignPriceColor(if !colorBreakoutBars then Color.CURRENT else
                 if col<0 then Color.MAGENTA else
                 if col>0 then Color.CYAN else Color.CURRENT);
                
#-- 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
392 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