Iván González

Trend Lines with an Angle Filter

Category: Indicators By: Iván González Created: November 30, 2026, 11:19 AM
November 30, 2026, 11:19 AM
Indicators
0 Comments
Trend Lines with an Angle Filter

Introduction

 

Drawing trendlines by hand has two problems: everyone draws them a little differently, and nobody redraws them as fast as the market moves. Trend Lines by Kris draws them for you, with a fixed set of rules, so the same chart always gives the same lines.

It looks for two kinds of lines. Supply lines (red) join falling swing highs, the ceiling of a move down or of a pullback. Demand lines (green) join rising swing lows, the floor of a move up. Each line runs forward bar by bar until a candle body closes through it, and there it stops. Every line carries a label with its angle, so you can tell at a glance a gentle slope from a steep one.

Theory Behind the Indicator

 

Pivots. A swing high is a high with prd lower highs on each side (a swing low is the mirror). A pivot is only known prd bars after it happens, so a line appears prd bars after its second pivot. Once drawn, its start never moves.

Building a supply line. On each new swing high, the indicator compares it with the previous one:

  • the new high must be lower than the previous one, and no more than prd x 7 bars away from it;
  • either the previous supply line is already broken, or the previous high was itself lower than the one before it (a chain of lower highs);
  • no close, from the first pivot up to the current bar, may sit above the line. Wicks may poke through; closes may not;
  • the angle of the line must be 30 degrees or less.

Demand lines follow the same rules with rising swing lows and no close below the line.

The angle. A raw slope in price per bar means nothing from one instrument to another. Here the slope is measured on a virtual box 500 bars wide and 166.7 units high, where the height stands for the price range of the last 500 bars. The result is an angle between 0 and 90 degrees that reads the same on a stock, an index or a currency pair. Lines steeper than 30 degrees are rejected: they are usually two pivots of a sharp move, not a trend.

The break. The live line is extended on every bar. It is broken when a candle body crosses it: the higher of open and close above a supply line, the lower of the two below a demand line. The line then ends on that candle. The last three lines of each side stay on the chart.

Key Features at a Glance

 

  • Automatic supply (falling highs) and demand (rising lows) trendlines from swing pivots.
  • Validation by closes: no close beyond the line since its first pivot.
  • Normalised angle filter (30 degrees or less), with the angle printed on each line.
  • Lines that run bar by bar and stop on the candle body that breaks them.
  • The last three lines of each side kept on the chart.
  • Supply and demand sides can be switched on or off separately.

How to Read the Indicator

 

  • Red line still reaching the last bar: an active supply line. Price has respected it on every close so far.
  • Green line still reaching the last bar: an active demand line.
  • A line that ends before the last bar: it was broken by the candle body on its last bar, or it was replaced by a newer line of the same side.
  • The angle label: low values (under 10 degrees) are shallow, orderly trends; values close to 30 are steep and break more easily.

Practical Applications

 

  • Breakout entries: a candle body closing above a supply line is the classic signal that a correction or a downtrend has run out of sellers.
  • Trend support: in an uptrend, the active demand line is a reference for pullbacks and for placing a stop below it.
  • Reading structure: a series of supply lines that keep breaking and being replaced by flatter ones tells you selling pressure is fading.
  • Sensitivity: a smaller prd gives more, shorter lines for intraday; a larger one keeps only the major swings.

Indicator Configuration

 

  • prd (default 20): bars on each side of a swing high or low (5 to 50). Higher values give fewer but more significant lines.
  • showSupply (default 1): draw the supply lines (1 on, 0 off).
  • showDemand (default 1): draw the demand lines (1 on, 0 off).
  • lineWidth (default 1): line thickness, 1 to 4.
  • maxAngle (default 30): steepest angle accepted for a new line, in degrees.
  • keepLines (default 3): lines kept on the chart for each side.
  • Colours: supply (red) and demand (green), as RGB values.

The Code

 

