Bonjour,
Le code que vous avez collé est le Zig Zag officiel de TradingView : presque tout le travail est fait par la librairie TradingView/ZigZag/9. Je l’ai donc traduit à partir du code de cette librairie, pour qu’il trace la même chose.
Fonctionnement :
- Un pivot est confirmé quand le plus haut (ou le plus bas) d’une bougie dépasse les N bougies de chaque côté, N étant la moitié de
pivotLegs (10 → 5 bougies de chaque côté). - Un pivot du côté opposé n’est retenu comme retournement que s’il s’éloigne d’au moins
devPct % du dernier pivot. Si un nouveau pivot du même côté est plus extrême, le dernier pivot se déplace jusqu’à lui. - Chaque pivot affiche son prix, la variation depuis le pivot précédent (en points ou en %) et le volume cumulé de la jambe, de la bougie qui suit le pivot précédent jusqu’au pivot.
- La jambe en pointillés est le pivot projeté : le prochain pivot qui atteint déjà le % mais qui n’a pas encore ses N bougies à droite. Elle est recalculée à chaque tick et change ou disparaît dès qu’elle est confirmée ou invalidée.
Paramètres :
devPct : écart minimum en %. TradingView utilise 5 par défaut ; d’après votre capture, vous utilisez une valeur bien plus faible (les plus petites jambes font environ 0,17 %).pivotLegs : nombre de bougies pour confirmer un pivot.showProjection, showPrice, showChange, chgMode (0 = points, 1 = %), showVolume : ce qui est affiché.maxLegs : nombre de jambes récentes tracées.
//------------------------------------------------------------------//
// PRC_Zig Zag with labels (price / change / volume)
// version = 0
// 09.10.2026
// Ivan Gonzalez @ www.prorealcode.com
// Sharing ProRealTime knowledge
// Based on the TradingView built-in Zig Zag (ZigZag library v9)
//------------------------------------------------------------------//
DEFPARAM DRAWONLASTBARONLY = true
//------------------------------------------------------------------//
// Inputs
//------------------------------------------------------------------//
devPct = 5 // price deviation for reversals (%)
pivotLegs = 10 // bars required to confirm a pivot (half on each side)
showProjection = 1 // 1 = draw the projected (not yet confirmed) leg, dashed
showPrice = 1 // 1 = display reversal price
showChange = 1 // 1 = display price change from the previous pivot
chgMode = 0 // 0 = absolute change, 1 = percent change
showVolume = 1 // 1 = display cumulative volume of the leg
maxLegs = 200 // number of most recent legs drawn
//------------------------------------------------------------------//
// Pivot engine
//------------------------------------------------------------------//
ONCE nArch = 0
ONCE hasLast = 0
ONCE sumVol = 0
ONCE cumV = 0
ONCE lpX = 0
ONCE lpY = 0
ONCE lpDir = 0
ONCE lpVol = 0
ONCE lpOf = 0
depth = max(2, floor(pivotLegs / 2))
cumV = cumV + volume
atrV = averagetruerange[14](close)
hhR = highest[depth](high)
llR = lowest[depth](low)
// The last pivot lives in scalars: it can still move, and scalars are
// rewound between ticks while arrays are not. A pivot is copied to the
// arrays only once the next one starts, into a new slot.
newPiv = 0
IF barindex >= depth THEN
sumVol = sumVol + volume[depth]
ENDIF
IF barindex >= 2 * depth THEN
// high[depth] must be >= the depth bars on its right and > the depth bars on its left
phOk = high[depth] >= hhR AND high[depth] > hhR[depth + 1]
plOk = low[depth] <= llR AND low[depth] < llR[depth + 1]
FOR kSd = 0 TO 1 DO
IF kSd = 0 THEN
cOk = phOk
cDir = 1
cY = high[depth]
ELSE
cOk = plOk
cDir = -1
cY = low[depth]
ENDIF
IF cOk THEN
cX = barindex - depth
IF hasLast = 0 THEN
lpX = cX
lpY = cY
lpDir = cDir
lpVol = sumVol
lpOf = 0.5 * atrV[depth]
hasLast = 1
sumVol = 0
newPiv = 1
ELSIF lpDir = cDir THEN
// same side: the pivot only moves to a more extreme price
IF cDir * cY > cDir * lpY THEN
lpX = cX
lpY = cY
lpVol = lpVol + sumVol
lpOf = 0.5 * atrV[depth]
sumVol = 0
newPiv = 1
ENDIF
ELSE
// opposite side: a reversal needs devPct % from the last pivot
devV = 100 * (cY - lpY) / abs(lpY)
IF cDir * devV >= devPct THEN
nArch = nArch + 1
$pX[nArch] = lpX
$pY[nArch] = lpY
$pDir[nArch] = lpDir
$pVol[nArch] = lpVol
$pOf[nArch] = lpOf
lpX = cX
lpY = cY
lpDir = cDir
lpVol = sumVol
lpOf = 0.5 * atrV[depth]
sumVol = 0
newPiv = 1
ENDIF
ENDIF
ENDIF
NEXT
ENDIF
//------------------------------------------------------------------//
// Drawing
//------------------------------------------------------------------//
IF islastbarupdate THEN
// Projected leg: what the next pivot would be if it were confirmed now
projOn = 0
hideLast = 0
IF showProjection = 1 AND hasLast = 1 AND newPiv = 0 AND barindex > lpX THEN
nSince = barindex - lpX
oDir = -lpDir
sameV = lpY
sameOff = -1
oppOk = 0
oppV = lpY
oppOff = -1
FOR iBk = 0 TO nSince - 1 DO
IF lpDir = 1 THEN
sameC = high[iBk]
oppC = low[iBk]
ELSE
sameC = low[iBk]
oppC = high[iBk]
ENDIF
// same side: furthest price beyond the last pivot (latest bar on ties)
IF lpDir * sameC > lpDir * sameV THEN
sameV = sameC
sameOff = iBk
ENDIF
// opposite side: furthest candidate that reaches devPct and is a
// pivot on its left (depth bars), even if its right side is incomplete
IF oDir * 100 * (oppC - lpY) / abs(lpY) >= devPct THEN
IF oppOk = 0 OR oDir * oppC > oDir * oppV THEN
cValid = 1
IF iBk > 0 THEN
FOR jR = 1 TO min(depth, iBk) DO
IF lpDir = 1 THEN
vR = low[iBk - jR]
ELSE
vR = high[iBk - jR]
ENDIF
IF oDir * vR > oDir * oppC THEN
cValid = 0
ENDIF
NEXT
ENDIF
FOR jL = 1 TO depth DO
IF lpDir = 1 THEN
vL = low[iBk + jL]
ELSE
vL = high[iBk + jL]
ENDIF
IF oDir * vL >= oDir * oppC THEN
cValid = 0
ENDIF
NEXT
IF cValid = 1 THEN
oppOk = 1
oppV = oppC
oppOff = iBk
ENDIF
ENDIF
ENDIF
NEXT
IF sameOff >= 0 AND nArch >= 1 AND NOT (oppOk = 1 AND oppOff > sameOff) THEN
// price went beyond the last pivot: that leg is still running
hideLast = 1
projOn = 1
pjX1 = $pX[nArch]
pjY1 = $pY[nArch]
pjX2 = barindex - sameOff
pjY2 = sameV
pjDir = lpDir
pjVol = cumV[sameOff] - cumV[barindex - $pX[nArch]]
pjOf = 0.5 * atrV[sameOff]
ELSIF oppOk = 1 THEN
projOn = 1
pjX1 = lpX
pjY1 = lpY
pjX2 = barindex - oppOff
pjY2 = oppV
pjDir = oDir
pjVol = cumV[oppOff] - cumV[nSince]
pjOf = 0.5 * atrV[oppOff]
ENDIF
ENDIF
// Without any confirmed reversal there is nothing to draw: say why
IF nArch = 0 THEN
DRAWTEXT("Zig Zag: no reversal of #devPct# % in the loaded history. Lower devPct.", 250, -120, SansSerif, Bold, 12) ANCHOR(TOPLEFT, XSHIFT, YSHIFT) COLOURED(200, 30, 30)
ENDIF
// Archived legs, then the last confirmed leg, then the projection
hasTxt = max(showPrice, showChange)
firstK = max(2, nArch - maxLegs + 2)
FOR kP = firstK TO nArch + 2 DO
segOn = 0
IF kP <= nArch THEN
sX1 = $pX[kP - 1]
sY1 = $pY[kP - 1]
sX2 = $pX[kP]
sY2 = $pY[kP]
sDir = $pDir[kP]
sVol = $pVol[kP]
sOf = $pOf[kP]
sDash = 0
segOn = 1
ELSIF kP = nArch + 1 THEN
IF hasLast = 1 AND hideLast = 0 THEN
sX1 = $pX[nArch]
sY1 = $pY[nArch]
sX2 = lpX
sY2 = lpY
sDir = lpDir
sVol = lpVol
sOf = lpOf
sDash = 0
segOn = 1
ENDIF
ELSIF projOn = 1 THEN
sX1 = pjX1
sY1 = pjY1
sX2 = pjX2
sY2 = pjY2
sDir = pjDir
sVol = pjVol
sOf = pjOf
sDash = 1
segOn = 1
ENDIF
IF segOn = 1 THEN
IF sDash = 1 THEN
DRAWSEGMENT(sX1, sY1, sX2, sY2) COLOURED(41, 98, 255) STYLE(dottedline2, 2)
ELSE
DRAWSEGMENT(sX1, sY1, sX2, sY2) COLOURED(41, 98, 255) STYLE(line, 2)
ENDIF
chgV = round((sY2 - sY1) / ticksize) * ticksize
chgP = round(10000 * (sY2 - sY1) / abs(sY1)) / 100
// highs: green text above the bar / lows: red text below the bar
IF sDir = 1 THEN
txR = 76
txG = 175
txB = 80
yVol = sY2 + sOf
IF showVolume = 1 THEN
yTxt = yVol + 1.4 * sOf
ELSE
yTxt = yVol
ENDIF
ELSE
txR = 242
txG = 54
txB = 69
yTxt = sY2 - sOf
IF hasTxt = 1 THEN
yVol = yTxt - 1.4 * sOf
ELSE
yVol = yTxt
ENDIF
ENDIF
IF hasTxt = 1 THEN
IF showChange = 0 THEN
DRAWTEXT("#sY2#", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ELSIF chgMode = 0 THEN
IF showPrice = 1 THEN
IF chgV > 0 THEN
DRAWTEXT("#sY2# (+#chgV#)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ELSE
DRAWTEXT("#sY2# (#chgV#)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ENDIF
ELSE
IF chgV > 0 THEN
DRAWTEXT("(+#chgV#)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ELSE
DRAWTEXT("(#chgV#)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ENDIF
ENDIF
ELSE
IF showPrice = 1 THEN
IF chgP > 0 THEN
DRAWTEXT("#sY2# (+#chgP#%)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ELSE
DRAWTEXT("#sY2# (#chgP#%)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ENDIF
ELSE
IF chgP > 0 THEN
DRAWTEXT("(+#chgP#%)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ELSE
DRAWTEXT("(#chgP#%)", sX2, yTxt, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ENDIF
ENDIF
ENDIF
ENDIF
IF showVolume = 1 THEN
DRAWTEXT("#sVol#", sX2, yVol, SansSerif, Standard, 10) COLOURED(txR, txG, txB)
ENDIF
ENDIF
NEXT
ENDIF
RETURN