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.
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:
prd x 7 bars away from it;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.
prd gives more, shorter lines for intraday; a larger one keeps only the major swings.
//---------------------------------------------------------------
// 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.