Support/R, Trend, Ema, Fvg,

GreenBoyz · study · 328 行 · 点赞 648 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("SMA Trend + Major S/R + Trendlines + FVG Entries",
     overlay=true, max_lines_count=100, max_boxes_count=100)
 
// ══════════════════════════════════════
// INPUTS
// ══════════════════════════════════════
fastLen = input.int(9, "Fast SMA", minval=1, group="SMAs")
midLen  = input.int(21, "Middle SMA", minval=1, group="SMAs")
slowLen = input.int(100, "Slow SMA", minval=1, group="SMAs")
 
fastCol = input.color(color.aqua, "Fast SMA color", group="SMAs")
midCol  = input.color(color.orange, "Middle SMA color", group="SMAs")
slowCol = input.color(color.purple, "Slow SMA color", group="SMAs")
 
paintBars = input.bool(true, "Color candles", group="Trend")
bullCol = input.color(color.lime, "Bullish color", group="Trend")
bearCol = input.color(color.red, "Bearish color", group="Trend")
flatCol = input.color(color.gray, "Neutral color", group="Trend")
 
showSR = input.bool(true, "Show major levels", group="Major Support / Resistance")
majorLen = input.int(15, "Major pivot strength", minval=2, maxval=100,
     group="Major Support / Resistance")
keepLevels = input.int(6, "Levels to keep per side", minval=1, maxval=20,
     group="Major Support / Resistance")
supCol = input.color(color.green, "Support color",
     group="Major Support / Resistance")
resCol = input.color(color.red, "Resistance color",
     group="Major Support / Resistance")
 
showTL = input.bool(true, "Show auto trendlines", group="Auto Trendlines")
trendLen = input.int(5, "Trendline pivot strength", minval=2, maxval=100,
     group="Auto Trendlines")
upTLCol = input.color(color.teal, "Low-to-low line color",
     group="Auto Trendlines")
downTLCol = input.color(color.fuchsia, "High-to-high line color",
     group="Auto Trendlines")
 
showFVG = input.bool(true, "Show fair value gaps", group="Fair Value Gaps")
minGapATR = input.float(0.10, "Minimum gap size / ATR",
     minval=0.0, step=0.05, group="Fair Value Gaps")
maxGaps = input.int(20, "Maximum active gaps", minval=1, maxval=50,
     group="Fair Value Gaps")
bullGapCol = input.color(color.teal, "Bullish gap color",
     group="Fair Value Gaps")
bearGapCol = input.color(color.maroon, "Bearish gap color",
     group="Fair Value Gaps")
 
showEntry = input.bool(true, "Highlight potential entry zone",
     group="Entry Zones")
longEntryCol = input.color(color.lime, "Long zone color",
     group="Entry Zones")
shortEntryCol = input.color(color.orange, "Short zone color",
     group="Entry Zones")
 
showTable = input.bool(true, "Show dashboard", group="Dashboard")
tableBg = input.color(color.new(color.black, 15), "Background",
     group="Dashboard")
textCol = input.color(color.white, "Text color", group="Dashboard")
 
// ══════════════════════════════════════
// SMAs AND TREND
// ══════════════════════════════════════
fast = ta.sma(close, fastLen)
mid  = ta.sma(close, midLen)
slow = ta.sma(close, slowLen)
 
plot(fast, "Fast SMA", color=fastCol, linewidth=2)
plot(mid, "Middle SMA", color=midCol, linewidth=2)
plot(slow, "Slow SMA", color=slowCol, linewidth=2)
 
ready = not na(fast) and not na(mid) and not na(slow)
bull = ready and fast > mid and mid > slow
bear = ready and fast < mid and mid < slow
 
trendCol = bull ? bullCol : bear ? bearCol : flatCol
trendText = not ready ? "WAITING" : bull ? "BULLISH" : bear ? "BEARISH" : "NEUTRAL"
 
barcolor(paintBars and ready ? trendCol : na)
 
// ══════════════════════════════════════
// MAJOR SUPPORT / RESISTANCE
// ══════════════════════════════════════
majorHigh = ta.pivothigh(high, majorLen, majorLen)
majorLow  = ta.pivotlow(low, majorLen, majorLen)
 
var array<line> supportLines = array.new<line>()
var array<line> resistanceLines = array.new<line>()
 
