MM FLOW Buy and Sell Volume plus Fast MACD

jmar1984 · study · 169 行 · 点赞 17 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0
// © jmar1984 (Jason Marion)
// MM FLOW  ·  created by Jason Marion
//   Buy vs sell volume for every candle + a fast MACD that flags turns early.
//   Small triangles = MACD crossed its signal line (green = turning up, red = turning down).
 
//@version=6
indicator("MM FLOW", "MM FLOW", overlay = false,
     format = format.volume, precision = 0)
 
// ═════════════════════════════════════════════════════════════════════════════
//  Own pane, same as TradingView's native Volume indicator.
//
//      green column UP   from zero = BUY  volume
//      red   column DOWN from zero = SELL volume
//
//  Two full-width native columns per candle. The winner is solid, the loser
//  is faded, so which side won reads instantly.
//
//  Built on plain plot() columns — the same call the native volume uses.
//
//  v2: uses REAL bid/ask buy and sell volume from TradingView's footprint
//  data when available (Premium plan). Old intrabar guess is the backup.
// ═════════════════════════════════════════════════════════════════════════════
 
// ───────────────────────────── INPUTS ────────────────────────────────────────
string style  = input.string("Mirror", "Style", options = ["Mirror", "Stacked"],
     tooltip = "Mirror  → green up for buys, red down for sells.\nStacked → one column, red base + green top, height = total volume.")
bool   fadeLo = input.bool(true, "Fade the losing side")
bool   showMa = input.bool(false, "Average line")
int    maLen  = input.int(30, "Average length", minval = 2, maxval = 200)
 
