
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