SMA Fill, Cross, S/R, Trendlines, FVG by Green Boyz

GreenBoyz · study · 493 行 · 点赞 13 · TradingView 原页

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

Pine Script

//@version=6
indicator("SMA 13/21 Fill + Cross + S/R + Trendlines + FVG",
     overlay=true, max_lines_count=100, max_boxes_count=100)
 
// ══════════════════════════════════════
// INPUTS: SMAs
// ══════════════════════════════════════
fastLen = input.int(13, "Fast SMA length",
     minval=1, group="SMAs")
 
midLen = input.int(21, "Middle SMA length",
     minval=1, group="SMAs")
 
slowLen = input.int(100, "Slow SMA length",
     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")
 
smaWidth = input.int(2, "SMA line thickness",
     minval=1, maxval=4, group="SMAs")
 
// ══════════════════════════════════════
// INPUTS: SMA FILL
// ══════════════════════════════════════
showFill = input.bool(true, "Show fill between fast / middle SMAs",
     group="SMA Fill")
 
bullFillCol = input.color(color.lime, "Bullish fill color",
     group="SMA Fill")
 
bearFillCol = input.color(color.red, "Bearish fill color",
     group="SMA Fill")
 
neutralFillCol = input.color(color.gray, "Equal SMAs fill color",
     group="SMA Fill")
 
fillTransparency = input.int(85, "Fill transparency",
     minval=0, maxval=100,
     group="SMA Fill")
 
// ══════════════════════════════════════
// INPUTS: SMA CROSS MARKERS
// ══════════════════════════════════════
showCross = input.bool(true, "Show SMA cross markers",
     group="SMA Cross Markers")
 
crossUpCol = input.color(color.lime, "Cross-up marker color",
     group="SMA Cross Markers")
 
crossDownCol = input.color(color.red, "Cross-down marker color",
     group="SMA Cross Markers")
 
crossTextCol = input.color(color.white, "Marker text color",
     group="SMA Cross Markers")
 
// ══════════════════════════════════════
// INPUTS: TREND / CANDLE COLORS
// ══════════════════════════════════════
paintBars = input.bool(true, "Color candles",
     group="Trend")
 
bullCol = input.color(color.lime, "Bullish trend color",
     group="Trend")
 
bearCol = input.color(color.red, "Bearish trend color",
     group="Trend")
 
flatCol = input.color(color.gray, "Neutral / waiting color",
     group="Trend")
 
// ══════════════════════════════════════
// INPUTS: SUPPORT / RESISTANCE
// ══════════════════════════════════════
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")
 
srLength = input.int(20, "S/R line length (bars)",
     minval=1, maxval=500,
     group="Major Support / Resistance")
 
srWidth = input.int(1, "S/R line thickness",
     minval=1, maxval=4,
     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")
 
// ══════════════════════════════════════
// INPUTS: AUTO TRENDLINES
// ══════════════════════════════════════
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")
 
tlWidth = input.int(2, "Trendline thickness",
     minval=1, maxval=4,
     group="Auto Trendlines")
 
// ══════════════════════════════════════
// INPUTS: FAIR VALUE GAPS
// ══════════════════════════════════════
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")
 
bullGapFillCol = input.color(color.teal, "Bullish FVG fill color",
     group="Fair Value Gaps")
 
bearGapFillCol = input.color(color.maroon, "Bearish FVG fill color",
     group="Fair Value Gaps")
 
bullGapBorderCol = input.color(color.teal, "Bullish FVG border color",
     group="Fair Value Gaps")
 
bearGapBorderCol = input.color(color.maroon, "Bearish FVG border color",
     group="Fair Value Gaps")
 
fvgTextCol = input.color(color.white, "FVG text color",
     group="Fair Value Gaps")
 
fvgTransparency = input.int(88, "FVG fill transparency",
     minval=0, maxval=100,
     group="Fair Value Gaps")
 
fvgExtendBars = input.int(20, "FVG extension (bars)",
     minval=1, maxval=500,
     group="Fair Value Gaps")
 
fvgFollowPrice = input.bool(false,
     "Keep FVG extending ahead of current candle",
     group="Fair Value Gaps")
 
// ══════════════════════════════════════
// INPUTS: DASHBOARD
// ══════════════════════════════════════
showTable = input.bool(true, "Show dashboard",
     group="Dashboard")
 
tableBg = input.color(color.new(color.black, 15),
     "Background color", group="Dashboard")
 
tableHeaderBg = input.color(color.new(color.blue, 50),
     "Header background color", group="Dashboard")
 
tableBorderCol = input.color(color.gray,
     "Border color", group="Dashboard")
 
tableTextCol = input.color(color.white,
     "Text color", group="Dashboard")
 
tableHeaderTextCol = input.color(color.white,
     "Header text color", group="Dashboard")
 
// ══════════════════════════════════════
// SMAs
// ══════════════════════════════════════
fast = ta.sma(close, fastLen)
mid = ta.sma(close, midLen)
slow = ta.sma(close, slowLen)
 
fastPlot = plot(fast, "Fast SMA",
     color=fastCol, linewidth=smaWidth)
 
midPlot = plot(mid, "Middle SMA",
     color=midCol, linewidth=smaWidth)
 
plot(slow, "Slow SMA",
     color=slowCol, linewidth=smaWidth)
 
// ══════════════════════════════════════
// FILL BETWEEN FAST AND MIDDLE SMAs
// ══════════════════════════════════════
fillBaseCol = fast > mid ? bullFillCol : fast < mid ? bearFillCol : neutralFillCol
 
fill(fastPlot, midPlot,
     color=showFill ? color.new(fillBaseCol, fillTransparency) : na,
     title="Fast / Middle SMA Fill",
     fillgaps=false)
 
// ══════════════════════════════════════
// CONFIRMED SMA CROSS MARKERS
// ══════════════════════════════════════
crossUp = ta.crossover(fast, mid)
crossDown = ta.crossunder(fast, mid)
 
confirmedCrossUp = crossUp and barstate.isconfirmed
confirmedCrossDown = crossDown and barstate.isconfirmed
 
plotshape(showCross and confirmedCrossUp,
     title="SMA Cross Up",
     style=shape.triangleup,
     location=location.belowbar,
     color=crossUpCol,
     size=size.tiny,
     text="UP",
     textcolor=crossTextCol)
 
plotshape(showCross and confirmedCrossDown,
     title="SMA Cross Down",
     style=shape.triangledown,
     location=location.abovebar,
     color=crossDownCol,
     size=size.tiny,
     text="DN",
     textcolor=crossTextCol)
 
// ══════════════════════════════════════
// TREND / CANDLE COLORS
// ══════════════════════════════════════
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=bar_index - majorLen,
             y1=majorLow,
             x2=bar_index - majorLen + srLength,
             y2=majorLow,
             xloc=xloc.bar_index,
             extend=extend.none,
             color=supCol,
             width=srWidth)
 
        array.push(supportLines, newSupport)
 
        if array.size(supportLines) > keepLevels
            line.delete(array.shift(supportLines))
 
    if not na(majorHigh)
        newResistance = line.new(
             x1=bar_index - majorLen,
             y1=majorHigh,
             x2=bar_index - majorLen + srLength,
             y2=majorHigh,
             xloc=xloc.bar_index,
             extend=extend.none,
             color=resCol,
             width=srWidth)
 
        array.push(resistanceLines, newResistance)
 
        if array.size(resistanceLines) > keepLevels
            line.delete(array.shift(resistanceLines))
 
// Broken levels become dashed after a candle closes.
// Retained levels remain available to the dashboard,
// even after their visible line segment ends.
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=tlWidth)
 
        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=tlWidth)
 
        previousHigh := swingHigh
        previousHighTime := time[trendLen]
 
