CRYPTOMASH Multi EMA + Smart Money Levels

danny25m · study · 331 行 · 点赞 24 · TradingView 原页

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

Pine Script

//@version=6
indicator("Multi EMA + OB + FVG + POC + PDH/PDL", shorttitle="EMA+OB+FVG+POC+HL", overlay=true, max_boxes_count=200, max_lines_count=150, max_labels_count=150)
 
// ═══════════ EMA Inputs ═══════════
len1 = input.int(10,  "EMA 1 Length", minval=1, group="EMAs")
len2 = input.int(14,  "EMA 2 Length", minval=1, group="EMAs")
len3 = input.int(21,  "EMA 3 Length", minval=1, group="EMAs")
len4 = input.int(50,  "EMA 4 Length", minval=1, group="EMAs")
len5 = input.int(100, "EMA 5 Length", minval=1, group="EMAs")
len6 = input.int(200, "EMA 6 Length", minval=1, group="EMAs")
src  = input.source(close, "Source", group="EMAs")
 
// ═══════════ Order Block Inputs ═══════════
obLookback   = input.int(5,   "Impulse Lookback (bars)", minval=2, maxval=20, group="Order Blocks")
obThreshold  = input.float(1.5, "Impulse Strength (x avg range)", minval=0.5, step=0.1, group="Order Blocks")
maxBlocks    = input.int(10,  "Max Order Blocks Shown", minval=1, maxval=50, group="Order Blocks")
deleteMitigatedOB = input.bool(true, "Remove Mitigated Blocks", group="Order Blocks")
minOBScore   = input.int(1, "Min OB Score to Show", minval=1, maxval=100, group="Order Blocks")
bullColor    = input.color(color.new(color.green, 80), "Bullish OB Color", group="Order Blocks")
bearColor    = input.color(color.new(color.red, 80),   "Bearish OB Color", group="Order Blocks")
 
// ═══════════ FVG Inputs ═══════════
fvgMinSize      = input.float(0.5, "Min Gap Size (x ATR)", minval=0.1, step=0.1, group="Fair Value Gaps")
fvgDisplacement = input.float(1.5, "Displacement Candle (x avg body)", minval=0.5, step=0.1, group="Fair Value Gaps")
maxFVG          = input.int(8, "Max FVGs Shown", minval=1, maxval=30, group="Fair Value Gaps")
removeFilled    = input.bool(true, "Remove Filled FVGs", group="Fair Value Gaps")
fillRule        = input.string("50% Fill (CE)", "FVG Invalidation", options=["50% Fill (CE)", "Full Fill"], group="Fair Value Gaps")
minFVGScore     = input.int(1, "Min FVG Score to Show", minval=1, maxval=100, group="Fair Value Gaps")
bullFVGColor    = input.color(color.new(color.teal, 75),   "Bullish FVG Color", group="Fair Value Gaps")
bearFVGColor    = input.color(color.new(color.maroon, 75), "Bearish FVG Color", group="Fair Value Gaps")
 
// ═══════════ POC Inputs ═══════════
showPrevPOC = input.bool(true, "Show Previous Day POC", group="POC")
showDayPOC  = input.bool(true, "Show Current Day POC (Developing)", group="POC")
pocRows     = input.int(50, "Profile Rows", minval=10, maxval=200, group="POC")
prevPocColor = input.color(color.yellow, "Previous Day POC Color", group="POC")
dayPocColor  = input.color(color.red, "Current Day POC Color", group="POC")
pocWidth    = input.int(2, "POC Line Width", minval=1, maxval=4, group="POC")
keepOldPOC  = input.bool(false, "Keep Historical POC Lines", group="POC")
 
// ═══════════ PDH/PDL Inputs ═══════════
showPDHL   = input.bool(true, "Show Previous Day High/Low", group="Previous Day High/Low")
pdhColor   = input.color(color.lime, "PDH Color", group="Previous Day High/Low")
pdlColor   = input.color(color.fuchsia, "PDL Color", group="Previous Day High/Low")
pdhlWidth  = input.int(1, "Line Width", minval=1, maxval=4, group="Previous Day High/Low")
pdhlStyle  = input.string("Dashed", "Line Style", options=["Solid", "Dashed", "Dotted"], group="Previous Day High/Low")
keepOldPDHL = input.bool(false, "Keep Historical PDH/PDL Lines", group="Previous Day High/Low")
 
