MTF Candle Direction Dashboard + Session Volume Alerts

arup1969 · study · 169 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
indicator("MTF Candle Direction Dashboard", "MTF Candle Dir", overlay = true)
 
showDashboardInput = input.bool(true, "Show Dashboard", group = "Dashboard")
dashboardSizeInput = input.string("Normal", "Triangle/Text Size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Dashboard")
bullColorInput    = input.color(#089981,     "Bullish",        inline = "dbcolor", group = "Dashboard")
bearColorInput    = input.color(#f23645,     "Bearish",        inline = "dbcolor", group = "Dashboard")
neutralColorInput = input.color(color.gray,  "Neutral (Doji)", inline = "dbcolor", group = "Dashboard")
bgColorInput      = input.color(color.white, "Background", group = "Dashboard")
labelColorInput   = input.color(color.black, "Label Text", group = "Dashboard")
sessionModeInput  = input.string("Chart", "HTF Session", options = ["Chart", "Regular", "Extended"], group = "Dashboard", tooltip = "Chart = follow the chart's own session setting")
 
string dashboardTextSize = dashboardSizeInput == "Tiny" ? size.tiny : dashboardSizeInput == "Small" ? size.small : dashboardSizeInput == "Large" ? size.large : dashboardSizeInput == "Huge" ? size.huge : size.normal
 
// Session: default follows the chart so HTF bars match what you see
string htfTicker = sessionModeInput == "Chart" ? syminfo.tickerid : ticker.modify(syminfo.tickerid, session = sessionModeInput == "Extended" ? session.extended : session.regular)
 
f_candleDirs() =>
    int dirCur  = close    > open    ? 1 : close    < open    ? -1 : 0
    int dirPrev = close[1] > open[1] ? 1 : close[1] < open[1] ? -1 : 0
    [dirCur, dirPrev]
 
// lookahead_on: table only draws on the last bar, so this returns the true current HTF bar (no future leak)
[dir15mCur, dir15mPrev] = request.security(htfTicker, "15",  f_candleDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[dir1hCur,  dir1hPrev]  = request.security(htfTicker, "60",  f_candleDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[dir4hCur,  dir4hPrev]  = request.security(htfTicker, "240", f_candleDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[dirDCur,   dirDPrev]   = request.security(htfTicker, "D",   f_candleDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[dirWCur,   dirWPrev]   = request.security(htfTicker, "W",   f_candleDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
 
f_dirText(int d)  => d == 1 ? "▲" : d == -1 ? "▼" : "■"
f_dirColor(int d) => d == 1 ? bullColorInput : d == -1 ? bearColorInput : neutralColorInput
 
var table dashTable = table.new(position.middle_right, 3, 6, bgcolor = bgColorInput, border_width = 1, border_color = color.new(color.gray, 50))
 
f_row(int row, string tf, int cur, int prev) =>
    table.cell(dashTable, 0, row, tf,             text_color = labelColorInput,  text_size = dashboardTextSize, bgcolor = bgColorInput)
    table.cell(dashTable, 1, row, f_dirText(cur),  text_color = f_dirColor(cur),  text_size = dashboardTextSize, bgcolor = bgColorInput)
    table.cell(dashTable, 2, row, f_dirText(prev), text_color = f_dirColor(prev), text_size = dashboardTextSize, bgcolor = bgColorInput)
 
if showDashboardInput and barstate.islast
    table.cell(dashTable, 0, 0, "TF",       text_color = labelColorInput, text_size = dashboardTextSize, bgcolor = bgColorInput)
    table.cell(dashTable, 1, 0, "Current",  text_color = labelColorInput, text_size = dashboardTextSize, bgcolor = bgColorInput)
    table.cell(dashTable, 2, 0, "Previous", text_color = labelColorInput, text_size = dashboardTextSize, bgcolor = bgColorInput)
    f_row(1, "15M", dir15mCur, dir15mPrev)
    f_row(2, "1H",  dir1hCur,  dir1hPrev)
    f_row(3, "4H",  dir4hCur,  dir4hPrev)
    f_row(4, "D",   dirDCur,   dirDPrev)
    f_row(5, "W",   dirWCur,   dirWPrev)
 
// ── Flip alerts: at each new 1H / 4H / Daily candle open, last closed vs one before ──
f_closedDirs() =>
    int d = close > open ? 1 : close < open ? -1 : 0
    [d[1], d[2], time]
 
[c1hLast, c1hPrior, t1h] = request.security(htfTicker, "60",  f_closedDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[c4hLast, c4hPrior, t4h] = request.security(htfTicker, "240", f_closedDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[cDLast,  cDPrior,  tD]  = request.security(htfTicker, "D",   f_closedDirs(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
 
bool new1h = ta.change(t1h) != 0
bool new4h = ta.change(t4h) != 0
bool newD  = ta.change(tD)  != 0
 
string flipMsg = ""
if new1h and c1hPrior == -1 and c1hLast == 1
    flipMsg += "1H BEAR→BULL | "
if new1h and c1hPrior == 1 and c1hLast == -1
    flipMsg += "1H BULL→BEAR | "
if new4h and c4hPrior == -1 and c4hLast == 1
    flipMsg += "4H BEAR→BULL | "
if new4h and c4hPrior == 1 and c4hLast == -1
    flipMsg += "4H BULL→BEAR | "
if newD and cDPrior == -1 and cDLast == 1
    flipMsg += "D BEAR→BULL | "
if newD and cDPrior == 1 and cDLast == -1
    flipMsg += "D BULL→BEAR | "
 
// ── Volume: Pre-market 04:00–09:30 and RTH 09:30–16:00 NY time (EST/EDT auto), always extended data ──
string volTicker = ticker.modify(syminfo.tickerid, session = session.extended)
 
f_avgLast(array<float> a, int n) =>
    int sz = a.size()
    float result = na
    if sz >= n
        float s = 0.0
        for i = sz - n to sz - 1
            s += a.get(i)
        result := s / n
    result
 
f_sessVol() =>
    bool inPM  = not na(time(timeframe.period, "0400-0930:23456", "America/New_York"))
    bool inRTH = not na(time(timeframe.period, "0930-1600:23456", "America/New_York"))
    bool pmStart  = inPM  and not inPM[1]
    bool rthStart = inRTH and not inRTH[1]
    var float pmVol   = 0.0
    var float rthVol  = 0.0
    var int   pmId    = 0
    var int   rthId   = 0
    var bool  pmSeen  = false
    var bool  rthSeen = false
    var array<float> pmHist  = array.new<float>()
    var array<float> rthHist = array.new<float>()
    // Archive the previous full session total, then reset
    if pmStart
        if pmSeen
            pmHist.push(pmVol)
            if pmHist.size() > 30
                pmHist.shift()
        pmSeen := true
        pmVol  := 0.0
        pmId   += 1
    if rthStart
        if rthSeen
            rthHist.push(rthVol)
            if rthHist.size() > 30
                rthHist.shift()
        rthSeen := true
        rthVol  := 0.0
        rthId   += 1
    if inPM
        pmVol += nz(volume)
    if inRTH
        rthVol += nz(volume)
    [pmVol, f_avgLast(pmHist, 7), f_avgLast(pmHist, 30), pmId, rthVol, f_avgLast(rthHist, 7), f_avgLast(rthHist, 30), rthId]
 
[pmVol, pmAvg7, pmAvg30, pmId, rthVol, rthAvg7, rthAvg30, rthId] = request.security(volTicker, "5", f_sessVol(), gaps = barmerge.gaps_off)
 
// varip = survives realtime ticks, so each volume alert fires once per session
varip int  lastPmId   = -1
varip int  lastRthId  = -1
varip bool pm7Done    = false
varip bool pm30Done   = false
varip bool rth7Done   = false
varip bool rth30Done  = false
varip int  lastFlipBar = -1
 
if pmId != lastPmId
    lastPmId := pmId
    pm7Done  := false
    pm30Done := false
if rthId != lastRthId
    lastRthId := rthId
    rth7Done  := false
    rth30Done := false
 
f_v(float v) => str.tostring(v, format.volume)
 
string volMsg = ""
if not pm7Done and not na(pmAvg7) and pmVol > pmAvg7
    pm7Done := true
    volMsg  += "PM VOL > 7D AVG (" + f_v(pmVol) + " vs " + f_v(pmAvg7) + ") | "
if not pm30Done and not na(pmAvg30) and pmVol > pmAvg30
    pm30Done := true
    volMsg   += "PM VOL > 30D AVG (" + f_v(pmVol) + " vs " + f_v(pmAvg30) + ") | "
if not rth7Done and not na(rthAvg7) and rthVol > rthAvg7
    rth7Done := true
    volMsg   += "RTH VOL > 7D AVG (" + f_v(rthVol) + " vs " + f_v(rthAvg7) + ") | "
if not rth30Done and not na(rthAvg30) and rthVol > rthAvg30
    rth30Done := true
    volMsg    += "RTH VOL > 30D AVG (" + f_v(rthVol) + " vs " + f_v(rthAvg30) + ") | "
 
string msg = ""
if flipMsg != "" and lastFlipBar != bar_index
    lastFlipBar := bar_index
    msg += flipMsg
msg += volMsg
 
if msg != ""
    alert(syminfo.ticker + " | " + msg + "Price " + str.tostring(close, format.mintick), alert.freq_all)

← 返回列表