// ══════════════════════════════════════
// FAIR VALUE GAPS
// ══════════════════════════════════════
type Gap
    box drawing
    bool bullish
 
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
    // Full wick fills remove gaps.
    // Partial fills keep the original boundaries.
    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 + fvgExtendBars,
             bottom=high[2],
             xloc=xloc.bar_index,
             extend=extend.none,
             border_color=bullGapBorderCol,
             bgcolor=color.new(bullGapFillCol, fvgTransparency),
             text="Bull FVG",
             text_color=fvgTextCol,
             text_size=size.tiny)
 
        array.push(gaps, Gap.new(b, true))
 
    if newBearGap
        b = box.new(
             left=bar_index,
             top=low[2],
             right=bar_index + fvgExtendBars,
             bottom=high,
             xloc=xloc.bar_index,
             extend=extend.none,
             border_color=bearGapBorderCol,
             bgcolor=color.new(bearGapFillCol, fvgTransparency),
             text="Bear FVG",
             text_color=fvgTextCol,
             text_size=size.tiny)
 
        array.push(gaps, Gap.new(b, false))
 
    while array.size(gaps) > maxGaps
        oldGap = array.shift(gaps)
        box.delete(oldGap.drawing)
 
// Optional moving right edge for active FVG boxes.
if showFVG and fvgFollowPrice and array.size(gaps) > 0
    for i = 0 to array.size(gaps) - 1
        gap = array.get(gaps, i)
        box.set_right(gap.drawing, bar_index + fvgExtendBars)
 
// ══════════════════════════════════════
// DASHBOARD
// ══════════════════════════════════════
priceText(float value) =>
    na(value) ? "N/A" : str.tostring(value, format.mintick)
 
var table dashboard = table.new(
     position.top_right, 2, 4,
     bgcolor=tableBg,
     border_color=tableBorderCol,
     border_width=1)
 
if barstate.islast
    if showTable
        table.cell(dashboard, 0, 0, "TREND / LEVELS",
             text_color=tableHeaderTextCol,
             bgcolor=tableHeaderBg)
 
        table.cell(dashboard, 1, 0, syminfo.ticker,
             text_color=tableHeaderTextCol,
             bgcolor=tableHeaderBg)
 
        table.cell(dashboard, 0, 1, "Current trend",
             text_color=tableTextCol)
 
        table.cell(dashboard, 1, 1, trendText,
             text_color=trendCol)
 
        table.cell(dashboard, 0, 2, "Nearest support",
             text_color=tableTextCol)
 
        table.cell(dashboard, 1, 2, priceText(nearestSupport),
             text_color=supCol)
 
        table.cell(dashboard, 0, 3, "Nearest resistance",
             text_color=tableTextCol)
 
        table.cell(dashboard, 1, 3, priceText(nearestResistance),
             text_color=resCol)
    else
        table.clear(dashboard, 0, 0, 1, 3)

← 返回列表