// ═══════════ EMA Calculations ═══════════
ema1 = ta.ema(src, len1)
ema2 = ta.ema(src, len2)
ema3 = ta.ema(src, len3)
ema4 = ta.ema(src, len4)
ema5 = ta.ema(src, len5)
ema6 = ta.ema(src, len6)
 
plot(ema1, "EMA 10",  color=color.aqua,   linewidth=1)
plot(ema2, "EMA 14",  color=color.lime,   linewidth=1)
plot(ema3, "EMA 21",  color=color.yellow, linewidth=1)
plot(ema4, "EMA 50",  color=color.orange, linewidth=2)
plot(ema5, "EMA 100", color=color.red,    linewidth=2)
plot(ema6, "EMA 200", color=color.white,  linewidth=2)
 
// ═══════════ Shared Metrics ═══════════
atr      = ta.atr(14)
avgRange = ta.sma(high - low, 20)
avgBody  = ta.sma(math.abs(close - open), 20)
avgVol   = ta.sma(volume, 20)
 
clamp01(x) => math.max(0.0, math.min(1.0, x))
 
// ═══════════ SCORING ═══════════
obScore(int obIdx, bool isBull) =>
    disp = isBull ? (close - close[obLookback]) : (close[obLookback] - close)
    minDisp = obThreshold * avgRange * obLookback / 2
    impulsePts = 40 * clamp01((disp / minDisp - 1) / 2)
    volSum = 0.0
    for i = 0 to obLookback - 1
        volSum += volume[i]
    impVol = volSum / obLookback
    volPts = 30 * clamp01((impVol / avgVol - 0.8) / 1.2)
    obRange = high[obIdx] - low[obIdx]
    obBody  = math.abs(close[obIdx] - open[obIdx])
    bodyPts = obRange > 0 ? 20 * clamp01(obBody / obRange) : 0
    freshPts = 10.0
    math.round(math.max(1, math.min(100, impulsePts + volPts + bodyPts + freshPts)))
 
fvgScore(float gapSize, bool isBull) =>
    sizePts = 35 * clamp01((gapSize / atr - fvgMinSize) / (2.0 - fvgMinSize))
    midBody = math.abs(close[1] - open[1])
    dispPts = 35 * clamp01((midBody / avgBody - fvgDisplacement) / fvgDisplacement)
    volPts = 20 * clamp01((volume[1] / avgVol - 0.8) / 1.7)
    midRange = high[1] - low[1]
    closePos = midRange > 0 ? (isBull ? (close[1] - low[1]) / midRange : (high[1] - close[1]) / midRange) : 0
    closePts = 10 * clamp01((closePos - 0.5) / 0.4)
    math.round(math.max(1, math.min(100, sizePts + dispPts + volPts + closePts)))
 
