The failure swing is one of the oldest RSI signals, described by Welles Wilder himself, and one of the least used, because it is hard to see. RSI drops below 30, bounces, makes a higher low that holds above 30, and then breaks above the top of the bounce: that break is a bottom failure swing. The top one is the mirror image around 70. It reads a change of momentum without any divergence or crossing of moving averages.
RSI Swing Points by AlgoNorth finds these swings with fixed rules and puts them where you trade: on the price chart. The points A, B and C are marked on the candles with their RSI values, the break gets a FAILURE SWING tag, a line on price shows exactly where RSI would sit at 50, and a small RSI map to the right of the chart shows the last swing that fired and the one forming next.
The bottom failure swing, built from confirmed RSI pivots (pivL bars on the left, pivR on the right):
maxSpan bars after A, it is dropped.The top failure swing runs the same machine on the mirrored RSI: A above 70, B the low of the pullback, C a lower high that stays below 70, and it fires when RSI closes below B.
Held or failed. After a swing fires, it has held if price does not close back through C’s price within holdN bars. The indicator counts how often tops and bottoms have held on your chart.
The RSI 50 line. RSI is a ratio of smoothed gains and losses, so for each bar there is exactly one closing price that would put RSI at 50. That price is drawn as a line on the chart. A close above it means RSI above 50, a close below it means RSI below 50, and its distance from price is the room left before momentum flips.
The soft cloud. Five layers from the RSI 50 line toward price, densest at the line and fading outward. The cloud is wider the further RSI sits from 50 (scaled by the smoothed gap between the line and price, never narrower than ribAtr ATRs), and when RSI crosses 50 it twists over the line to the other side.
mapWid columns when longer). The yellow dot is the next point the forming swing is waiting for (“A FORMING”, “B FORMING” or “C FORMING”). Once B is in, the NEXT line shows the RSI level the swing fires through, with ARMED when C is in or NEEDS C when it is not.holdN bars, with the number of cases.
pivR bars after they print.
//---------------------------------------------------------------
// PRC_RSI Swing Points
// version = 0
// 26.11.2026
// Iván González @ www.prorealcode.com
// Author: AlgoNorth
// Sharing ProRealTime knowledge
//--------------------------------------------------------------------//
DEFPARAM DRAWONLASTBARONLY = true
// ===================== INPUTS
// --- RSI swing points
rsiLen = 14 // RSI length
pivL = 3 // pivot left bars
pivR = 2 // pivot right bars: A and C are known this many bars after they print
maxSpan = 120 // a swing that has not fired this many bars after A is dropped
holdN = 20 // a swing held if price does not close back through C's price within this many bars
// --- RSI levels
hiLvl = 70 // upper level (overbought): a top failure swing needs A above it
midLvl = 50 // middle level: the RSI 50 line on price
loLvl = 30 // lower level (oversold): a bottom failure swing needs A below it
// --- On price
showLine = 1 // RSI 50 line on price
showSwings = 1 // swing points A, B, C and the FAILURE SWING tag on price
tagGap = 0.6 // tag gap past the candles (ATR)
wickGap = 0.15 // gap between wick and stem (ATR)
ptStem = 1.0 // swing point stem length (ATR)
// --- Soft cloud
showRib = 1 // show cloud
ribFull = 22 // RSI distance from the middle level for full width
ribMin = 0.25 // minimum spread (share of the smoothed line-to-price gap)
ribMax = 0.9 // full spread (share of the smoothed line-to-price gap)
ribAtr = 1.5 // minimum width (ATR)
ribLen = 8 // cloud smoothing
xLen = 2 // cross-over speed
cloudTr = 53 // transparency of the inner layer (0 to 88, percent)
// --- RSI swing map
showMap = 1 // show map
mapWid = 40 // max width (bars)
mapGap = 14 // distance from the last candle (bars)
showFill = 1 // depth fill
fillTr = 83 // transparency of each fill layer (percent)
dotSize = 1 // dot size: 0 tiny, 1 small, 2 normal
// --- Panel
showPanel = 1 // show panel
panelSize = 9 // panel font size
panelX = -10 // panel right edge, pixels from the right border
panelY = -10 // panel top edge, pixels from the top border
// --- Style
lightBg = 1 // 1 = light chart background (dark lines and text), 0 = dark background
bullR = 0 // bull
bullG = 230
bullB = 118
bearR = 255 // bear
bearG = 23
bearB = 68
neutR = 120 // neutral
neutG = 123
neutB = 134
// ===================== CONSTANTS
ONCE trStp = (88 - cloudTr) / 4
ONCE alR = 2 / (ribLen + 1)
ONCE alX = 2 / (xLen + 4 + 1)
ONCE fillA = round(255 * (100 - fillTr) / 100)
IF barindex = 0 THEN
IF lightBg = 1 THEN
lcR = 19
lcG = 23
lcB = 34
tcR = 67
tcG = 70
tcB = 81
fcR = 166
fcG = 99
fcB = 0
bgR = 255
bgG = 255
bgB = 255
ELSE
lcR = 255
lcG = 255
lcB = 255
tcR = 209
tcG = 212
tcB = 220
fcR = 255
fcG = 214
fcB = 0
bgR = 19
bgG = 23
bgB = 34
ENDIF
FOR q = 1 TO 5 DO
$cloudA[q] = round(255 * (100 - (cloudTr + (q - 1) * trStp)) / 100)
NEXT
IF dotSize <= 0 THEN
dotSz = 10
ELSIF dotSize = 1 THEN
dotSz = 13
ELSE
dotSz = 18
ENDIF
ENDIF
// ===================== RSI (Wilder, built like the platform RSI)
ONCE avgUp = 0
ONCE avgDn = 0
ONCE rsiOk = 0
IF barindex >= 1 THEN
chg = close - close[1]
ELSE
chg = 0
ENDIF
upC = max(chg, 0)
dnC = max(0 - chg, 0)
IF barindex = rsiLen THEN
avgUp = summation[rsiLen](upC) / rsiLen
avgDn = summation[rsiLen](dnC) / rsiLen
rsiOk = 1
ELSIF barindex > rsiLen THEN
avgUp = (avgUp[1] * (rsiLen - 1) + upC) / rsiLen
avgDn = (avgDn[1] * (rsiLen - 1) + dnC) / rsiLen
ENDIF
IF rsiOk = 1 THEN
IF avgDn = 0 THEN
rsiV = 100
ELSIF avgUp = 0 THEN
rsiV = 0
ELSE
rsiV = 100 - 100 / (1 + avgUp / avgDn)
ENDIF
rsiN = rsiV
ELSE
rsiV = undefined
rsiN = midLvl
ENDIF
atrV = averagetruerange[14](close)
IF barindex >= 13 THEN
atrN = atrV
ELSE
atrN = 0
ENDIF
// ===================== FAILURE SWING MACHINES
// Bottom swing: A = RSI pivot low below the lower level, B = the rally high,
// C = a higher pivot low that holds; it fires when RSI closes above B.
// The top swing runs the same machine on the mirrored RSI (100 - RSI).
ONCE bSt = 0
ONCE tSt = 0
ONCE nS = 0
plOk = 0
phOk = 0
IF barindex >= rsiLen + pivL + pivR THEN
IF rsiV[pivR] < lowest[pivR](rsiV) AND rsiV[pivR] <= lowest[pivL](rsiV)[pivR + 1] THEN
plOk = 1
plV = rsiV[pivR]
ENDIF
IF rsiV[pivR] > highest[pivR](rsiV) AND rsiV[pivR] >= highest[pivL](rsiV)[pivR + 1] THEN
phOk = 1
phV = rsiV[pivR]
ENDIF
ENDIF
px = barindex - pivR
fireBull = 0
fireBear = 0
IF rsiOk = 1 THEN
// --- bottom machine
IF bSt > 0 AND barindex - bAX > maxSpan THEN
bSt = 0
ENDIF
IF plOk = 1 THEN
IF plV < loLvl AND (bSt <> 1 OR plV < bAV) THEN
bAV = plV
bAX = px
bAP = low[pivR]
bSt = 1
ELSIF bSt = 2 OR (bSt = 3 AND plV < bCV) THEN
// B is the rally's high: it moves to any bar before C where RSI went higher
kEnd = barindex - bBX - 1
IF kEnd > pivR THEN
FOR k = pivR + 1 TO kEnd DO
IF rsiV[k] > bBV THEN
bBV = rsiV[k]
bBX = barindex - k
bBP = high[k]
ENDIF
NEXT
ENDIF
bCV = plV
bCX = px
bCP = low[pivR]
bSt = 3
ENDIF
ENDIF
IF phOk = 1 AND (bSt = 1 OR (bSt = 2 AND phV > bBV)) THEN
bBV = phV
bBX = px
bBP = high[pivR]
bSt = 2
ENDIF
// C has to hold: a close with RSI back past the level drops the swing
IF bSt = 3 AND rsiV < loLvl THEN
bSt = 0
ENDIF
IF bSt = 3 AND rsiV > bBV THEN
fireBull = 1
bSt = 0
ENDIF
// --- top machine (mirrored RSI)
mHi = 100 - hiLvl
IF tSt > 0 AND barindex - tAX > maxSpan THEN
tSt = 0
ENDIF
IF phOk = 1 THEN
tLo = 100 - phV
IF tLo < mHi AND (tSt <> 1 OR tLo < tAV) THEN
tAV = tLo
tAX = px
tAP = high[pivR]
tSt = 1
ELSIF tSt = 2 OR (tSt = 3 AND tLo < tCV) THEN
kEnd = barindex - tBX - 1
IF kEnd > pivR THEN
FOR k = pivR + 1 TO kEnd DO
IF 100 - rsiV[k] > tBV THEN
tBV = 100 - rsiV[k]
tBX = barindex - k
tBP = low[k]
ENDIF
NEXT
ENDIF
tCV = tLo
tCX = px
tCP = high[pivR]
tSt = 3
ENDIF
ENDIF
IF plOk = 1 THEN
tHi = 100 - plV
IF tSt = 1 OR (tSt = 2 AND tHi > tBV) THEN
tBV = tHi
tBX = px
tBP = low[pivR]
tSt = 2
ENDIF
ENDIF
IF tSt = 3 AND 100 - rsiV < mHi THEN
tSt = 0
ENDIF
IF tSt = 3 AND 100 - rsiV > tBV THEN
fireBear = 1
tSt = 0
ENDIF
ENDIF
// ===================== RSI 50 LINE
// The price this bar would have to close at for RSI to equal the middle level.
IF barindex > rsiLen THEN
lvC = max(0.01, min(99.99, midLvl))
rsM = lvC / (100 - lvC)
upM = (rsiLen - 1) * (rsM * avgDn[1] - avgUp[1])
IF upM >= 0 THEN
mid50 = close[1] + upM
ELSE
mid50 = close[1] - (rsiLen - 1) * (avgUp[1] / rsM - avgDn[1])
ENDIF
midOk = 1
ELSE
mid50 = undefined
midOk = 0
ENDIF
ONCE trendS = 0
ONCE flipBar = 0
IF rsiOk = 0 THEN
sideS = 0
ELSIF rsiV >= midLvl THEN
sideS = 1
ELSE
sideS = -1
ENDIF
IF sideS <> trendS THEN
trendS = sideS
flipBar = barindex
ENDIF
IF trendS = 1 THEN
scR = bullR
scG = bullG
scB = bullB
ELSIF trendS = -1 THEN
scR = bearR
scG = bearG
scB = bearB
ELSE
scR = neutR
scG = neutG
scB = neutB
ENDIF
// ===================== SOFT CLOUD
// Five layers from the line toward price. Width = line-to-price gap scaled by how far RSI
// sits from the middle level, never narrower than the ATR floor, smoothed twice. Its side
// is eased toward the current side, so the cloud twists over the line when RSI crosses.
ONCE gapS = 0
ONCE strS = 0
ONCE magA = 0
ONCE magV = 0
ONCE momS = 0
IF midOk = 1 THEN
gapX = abs(close - mid50)
ELSE
gapX = 0
ENDIF
IF barindex = rsiLen + ribLen THEN
gapS = summation[ribLen](gapX) / ribLen
ELSIF barindex > rsiLen + ribLen THEN
gapS = gapS[1] + alR * (gapX - gapS[1])
ENDIF
strX = min(1, abs(rsiN - midLvl) / ribFull)
momX = abs(rsiN - midLvl)
IF barindex = ribLen - 1 THEN
strS = summation[ribLen](strX) / ribLen
momS = summation[ribLen](momX) / ribLen
ELSIF barindex > ribLen - 1 THEN
strS = strS[1] + alR * (strX - strS[1])
momS = momS[1] + alR * (momX - momS[1])
ENDIF
magRaw = max(gapS * (ribMin + (max(ribMax, ribMin) - ribMin) * strS), ribAtr * atrN)
IF barindex = ribLen - 1 THEN
magA = summation[ribLen](magRaw) / ribLen
ELSIF barindex > ribLen - 1 THEN
magA = magA[1] + alR * (magRaw - magA[1])
ENDIF
IF barindex = 2 * ribLen - 2 THEN
magV = summation[ribLen](magA) / ribLen
ELSIF barindex > 2 * ribLen - 2 THEN
magV = magV[1] + alR * (magA - magV[1])
ENDIF
// two-pass EMA of the side with a fractional length
IF barindex = 0 THEN
e1 = trendS
e2 = e1
ELSE
e1 = e1[1] + (trendS - e1[1]) * alX
e2 = e2[1] + (e1 - e2[1]) * alX
ENDIF
ribW = e2 * magV
IF showRib = 1 AND trendS <> 0 AND midOk = 1 THEN
ribOn = 1
ELSE
ribOn = 0
ENDIF
IF ribW >= 0 THEN
ccR = bullR
ccG = bullG
ccB = bullB
ELSE
ccR = bearR
ccG = bearG
ccB = bearB
ENDIF
IF ribOn = 1 THEN
baseL = mid50
lay1 = mid50 + ribW * 0.2
lay2 = mid50 + ribW * 0.4
lay3 = mid50 + ribW * 0.6
lay4 = mid50 + ribW * 0.8
lay5 = mid50 + ribW
ca1 = $cloudA[1]
ca2 = $cloudA[2]
ca3 = $cloudA[3]
ca4 = $cloudA[4]
ca5 = $cloudA[5]
ELSE
baseL = close
lay1 = close
lay2 = close
lay3 = close
lay4 = close
lay5 = close
ca1 = 0
ca2 = 0
ca3 = 0
ca4 = 0
ca5 = 0
ENDIF
COLORBETWEEN(baseL, lay1, ccR, ccG, ccB, ca1)
COLORBETWEEN(lay1, lay2, ccR, ccG, ccB, ca2)
COLORBETWEEN(lay2, lay3, ccR, ccG, ccB, ca3)
COLORBETWEEN(lay3, lay4, ccR, ccG, ccB, ca4)
COLORBETWEEN(lay4, lay5, ccR, ccG, ccB, ca5)
// ===================== FIRED SWINGS: register and place the marks on price
// Each swing: A, B, C (bar, price, RSI), the break bar, and where its marks sit.
// The marks clear the wicks three bars either side, plus the RSI 50 line and cloud.
IF fireBull = 1 OR fireBear = 1 THEN
IF fireBull = 1 THEN
fDr = 1
$sAV[nS] = bAV
$sBV[nS] = bBV
$sCV[nS] = bCV
fAX = bAX
fBX = bBX
fCX = bCX
$sAP[nS] = bAP
$sBP[nS] = bBP
$sCP[nS] = bCP
ELSE
fDr = -1
$sAV[nS] = 100 - tAV
$sBV[nS] = 100 - tBV
$sCV[nS] = 100 - tCV
fAX = tAX
fBX = tBX
fCX = tCX
$sAP[nS] = tAP
$sBP[nS] = tBP
$sCP[nS] = tCP
ENDIF
$sDir[nS] = fDr
$sAX[nS] = fAX
$sBX[nS] = fBX
$sCX[nS] = fCX
$sKX[nS] = barindex
atS = atrN
$sAtr[nS] = atS
stemL = ptStem * atS
dA = 0 - fDr
IF showLine = 1 OR showRib = 1 THEN
dodge = 1
ELSE
dodge = 0
ENDIF
FOR pq = 0 TO 3 DO
IF pq = 0 THEN
agoQ = barindex - fAX
dq = dA
padQ = stemL
reachQ = 2 * stemL
ELSIF pq = 1 THEN
agoQ = barindex - fBX
dq = 0 - dA
padQ = stemL
reachQ = 2 * stemL
ELSIF pq = 2 THEN
agoQ = barindex - fCX
dq = dA
padQ = stemL
reachQ = 2 * stemL
ELSE
agoQ = 0
dq = fDr
padQ = tagGap * atS
reachQ = 4 * tagGap * atS
ENDIF
IF dq = 1 THEN
extQ = high[agoQ]
ELSE
extQ = low[agoQ]
ENDIF
FOR k = max(agoQ - 3, 0) TO agoQ + 3 DO
IF dq = 1 THEN
extQ = max(extQ, high[k])
ELSE
extQ = min(extQ, low[k])
ENDIF
NEXT
yQm = extQ
IF dodge = 1 AND midOk[agoQ] = 1 THEN
lnQ = mid50[agoQ]
IF showRib = 1 THEN
rwQ = ribW[agoQ]
ELSE
rwQ = 0
ENDIF
zHi = max(lnQ, lnQ + rwQ)
zLo = min(lnQ, lnQ + rwQ)
IF dq = 1 AND zHi > extQ AND zLo < extQ + reachQ THEN
yQm = zHi
ENDIF
IF dq = -1 AND zLo < extQ AND zHi > extQ - reachQ THEN
yQm = zLo
ENDIF
ENDIF
yQm = yQm + dq * padQ
IF pq = 0 THEN
$sYA[nS] = yQm
ELSIF pq = 1 THEN
$sYB[nS] = yQm
ELSIF pq = 2 THEN
$sYC[nS] = yQm
ELSE
$sFY[nS] = yQm
fsY = yQm
ENDIF
NEXT
IF fDr = 1 THEN
$sStem[nS] = high + wickGap * atS
ELSE
$sStem[nS] = low - wickGap * atS
ENDIF
fsBar = barindex
fsDir = fDr
fsAtr = atS
nS = nS + 1
ELSIF showSwings = 1 AND nS > 0 AND barindex - fsBar <= 2 THEN
// the 2 candles after the break can still reach the tag: push it further out
IF fsDir = 1 THEN
needY = high + tagGap * fsAtr
ELSE
needY = low - tagGap * fsAtr
ENDIF
IF fsDir * (needY - fsY) > 0 THEN
fsY = needY
$sFY[nS - 1] = fsY
ENDIF
ENDIF
// ===================== DRAWING
IF islastbarupdate THEN
// --- hold test of every swing: held = no close back through C's price within holdN bars
nBull = 0
hBull = 0
nBear = 0
hBear = 0
curRes = 0
IF nS > 0 THEN
FOR iS = 0 TO nS - 1 DO
kx = $sKX[iS]
cpS = $sCP[iS]
dS = $sDir[iS]
rS = 0
jEnd = min(kx + holdN, barindex)
IF jEnd > kx THEN
FOR j = kx + 1 TO jEnd DO
IF rS = 0 THEN
cJ = close[barindex - j]
IF (dS = 1 AND cJ < cpS) OR (dS = -1 AND cJ > cpS) THEN
rS = -1
ENDIF
ENDIF
NEXT
ENDIF
IF rS = 0 AND barindex - kx >= holdN THEN
rS = 1
ENDIF
IF rS <> 0 THEN
IF dS = 1 THEN
nBull = nBull + 1
IF rS = 1 THEN
hBull = hBull + 1
ENDIF
ELSE
nBear = nBear + 1
IF rS = 1 THEN
hBear = hBear + 1
ENDIF
ENDIF
ENDIF
IF iS = nS - 1 THEN
curRes = rS
ENDIF
NEXT
ENDIF
// --- RSI 50 line glyph colours
IF showLine = 1 THEN
mlA = 204
ELSE
mlA = 166
ENDIF
// --- swing points on price (last 45 swings)
IF showSwings = 1 AND nS > 0 THEN
firstS = max(0, nS - 45)
FOR iS = firstS TO nS - 1 DO
dS = $sDir[iS]
atS = $sAtr[iS]
IF dS = 1 THEN
dcR = bullR
dcG = bullG
dcB = bullB
ELSE
dcR = bearR
dcG = bearG
dcB = bearB
ENDIF
FOR pq = 0 TO 2 DO
IF pq = 0 THEN
xq = $sAX[iS]
wq = $sAP[iS]
yq = $sYA[iS]
vq = round($sAV[iS] * 10) / 10
dq = 0 - dS
ELSIF pq = 1 THEN
xq = $sBX[iS]
wq = $sBP[iS]
yq = $sYB[iS]
vq = round($sBV[iS] * 10) / 10
dq = dS
ELSE
xq = $sCX[iS]
wq = $sCP[iS]
yq = $sYC[iS]
vq = round($sCV[iS] * 10) / 10
dq = 0 - dS
ENDIF
DRAWSEGMENT(xq, wq + dq * wickGap * atS, xq, yq) COLOURED(lcR, lcG, lcB, 255) STYLE(dottedline, 1)
DRAWTEXT("●", xq, yq, SansSerif, Standard, dotSz) COLOURED(lcR, lcG, lcB, 255)
yl = yq + dq * 0.45 * atS
yv = yq + dq * 0.95 * atS
IF pq = 0 THEN
DRAWTEXT("A", xq, yl, SansSerif, Bold, 12) COLOURED(lcR, lcG, lcB, 255)
ELSIF pq = 1 THEN
DRAWTEXT("B", xq, yl, SansSerif, Bold, 12) COLOURED(lcR, lcG, lcB, 255)
ELSE
DRAWTEXT("C", xq, yl, SansSerif, Bold, 12) COLOURED(lcR, lcG, lcB, 255)
ENDIF
DRAWTEXT("RSI #vq#", xq, yv, SansSerif, Bold, 9) COLOURED(tcR, tcG, tcB, 255)
NEXT
kx = $sKX[iS]
fy = $sFY[iS]
DRAWSEGMENT(kx, $sStem[iS], kx, fy) COLOURED(lcR, lcG, lcB, 255) STYLE(dottedline, 1)
IF dS = 1 THEN
DRAWTEXT("FAILURE SWING ▲", kx, fy + 0.3 * atS, SansSerif, Bold, 10) COLOURED(dcR, dcG, dcB, 255)
ELSE
DRAWTEXT("FAILURE SWING ▼", kx, fy - 0.3 * atS, SansSerif, Bold, 10) COLOURED(dcR, dcG, dcB, 255)
ENDIF
NEXT
ENDIF
// --- the swing forming next: the one further along; when level, a top while
// RSI is above the middle level, a bottom below it
IF tSt > bSt OR (tSt = bSt AND trendS = 1) THEN
nxTop = 1
nxSt = tSt
nxAX = tAX
nxBX = tBX
nxCX = tCX
nA = 100 - tAV
nB = 100 - tBV
nC = 100 - tCV
ncR = bearR
ncG = bearG
ncB = bearB
ELSE
nxTop = 0
nxSt = bSt
nxAX = bAX
nxBX = bBX
nxCX = bCX
nA = bAV
nB = bBV
nC = bCV
ncR = bullR
ncG = bullG
ncB = bullB
ENDIF
IF nxSt >= 2 THEN
nxBOk = 1
lvC = max(0.01, min(99.99, nB))
rsM = lvC / (100 - lvC)
upM = (rsiLen - 1) * (rsM * avgDn[1] - avgUp[1])
IF upM >= 0 THEN
trigPx = close[1] + upM
ELSE
trigPx = close[1] - (rsiLen - 1) * (avgUp[1] / rsM - avgDn[1])
ENDIF
ELSE
nxBOk = 0
ENDIF
// --- RSI swing map, to the right of the last candle
IF showMap = 1 THEN
xMs = barindex + mapGap
IF nS = 0 THEN
DRAWTEXT("no failure swing yet", xMs + 8, close, SansSerif, Bold, 9) COLOURED(tcR, tcG, tcB, 255)
ELSE
cu = nS - 1
cuDir = $sDir[cu]
cuAX = $sAX[cu]
cuBX = $sBX[cu]
cuCX = $sCX[cu]
cuKX = $sKX[cu]
cuBV = $sBV[cu]
IF barindex - cuAX + 6 >= 990 THEN
nBk = barindex - cuKX
DRAWTEXT("last failure swing #nBk# bars back · too far to map", xMs + 20, close, SansSerif, Bold, 9) COLOURED(tcR, tcG, tcB, 255)
ELSE
IF cuDir = 1 THEN
dcR = bullR
dcG = bullG
dcB = bullB
ELSE
dcR = bearR
dcG = bearG
dcB = bearB
ENDIF
fm = 0
IF nxSt >= 1 THEN
fm = 1
ENDIF
// from 6 bars before the earlier A up to the current bar
IF fm = 1 THEN
startAgo = max(barindex - cuAX, barindex - nxAX) + 6
ELSE
startAgo = barindex - cuAX + 6
ENDIF
spanM = startAgo + 1
wM = min(spanM, mapWid)
IF midOk = 1 THEN
mC = mid50
ELSE
mC = close
ENDIF
mH = (highest[80](high) - lowest[80](low)) / 2
// one RSI sample per column, plus the swing points each on a column of its own bar
nW = 0
FOR c = 0 TO wM - 1 DO
$rep[nW] = startAgo - round(c * (spanM - 1) / (wM - 1))
nW = nW + 1
NEXT
$mk[0] = barindex - cuAX
$mk[1] = barindex - cuBX
$mk[2] = barindex - cuCX
$mk[3] = barindex - cuKX
nF = 0
IF fm = 1 THEN
$fk[0] = barindex - nxAX
nF = 1
IF nxSt >= 2 THEN
$fk[1] = barindex - nxBX
nF = 2
ENDIF
IF nxSt = 3 THEN
$fk[2] = barindex - nxCX
nF = 3
ENDIF
ENDIF
// the next swing point the forming swing is waiting on, until it confirms
cdOk = 0
cdLet = 0
IF fm = 1 AND nxSt < 3 THEN
IF nxSt = 1 THEN
s1 = 1
kTo = barindex - nxAX - 1
ELSE
s1 = 0
kTo = barindex - nxBX - 1
ENDIF
IF s1 = nxTop THEN
lastHi = 1
ELSE
lastHi = 0
ENDIF
IF s1 = 1 THEN
lastV = nA
ELSE
lastV = nB
ENDIF
hiOk = 0
loOk = 0
IF kTo >= 0 THEN
FOR k = 0 TO kTo DO
vk = rsiN[k]
IF hiOk = 0 OR vk > hiV + 0.000000001 THEN
hiV = vk
hiK = k
hiOk = 1
ENDIF
IF loOk = 0 OR vk < loV - 0.000000001 THEN
loV = vk
loK = k
loOk = 1
ENDIF
NEXT
ENDIF
IF hiOk = 1 THEN
IF lastHi = 1 THEN
movK = hiK
nxtK = loK
nxtV = loV
movPast = 0
IF hiV > lastV THEN
movPast = 1
ENDIF
ELSE
movK = loK
nxtK = hiK
nxtV = hiV
movPast = 0
IF loV < lastV THEN
movPast = 1
ENDIF
ENDIF
moving = 0
IF movPast = 1 AND NOT (nxtK > movK AND nxtK < pivR) THEN
moving = 1
ENDIF
cdOk = 1
IF moving = 1 THEN
cdAgo = movK
IF s1 = 1 THEN
cdLet = 1
ELSE
cdLet = 2
ENDIF
ELSE
cdAgo = nxtK
IF s1 = 1 THEN
cdLet = 2
ELSIF (nxTop = 1 AND nxtV > hiLvl) OR (nxTop = 0 AND nxtV < loLvl) THEN
cdLet = 1
ELSE
cdLet = 3
ENDIF
ENDIF
ENDIF
ENDIF
// add the marks that are not already a column
FOR g = 0 TO 3 + nF DO
IF g <= 3 THEN
gv = $mk[g]
ELSE
gv = $fk[g - 4]
ENDIF
inc = 0
FOR c = 0 TO nW - 1 DO
IF $rep[c] = gv THEN
inc = 1
ENDIF
NEXT
IF inc = 0 THEN
$rep[nW] = gv
nW = nW + 1
ENDIF
NEXT
IF cdOk = 1 THEN
inc = 0
FOR c = 0 TO nW - 1 DO
IF $rep[c] = cdAgo THEN
inc = 1
ENDIF
NEXT
IF inc = 0 THEN
$rep[nW] = cdAgo
nW = nW + 1
ENDIF
ENDIF
// columns in time order (oldest first)
FOR c = 1 TO nW - 1 DO
keyV = $rep[c]
j = c - 1
WHILE j >= 0 AND $rep[max(j, 0)] < keyV DO
$rep[j + 1] = $rep[j]
j = j - 1
WEND
$rep[j + 1] = keyV
NEXT
FOR c = 0 TO nW - 1 DO
IF $rep[c] = $mk[0] THEN
colA = c
ENDIF
IF $rep[c] = $mk[1] THEN
colB = c
ENDIF
IF $rep[c] = $mk[2] THEN
colC = c
ENDIF
IF $rep[c] = $mk[3] THEN
colK = c
ENDIF
IF cdOk = 1 THEN
IF $rep[c] = cdAgo THEN
colQ = c
ENDIF
ENDIF
NEXT
FOR g = 0 TO 2 DO
IF g < nF THEN
FOR c = 0 TO nW - 1 DO
IF $rep[c] = $fk[g] THEN
$fcol[g] = c
ENDIF
NEXT
ENDIF
NEXT
rMax = hiLvl
rMin = loLvl
FOR c = 0 TO nW - 1 DO
agoC = $rep[c]
vC = rsiN[agoC]
$vals[c] = vC
rMax = max(rMax, vC)
rMin = min(rMin, vC)
NEXT
topY = mC + (max(10, min(90, rMax + 7)) - midLvl) / 40 * mH
botY = mC + (max(10, min(90, rMin - 7)) - midLvl) / 40 * mH
// display columns: where the RSI line crosses the middle level between two
// samples, a column is added at the crossing itself
nD = 0
FOR c = 0 TO nW - 1 DO
vC = $vals[c]
IF c > 0 THEN
vP = $vals[c - 1]
IF (vP >= midLvl AND vC < midLvl) OR (vP < midLvl AND vC >= midLvl) THEN
IF vP = midLvl THEN
$dx[nD - 1] = 1
ELSE
$dv[nD] = midLvl
$dx[nD] = 1
nD = nD + 1
ENDIF
ENDIF
ENDIF
$di[c] = nD
$dv[nD] = vC
$dx[nD] = 0
nD = nD + 1
NEXT
x0m = xMs
x1m = x0m + nD - 1
// depth fill: each run above / below the middle level is one shape, filled with
// five stacked layers from the middle level toward the RSI line
IF showFill = 1 AND fillTr < 100 THEN
nRun = 0
ls = 0
FOR d = 1 TO nD - 1 DO
IF $dx[d] = 1 AND d < nD - 1 THEN
$lbS[nRun] = ls
$lbE[nRun] = d
nRun = nRun + 1
ls = d
ENDIF
NEXT
$lbS[nRun] = ls
$lbE[nRun] = nD - 1
nRun = nRun + 1
budget = 93
FOR pass = 0 TO 4 DO
fr = pass * 0.2
FOR kr = 0 TO nRun - 1 DO
a0 = $lbS[kr]
a1 = $lbE[kr]
IF a1 > a0 AND budget > 0 THEN
sm = 0
FOR d = a0 TO a1 DO
sm = sm + $dv[d] - midLvl
NEXT
IF sm >= 0 THEN
zR = bullR
zG = bullG
zB = bullB
ELSE
zR = bearR
zG = bearG
zB = bearB
ENDIF
FOR d = a0 TO a1 - 1 DO
vo1 = $dv[d]
vo2 = $dv[d + 1]
yo1 = mC + (max(10, min(90, vo1)) - midLvl) / 40 * mH
yo2 = mC + (max(10, min(90, vo2)) - midLvl) / 40 * mH
yi1 = mC + (max(10, min(90, midLvl + (vo1 - midLvl) * fr)) - midLvl) / 40 * mH
yi2 = mC + (max(10, min(90, midLvl + (vo2 - midLvl) * fr)) - midLvl) / 40 * mH
DRAWTRIANGLE(x0m + d, yo1, x0m + d + 1, yo2, x0m + d + 1, yi2) COLOURED(zR, zG, zB, 0) FILLCOLOR(zR, zG, zB, fillA)
DRAWTRIANGLE(x0m + d, yo1, x0m + d + 1, yi2, x0m + d, yi1) COLOURED(zR, zG, zB, 0) FILLCOLOR(zR, zG, zB, fillA)
NEXT
budget = budget - 1
ENDIF
NEXT
NEXT
ENDIF
// level lines and tags
fpV = round(cuBV * 10) / 10
FOR iL = 0 TO 2 DO
IF iL = 0 THEN
lv = loLvl
ELSIF iL = 1 THEN
lv = midLvl
ELSE
lv = hiLvl
ENDIF
yLv = mC + (max(10, min(90, lv)) - midLvl) / 40 * mH
IF iL = 1 AND showLine = 1 THEN
DRAWSEGMENT(x0m - 1, yLv, x1m + 1, yLv) COLOURED(lcR, lcG, lcB, mlA) STYLE(dottedline2, 2)
ELSE
DRAWSEGMENT(x0m - 1, yLv, x1m + 1, yLv) COLOURED(tcR, tcG, tcB, 166) STYLE(dottedline2, 1)
ENDIF
farB = 1
IF nxBOk = 1 THEN
IF abs(nB - lv) <= 6 THEN
farB = 0
ENDIF
ENDIF
IF abs(cuBV - lv) > 6 AND farB = 1 THEN
IF iL = 0 THEN
DRAWTEXT("#lv#", x1m + 4, yLv, SansSerif, Bold, 9) COLOURED(bullR, bullG, bullB, 255)
ELSIF iL = 1 THEN
DRAWTEXT("#lv#", x1m + 4, yLv, SansSerif, Bold, 9) COLOURED(neutR, neutG, neutB, 255)
ELSE
DRAWTEXT("#lv#", x1m + 4, yLv, SansSerif, Bold, 9) COLOURED(bearR, bearG, bearB, 255)
ENDIF
ENDIF
NEXT
DRAWTEXT("RSI SWING POINTS · length #rsiLen#", x0m + 10, topY, SansSerif, Bold, 9) COLOURED(tcR, tcG, tcB, 255)
// failure point; when the forming swing's fire level is close, the labels split
yF = mC + (max(10, min(90, cuBV)) - midLvl) / 40 * mH
tight = 0
IF nxBOk = 1 THEN
IF abs(nB - cuBV) < 5 THEN
tight = 1
ENDIF
ENDIF
yFl = yF
IF tight = 1 THEN
IF cuBV >= nB THEN
yFl = yF + mH * 0.06
ELSE
yFl = yF - mH * 0.06
ENDIF
ENDIF
DRAWSEGMENT(x0m + $di[colB], yF, x1m + 1, yF) COLOURED(lcR, lcG, lcB, 255) STYLE(dottedline2, 1)
DRAWTEXT("FAILURE POINT · RSI #fpV#", x1m + 14, yFl, SansSerif, Bold, 9) COLOURED(lcR, lcG, lcB, 255)
// RSI line
FOR d = 0 TO nD - 2 DO
ya = mC + (max(10, min(90, $dv[d])) - midLvl) / 40 * mH
yb = mC + (max(10, min(90, $dv[d + 1])) - midLvl) / 40 * mH
DRAWSEGMENT(x0m + d, ya, x0m + d + 1, yb) COLOURED(lcR, lcG, lcB, 255) STYLE(line, 2)
NEXT
// swing point dots and letters of the last swing; a letter is shared with the
// forming swing when both sit on the same bar and side
FOR iL = 0 TO 2 DO
$shr[iL] = -1
NEXT
IF fm = 1 AND nF > 0 THEN
FOR g = 0 TO nF - 1 DO
cf = $fcol[g]
// side of the forming letter: B is on the other side from A and C
IF g = 1 THEN
fsd = 1 - nxTop
ELSE
fsd = nxTop
ENDIF
FOR iL = 0 TO 2 DO
IF iL = 0 THEN
cm = colA
ELSIF iL = 1 THEN
cm = colB
ELSE
cm = colC
ENDIF
// side of the last swing's letter (1 = above the point)
IF iL = 1 THEN
lsd = 0
IF cuDir = 1 THEN
lsd = 1
ENDIF
ELSE
lsd = 0
IF cuDir = -1 THEN
lsd = 1
ENDIF
ENDIF
IF cm = cf AND lsd = fsd THEN
$shr[iL] = g
ENDIF
NEXT
NEXT
ENDIF
mOff = mH * 0.12
FOR iL = 0 TO 2 DO
IF iL = 0 THEN
cm = colA
ELSIF iL = 1 THEN
cm = colB
ELSE
cm = colC
ENDIF
xm = x0m + $di[cm]
ym = mC + (max(10, min(90, $vals[cm])) - midLvl) / 40 * mH
DRAWTEXT("●", xm, ym, SansSerif, Standard, dotSz) COLOURED(lcR, lcG, lcB, 255)
IF (iL = 1 AND cuDir = 1) OR (iL <> 1 AND cuDir = -1) THEN
yt = ym + mOff
ELSE
yt = ym - mOff
ENDIF
sg = $shr[iL]
IF iL = 0 THEN
IF sg = 0 THEN
DRAWTEXT("A·A", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 1 THEN
DRAWTEXT("A·B", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 2 THEN
DRAWTEXT("A·C", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("A", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ELSIF iL = 1 THEN
IF sg = 0 THEN
DRAWTEXT("B·A", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 1 THEN
DRAWTEXT("B·B", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 2 THEN
DRAWTEXT("B·C", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("B", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ELSE
IF sg = 0 THEN
DRAWTEXT("C·A", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 1 THEN
DRAWTEXT("C·B", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF sg = 2 THEN
DRAWTEXT("C·C", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("C", xm, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ENDIF
NEXT
xK = x0m + $di[colK]
yK = mC + (max(10, min(90, $vals[colK])) - midLvl) / 40 * mH
DRAWTEXT("●", xK, yK, SansSerif, Standard, dotSz) COLOURED(dcR, dcG, dcB, 255)
IF cuDir = 1 THEN
DRAWTEXT("BREAK ▲", xK + 5, yK, SansSerif, Bold, 9) COLOURED(dcR, dcG, dcB, 255)
ELSE
DRAWTEXT("BREAK ▼", xK + 5, yK, SansSerif, Bold, 9) COLOURED(dcR, dcG, dcB, 255)
ENDIF
// the swing forming next: its confirmed points, the one it waits on, and its fire level
IF fm = 1 THEN
FOR g = 0 TO nF - 1 DO
shared = 0
FOR iL = 0 TO 2 DO
IF $shr[iL] = g THEN
shared = 1
ENDIF
NEXT
IF shared = 0 THEN
cf = $fcol[g]
xf = x0m + $di[cf]
yFm = mC + (max(10, min(90, $vals[cf])) - midLvl) / 40 * mH
DRAWTEXT("●", xf, yFm, SansSerif, Standard, dotSz) COLOURED(lcR, lcG, lcB, 255)
IF (g = 1 AND nxTop = 0) OR (g <> 1 AND nxTop = 1) THEN
yt = yFm + mOff
ELSE
yt = yFm - mOff
ENDIF
IF g = 0 THEN
DRAWTEXT("A", xf, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSIF g = 1 THEN
DRAWTEXT("B", xf, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("C", xf, yt, SansSerif, Bold, 12) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ENDIF
NEXT
IF cdOk = 1 THEN
xq = x0m + $di[colQ]
yq = mC + (max(10, min(90, $vals[colQ])) - midLvl) / 40 * mH
DRAWTEXT("●", xq, yq, SansSerif, Standard, dotSz) COLOURED(fcR, fcG, fcB, 255)
IF cdLet = 1 THEN
DRAWTEXT("A FORMING", xq - 6, yq, SansSerif, Bold, 9) COLOURED(fcR, fcG, fcB, 255)
ELSIF cdLet = 2 THEN
DRAWTEXT("B FORMING", xq - 6, yq, SansSerif, Bold, 9) COLOURED(fcR, fcG, fcB, 255)
ELSE
DRAWTEXT("C FORMING", xq - 6, yq, SansSerif, Bold, 9) COLOURED(fcR, fcG, fcB, 255)
ENDIF
ENDIF
IF nxBOk = 1 THEN
yN = mC + (max(10, min(90, nB)) - midLvl) / 40 * mH
yNl = yN
IF tight = 1 THEN
IF nB > cuBV THEN
yNl = yN + mH * 0.06
ELSE
yNl = yN - mH * 0.06
ENDIF
ENDIF
nBT = round(nB * 10) / 10
fc1 = $fcol[1]
DRAWSEGMENT(x0m + $di[fc1], yN, x1m + 1, yN) COLOURED(ncR, ncG, ncB, 204) STYLE(dottedline2, 1)
IF nxTop = 1 AND nxSt = 3 THEN
DRAWTEXT("NEXT ▼ FIRES BELOW RSI #nBT# · ARMED", x1m + 20, yNl, SansSerif, Bold, 9) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxTop = 1 THEN
DRAWTEXT("NEXT ▼ FIRES BELOW RSI #nBT# · NEEDS C", x1m + 20, yNl, SansSerif, Bold, 9) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxSt = 3 THEN
DRAWTEXT("NEXT ▲ FIRES ABOVE RSI #nBT# · ARMED", x1m + 20, yNl, SansSerif, Bold, 9) COLOURED(ncR, ncG, ncB, 255)
ELSE
DRAWTEXT("NEXT ▲ FIRES ABOVE RSI #nBT# · NEEDS C", x1m + 20, yNl, SansSerif, Bold, 9) COLOURED(ncR, ncG, ncB, 255)
ENDIF
ENDIF
ENDIF
// footer: how tops and bottoms have each held
IF nBear > 0 THEN
pTop = round(100 * hBear / nBear)
ENDIF
IF nBull > 0 THEN
pBot = round(100 * hBull / nBull)
ENDIF
IF nBear > 0 AND nBull > 0 THEN
DRAWTEXT("failure swings held #holdN# bars · tops #pTop#% · n #nBear# · bottoms #pBot#% · n #nBull#", x0m + 24, botY, SansSerif, Standard, 9) COLOURED(tcR, tcG, tcB, 255)
ELSIF nBear > 0 THEN
DRAWTEXT("failure swings held #holdN# bars · tops #pTop#% · n #nBear# · bottoms not enough history", x0m + 24, botY, SansSerif, Standard, 9) COLOURED(tcR, tcG, tcB, 255)
ELSIF nBull > 0 THEN
DRAWTEXT("failure swings held #holdN# bars · tops not enough history · bottoms #pBot#% · n #nBull#", x0m + 24, botY, SansSerif, Standard, 9) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("failure swings held #holdN# bars · tops not enough history · bottoms not enough history", x0m + 24, botY, SansSerif, Standard, 9) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ENDIF
ENDIF
ENDIF
// --- panel (top right)
IF showPanel = 1 THEN
xRp = panelX
xLp = panelX - 470
xKc = xLp + 105
xVc = xLp + 325
xMc = xLp + 235
rowH = 17
yTopP = panelY
yBotP = panelY - 12 * rowH - 6
DRAWRECTANGLE(xLp, yTopP, xRp, yBotP) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 120) FILLCOLOR(bgR, bgG, bgB, 240)
DRAWRECTANGLE(xLp, yTopP, xRp, yTopP - rowH) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(scR, scG, scB, 255) FILLCOLOR(scR, scG, scB, 255)
y0 = yTopP - 9
DRAWTEXT("RSI Swing Points [AlgoNorth]", xMc, y0, SansSerif, Bold, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(11, 14, 20, 255)
y1 = y0 - rowH
DRAWTEXT("RSI #rsiLen# · pivots #pivL# / #pivR# · levels #hiLvl# / #midLvl# / #loLvl# · hold #holdN# bars · cloud #ribFull# / #ribLen# / #xLen#", xMc, y1, SansSerif, Standard, panelSize - 1) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
y2 = y1 - rowH
IF trendS = 1 THEN
DRAWTEXT("BULL · RSI ABOVE #midLvl#", xMc, y2, SansSerif, Bold, panelSize + 3) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(scR, scG, scB, 255)
ELSIF trendS = -1 THEN
DRAWTEXT("BEAR · RSI BELOW #midLvl#", xMc, y2, SansSerif, Bold, panelSize + 3) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(scR, scG, scB, 255)
ELSE
DRAWTEXT("WAITING FOR RSI", xMc, y2, SansSerif, Bold, panelSize + 3) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(scR, scG, scB, 255)
ENDIF
// the read: RSI and its zone, momentum, bars on this side
y3 = y2 - rowH
y4 = y3 - rowH
y5 = y4 - rowH
IF trendS = 0 THEN
DRAWTEXT("waiting for RSI history", xMc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
rsiT = round(rsiV * 10) / 10
DRAWTEXT("RSI", xKc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
IF rsiV > hiLvl THEN
DRAWTEXT("#rsiT# · overbought", xVc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSIF rsiV >= midLvl THEN
DRAWTEXT("#rsiT# · bullish", xVc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSIF rsiV >= loLvl THEN
DRAWTEXT("#rsiT# · bearish", xVc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("#rsiT# · oversold", xVc, y3, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
momD = momS - momS[1]
DRAWTEXT("Momentum", xKc, y4, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
IF momD > 0.15 THEN
DRAWTEXT("building", xVc, y4, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSIF momD < -0.15 THEN
DRAWTEXT("fading", xVc, y4, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("steady", xVc, y4, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
onSide = barindex - flipBar + 1
IF trendS = 1 THEN
DRAWTEXT("Bars above #midLvl#", xKc, y5, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("Bars below #midLvl#", xKc, y5, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
DRAWTEXT("#onSide#", xVc, y5, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
// flip level: the RSI 50 line on this bar
y6 = y5 - rowH
IF trendS = 1 THEN
DRAWTEXT("Flips bear below", xKc, y6, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
fvR = bearR
fvG = bearG
fvB = bearB
ELSIF trendS = -1 THEN
DRAWTEXT("Flips bull above", xKc, y6, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
fvR = bullR
fvG = bullG
fvB = bullB
ELSE
DRAWTEXT("Flip level", xKc, y6, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
fvR = tcR
fvG = tcG
fvB = tcB
ENDIF
IF atrN > 0 THEN
aP = atrN
ELSE
aP = 1
ENDIF
IF midOk = 1 THEN
m50T = round(mid50 / ticksize) * ticksize
fAtr = round(abs(close - mid50) / aP * 10) / 10
DRAWTEXT("#m50T# · #fAtr# ATR away", xVc, y6, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(fvR, fvG, fvB, 255)
ELSE
DRAWTEXT("—", xVc, y6, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(fvR, fvG, fvB, 255)
ENDIF
// last failure swing and how it played out
y7 = y6 - rowH
DRAWTEXT("Last failure swing", xKc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
IF nS = 0 THEN
DRAWTEXT("—", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
cuDir = $sDir[nS - 1]
nAgoS = barindex - $sKX[nS - 1]
liveN = min(nAgoS, holdN)
IF cuDir = 1 THEN
IF curRes = 1 THEN
DRAWTEXT("▲ bottom · #nAgoS# bars ago · held", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bullR, bullG, bullB, 255)
ELSIF curRes = -1 THEN
DRAWTEXT("▲ bottom · #nAgoS# bars ago · failed", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bullR, bullG, bullB, 255)
ELSE
DRAWTEXT("▲ bottom · #nAgoS# bars ago · live #liveN# of #holdN#", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bullR, bullG, bullB, 255)
ENDIF
ELSE
IF curRes = 1 THEN
DRAWTEXT("▼ top · #nAgoS# bars ago · held", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bearR, bearG, bearB, 255)
ELSIF curRes = -1 THEN
DRAWTEXT("▼ top · #nAgoS# bars ago · failed", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bearR, bearG, bearB, 255)
ELSE
DRAWTEXT("▼ top · #nAgoS# bars ago · live #liveN# of #holdN#", xVc, y7, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(bearR, bearG, bearB, 255)
ENDIF
ENDIF
ENDIF
// next swing: stage and RSI at A, B, C
y8 = y7 - rowH
DRAWTEXT("Next swing", xKc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
nAT = round(nA * 10) / 10
nBT = round(nB * 10) / 10
nCT = round(nC * 10) / 10
IF nxTop = 1 THEN
IF nxSt = 1 THEN
DRAWTEXT("▼ top · A in · #nAT# → — → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxSt = 2 THEN
DRAWTEXT("▼ top · A, B in · #nAT# → #nBT# → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxSt = 3 THEN
DRAWTEXT("▼ top · ARMED · #nAT# → #nBT# → #nCT#", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSE
DRAWTEXT("▼ top · not started · — → — → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ENDIF
ELSE
IF nxSt = 1 THEN
DRAWTEXT("▲ bottom · A in · #nAT# → — → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxSt = 2 THEN
DRAWTEXT("▲ bottom · A, B in · #nAT# → #nBT# → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSIF nxSt = 3 THEN
DRAWTEXT("▲ bottom · ARMED · #nAT# → #nBT# → #nCT#", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSE
DRAWTEXT("▲ bottom · not started · — → — → —", xVc, y8, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ENDIF
ENDIF
// the close that fires the forming swing
y9 = y8 - rowH
IF nxTop = 1 THEN
DRAWTEXT("Fires below", xKc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("Fires above", xKc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
IF nxBOk = 0 THEN
DRAWTEXT("after B prints", xVc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSE
trT = round(trigPx / ticksize) * ticksize
IF nxSt = 2 THEN
DRAWTEXT("#trT# · RSI #nBT# · arms at C", xVc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSIF (nxTop = 1 AND close <= trigPx) OR (nxTop = 0 AND close >= trigPx) THEN
DRAWTEXT("#trT# · RSI #nBT# · at trigger", xVc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ELSE
tAtr = round(abs(close - trigPx) / aP * 10) / 10
DRAWTEXT("#trT# · RSI #nBT# · #tAtr# ATR away", xVc, y9, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(ncR, ncG, ncB, 255)
ENDIF
ENDIF
// how swings of the kind forming next have held
y10 = y9 - rowH
IF nxTop = 1 THEN
DRAWTEXT("Tops held #holdN# bars", xKc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
IF nBear > 0 THEN
pTop = round(100 * hBear / nBear)
DRAWTEXT("#pTop#% · n #nBear#", xVc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("not enough history", xVc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ELSE
DRAWTEXT("Bottoms held #holdN# bars", xKc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
IF nBull > 0 THEN
pBot = round(100 * hBull / nBull)
DRAWTEXT("#pBot#% · n #nBull#", xVc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ELSE
DRAWTEXT("not enough history", xVc, y10, SansSerif, Standard, panelSize) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ENDIF
// last bar, so you can see the panel has recalculated
y11 = y10 - rowH
dD = date
tT = time
nBars = barindex + 1
DRAWTEXT("Last bar #dD# #tT# · #nBars# bars", xMc, y11, SansSerif, Standard, panelSize - 1) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(tcR, tcG, tcB, 255)
ENDIF
ENDIF
// ===================== RSI 50 LINE (return)
IF showLine = 1 THEN
m50A = 255
ELSE
m50A = 0
ENDIF
RETURN mid50 COLOURED(lcR, lcG, lcB, m50A) STYLE(line, 2) AS "RSI 50 line"
Original author: AlgoNorth, published open source under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International licence (CC BY-NC-SA 4.0).