本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="engulf swing HL", shorttitle="GURU", overlay=true)
// ============================================================
// MASTER TOGGLES
// ============================================================
enableEngulfing = input.bool(false, "Enable Engulfing Candles", group="Master Toggles",
tooltip="Show Engulfing triangles on the price chart.")
enableSwingGuide = input.bool(true, "Enable Swing Guide", group="Master Toggles",
tooltip="Show Swing Guide lines, labels and alerts. Table is controlled by its own Show Dashboard input.")
// ============================================================
// ENGULFING CANDLES (price chart)
// ============================================================
openBarPrevious = open[1]
closeBarPrevious = close[1]
openBarCurrent = open
closeBarCurrent = close
bullishEngulfing = openBarCurrent <= closeBarPrevious and openBarCurrent < openBarPrevious and closeBarCurrent > openBarPrevious
bearishEngulfing = openBarCurrent >= closeBarPrevious and openBarCurrent > openBarPrevious and closeBarCurrent < openBarPrevious
plotshape(bullishEngulfing and enableEngulfing,
style=shape.triangleup, location=location.belowbar,
color=color.new(color.green, 0), size=size.tiny)
plotshape(bearishEngulfing and enableEngulfing,
style=shape.triangledown, location=location.abovebar,
color=color.new(color.red, 0), size=size.tiny)
alertcondition(bullishEngulfing and enableEngulfing, title="Bullish Engulfing",
message="[CurrencyPair] [TimeFrame], Bullish candle engulfing previous candle")
alertcondition(bearishEngulfing and enableEngulfing, title="Bearish Engulfing",
message="[CurrencyPair] [TimeFrame], Bearish candle engulfing previous candle")
// Backtest range inputs (strategy calls stay commented out)
FromMonth = input.int(defval=1, title="From Month", minval=1, maxval=12)
FromDay = input.int(defval=1, title="From Day", minval=1, maxval=31)
FromYear = input.int(defval=2017, title="From Year", minval=2017)
ToMonth = input.int(defval=1, title="To Month", minval=1, maxval=12)
ToDay = input.int(defval=1, title="To Day", minval=1, maxval=31)
ToYear = input.int(defval=9999, title="To Year", minval=2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
window() =>
time >= start and time <= finish ? true : false
// strategy.entry("L", strategy.long, 25000, when=bullishEngulfing == 1 and window())
// strategy.exit("exit", "L", profit=1000, loss=50)
// strategy.entry("S", strategy.short, 25000, when=bearishEngulfing == 1 and window())
// strategy.exit("exit", "S", profit=1000, loss=50)
// ============================================================
// SWING GUIDE (price chart)
// ============================================================
groupCalc = "Swing"
groupVisual = "Appearance"
groupTable = "Dashboard"
sensitivity = input.int(12, "Swing Sensitivity", minval=1, step=1, group=groupCalc,
tooltip="How big a move must be to count as a new swing leg.")
repaint = input.bool(true, "Repaint Forming Leg", group=groupCalc,
tooltip="On: last leg updates live. Off: leg drawn only once confirmed.")
bullColor = input.color(#00C896, "Bullish Structure", inline="col", group=groupVisual,
tooltip="Color for up legs and bullish structure points: High+ / Low+.")
bearColor = input.color(#E04B52, "Bearish Structure", inline="col", group=groupVisual,
tooltip="Color for down legs and bearish structure points: High- / Low-.")
labelTransparency = input.int(35, "Structure Label Transparency", minval=0, maxval=90, group=groupVisual)
showSwing = input.bool(true, "Show Swing Line", group=groupVisual)
showStructLabels = input.bool(true, "Show Structure Labels", group=groupVisual)
showTable = input.bool(true, "Show Dashboard", group=groupTable)
dashPos = input.string("Top Right", "Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=groupTable)
warnColor = #E0A800
neutColor = #6B7280
textColor = color.white
tableBg = color.new(#0F131A, 5)
headerBg = color.new(#1A1F2B, 0)
leftColBg = color.new(#11161D, 0)
borderColor = color.new(color.white, 65)
frameColor = color.new(color.white, 55)
// Helpers
f_lab(string p) =>
p == "HH" ? "High+" : p == "HL" ? "Low+" : p == "LH" ? "High-" : "Low-"
f_pos(string s) =>
s == "Top Left" ? position.top_left : s == "Bottom Right" ? position.bottom_right : s == "Bottom Left" ? position.bottom_left : position.top_right
f_now(int t, string p) =>
r = "Waiting"
if t == 1 and p == "HH"
r := "Uptrend"
else if t == 1 and p == "HL"
r := "Healthy Pullback"
else if t == 1 and p == "LH"
r := "Possible Reversal Down"
else if t == -1 and p == "LL"
r := "Downtrend"
else if t == -1 and p == "LH"
r := "Weak Bounce"
else if t == -1 and p == "HL"
r := "Possible Reversal Up"
r
f_structure(int t, string p) =>
r = "Waiting"
if t == 1 and (p == "HH" or p == "HL")
r := "Bullish"
else if t == -1 and (p == "LL" or p == "LH")
r := "Bearish"
else if t == 1 and p == "LH"
r := "Bearish Shift"
else if t == -1 and p == "HL"
r := "Bullish Shift"
r
f_lastpoint(string p) =>
r = "Waiting"
if p == "HH"
r := "New High"
else if p == "HL"
r := "Higher Low"
else if p == "LH"
r := "Lower High"
else if p == "LL"
r := "New Low"
r
f_bias(int t, string p) =>
r = "Wait"
if t == 1 and p != "LH"
r := "Long Preferred"
else if t == -1 and p != "HL"
r := "Short Preferred"
r
f_risk(int t, string p) =>
r = "No Clear Structure"
if t == 1 and p == "HH"
r := "Trend Extension"
else if t == 1 and p == "HL"
r := "Pullback Zone"
else if t == -1 and p == "LL"
r := "Breakdown"
else if t == -1 and p == "LH"
r := "Weak Recovery"
r
f_cellColor(string s) =>
c = color.new(neutColor, 25)
if s == "Uptrend" or s == "Healthy Pullback" or s == "Bullish" or s == "New High" or s == "Higher Low" or s == "Long Preferred" or s == "Pullback Zone"
c := color.new(bullColor, 15)
else if s == "Downtrend" or s == "Weak Bounce" or s == "Bearish" or s == "New Low" or s == "Lower High" or s == "Short Preferred" or s == "Breakdown" or s == "Weak Recovery"
c := color.new(bearColor, 15)
else if s == "Possible Reversal Up" or s == "Possible Reversal Down" or s == "Bullish Shift" or s == "Bearish Shift" or s == "Trend Extension"
c := color.new(warnColor, 15)
c
// Calculations
deviation = 5
backstep = 2
highRun = ta.barssince(not (ta.highest(high, sensitivity) - high > deviation * syminfo.mintick)[1])
lowRun = ta.barssince(not (low - ta.lowest(low, sensitivity) > deviation * syminfo.mintick)[1])
swingDir = ta.barssince(not (highRun > lowRun)) >= backstep ? -1 : 1
flip = ta.change(swingDir) != 0
var float extremePrice = low
var int extremeBar = bar_index
var float pivotPrice = low
var int pivotBar = bar_index
var float tipPrice = high
var int tipBar = bar_index
var float prevHigh = na
var float prevLow = na
var string lastPivotType = na
var int trendState = 0
if flip
pivotPrice := tipPrice
pivotBar := tipBar
tipPrice := extremePrice
tipBar := extremeBar
if swingDir > 0
lowType = na(prevLow) or pivotPrice < prevLow ? "LL" : "HL"
lastPivotType := lowType
prevLow := pivotPrice
if lowType == "LL"
trendState := -1
else
highType = na(prevHigh) or pivotPrice > prevHigh ? "HH" : "LH"
lastPivotType := highType
prevHigh := pivotPrice
if highType == "HH"
trendState := 1
if swingDir > 0
if high > tipPrice
tipPrice := high
tipBar := bar_index
extremePrice := low
extremeBar := bar_index
if low < extremePrice
extremePrice := low
extremeBar := bar_index
else
if low < tipPrice
tipPrice := low
tipBar := bar_index
extremePrice := high
extremeBar := bar_index
if high > extremePrice
extremePrice := high
extremeBar := bar_index
// Signal engine
tipType = swingDir > 0 ? (tipPrice > prevHigh ? "HH" : "LH") : (tipPrice < prevLow ? "LL" : "HL")
bullFlip = trendState == 1 and trendState[1] != 1
bearFlip = trendState == -1 and trendState[1] != -1
// Visual state (respects master toggle)
isBullTip = tipType == "HH" or tipType == "HL"
legColor = color.new(swingDir > 0 ? bullColor : bearColor, enableSwingGuide ? 0 : 100)
labelBg = color.new(isBullTip ? bullColor : bearColor, enableSwingGuide ? labelTransparency : 100)
textColorVis = enableSwingGuide ? textColor : color.new(textColor, 100)
labelStyle = swingDir > 0 ? label.style_label_down : label.style_label_up
// Plots
line swingLine = na
label swingLabel = na
if enableSwingGuide and showSwing and repaint
swingLine := line.new(pivotBar, pivotPrice, tipBar, tipPrice, xloc.bar_index,
color=legColor, width=2)
if showStructLabels
swingLabel := label.new(tipBar, tipPrice, f_lab(tipType), xloc.bar_index, yloc.price,
color=labelBg, style=labelStyle, textcolor=textColorVis, size=size.small)
if not flip
line.delete(swingLine[1])
label.delete(swingLabel[1])
if enableSwingGuide and showSwing and not repaint
if flip
isBullTipPrev = tipType[1] == "HH" or tipType[1] == "HL"
legColorPrev = color.new(swingDir[1] > 0 ? bullColor : bearColor, 0)
labelBgPrev = color.new(isBullTipPrev ? bullColor : bearColor, labelTransparency)
labelStylePrev = swingDir[1] > 0 ? label.style_label_down : label.style_label_up
swingLine := line.new(pivotBar[1], pivotPrice[1], tipBar[1], tipPrice[1], xloc.bar_index,
color=legColorPrev, width=2)
if showStructLabels
swingLabel := label.new(tipBar[1], tipPrice[1], f_lab(tipType[1]), xloc.bar_index, yloc.price,
color=labelBgPrev, style=labelStylePrev, textcolor=textColor, size=size.small)
// Alerts
tipMoved = tipPrice != tipPrice[1]
alertcondition(tipType == "HH" and tipMoved and enableSwingGuide, "New Higher High",
"Swing Guide {{ticker}}: Higher High (High+) at {{time}}")
alertcondition(tipType == "LH" and tipMoved and enableSwingGuide, "New Lower High",
"Swing Guide {{ticker}}: Lower High (High-) at {{time}}")
alertcondition(tipType == "HL" and tipMoved and enableSwingGuide, "New Higher Low",
"Swing Guide {{ticker}}: Higher Low (Low+) at {{time}}")
alertcondition(tipType == "LL" and tipMoved and enableSwingGuide, "New Lower Low",
"Swing Guide {{ticker}}: Lower Low (Low-) at {{time}}")
alertcondition(flip and enableSwingGuide, "Direction Changed",
"Swing Guide {{ticker}}: swing direction changed at {{time}}")
alertcondition(bullFlip and enableSwingGuide, "Bullish Structure",
"Swing Guide {{ticker}}: Bullish structure (Higher High) at {{time}}")
alertcondition(bearFlip and enableSwingGuide, "Bearish Structure",
"Swing Guide {{ticker}}: Bearish structure (Lower Low) at {{time}}")
if bullFlip and enableSwingGuide
alert("Swing Guide: Bullish structure (Higher High)", alert.freq_once_per_bar_close)
if bearFlip and enableSwingGuide
alert("Swing Guide: Bearish structure (Lower Low)", alert.freq_once_per_bar_close)
// Table (now on price chart because overlay=true)
if showTable and barstate.islast
nowText = f_now(trendState, lastPivotType)
structText = f_structure(trendState, lastPivotType)
pointText = f_lastpoint(lastPivotType)
biasText = f_bias(trendState, lastPivotType)
riskText = f_risk(trendState, lastPivotType)
var table proTable = table.new(f_pos(dashPos), 2, 6,
bgcolor=tableBg, border_width=1, border_color=borderColor,
frame_color=frameColor, frame_width=1)
table.cell(proTable, 0, 0, "Swing Guide", text_color=textColor, bgcolor=headerBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 0, "Status", text_color=textColor, bgcolor=headerBg, text_halign=text.align_center, text_size=size.small)
table.cell(proTable, 0, 1, "Now", text_color=textColor, bgcolor=leftColBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 1, nowText, text_color=textColor, bgcolor=f_cellColor(nowText), text_halign=text.align_center, text_size=size.small)
table.cell(proTable, 0, 2, "Structure", text_color=textColor, bgcolor=leftColBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 2, structText, text_color=textColor, bgcolor=f_cellColor(structText), text_halign=text.align_center, text_size=size.small)
table.cell(proTable, 0, 3, "Last Point", text_color=textColor, bgcolor=leftColBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 3, pointText, text_color=textColor, bgcolor=f_cellColor(pointText), text_halign=text.align_center, text_size=size.small)
table.cell(proTable, 0, 4, "Bias", text_color=textColor, bgcolor=leftColBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 4, biasText, text_color=textColor, bgcolor=f_cellColor(biasText), text_halign=text.align_center, text_size=size.small)
table.cell(proTable, 0, 5, "Risk", text_color=textColor, bgcolor=leftColBg, text_halign=text.align_left, text_size=size.small)
table.cell(proTable, 1, 5, riskText, text_color=textColor, bgcolor=f_cellColor(riskText), text_halign=text.align_center, text_size=size.small)