scoreColor(int s) =>
    s >= 80 ? color.new(#FFD700, 0) : s >= 60 ? color.new(color.white, 10) : s >= 40 ? color.new(color.gray, 20) : color.new(color.gray, 50)
 
// ═══════════ Order Block Logic ═══════════
bullImpulse = close > close[obLookback] and (close - close[obLookback]) > obThreshold * avgRange * obLookback / 2
bearImpulse = close < close[obLookback] and (close[obLookback] - close) > obThreshold * avgRange * obLookback / 2
 
var array<box> bullBoxes = array.new<box>()
var array<box> bearBoxes = array.new<box>()
 
findBullOB() =>
    int idx = na
    for i = 1 to obLookback + 3
        if close[i] < open[i]
            idx := i
            break
    idx
 
findBearOB() =>
    int idx = na
    for i = 1 to obLookback + 3
        if close[i] > open[i]
            idx := i
            break
    idx
 
if bullImpulse and not bullImpulse[1]
    obIdx = findBullOB()
    if not na(obIdx)
        s = obScore(obIdx, true)
        if s >= minOBScore
            b = box.new(bar_index - obIdx, high[obIdx], bar_index, low[obIdx], border_color=color.new(color.green, 50), bgcolor=bullColor, extend=extend.right, text="OB " + str.tostring(s), text_size=size.small, text_color=scoreColor(s), text_halign=text.align_left)
            array.push(bullBoxes, b)
            if array.size(bullBoxes) > maxBlocks
                box.delete(array.shift(bullBoxes))
 
if bearImpulse and not bearImpulse[1]
    obIdx = findBearOB()
    if not na(obIdx)
        s = obScore(obIdx, false)
        if s >= minOBScore
            b = box.new(bar_index - obIdx, high[obIdx], bar_index, low[obIdx], border_color=color.new(color.red, 50), bgcolor=bearColor, extend=extend.right, text="OB " + str.tostring(s), text_size=size.small, text_color=scoreColor(s), text_halign=text.align_left)
            array.push(bearBoxes, b)
            if array.size(bearBoxes) > maxBlocks
                box.delete(array.shift(bearBoxes))
 
if deleteMitigatedOB
    if array.size(bullBoxes) > 0
        for i = array.size(bullBoxes) - 1 to 0
            b = array.get(bullBoxes, i)
            if close < box.get_bottom(b)
                box.delete(b)
                array.remove(bullBoxes, i)
    if array.size(bearBoxes) > 0
        for i = array.size(bearBoxes) - 1 to 0
            b = array.get(bearBoxes, i)
            if close > box.get_top(b)
                box.delete(b)
                array.remove(bearBoxes, i)
 
// ═══════════ Strong FVG Logic ═══════════
bullGapSize = low - high[2]
bearGapSize = low[2] - high
 
midBody = math.abs(close[1] - open[1])
strongMid = midBody > fvgDisplacement * avgBody
 
bullFVG = bullGapSize > fvgMinSize * atr and strongMid and close[1] > open[1]
bearFVG = bearGapSize > fvgMinSize * atr and strongMid and close[1] < open[1]
 
var array<box> bullFVGs = array.new<box>()
var array<box> bearFVGs = array.new<box>()
 
if bullFVG
    s = fvgScore(bullGapSize, true)
    if s >= minFVGScore
        b = box.new(bar_index - 2, low, bar_index, high[2], border_color=color.new(color.teal, 40), bgcolor=bullFVGColor, extend=extend.right, text="FVG " + str.tostring(s), text_size=size.small, text_color=scoreColor(s), text_halign=text.align_left)
        array.push(bullFVGs, b)
        if array.size(bullFVGs) > maxFVG
            box.delete(array.shift(bullFVGs))
 
if bearFVG
    s = fvgScore(bearGapSize, false)
    if s >= minFVGScore
        b = box.new(bar_index - 2, low[2], bar_index, high, border_color=color.new(color.maroon, 40), bgcolor=bearFVGColor, extend=extend.right, text="FVG " + str.tostring(s), text_size=size.small, text_color=scoreColor(s), text_halign=text.align_left)
        array.push(bearFVGs, b)
        if array.size(bearFVGs) > maxFVG
            box.delete(array.shift(bearFVGs))
 
// ═══════════ FVG Fill / Invalidation ═══════════
if removeFilled
    if array.size(bullFVGs) > 0
        for i = array.size(bullFVGs) - 1 to 0
            b = array.get(bullFVGs, i)
            top = box.get_top(b)
            bot = box.get_bottom(b)
            level = fillRule == "50% Fill (CE)" ? (top + bot) / 2 : bot
            if low < level
                box.delete(b)
                array.remove(bullFVGs, i)
    if array.size(bearFVGs) > 0
        for i = array.size(bearFVGs) - 1 to 0
            b = array.get(bearFVGs, i)
            top = box.get_top(b)
            bot = box.get_bottom(b)
            level = fillRule == "50% Fill (CE)" ? (top + bot) / 2 : top
            if high > level
                box.delete(b)
                array.remove(bearFVGs, i)
 
// ═══════════ POC Engine ═══════════
var array<float> dHighs = array.new<float>()
var array<float> dLows  = array.new<float>()
var array<float> dVols  = array.new<float>()
var line  prevPocLine  = na
var label prevPocLabel = na
var line  dayPocLine   = na
var label dayPocLabel  = na
var int   dayStartBar  = na
 
newDay = ta.change(time("D")) != 0
 
computePOC() =>
    float poc = na
    n = array.size(dVols)
    if n > 0
        dayHi = array.max(dHighs)
        dayLo = array.min(dLows)
        if dayHi > dayLo
            step = (dayHi - dayLo) / pocRows
            rowVol = array.new<float>(pocRows, 0.0)
            for i = 0 to n - 1
                bh = array.get(dHighs, i)
                bl = array.get(dLows, i)
                bv = array.get(dVols, i)
                r1 = math.max(0, math.min(pocRows - 1, int(math.floor((bl - dayLo) / step))))
                r2 = math.max(0, math.min(pocRows - 1, int(math.floor((bh - dayLo) / step))))
                share = bv / (r2 - r1 + 1)
                for r = r1 to r2
                    array.set(rowVol, r, array.get(rowVol, r) + share)
            maxIdx = 0
            maxVol = 0.0
            for r = 0 to pocRows - 1
                if array.get(rowVol, r) > maxVol
                    maxVol := array.get(rowVol, r)
                    maxIdx := r
            poc := dayLo + (maxIdx + 0.5) * step
        else
            poc := dayHi
    poc
 
// ═══════════ PDH/PDL Engine ═══════════
var line  pdhLine  = na
var label pdhLabel = na
var line  pdlLine  = na
var label pdlLabel = na
 
pdhlLineStyle = pdhlStyle == "Solid" ? line.style_solid : pdhlStyle == "Dashed" ? line.style_dashed : line.style_dotted
 
// ═══════════ Day Rollover ═══════════
if newDay
    // Capture yesterday's stats BEFORE clearing
    prevPOC  = computePOC()
    float pdh = array.size(dHighs) > 0 ? array.max(dHighs) : na
    float pdl = array.size(dLows)  > 0 ? array.min(dLows)  : na
    array.clear(dHighs)
    array.clear(dLows)
    array.clear(dVols)
    dayStartBar := bar_index
 
    // Yellow pPOC
    if showPrevPOC and not na(prevPOC)
        if not keepOldPOC
            line.delete(prevPocLine)
            label.delete(prevPocLabel)
        else if not na(prevPocLine)
            line.set_x2(prevPocLine, bar_index - 1)
        prevPocLine  := line.new(bar_index, prevPOC, bar_index + 1, prevPOC, color=prevPocColor, width=pocWidth, style=line.style_solid)
        prevPocLabel := label.new(bar_index, prevPOC, "pPOC", style=label.style_label_left, color=color.new(color.black, 100), textcolor=prevPocColor, size=size.small)
 
    // PDH / PDL
    if showPDHL and not na(pdh) and not na(pdl)
        if not keepOldPDHL
            line.delete(pdhLine)
            label.delete(pdhLabel)
            line.delete(pdlLine)
            label.delete(pdlLabel)
        else
            if not na(pdhLine)
                line.set_x2(pdhLine, bar_index - 1)
            if not na(pdlLine)
                line.set_x2(pdlLine, bar_index - 1)
        pdhLine  := line.new(bar_index, pdh, bar_index + 1, pdh, color=pdhColor, width=pdhlWidth, style=pdhlLineStyle)
        pdhLabel := label.new(bar_index, pdh, "PDH", style=label.style_label_left, color=color.new(color.black, 100), textcolor=pdhColor, size=size.small)
        pdlLine  := line.new(bar_index, pdl, bar_index + 1, pdl, color=pdlColor, width=pdhlWidth, style=pdhlLineStyle)
        pdlLabel := label.new(bar_index, pdl, "PDL", style=label.style_label_left, color=color.new(color.black, 100), textcolor=pdlColor, size=size.small)
 
    // Reset red developing POC
    line.delete(dayPocLine)
    label.delete(dayPocLabel)
    dayPocLine  := na
    dayPocLabel := na
 
// Accumulate current day's bars
array.push(dHighs, high)
array.push(dLows, low)
array.push(dVols, volume)
if na(dayStartBar)
    dayStartBar := bar_index
 
// ═══════════ Extend lines through today ═══════════
if showPrevPOC and not na(prevPocLine)
    line.set_x2(prevPocLine, bar_index)
    label.set_x(prevPocLabel, bar_index)
 
if showPDHL
    if not na(pdhLine)
        line.set_x2(pdhLine, bar_index)
        label.set_x(pdhLabel, bar_index)
    if not na(pdlLine)
        line.set_x2(pdlLine, bar_index)
        label.set_x(pdlLabel, bar_index)
 
// ═══════════ Current Day POC (red, developing) ═══════════
if showDayPOC
    dayPOC = computePOC()
    if not na(dayPOC)
        if na(dayPocLine)
            dayPocLine  := line.new(dayStartBar, dayPOC, bar_index, dayPOC, color=dayPocColor, width=pocWidth, style=line.style_solid)
            dayPocLabel := label.new(bar_index, dayPOC, "dPOC", style=label.style_label_left, color=color.new(color.black, 100), textcolor=dayPocColor, size=size.small)
        else
            line.set_xy1(dayPocLine, dayStartBar, dayPOC)
            line.set_xy2(dayPocLine, bar_index, dayPOC)
            label.set_xy(dayPocLabel, bar_index, dayPOC)

← 返回列表