color cBuy  = input.color(#FFFFFF, "Buy",  inline = "c", group = "Colors")
color cSell = input.color(#2F54B5, "Sell", inline = "c", group = "Colors")
color cMa   = input.color(#FFB300, "Avg",  inline = "c", group = "Colors")
int   winT  = input.int(0,  "Winner transparency", minval = 0, maxval = 90, group = "Colors")
int   loseT = input.int(74, "Loser transparency",  minval = 0, maxval = 90, group = "Colors")
 
const string GM = "MACD"
bool  macdOn   = input.bool(true,  "Show MACD", group = GM)
int   macdFast = input.int(5,  "Fast length",   minval = 1, maxval = 100, group = GM)
int   macdSlow = input.int(35, "Slow length",   minval = 2, maxval = 200, group = GM)
int   macdSig  = input.int(5,  "Signal length", minval = 1, maxval = 100, group = GM,
     tooltip = "The signal line is an EMA of the MACD line.")
bool  macdHist = input.bool(false, "Show histogram", group = GM)
float macdSize = input.float(90, "MACD size (% of pane)", minval = 10, maxval = 100, step = 5, group = GM,
     tooltip = "MACD is in price units and volume is in contracts, so the two cannot share a raw scale. The MACD is normalised to fit the pane — shape and crossovers are exact, the absolute numbers are not.")
color cMacd    = input.color(#FF0080, "MACD",   inline = "m", group = GM)
color cSig     = input.color(#00FF00, "Signal", inline = "m", group = GM)
 
string manRes = input.timeframe("", "Sampling resolution", group = "Data",
     tooltip = "Leave blank for automatic. Automatic aims for 8-20 intrabars per candle so the buy/sell split is meaningful.")
bool   useSec = input.bool(true, "Use seconds data on low timeframes", group = "Data",
     tooltip = "Seconds intrabars need a Premium plan. Without one the script falls back on its own.")
 
// NEW: real bid/ask footprint data (added last so your saved settings stay put)
bool useFp   = input.bool(true, "Use real bid/ask footprint data", group = "Data",
     tooltip = "On = true buys vs sells from TradingView's footprint data (Premium plan). Bars with no footprint data fall back to the old intrabar guess.")
int  fpTicks = input.int(4, "Footprint ticks per row", minval = 1, maxval = 100, group = "Data",
     tooltip = "Only affects row size, not the totals. NQ / ES: 4 ticks = 1 point.")
 
// ─────────────────────── SAMPLING RESOLUTION ─────────────────────────────────
int chartSec = math.max(60, nz(timeframe.in_seconds(timeframe.period), 60))
 
pickRes(int cs, bool sec) =>
    sec and cs <= 60   ? "5S"  :
     sec and cs <= 300 ? "15S" :
     cs <= 3600   ? "1"  :
     cs <= 14400  ? "5"  :
     cs <= 86400  ? "15" : "60"
 
string autoRes = pickRes(chartSec, useSec)
string ltfRes  = manRes == "" ? autoRes : manRes
 
[lO, lC, lV] = request.security_lower_tf(syminfo.tickerid, ltfRes, [open, close, volume])
 
// ───────────────────── BUY / SELL CLASSIFICATION ─────────────────────────────
// 1st choice: real buys (at the ask) and sells (at the bid) from footprint data.
footprint fp = request.footprint(fpTicks)
float fpBuy  = useFp and not na(fp) ? fp.buy_volume()  : na
float fpSell = useFp and not na(fp) ? fp.sell_volume() : na
 
float buyV  = 0.0
float sellV = 0.0
int   n     = lV.size()
 
if not na(fpBuy) and not na(fpSell)
    buyV  := fpBuy
    sellV := fpSell
else if n > 0
    for i = 0 to n - 1
        float v = lV.get(i)
        if not na(v) and v > 0
            float c = lC.get(i)
            float o = lO.get(i)
            float b = c > o ? v : c < o ? 0.0 : v * 0.5
            buyV  += b
            sellV += v - b
else
    // fallback when no intrabar data: split by where the candle closed in range
    float v = nz(volume)
    float r = high - low
    float b = r > 0 ? v * ((close - low) / r) : v * 0.5
    buyV  := b
    sellV := v - b
 
float totV  = buyV + sellV
float delta = buyV - sellV
 
bool mirror = style == "Mirror"
 
// winner solid, loser faded
int tBuy  = not fadeLo or buyV  >= sellV ? winT : loseT
int tSell = not fadeLo or sellV >= buyV  ? winT : loseT
 
// ───────────────────────────── PLOTS ─────────────────────────────────────────
// MIRROR — buy column up from zero, sell column down from zero.
plot(mirror ?  buyV  : na, "Buy",  color = color.new(cBuy,  tBuy),  style = plot.style_columns)
plot(mirror ? -sellV : na, "Sell", color = color.new(cSell, tSell), style = plot.style_columns)
 
// STACKED — full column in buy colour, then the sell portion painted over the
// bottom of it: red base, green on top.
plot(mirror ? na : totV,  "Total (buy on top)", color = color.new(cBuy,  tBuy),  style = plot.style_columns)
plot(mirror ? na : sellV, "Sell (base)",        color = color.new(cSell, tSell), style = plot.style_columns)
 
float maUp = ta.sma(mirror ? math.max(buyV, sellV) : totV, maLen)
plot(showMa ? maUp : na, "Average", color = color.new(cMa, 10), linewidth = 1)
plot(showMa and mirror ? -maUp : na, "Average (sell)", color = color.new(cMa, 45), linewidth = 1)
 
// ───────────────────────────── MACD ──────────────────────────────────────────
// MACD lives in price units, volume in contracts, so it is normalised into the
// pane's own range. Crossovers and shape are exact; the raw values are not.
float macdLine = ta.ema(close, macdFast) - ta.ema(close, macdSlow)
float sigLine  = ta.ema(macdLine, macdSig)
float macdBar  = macdLine - sigLine
 
float paneRef = ta.highest(mirror ? math.max(buyV, sellV) : totV, 200)
float macdRef = ta.highest(math.max(math.abs(macdLine), math.abs(sigLine)), 200)
float k       = macdRef > 0 and paneRef > 0 ? (paneRef * macdSize / 100.0) / macdRef : 0.0
float centre  = mirror ? 0.0 : paneRef / 2.0        // stacked pane has no zero line
 
plot(macdOn ? centre + macdLine * k : na, "MACD",   color = color.new(cMacd, 0), linewidth = 2)
plot(macdOn ? centre + sigLine  * k : na, "Signal", color = color.new(cSig,  0), linewidth = 2)
plot(macdOn and macdHist ? centre + macdBar * k : na, "MACD histogram",
     style = plot.style_columns, color = color.new(macdBar >= 0 ? cMacd : cSig, 55))
plot(macdOn and not mirror ? centre : na, "MACD zero", color = color.new(color.gray, 70))
 
hline(0, "Zero", color = color.new(color.gray, 45))
 
// turn markers: MACD crossing its signal line, drawn ON the MACD line where the cross happens
bool showX  = input.bool(false, "Mark MACD turns with triangles", group = GM)
bool xClean = input.bool(true, "Only real turns (up below zero / down above zero)", group = GM,
     tooltip = "On = skips the small wiggles: a turn up only counts when MACD is below zero, a turn down only when it's above zero.")
bool xUp   = macdOn and showX and ta.crossover(macdLine, sigLine)  and (not xClean or macdLine < 0)
bool xDn   = macdOn and showX and ta.crossunder(macdLine, sigLine) and (not xClean or macdLine > 0)
float xY   = centre + macdLine * k
plotshape(xUp ? xY : na, "Turning up",   shape.triangleup,   location.absolute, color.new(#00E676, 0), size = size.small)
plotshape(xDn ? xY : na, "Turning down", shape.triangledown, location.absolute, color.new(#FF1744, 0), size = size.small)
 
alertcondition(ta.crossover(macdLine, sigLine),  "MACD cross up",   "MACD crossed above its signal line")
alertcondition(ta.crossunder(macdLine, sigLine), "MACD cross down", "MACD crossed below its signal line")
 
// ───────────────────────────── ALERTS ────────────────────────────────────────
float avgV = ta.sma(totV, maLen)
bool  bigBar = not na(avgV) and avgV > 0 and totV > avgV * 1.5
alertcondition(bigBar and delta > 0, "Big volume — buyers",  "Large volume candle with buy-side control")
alertcondition(bigBar and delta < 0, "Big volume — sellers", "Large volume candle with sell-side control")
alertcondition(ta.crossover(delta, 0),  "Delta flipped positive", "Buy volume overtook sell volume")
alertcondition(ta.crossunder(delta, 0), "Delta flipped negative", "Sell volume overtook buy volume")

← 返回列表