if barstate.isconfirmed and showSR
    if not na(majorLow)
        newSupport = line.new(
             x1=time[majorLen], y1=majorLow,
             x2=time, y2=majorLow,
             xloc=xloc.bar_time, extend=extend.right,
             color=supCol, width=2)
        array.push(supportLines, newSupport)
        if array.size(supportLines) > keepLevels
            line.delete(array.shift(supportLines))
 
    if not na(majorHigh)
        newResistance = line.new(
             x1=time[majorLen], y1=majorHigh,
             x2=time, y2=majorHigh,
             xloc=xloc.bar_time, extend=extend.right,
             color=resCol, width=2)
        array.push(resistanceLines, newResistance)
        if array.size(resistanceLines) > keepLevels
            line.delete(array.shift(resistanceLines))
 
// Broken levels become dashed after a candle closes.
// Nearest support/resistance is selected by current price,
// including levels that have changed sides.
float nearestSupport = na
float nearestResistance = na
 
if array.size(supportLines) > 0
    for i = 0 to array.size(supportLines) - 1
        levelLine = array.get(supportLines, i)
        level = line.get_y1(levelLine)
        if barstate.isconfirmed and close < level
            line.set_style(levelLine, line.style_dashed)
        if level <= close
            nearestSupport := na(nearestSupport) ? level : math.max(nearestSupport, level)
        else
            nearestResistance := na(nearestResistance) ? level : math.min(nearestResistance, level)
 
if array.size(resistanceLines) > 0
    for i = 0 to array.size(resistanceLines) - 1
        levelLine = array.get(resistanceLines, i)
        level = line.get_y1(levelLine)
        if barstate.isconfirmed and close > level
            line.set_style(levelLine, line.style_dashed)
        if level >= close
            nearestResistance := na(nearestResistance) ? level : math.min(nearestResistance, level)
        else
            nearestSupport := na(nearestSupport) ? level : math.max(nearestSupport, level)
 
// ══════════════════════════════════════
// AUTO TRENDLINES
// ══════════════════════════════════════
swingLow = ta.pivotlow(low, trendLen, trendLen)
swingHigh = ta.pivothigh(high, trendLen, trendLen)
 
var float previousLow = na
var float previousHigh = na
var int previousLowTime = na
var int previousHighTime = na
 
var line lowTrendline = na
var line highTrendline = na
 
if barstate.isconfirmed and showTL
    if not na(swingLow)
        if not na(previousLow)
            if not na(lowTrendline)
                line.delete(lowTrendline)
            lowTrendline := line.new(
                 x1=previousLowTime, y1=previousLow,
                 x2=time[trendLen], y2=swingLow,
                 xloc=xloc.bar_time, extend=extend.right,
                 color=upTLCol, width=2)
        previousLow := swingLow
        previousLowTime := time[trendLen]
 
    if not na(swingHigh)
        if not na(previousHigh)
            if not na(highTrendline)
                line.delete(highTrendline)
            highTrendline := line.new(
                 x1=previousHighTime, y1=previousHigh,
                 x2=time[trendLen], y2=swingHigh,
                 xloc=xloc.bar_time, extend=extend.right,
                 color=downTLCol, width=2)
        previousHigh := swingHigh
        previousHighTime := time[trendLen]
 
// ══════════════════════════════════════
// FAIR VALUE GAPS
// ══════════════════════════════════════
type Gap
    box drawing
    bool bullish
    int born
 
var array<Gap> gaps = array.new<Gap>()
 
atr = ta.atr(14)
 
newBullGap = bar_index >= 2 and low > high[2] and
     low - high[2] >= atr * minGapATR
 
newBearGap = bar_index >= 2 and high < low[2] and
     low[2] - high >= atr * minGapATR
 
if barstate.isconfirmed and showFVG
    // A full wick fill removes the entire gap.
    // Partial fills leave the original boundaries unchanged.
    if array.size(gaps) > 0
        for i = array.size(gaps) - 1 to 0
            gap = array.get(gaps, i)
            gapTop = box.get_top(gap.drawing)
            gapBottom = box.get_bottom(gap.drawing)
            filled = gap.bullish ? low <= gapBottom : high >= gapTop
            if filled
                box.delete(gap.drawing)
                array.remove(gaps, i)
 
    if newBullGap
        b = box.new(
             left=bar_index, top=low,
             right=bar_index + 1, bottom=high[2],
             extend=extend.right,
             border_color=bullGapCol,
             bgcolor=color.new(bullGapCol, 88),
             text="Bull FVG", text_color=textCol,
             text_size=size.tiny)
        array.push(gaps, Gap.new(b, true, bar_index))
 
    if newBearGap
        b = box.new(
             left=bar_index, top=low[2],
             right=bar_index + 1, bottom=high,
             extend=extend.right,
             border_color=bearGapCol,
             bgcolor=color.new(bearGapCol, 88),
             text="Bear FVG", text_color=textCol,
             text_size=size.tiny)
        array.push(gaps, Gap.new(b, false, bar_index))
 
    while array.size(gaps) > maxGaps
        oldGap = array.shift(gaps)
        box.delete(oldGap.drawing)
 