//---------------------------------------------------------------
// PRC_Trend Lines by Kris
// version = 0
// 30.11.2026
// Iván González @ www.prorealcode.com
// Author: KRISTRADER1
// Sharing ProRealTime knowledge
//--------------------------------------------------------------------//
defparam drawonlastbaronly = true
//--------------------------------------------------------------------//
// Inputs
//--------------------------------------------------------------------//
prd = 20            // Sensitivity: bars on each side of a swing pivot (5..50)
showSupply = 1      // 1 = draw supply (falling) lines
showDemand = 1      // 1 = draw demand (rising) lines
lineWidth = 1       // line width (1..4)
maxAngle = 30       // maximum normalised angle accepted for a new line
keepLines = 3       // lines kept on the chart per side
// Supply colour (red) and demand colour (green)
supR = 242
supG = 54
supB = 69
demR = 76
demG = 175
demB = 80
//--------------------------------------------------------------------//
// Angle normalisation: slope measured on a virtual 500 x 166 bar box
//--------------------------------------------------------------------//
ONCE normBars = 500
ONCE normHeight = 500 / 3
ONCE maxDist = prd * 7
xAxis = min(max(1, barindex), normBars)
yAxis = highest[xAxis](high) - lowest[xAxis](low)
// Prices stored per bar: a variable offset such as close[barindex - b]
// inside a long loop is not reliable, an array read by bar number is
$cA[barindex] = close
$oA[barindex] = open
//--------------------------------------------------------------------//
// Pivots (strict with the bars after, >= with the bars before)
//--------------------------------------------------------------------//
newPH = 0
newPL = 0
hhR = highest[prd](high)
llR = lowest[prd](low)
IF barindex >= 2 * prd THEN
   IF high[prd] > hhR AND high[prd] >= hhR[prd + 1] THEN
      newPH = 1
   ENDIF
   IF low[prd] < llR AND low[prd] <= llR[prd + 1] THEN
      newPL = 1
   ENDIF
ENDIF
//--------------------------------------------------------------------//
// Supply logic: two falling pivot highs with no close above the line
//--------------------------------------------------------------------//
ONCE supCnt = 0
ONCE supBroken = 1
ONCE supActive = 0
ONCE nS = 0
IF showSupply AND newPH THEN
   sp2 = sp1
   sx2 = sx1
   sp1 = sp0
   sx1 = sx0
   sp0 = high[prd]
   sx0 = barindex - prd
   supCnt = min(supCnt + 1, 10)
   IF supCnt > 1 THEN
      IF sp0 < sp1 AND (sx0 - sx1) <= maxDist THEN
         valid = supBroken
         IF NOT valid AND supCnt > 2 THEN
            valid = sp1 < sp2
         ENDIF
         IF valid THEN
            ok = 1
            slope = (sp0 - sp1) / (sx0 - sx1)
            FOR b = sx1 + 1 TO barindex DO
               IF $cA[b] > sp1 + slope * (b - sx1) THEN
                  ok = 0
                  BREAK
               ENDIF
            NEXT
            IF ok THEN
               ang = 0
               IF yAxis <> 0 THEN
                  ang = abs(round(atan(normHeight * (sp0 - sp1) / (yAxis * (sx0 - sx1))) * 100) / 100)
               ENDIF
               IF ang <= maxAngle THEN
                  sLX1 = sx1
                  sLY1 = sp1
                  sLX2 = sx0
                  sLY2 = sp0
                  supBroken = 0
                  supActive = 1
                  // archive: one new slot per line (pure assignment, safe on live ticks)
                  $sX1[nS] = sx1
                  $sY1[nS] = sp1
                  $sX2[nS] = sx0
                  $sY2[nS] = sp0
                  $sAng[nS] = ang
                  $sBorn[nS] = barindex
                  nS = nS + 1
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF
ENDIF
// the active supply line breaks when a candle BODY closes above it
IF supActive AND NOT supBroken THEN
   lineNow = sLY1 + (sLY2 - sLY1) * (barindex - sLX1) / (sLX2 - sLX1)
   IF max(open, close) > lineNow THEN
      supBroken = 1
   ENDIF
