本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("GANN Universal Clean | Colored Candlestick Patterns V3.1.3", shorttitle="GANN Clean V3.1.3", overlay=true, behind_chart=false, max_lines_count=450, max_boxes_count=450, max_labels_count=300)
//====================================================
// GANN UNIVERSAL CLEAN V3.1.3
// Visual-only indicator | Pine Script v6
// Square of 9 + 12 Candlestick Patterns
// No trading signals or orders
//====================================================
//====================================================
// 01 | MAIN CHART CANDLE APPEARANCE
//====================================================
g01 = "01 | Main Chart Candles"
useBW = input.bool(true, "Use Black & White Candles", group=g01)
chartBull = input.color(color.white, "Bullish Candle - White", group=g01)
chartBear = input.color(color.black, "Bearish Candle - Black", group=g01)
chartBorder = input.color(color.black, "Candle Border and Wicks", group=g01)
// Color original price candles.
barcolor(useBW ? (close >= open ? chartBull : chartBear) : na)
// Draw black/white candles over original chart.
// The indicator uses behind_chart=false.
plotcandle(
useBW ? open : na,
useBW ? high : na,
useBW ? low : na,
useBW ? close : na,
title="Black and White Price Candles",
color=close >= open ? chartBull : chartBear,
wickcolor=chartBorder,
bordercolor=chartBorder)
//====================================================
// 02 | GANN LEVEL DISPLAY
//====================================================
g02 = "02 | Gann Square of 9"
gridMode = input.string("Full Chart", "Level Extension", options=["Full Chart", "From Confirmation"], group=g02)
showLowGrid = input.bool(true, "Show Swing Low Levels", group=g02)
showHighGrid = input.bool(true, "Show Swing High Levels", group=g02)
bothDirections = input.bool(true, "Project Both Directions", group=g02)
steps45 = input.int(8, "Gann Steps", minval=1, maxval=8, group=g02)
showLevelLabels = input.bool(true, "Show Angles and Prices", group=g02)
labelOffset = input.int(8, "Label Offset Bars", minval=1, maxval=50, group=g02)
//====================================================
// 03 | GANN DECLUTTER
//====================================================
g03 = "03 | Gann Chart Cleanup"
skipNearby = input.bool(true, "Skip Overlapping Levels", group=g03)
minimumGapATR = input.float(0.8, "Minimum Level Gap ATR", minval=0.05, step=0.05, group=g03)
majorOnly = input.bool(true, "Show Major Angles Only", group=g03)
showZero = input.bool(true, "Show Zero Degree Anchors", group=g03)
maxDistanceATR = input.float(45.0, "Maximum Level Distance ATR", minval=1, group=g03)
//====================================================
// 04 | UNIVERSAL SCALING
//====================================================
g04 = "04 | Universal Asset Scaling"
scaleMode = input.string("Auto ATR", "Scale Mode", options=["Auto ATR", "Auto Percent", "Fixed Gann"], group=g04)
atrLength = input.int(14, "ATR Period", minval=2, group=g04)
atrSpacing = input.float(1.5, "45 Degree ATR Spacing", minval=0.1, step=0.1, group=g04)
percentSpacing = input.float(0.25, "45 Degree Percent Spacing", minval=0.001, group=g04)
fixedScale = input.float(1.0, "Fixed Gann Scale", minval=0.000001, group=g04)
//====================================================
// 05 | SWING ANCHORS
//====================================================
g05 = "05 | Gann Anchor Detection"
anchorMode = input.string("Confirmed Swings", "Anchor Mode", options=["Confirmed Swings", "Manual"], group=g05)
pivotLeft = input.int(8, "Pivot Left", minval=2, maxval=50, group=g05)
pivotRight = input.int(8, "Pivot Right", minval=2, maxval=50, group=g05)
minSwingATR = input.float(1.0, "Minimum New Anchor ATR", minval=0, step=0.1, group=g05)
manualLow = input.float(0.0, "Manual Low Anchor", minval=0, group=g05)
manualHigh = input.float(0.0, "Manual High Anchor", minval=0, group=g05)
//====================================================
// 06 | GANN COLORS
//====================================================
g06 = "06 | Gann Colors"
lowColor = input.color(color.rgb(0, 150, 130), "Low Grid - Teal", group=g06)
highColor = input.color(color.rgb(240, 150, 35), "High Grid - Orange", group=g06)
majorTransparency = input.int(10, "Major Level Transparency", minval=0, maxval=100, group=g06)
minorTransparency = input.int(65, "Minor Level Transparency", minval=0, maxval=100, group=g06)
//====================================================
// 07 | SELECT CANDLESTICK PATTERNS
//====================================================
g07 = "07 | Candlestick Pattern Selection"
showPictures = input.bool(true, "Show Colored Pattern Pictures", group=g07)
showNames = input.bool(true, "Show Pattern Names", group=g07)
patternChoice = input.string(
"All Patterns",
"Pattern Selection",
options=[
"All Patterns",
"Bullish Only",
"Bearish Only",
"Bullish Engulfing",
"Hammer",
"Morning Star",
"Three White Soldiers",
"Piercing Line",
"Bullish Marubozu",
"Bearish Engulfing",
"Shooting Star",
"Evening Star",
"Three Black Crows",
"Dark Cloud Cover",
"Bearish Marubozu"
],
group=g07)
//====================================================
// 08 | PATTERN PICTURE SIZE / POSITION
//====================================================
g08 = "08 | Pattern Picture Placement"
pictureHeightATR = input.float(0.50, "Picture Height ATR", minval=0.15, maxval=3.0, step=0.05, group=g08)
pictureGapATR = input.float(0.75, "Distance From Pattern ATR", minval=0.15, maxval=4.0, step=0.05, group=g08)
bullCooldown = input.int(7, "Bullish Pattern Cooldown", minval=0, maxval=50, group=g08)
bearCooldown = input.int(7, "Bearish Pattern Cooldown", minval=0, maxval=50, group=g08)
maxPictures = input.int(40, "Historical Pattern Pictures", minval=5, maxval=100, group=g08)
//====================================================
// 09 | COLORED PATTERN PICTURES
//====================================================
g09 = "09 | Pattern Illustration Colors"
pictureGreen = input.color(color.rgb(16,185,129), "Green Candle Bodies", group=g09)
pictureRed = input.color(color.rgb(239,68,68), "Red Candle Bodies", group=g09)
greenBorder = input.color(color.rgb(5,110,75), "Green Candle Wicks / Borders", group=g09)
redBorder = input.color(color.rgb(170,35,45), "Red Candle Wicks / Borders", group=g09)
bullTextColor = input.color(color.rgb(0,150,100), "Bullish Pattern Name", group=g09)
bearTextColor = input.color(color.rgb(210,50,65), "Bearish Pattern Name", group=g09)
//====================================================
// 10 | INDIVIDUAL BULLISH PATTERNS
//====================================================
g10 = "10 | Bullish Patterns"
useBullEngulf = input.bool(true, "Bullish Engulfing", group=g10)
useHammer = input.bool(true, "Hammer", group=g10)
useMorning = input.bool(true, "Morning Star", group=g10)
useSoldiers = input.bool(true, "Three White Soldiers", group=g10)
usePiercing = input.bool(true, "Piercing Line", group=g10)
useBullMaru = input.bool(true, "Bullish Marubozu", group=g10)
//====================================================
// 11 | INDIVIDUAL BEARISH PATTERNS
//====================================================
g11 = "11 | Bearish Patterns"
useBearEngulf = input.bool(true, "Bearish Engulfing", group=g11)
useShooting = input.bool(true, "Shooting Star", group=g11)
useEvening = input.bool(true, "Evening Star", group=g11)
useCrows = input.bool(true, "Three Black Crows", group=g11)
useDarkCloud = input.bool(true, "Dark Cloud Cover", group=g11)
useBearMaru = input.bool(true, "Bearish Marubozu", group=g11)
//====================================================
// 12 | PATTERN QUALITY
//====================================================
g12 = "12 | Pattern Detection Quality"
minPatternATR = input.float(0.25, "Minimum Pattern Candle Range ATR", minval=0, step=0.05, group=g12)
//====================================================
// 13 | BASE SERIES
//====================================================
atr = ta.atr(atrLength)
cRange = math.max(high - low, syminfo.mintick)
body = math.abs(close - open)
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low
bull = close > open
bear = close < open
bodyRatio = body / cRange
prevBody = math.abs(close[1] - open[1])
prevRange = math.max(high[1] - low[1], syminfo.mintick)
thirdBody = math.abs(close[2] - open[2])
thirdRange = math.max(high[2] - low[2], syminfo.mintick)
quality = not na(atr) and cRange >= atr * minPatternATR
//====================================================
// 14 | GANN SQUARE OF 9 MATHEMATICS
//====================================================
f_scale(float anchor, float anchorATR) =>
float result = fixedScale
if scaleMode != "Fixed Gann" and anchor > 0 and not na(anchorATR)
float spacing = scaleMode == "Auto ATR" ? anchorATR * atrSpacing : anchor * percentSpacing / 100.0
spacing := math.max(spacing, syminfo.mintick)
float delta = math.sqrt(anchor + spacing) - math.sqrt(anchor)
if delta > 0
result := math.pow(0.25 / delta, 2)
result
f_gann(float anchor, int signedStep, float scale) =>
float result = na
if not na(anchor) and anchor > 0 and not na(scale) and scale > 0
float adjustedRoot = math.sqrt(anchor * scale) + signedStep * 0.25
if adjustedRoot > 0
result := adjustedRoot * adjustedRoot / scale
result
f_major(int step) =>
step == 0 or step == 2 or step == 4 or step == 6 or step == 8
//====================================================
// 15 | GANN DRAWING STORAGE
//====================================================
var array<line> gannLines = array.new_line()
var array<label> gannLabels = array.new_label()
var array<float> activeLevels = array.new_float()
var float lastLowAnchor = na
var float lastHighAnchor = na
var float lastLowScale = na
var float lastHighScale = na
var int lastLowBar = na
var int lastHighBar = na
var bool manualReady = false
//====================================================
// 16 | CLEAR CURRENT GANN LEVELS
//====================================================
f_clearGann() =>
while array.size(gannLines) > 0
line.delete(array.shift(gannLines))
while array.size(gannLabels) > 0
label.delete(array.shift(gannLabels))
array.clear(activeLevels)
//====================================================
// 17 | SAFE OVERLAPPING LEVEL FILTER
//====================================================
f_acceptLevel(float level) =>
bool accepted = not na(level)
if accepted and skipNearby and array.size(activeLevels) > 0
for existingLevel in activeLevels
if math.abs(existingLevel - level) <= atr * minimumGapATR
accepted := false
accepted
//====================================================
// 18 | BUILD GANN LEVELS
//====================================================
f_drawGann(float anchor, float scale, int anchorBar, bool fromLow) =>
if not na(anchor) and anchor > 0 and not na(scale) and scale > 0
color gridColor = fromLow ? lowColor : highColor
string prefix = fromLow ? "L" : "H"
int originBar = na(anchorBar) ? bar_index : anchorBar
for pass = 0 to 1
if pass == 0 or bothDirections
int direction = pass == 0 ? (fromLow ? 1 : -1) : (fromLow ? -1 : 1)
for step = 0 to steps45
bool allowed = (step > 0 or showZero) and (not majorOnly or f_major(step))
// Do not draw the zero-degree anchor twice.
if pass == 1 and step == 0
allowed := false
if allowed
int signedStep = step * direction
float level = f_gann(anchor, signedStep, scale)
bool inRange = not na(level) and math.abs(level - anchor) <= atr * maxDistanceATR
if inRange and f_acceptLevel(level)
bool major = f_major(step)
string signText = signedStep > 0 ? "+" : signedStep < 0 ? "-" : ""
line ln = line.new(
x1=originBar,
y1=level,
x2=originBar + 1,
y2=level,
xloc=xloc.bar_index,
extend=gridMode == "Full Chart" ? extend.both : extend.right,
color=color.new(gridColor, major ? majorTransparency : minorTransparency),
width=major ? 2 : 1,
style=step == 0 ? line.style_dashed : major ? line.style_solid : line.style_dotted)
array.push(gannLines, ln)
array.push(activeLevels, level)
if showLevelLabels
string txt = prefix + signText + str.tostring(step * 45) + "° | " + str.tostring(level, format.mintick)
label lb = label.new(
x=bar_index + labelOffset,
y=level,
text=txt,
xloc=xloc.bar_index,
yloc=yloc.price,
style=label.style_none,
textcolor=gridColor,
size=size.tiny)
array.push(gannLabels, lb)
//====================================================
// 19 | REDRAW CLEAN GANN GRID
//====================================================
f_redrawGann() =>
f_clearGann()
if showLowGrid
f_drawGann(lastLowAnchor, lastLowScale, lastLowBar, true)
if showHighGrid
f_drawGann(lastHighAnchor, lastHighScale, lastHighBar, false)
//====================================================
// 20 | CONFIRMED SWING DETECTION
//====================================================
pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)
pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
bool redrawGann = false
if barstate.isconfirmed and not na(atr)
if anchorMode == "Confirmed Swings"
if not na(pivotLow)
bool acceptLow = na(lastLowAnchor) or math.abs(pivotLow - lastLowAnchor) >= atr * minSwingATR
if acceptLow
lastLowAnchor := pivotLow
lastLowScale := f_scale(pivotLow, atr)
lastLowBar := bar_index
redrawGann := true
if not na(pivotHigh)
bool acceptHigh = na(lastHighAnchor) or math.abs(pivotHigh - lastHighAnchor) >= atr * minSwingATR
if acceptHigh
lastHighAnchor := pivotHigh
lastHighScale := f_scale(pivotHigh, atr)
lastHighBar := bar_index
redrawGann := true
else
if not manualReady
if manualLow > 0
lastLowAnchor := manualLow
lastLowScale := f_scale(manualLow, atr)
lastLowBar := bar_index
if manualHigh > 0
lastHighAnchor := manualHigh
lastHighScale := f_scale(manualHigh, atr)
lastHighBar := bar_index
manualReady := true
redrawGann := true
if redrawGann
f_redrawGann()
if barstate.islast and array.size(gannLabels) > 0
for lb in gannLabels
label.set_x(lb, bar_index + labelOffset)
//====================================================
// 21 | SIX BULLISH PATTERNS
//====================================================
bullEngulf = bull and bear[1] and open <= close[1] and close >= open[1] and body >= prevBody * 0.8
hammer = lowerWick >= math.max(body, syminfo.mintick) * 2.0 and upperWick <= cRange * 0.2 and math.max(open, close) >= low + cRange * 0.65
morning = bear[2] and thirdBody / thirdRange > 0.5 and prevBody < thirdBody * 0.5 and bull and close > (open[2] + close[2]) / 2.0
soldiers = bull and bull[1] and bull[2] and close > close[1] and close[1] > close[2] and bodyRatio > 0.5 and prevBody / prevRange > 0.5
piercing = bear[1] and bull and open <= close[1] and close > (open[1] + close[1]) / 2.0 and close < open[1]
bullMaru = bull and bodyRatio >= 0.85 and upperWick <= cRange * 0.1 and lowerWick <= cRange * 0.1
//====================================================
// 22 | SIX BEARISH PATTERNS
//====================================================
bearEngulf = bear and bull[1] and open >= close[1] and close <= open[1] and body >= prevBody * 0.8
shooting = upperWick >= math.max(body, syminfo.mintick) * 2.0 and lowerWick <= cRange * 0.2 and math.min(open, close) <= low + cRange * 0.35
evening = bull[2] and thirdBody / thirdRange > 0.5 and prevBody < thirdBody * 0.5 and bear and close < (open[2] + close[2]) / 2.0
crows = bear and bear[1] and bear[2] and close < close[1] and close[1] < close[2] and bodyRatio > 0.5 and prevBody / prevRange > 0.5
darkCloud = bull[1] and bear and open >= close[1] and close < (open[1] + close[1]) / 2.0 and close > open[1]
bearMaru = bear and bodyRatio >= 0.85 and upperWick <= cRange * 0.1 and lowerWick <= cRange * 0.1
//====================================================
// 23 | PATTERN NAME LOOKUP
//====================================================
f_patternName(int id) =>
switch id
1 => "Bullish Engulfing"
2 => "Hammer"
3 => "Morning Star"
4 => "Three White Soldiers"
5 => "Piercing Line"
6 => "Bullish Marubozu"
7 => "Bearish Engulfing"
8 => "Shooting Star"
9 => "Evening Star"
10 => "Three Black Crows"
11 => "Dark Cloud Cover"
12 => "Bearish Marubozu"
=> ""
//====================================================
// 24 | DIRECT INDIVIDUAL PATTERN DETECTION
//====================================================
f_patternDetected(int id) =>
switch id
1 => useBullEngulf and bullEngulf
2 => useHammer and hammer
3 => useMorning and morning
4 => useSoldiers and soldiers
5 => usePiercing and piercing
6 => useBullMaru and bullMaru
7 => useBearEngulf and bearEngulf
8 => useShooting and shooting
9 => useEvening and evening
10 => useCrows and crows
11 => useDarkCloud and darkCloud
12 => useBearMaru and bearMaru
=> false
//====================================================
// 25 | PATTERN SELECTION
//====================================================
int selectedID = switch patternChoice
"Bullish Engulfing" => 1
"Hammer" => 2
"Morning Star" => 3
"Three White Soldiers" => 4
"Piercing Line" => 5
"Bullish Marubozu" => 6
"Bearish Engulfing" => 7
"Shooting Star" => 8
"Evening Star" => 9
"Three Black Crows" => 10
"Dark Cloud Cover" => 11
"Bearish Marubozu" => 12
=> 0
int chosenBull = 0
int chosenBear = 0
if quality
if selectedID > 0
if f_patternDetected(selectedID)
if selectedID <= 6
chosenBull := selectedID
else
chosenBear := selectedID
else
bool scanBull = patternChoice == "All Patterns" or patternChoice == "Bullish Only"
bool scanBear = patternChoice == "All Patterns" or patternChoice == "Bearish Only"
// One priority pattern per direction per bar.
if scanBull
if f_patternDetected(1)
chosenBull := 1
else if f_patternDetected(3)
chosenBull := 3
else if f_patternDetected(2)
chosenBull := 2
else if f_patternDetected(4)
chosenBull := 4
else if f_patternDetected(5)
chosenBull := 5
else if f_patternDetected(6)
chosenBull := 6
if scanBear
if f_patternDetected(7)
chosenBear := 7
else if f_patternDetected(9)
chosenBear := 9
else if f_patternDetected(8)
chosenBear := 8
else if f_patternDetected(10)
chosenBear := 10
else if f_patternDetected(11)
chosenBear := 11
else if f_patternDetected(12)
chosenBear := 12
//====================================================
// 26 | PICTURE OBJECT STORAGE
//====================================================
var array<line> pictureWicks = array.new_line()
var array<box> pictureBodies = array.new_box()
var array<label> pictureNames = array.new_label()
var array<int> pictureCounts = array.new_int()
var int lastBullPictureBar = na
var int lastBearPictureBar = na
//====================================================
// 27 | MINIATURE COLORED CANDLESTICK PICTURE
//====================================================
f_drawPicture(int id) =>
bool bullishFormation = id <= 6
int count = switch id
1 => 2
3 => 3
4 => 3
5 => 2
7 => 2
9 => 3
10 => 3
11 => 2
=> 1
// Determine the full formation's highest/lowest.
float patternHigh = high
float patternLow = low
for i = 0 to count - 1
patternHigh := math.max(patternHigh, high[i])
patternLow := math.min(patternLow, low[i])
float formationRange = math.max(patternHigh - patternLow, syminfo.mintick)
// Bullish illustrations go entirely BELOW the formation.
// Bearish illustrations go entirely ABOVE the formation.
float pictureBottom = bullishFormation ? patternLow - atr * (pictureGapATR + pictureHeightATR) : patternHigh + atr * pictureGapATR
float pictureTop = bullishFormation ? patternLow - atr * pictureGapATR : patternHigh + atr * (pictureGapATR + pictureHeightATR)
float displayRange = pictureTop - pictureBottom
for i = 0 to count - 1
int lag = count - 1 - i
// Compact drawing positions beside the real formation.
int cx = bar_index - 2 * (count - 1 - i)
float candleO = open[lag]
float candleH = high[lag]
float candleL = low[lag]
float candleC = close[lag]
float yO = pictureBottom + (candleO - patternLow) / formationRange * displayRange
float yH = pictureBottom + (candleH - patternLow) / formationRange * displayRange
float yL = pictureBottom + (candleL - patternLow) / formationRange * displayRange
float yC = pictureBottom + (candleC - patternLow) / formationRange * displayRange
bool candleBull = candleC >= candleO
color bodyColor = candleBull ? pictureGreen : pictureRed
color borderColor = candleBull ? greenBorder : redBorder
// Colored wick.
line wick = line.new(
x1=cx,
y1=yL,
x2=cx,
y2=yH,
xloc=xloc.bar_index,
color=borderColor,
width=2)
float bodyTop = math.max(yO, yC)
float bodyBottom = math.min(yO, yC)
float minimumBody = atr * pictureHeightATR * 0.06
if bodyTop - bodyBottom < minimumBody
float mid = (bodyTop + bodyBottom) / 2.0
bodyTop := mid + minimumBody / 2.0
bodyBottom := mid - minimumBody / 2.0
// Colored body.
box candleBox = box.new(
left=cx - 1,
top=bodyTop,
right=cx,
bottom=bodyBottom,
xloc=xloc.bar_index,
border_color=borderColor,
border_width=1,
bgcolor=bodyColor)
array.push(pictureWicks, wick)
array.push(pictureBodies, candleBox)
// Colored pattern name.
label nameLabel = na
if showNames
float nameY = bullishFormation ? pictureBottom - atr * 0.20 : pictureTop + atr * 0.20
int nameX = bar_index - (count - 1)
nameLabel := label.new(
x=nameX,
y=nameY,
text=f_patternName(id),
xloc=xloc.bar_index,
yloc=yloc.price,
style=label.style_none,
textcolor=bullishFormation ? bullTextColor : bearTextColor,
size=size.tiny)
array.push(pictureNames, nameLabel)
array.push(pictureCounts, count)
//====================================================
// 28 | INDEPENDENT BULLISH / BEARISH DISPLAY
//====================================================
bool bullReady = na(lastBullPictureBar) or bar_index - lastBullPictureBar > bullCooldown
bool bearReady = na(lastBearPictureBar) or bar_index - lastBearPictureBar > bearCooldown
if barstate.isconfirmed and showPictures and not na(atr)
// Bullish patterns.
if chosenBull > 0 and bullReady and bar_index >= 5
f_drawPicture(chosenBull)
lastBullPictureBar := bar_index
// Bearish patterns independently.
if chosenBear > 0 and bearReady and bar_index >= 5
f_drawPicture(chosenBear)
lastBearPictureBar := bar_index
//====================================================
// 29 | SAFE HISTORY MANAGEMENT
//====================================================
while array.size(pictureCounts) > maxPictures
int oldCount = array.shift(pictureCounts)
for i = 0 to oldCount - 1
if array.size(pictureWicks) > 0
line.delete(array.shift(pictureWicks))
if array.size(pictureBodies) > 0
box.delete(array.shift(pictureBodies))
if array.size(pictureNames) > 0
label oldName = array.shift(pictureNames)
if not na(oldName)
label.delete(oldName)
//====================================================
// 30 | DATA WINDOW
//====================================================
plot(lastLowAnchor, "Current Gann Low Anchor", display=display.data_window)
plot(lastHighAnchor, "Current Gann High Anchor", display=display.data_window)
// END | GANN UNIVERSAL CLEAN V3.1.3