// ══════════════════════════════════════
// ENTRY ZONE: NEWEST TREND-ALIGNED FVG
// ══════════════════════════════════════
int entryIndex = na
 
if array.size(gaps) > 0
    // Restore standard appearance before selecting a zone.
    for i = 0 to array.size(gaps) - 1
        gap = array.get(gaps, i)
        baseCol = gap.bullish ? bullGapCol : bearGapCol
        box.set_bgcolor(gap.drawing, color.new(baseCol, 88))
        box.set_border_color(gap.drawing, baseCol)
        box.set_border_width(gap.drawing, 1)
        box.set_text(gap.drawing, gap.bullish ? "Bull FVG" : "Bear FVG")
 
        if showEntry and ((bull and gap.bullish) or (bear and not gap.bullish))
            entryIndex := i
 
float entryTop = na
float entryBottom = na
bool longTouch = false
bool shortTouch = false
string entryText = "NONE"
 
if not na(entryIndex)
    selected = array.get(gaps, entryIndex)
    entryTop := box.get_top(selected.drawing)
    entryBottom := box.get_bottom(selected.drawing)
    selectedColor = selected.bullish ? longEntryCol : shortEntryCol
 
    box.set_bgcolor(selected.drawing, color.new(selectedColor, 75))
    box.set_border_color(selected.drawing, selectedColor)
    box.set_border_width(selected.drawing, 2)
    box.set_text(selected.drawing,
         selected.bullish ? "LONG WATCH ZONE" : "SHORT WATCH ZONE")
 
    entryText := selected.bullish ? "LONG WATCH" : "SHORT WATCH"
 
    // No touch signal on the candle that creates the gap.
    touched = bar_index > selected.born and
         low <= entryTop and high >= entryBottom
 
    longTouch := barstate.isconfirmed and touched and selected.bullish
    shortTouch := barstate.isconfirmed and touched and not selected.bullish
 
plotshape(longTouch, title="Long zone touch",
     style=shape.triangleup, location=location.belowbar,
     color=longEntryCol, size=size.tiny, text="L")
 
plotshape(shortTouch, title="Short zone touch",
     style=shape.triangledown, location=location.abovebar,
     color=shortEntryCol, size=size.tiny, text="S")
 
alertcondition(longTouch, "Long watch-zone touch",
     "Price touched a bullish trend-aligned FVG watch zone.")
alertcondition(shortTouch, "Short watch-zone touch",
     "Price touched a bearish trend-aligned FVG watch zone.")
 
// ══════════════════════════════════════
// DASHBOARD
// ══════════════════════════════════════
priceText(float value) =>
    na(value) ? "N/A" : str.tostring(value, format.mintick)
 
var table dashboard = table.new(position.top_right, 2, 7,
     bgcolor=tableBg,
     border_color=color.gray, border_width=1)
 
if barstate.islast
    if showTable
        table.cell(dashboard, 0, 0, "TREND / LEVELS",
             text_color=textCol, bgcolor=color.new(color.blue, 50))
        table.cell(dashboard, 1, 0, syminfo.ticker,
             text_color=textCol, bgcolor=color.new(color.blue, 50))
 
        table.cell(dashboard, 0, 1, "Current trend", text_color=textCol)
        table.cell(dashboard, 1, 1, trendText, text_color=trendCol)
 
        table.cell(dashboard, 0, 2, "Nearest support", text_color=textCol)
        table.cell(dashboard, 1, 2, priceText(nearestSupport), text_color=supCol)
 
        table.cell(dashboard, 0, 3, "Nearest resistance", text_color=textCol)
        table.cell(dashboard, 1, 3, priceText(nearestResistance), text_color=resCol)
 
        table.cell(dashboard, 0, 4, "Entry watch", text_color=textCol)
        table.cell(dashboard, 1, 4, entryText, text_color=trendCol)
 
        table.cell(dashboard, 0, 5, "Zone low", text_color=textCol)
        table.cell(dashboard, 1, 5, priceText(entryBottom), text_color=textCol)
 
        table.cell(dashboard, 0, 6, "Zone high", text_color=textCol)
        table.cell(dashboard, 1, 6, priceText(entryTop), text_color=textCol)
    else
        table.clear(dashboard, 0, 0, 1, 6)

← 返回列表