本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Stevesyl
//@version=6
indicator("Equalhigh Perfect Channel V3.1", overlay=true, max_lines_count=50, max_labels_count=50, max_bars_back=1500)
//=====================================================================
// 1. DETECTION
//=====================================================================
groupDetection = "1. Detection"
constructionMode = input.string("AUTO", "Construction", options=["AUTO", "UPPER HIGH", "UPPER CLOSE", "LOWER LOW", "LOWER CLOSE"], group=groupDetection)
pivotLeft = input.int(5, "Pivot gauche", minval=1, maxval=30, group=groupDetection)
pivotRight = input.int(5, "Pivot droit", minval=1, maxval=30, group=groupDetection)
lookback = input.int(300, "Lookback", minval=50, maxval=1000, group=groupDetection)
maxPivots = input.int(12, "Pivots testes", minval=4, maxval=25, group=groupDetection)
minChannelBars = input.int(35, "Longueur minimale", minval=10, maxval=500, group=groupDetection)
//=====================================================================
// 2. CONTACTS
//=====================================================================
groupContacts = "2. Contacts / precision"
atrLength = input.int(14, "ATR", minval=5, maxval=100, group=groupContacts)
anchorTouchATR = input.float(0.20, "Tolerance droite directrice (ATR)", minval=0.05, step=0.05, group=groupContacts)
translatedTouchATR = input.float(0.25, "Tolerance droite translatee (ATR)", minval=0.05, step=0.05, group=groupContacts)
breakATR = input.float(0.30, "Tolerance depassement (ATR)", minval=0.05, step=0.05, group=groupContacts)
minTouchGap = input.int(6, "Separation minimum entre 2 contacts", minval=1, maxval=50, group=groupContacts)
minAnchorTouches = input.int(3, "Contacts distincts directeurs minimum", minval=2, maxval=10, group=groupContacts)
minTranslatedTouches = input.int(1, "Contacts ligne translatee minimum", minval=1, maxval=10, group=groupContacts)
maxViolations = input.int(1, "Violations maximum", minval=0, maxval=10, group=groupContacts)
minWidthATR = input.float(0.50, "Largeur minimale canal (ATR)", minval=0.0, step=0.25, group=groupContacts)
//=====================================================================
// 3. GEOMETRIE
//=====================================================================
groupGeometry = "3. Geometrie"
allowUp = input.bool(true, "Canaux haussiers", group=groupGeometry)
allowDown = input.bool(true, "Canaux baissiers", group=groupGeometry)
allowFlat = input.bool(true, "Canaux horizontaux", group=groupGeometry)
flatATRper100 = input.float(0.50, "Seuil horizontal / 100 bougies (ATR)", minval=0.05, step=0.05, group=groupGeometry)
onlyActive = input.bool(false, "Uniquement canal encore actif", group=groupGeometry)
//=====================================================================
// 4. AFFICHAGE
//=====================================================================
groupDisplay = "4. Affichage"
projectionBars = input.int(60, "Projection a droite", minval=0, maxval=300, group=groupDisplay)
showMedian = input.bool(true, "Afficher mediane", group=groupDisplay)
showFill = input.bool(false, "Colorer interieur", group=groupDisplay)
showAnchors = input.bool(true, "Afficher A1 / A2 / T", group=groupDisplay)
showScore = input.bool(true, "Afficher score", group=groupDisplay)
scoreMinimum = input.float(50, "Score minimum", minval=0, maxval=100, step=5, group=groupDisplay)
//=====================================================================
// 5. ATR
//=====================================================================
atr = ta.atr(atrLength)
//=====================================================================
// 6. PIVOTS
//=====================================================================
phHigh = ta.pivothigh(high, pivotLeft, pivotRight)
phClose = ta.pivothigh(close, pivotLeft, pivotRight)
plLow = ta.pivotlow(low, pivotLeft, pivotRight)
plClose = ta.pivotlow(close, pivotLeft, pivotRight)
//=====================================================================
// 7. ARRAYS
//=====================================================================
var upHighX = array.new_int()
var upHighY = array.new_float()
var upCloseX = array.new_int()
var upCloseY = array.new_float()
var lowLowX = array.new_int()
var lowLowY = array.new_float()
var lowCloseX = array.new_int()
var lowCloseY = array.new_float()
//=====================================================================
// 8. ENREGISTREMENT PIVOTS
//=====================================================================
if not na(phHigh)
int px = bar_index - pivotRight
array.push(upHighX, px)
array.push(upHighY, phHigh)
if array.size(upHighX) > maxPivots
array.shift(upHighX)
array.shift(upHighY)
if not na(phClose)
int px = bar_index - pivotRight
array.push(upCloseX, px)
array.push(upCloseY, phClose)
if array.size(upCloseX) > maxPivots
array.shift(upCloseX)
array.shift(upCloseY)
if not na(plLow)
int px = bar_index - pivotRight
array.push(lowLowX, px)
array.push(lowLowY, plLow)
if array.size(lowLowX) > maxPivots
array.shift(lowLowX)
array.shift(lowLowY)
if not na(plClose)
int px = bar_index - pivotRight
array.push(lowCloseX, px)
array.push(lowCloseY, plClose)
if array.size(lowCloseX) > maxPivots
array.shift(lowCloseX)
array.shift(lowCloseY)
//=====================================================================
// 9. FONCTION DE RECHERCHE
//=====================================================================
f_findBest(array<int> xs, array<float> ys, bool upperAnchored, bool anchorClose) =>
float bScore = -1000000000.0
float bSlope = na
float bIntercept = na
float bOffset = na
int bStartX = na
int bAnchor2X = na
float bAnchor1Y = na
float bAnchor2Y = na
int bAnchorTouches = 0
int bTranslatedTouches = 0
int bViolations = 0
int bTangentX = na
float bTangentY = na
int n = array.size(xs)
if n >= 2
for i = 0 to n - 2
for j = i + 1 to n - 1
int x1 = array.get(xs, i)
float y1 = array.get(ys, i)
int x2 = array.get(xs, j)
float y2 = array.get(ys, j)
int channelBars = bar_index - x1
bool lengthOK = x2 > x1 and channelBars >= minChannelBars and channelBars <= lookback
if lengthOK
// ------------------------------------------------
// DROITE DIRECTRICE
// ------------------------------------------------
float slope = (y2 - y1) / float(x2 - x1)
float intercept = y1 - slope * float(x1)
float atrNow = nz(atr, syminfo.mintick)
// ------------------------------------------------
// DIRECTION DU CANAL
// ------------------------------------------------
float slope100ATR = slope * 100.0 / atrNow
bool isFlat = math.abs(slope100ATR) < flatATRper100
bool directionOK = (isFlat and allowFlat) or (not isFlat and slope > 0 and allowUp) or (not isFlat and slope < 0 and allowDown)
if directionOK
int anchorTouches = 0
int violations = 0
int lastAnchorTouchX = na
float extremeResidual = upperAnchored ? 1000000000.0 : -1000000000.0
int localTangentX = na
float localTangentY = na
int scanBars = math.min(lookback, bar_index + 1)
// =================================================
// PREMIERE PASSE
// =================================================
for k = 0 to scanBars - 1
int bx = bar_index - k
if bx >= x1
float baseLine = slope * float(bx) + intercept
float atrK = nz(atr[k], atrNow)
float touchTol = atrK * anchorTouchATR
float violationTol = atrK * breakATR
// Prix utilise pour tester la droite directrice
float anchorPrice = anchorClose ? close[k] : upperAnchored ? high[k] : low[k]
// -----------------------------------------
// CONTACT DIRECTEUR AVEC CLUSTERING
// -----------------------------------------
bool touching = math.abs(anchorPrice - baseLine) <= touchTol
if touching
bool newCluster = na(lastAnchorTouchX) or math.abs(lastAnchorTouchX - bx) >= minTouchGap
if newCluster
anchorTouches += 1
lastAnchorTouchX := bx
// -----------------------------------------
// VIOLATION DE LA DROITE DIRECTRICE
// -----------------------------------------
bool violation = upperAnchored ? anchorPrice > baseLine + violationTol : anchorPrice < baseLine - violationTol
if violation
violations += 1
// -----------------------------------------
// EXTREME OPPOSE POUR TRANSLATION
// -----------------------------------------
float oppositePrice = upperAnchored ? low[k] : high[k]
float residual = oppositePrice - baseLine
if upperAnchored
if residual < extremeResidual
extremeResidual := residual
localTangentX := bx
localTangentY := oppositePrice
else
if residual > extremeResidual
extremeResidual := residual
localTangentX := bx
localTangentY := oppositePrice
// =================================================
// TRANSLATION
// =================================================
float offset = extremeResidual
bool offsetDirectionOK = upperAnchored ? offset < 0 : offset > 0
float widthATR = math.abs(offset) / atrNow
bool widthOK = widthATR >= minWidthATR
// =================================================
// DEUXIEME PASSE
// =================================================
int translatedTouches = 0
int lastTranslatedTouchX = na
if offsetDirectionOK and widthOK
for k = 0 to scanBars - 1
int bx = bar_index - k
if bx >= x1
float baseLine = slope * float(bx) + intercept
float translatedLine = baseLine + offset
float atrK = nz(atr[k], atrNow)
float translatedTol = atrK * translatedTouchATR
float translatedPrice = upperAnchored ? low[k] : high[k]
bool touchingTranslated = math.abs(translatedPrice - translatedLine) <= translatedTol
if touchingTranslated
bool newCluster = na(lastTranslatedTouchX) or math.abs(lastTranslatedTouchX - bx) >= minTouchGap
if newCluster
translatedTouches += 1
lastTranslatedTouchX := bx
// =================================================
// CANAL ACTUEL
// =================================================
float currentBase = slope * float(bar_index) + intercept
float currentUpper = upperAnchored ? currentBase : currentBase + offset
float currentLower = upperAnchored ? currentBase + offset : currentBase
bool inside = close <= currentUpper + atrNow * breakATR and close >= currentLower - atrNow * breakATR
// =================================================
// SCORE
// =================================================
float anchorScore = math.min(float(anchorTouches) / 5.0, 1.0) * 35.0
float translatedScore = math.min(float(translatedTouches) / 4.0, 1.0) * 25.0
float lengthScore = math.min(float(channelBars) / 200.0, 1.0) * 15.0
float cleanlinessScore = math.max(0.0, 15.0 - float(violations) * 5.0)
float activeScore = inside ? 10.0 : 0.0
float score = anchorScore + translatedScore + lengthScore + cleanlinessScore + activeScore
score := math.max(0.0, math.min(score, 100.0))
// =================================================
// VALIDATION
// =================================================
bool valid = anchorTouches >= minAnchorTouches and translatedTouches >= minTranslatedTouches and violations <= maxViolations and offsetDirectionOK and widthOK and (not onlyActive or inside)
if valid and score > bScore
bScore := score
bSlope := slope
bIntercept := intercept
bOffset := offset
bStartX := x1
bAnchor2X := x2
bAnchor1Y := y1
bAnchor2Y := y2
bAnchorTouches := anchorTouches
bTranslatedTouches := translatedTouches
bViolations := violations
bTangentX := localTangentX
bTangentY := localTangentY
[bScore, bSlope, bIntercept, bOffset, bStartX, bAnchor2X, bAnchor1Y, bAnchor2Y, bAnchorTouches, bTranslatedTouches, bViolations, bTangentX, bTangentY]
//=====================================================================
// 10. MEILLEUR CANAL GLOBAL
//=====================================================================
var float bestScore = na
var float bestSlope = na
var float bestIntercept = na
var float bestOffset = na
var int bestStartX = na
var int bestAnchor2X = na
var float bestAnchor1Y = na
var float bestAnchor2Y = na
var int bestAnchorTouches = 0
var int bestTranslatedTouches = 0
var int bestViolations = 0
var int bestTangentX = na
var float bestTangentY = na
var bool bestIsUpper = true
var string bestType = ""
//=====================================================================
// 11. RECHERCHE SUR DERNIERE BOUGIE
//=====================================================================
if barstate.islast
bestScore := -1000000000.0
bestSlope := na
bestIntercept := na
bestOffset := na
bestStartX := na
bestAnchor2X := na
bestAnchor1Y := na
bestAnchor2Y := na
bestAnchorTouches := 0
bestTranslatedTouches := 0
bestViolations := 0
bestTangentX := na
bestTangentY := na
bestType := ""
// =============================================================
// UPPER HIGH
// =============================================================
if constructionMode == "AUTO" or constructionMode == "UPPER HIGH"
[s1, sl1, it1, of1, sx1, a2x1, a1y1, a2y1, at1, tt1, v1, tx1, ty1] = f_findBest(upHighX, upHighY, true, false)
if s1 > bestScore
bestScore := s1
bestSlope := sl1
bestIntercept := it1
bestOffset := of1
bestStartX := sx1
bestAnchor2X := a2x1
bestAnchor1Y := a1y1
bestAnchor2Y := a2y1
bestAnchorTouches := at1
bestTranslatedTouches := tt1
bestViolations := v1
bestTangentX := tx1
bestTangentY := ty1
bestIsUpper := true
bestType := "HIGH -> LOW"
// =============================================================
// UPPER CLOSE
// =============================================================
if constructionMode == "AUTO" or constructionMode == "UPPER CLOSE"
[s2, sl2, it2, of2, sx2, a2x2, a1y2, a2y2, at2, tt2, v2, tx2, ty2] = f_findBest(upCloseX, upCloseY, true, true)
if s2 > bestScore
bestScore := s2
bestSlope := sl2
bestIntercept := it2
bestOffset := of2
bestStartX := sx2
bestAnchor2X := a2x2
bestAnchor1Y := a1y2
bestAnchor2Y := a2y2
bestAnchorTouches := at2
bestTranslatedTouches := tt2
bestViolations := v2
bestTangentX := tx2
bestTangentY := ty2
bestIsUpper := true
bestType := "CLOSE HIGH -> LOW"
// =============================================================
// LOWER LOW
// =============================================================
if constructionMode == "AUTO" or constructionMode == "LOWER LOW"
[s3, sl3, it3, of3, sx3, a2x3, a1y3, a2y3, at3, tt3, v3, tx3, ty3] = f_findBest(lowLowX, lowLowY, false, false)
if s3 > bestScore
bestScore := s3
bestSlope := sl3
bestIntercept := it3
bestOffset := of3
bestStartX := sx3
bestAnchor2X := a2x3
bestAnchor1Y := a1y3
bestAnchor2Y := a2y3
bestAnchorTouches := at3
bestTranslatedTouches := tt3
bestViolations := v3
bestTangentX := tx3
bestTangentY := ty3
bestIsUpper := false
bestType := "LOW -> HIGH"
// =============================================================
// LOWER CLOSE
// =============================================================
if constructionMode == "AUTO" or constructionMode == "LOWER CLOSE"
[s4, sl4, it4, of4, sx4, a2x4, a1y4, a2y4, at4, tt4, v4, tx4, ty4] = f_findBest(lowCloseX, lowCloseY, false, true)
if s4 > bestScore
bestScore := s4
bestSlope := sl4
bestIntercept := it4
bestOffset := of4
bestStartX := sx4
bestAnchor2X := a2x4
bestAnchor1Y := a1y4
bestAnchor2Y := a2y4
bestAnchorTouches := at4
bestTranslatedTouches := tt4
bestViolations := v4
bestTangentX := tx4
bestTangentY := ty4
bestIsUpper := false
bestType := "CLOSE LOW -> HIGH"
//=====================================================================
// 12. OBJETS GRAPHIQUES
//=====================================================================
var line upperObject = na
var line lowerObject = na
var line medianObject = na
var linefill channelFill = na
var label infoLabel = na
var label anchor1Label = na
var label anchor2Label = na
var label tangentLabel = na
//=====================================================================
// 13. SUPPRESSION ANCIENS OBJETS
//=====================================================================
if barstate.islast
if not na(upperObject)
line.delete(upperObject)
if not na(lowerObject)
line.delete(lowerObject)
if not na(medianObject)
line.delete(medianObject)
if not na(channelFill)
linefill.delete(channelFill)
if not na(infoLabel)
label.delete(infoLabel)
if not na(anchor1Label)
label.delete(anchor1Label)
if not na(anchor2Label)
label.delete(anchor2Label)
if not na(tangentLabel)
label.delete(tangentLabel)
upperObject := na
lowerObject := na
medianObject := na
channelFill := na
infoLabel := na
anchor1Label := na
anchor2Label := na
tangentLabel := na
//=====================================================================
// 14. DESSIN
//=====================================================================
if barstate.islast and not na(bestSlope) and bestScore >= scoreMinimum
int rightX = bar_index + projectionBars
float baseStart = bestSlope * float(bestStartX) + bestIntercept
float baseRight = bestSlope * float(rightX) + bestIntercept
float upperStart = bestIsUpper ? baseStart : baseStart + bestOffset
float upperRight = bestIsUpper ? baseRight : baseRight + bestOffset
float lowerStart = bestIsUpper ? baseStart + bestOffset : baseStart
float lowerRight = bestIsUpper ? baseRight + bestOffset : baseRight
float medianStart = (upperStart + lowerStart) / 2.0
float medianRight = (upperRight + lowerRight) / 2.0
// -------------------------------------------------------------
// CANAL
// -------------------------------------------------------------
upperObject := line.new(bestStartX, upperStart, rightX, upperRight, xloc=xloc.bar_index, extend=extend.none, color=color.orange, width=2)
lowerObject := line.new(bestStartX, lowerStart, rightX, lowerRight, xloc=xloc.bar_index, extend=extend.none, color=color.orange, width=2)
// -------------------------------------------------------------
// MEDIANE
// -------------------------------------------------------------
if showMedian
medianObject := line.new(bestStartX, medianStart, rightX, medianRight, xloc=xloc.bar_index, extend=extend.none, color=color.new(color.orange, 65), style=line.style_dashed, width=1)
// -------------------------------------------------------------
// REMPLISSAGE
// -------------------------------------------------------------
if showFill
channelFill := linefill.new(upperObject, lowerObject, color.new(color.orange, 92))
// -------------------------------------------------------------
// A1 / A2 / T
// -------------------------------------------------------------
if showAnchors
anchor1Label := label.new(bestStartX, bestAnchor1Y, "A1", xloc=xloc.bar_index, style=bestIsUpper ? label.style_label_down : label.style_label_up, color=color.orange, textcolor=color.white, size=size.tiny)
anchor2Label := label.new(bestAnchor2X, bestAnchor2Y, "A2", xloc=xloc.bar_index, style=bestIsUpper ? label.style_label_down : label.style_label_up, color=color.orange, textcolor=color.white, size=size.tiny)
if not na(bestTangentX)
tangentLabel := label.new(bestTangentX, bestTangentY, "T", xloc=xloc.bar_index, style=bestIsUpper ? label.style_label_up : label.style_label_down, color=color.blue, textcolor=color.white, size=size.tiny)
// -------------------------------------------------------------
// INFORMATIONS
// -------------------------------------------------------------
if showScore
float atrNow = nz(atr, syminfo.mintick)
float normalizedSlope = bestSlope * 100.0 / atrNow
string direction = math.abs(normalizedSlope) < flatATRper100 ? "FLAT" : bestSlope > 0 ? "UP" : "DOWN"
float widthATR = math.abs(bestOffset) / atrNow
string txt = "PERFECT CHANNEL V3.1"
txt += "\n" + bestType + " | " + direction
txt += "\nScore : " + str.tostring(bestScore, "#") + " / 100"
txt += "\nContacts directeurs : " + str.tostring(bestAnchorTouches)
txt += "\nContacts translation : " + str.tostring(bestTranslatedTouches)
txt += "\nViolations : " + str.tostring(bestViolations)
txt += "\nLargeur : " + str.tostring(widthATR, "#.00") + " ATR"
float currentBase = bestSlope * float(bar_index) + bestIntercept
float labelY = bestIsUpper ? currentBase : currentBase + bestOffset
infoLabel := label.new(bar_index, labelY, txt, xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.black, 15), textcolor=color.white, size=size.small)