ENDIF
//--------------------------------------------------------------------//
// Demand logic: two rising pivot lows with no close below the line
//--------------------------------------------------------------------//
ONCE demCnt = 0
ONCE demBroken = 1
ONCE demActive = 0
ONCE nD = 0
IF showDemand AND newPL THEN
   dp2 = dp1
   dx2 = dx1
   dp1 = dp0
   dx1 = dx0
   dp0 = low[prd]
   dx0 = barindex - prd
   demCnt = min(demCnt + 1, 10)
   IF demCnt > 1 THEN
      IF dp0 > dp1 AND (dx0 - dx1) <= maxDist THEN
         valid = demBroken
         IF NOT valid AND demCnt > 2 THEN
            valid = dp1 > dp2
         ENDIF
         IF valid THEN
            ok = 1
            slope = (dp0 - dp1) / (dx0 - dx1)
            FOR b = dx1 + 1 TO barindex DO
               IF $cA[b] < dp1 + slope * (b - dx1) THEN
                  ok = 0
                  BREAK
               ENDIF
            NEXT
            IF ok THEN
               ang = 0
               IF yAxis <> 0 THEN
                  ang = abs(round(atan(normHeight * (dp0 - dp1) / (yAxis * (dx0 - dx1))) * 100) / 100)
               ENDIF
               IF ang <= maxAngle THEN
                  dLX1 = dx1
                  dLY1 = dp1
                  dLX2 = dx0
                  dLY2 = dp0
                  demBroken = 0
                  demActive = 1
                  $dX1[nD] = dx1
                  $dY1[nD] = dp1
                  $dX2[nD] = dx0
                  $dY2[nD] = dp0
                  $dAng[nD] = ang
                  $dBorn[nD] = barindex
                  nD = nD + 1
               ENDIF
            ENDIF
         ENDIF
      ENDIF
   ENDIF
ENDIF
// the active demand line breaks when a candle BODY closes below it
IF demActive AND NOT demBroken THEN
   lineNow = dLY1 + (dLY2 - dLY1) * (barindex - dLX1) / (dLX2 - dLX1)
   IF min(open, close) < lineNow THEN
      demBroken = 1
   ENDIF
ENDIF
//--------------------------------------------------------------------//
// Drawing: the last lines of each side. Each one runs from its first
// pivot until the candle that broke it, or until the bar before the
// next line replaced it, or up to the current bar while it is alive.
//--------------------------------------------------------------------//
IF islastbarupdate THEN
   lblOff = averagetruerange[14](close) * 0.6
   FOR k = max(0, nS - keepLines) TO nS - 1 DO
      x1 = $sX1[k]
      y1 = $sY1[k]
      sl = ($sY2[k] - y1) / ($sX2[k] - x1)
      tEnd = barindex
      IF k < nS - 1 THEN
         tEnd = $sBorn[k + 1] - 1
      ENDIF
      xEnd = tEnd
      FOR t = $sBorn[k] TO tEnd DO
         IF max($oA[t], $cA[t]) > y1 + sl * (t - x1) THEN
            xEnd = t
            BREAK
         ENDIF
      NEXT
      yEnd = y1 + sl * (xEnd - x1)
      IF lineWidth <= 1 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(supR, supG, supB) STYLE(line, 1)
      ELSIF lineWidth = 2 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(supR, supG, supB) STYLE(line, 2)
      ELSIF lineWidth = 3 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(supR, supG, supB) STYLE(line, 3)
      ELSE
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(supR, supG, supB) STYLE(line, 4)
      ENDIF
      angTxt = $sAng[k]
      DRAWTEXT("#angTxt#°", x1, y1 + lblOff) COLOURED(supR, supG, supB)
   NEXT
   FOR k = max(0, nD - keepLines) TO nD - 1 DO
      x1 = $dX1[k]
      y1 = $dY1[k]
      sl = ($dY2[k] - y1) / ($dX2[k] - x1)
      tEnd = barindex
      IF k < nD - 1 THEN
         tEnd = $dBorn[k + 1] - 1
      ENDIF
      xEnd = tEnd
      FOR t = $dBorn[k] TO tEnd DO
         IF min($oA[t], $cA[t]) < y1 + sl * (t - x1) THEN
            xEnd = t
            BREAK
         ENDIF
      NEXT
      yEnd = y1 + sl * (xEnd - x1)
      IF lineWidth <= 1 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(demR, demG, demB) STYLE(line, 1)
      ELSIF lineWidth = 2 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(demR, demG, demB) STYLE(line, 2)
      ELSIF lineWidth = 3 THEN
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(demR, demG, demB) STYLE(line, 3)
      ELSE
         DRAWSEGMENT(x1, y1, xEnd, yEnd) COLOURED(demR, demG, demB) STYLE(line, 4)
      ENDIF
      angTxt = $dAng[k]
      DRAWTEXT("#angTxt#°", x1, y1 - lblOff) COLOURED(demR, demG, demB)
   NEXT
ENDIF
RETURN

Original author: KRISTRADER1, published open source under the Mozilla Public License 2.0.

Download
Filename: PRC_Trend-Lines.itf
Downloads: 12
Iván González
Iván González Legend
I usually let my code do the talking, which explains why my bio is as empty as a newly created file. Bio to be initialized...
Author’s Profile

Comments

ProRealCode ProRealCode
Loading...