Forex Sessions Range Volume Footprint [BigBeluga]

BigBeluga · study · 422 行 · 点赞 390 · TradingView 原页

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

Pine Script

// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International  
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
 
//@version=6
indicator("Forex Sessions Range Volume Footprint [BigBeluga]", overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500, max_polylines_count=100)
 
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
sesTy = input.string("NY", "Session", options=["NY","London","Tokyo","Sydney","Custom"], group="Session", tooltip="Select the forex session to analyze. Choose 'Custom' to define your own hours.")
cusSe = input.string("0800-1700", "Custom Session (HHMM-HHMM)", active = sesTy == "Custom", group="Session", tooltip="Only used when Session = Custom. Enter the session hours in HHMM-HHMM format (e.g., 0800-1700).")
cusTz = input.string("America/New_York", "Custom Timezone", active = sesTy == "Custom", options=[
     "America/New_York", "America/Chicago", "America/Los_Angeles", "America/Toronto", "America/Vancouver", "America/Sao_Paulo", "America/Argentina/Buenos_Aires", "America/Bogota", "America/Lima", "America/Mexico_City",
     "Europe/London", "Europe/Berlin", "Europe/Paris", "Europe/Madrid", "Europe/Rome", "Europe/Moscow", "Europe/Istanbul", "Europe/Athens", "Europe/Warsaw", "Europe/Zurich",
     "Asia/Tokyo", "Asia/Shanghai", "Asia/Hong_Kong", "Asia/Singapore", "Asia/Seoul", "Asia/Kolkata", "Asia/Dubai", "Asia/Bangkok", "Asia/Jakarta", "Asia/Taipei", "Asia/Tehran",
     "Australia/Sydney", "Australia/Brisbane", "Australia/Adelaide", "Australia/Perth", "Pacific/Auckland", "Pacific/Honolulu",
     "Africa/Johannesburg", "Africa/Cairo", "Africa/Lagos",
     "Etc/UTC", "GMT", "UTC"
], group="Session", tooltip="Only used when Session = Custom. Select the specific timezone for your custom session hours.")
 
 
// Display & Emojis
useEm = input.bool(true, "Use Colored Emoji Blocks", group="Emoji Styling", tooltip="Replaces the standard block character with colored emojis to create a visual volume gradient.")
emoTh = input.string("Heatmap (Squares)", "Color Theme", options=[
     "Heatmap (Squares)", "Fire (Squares)", "Ocean (Squares)", "Monochrome (Squares)",
     "Heatmap (Circles)", "Fire (Circles)", "Ocean (Circles)", "Monochrome (Circles)",
     "Heatmap (Hearts)", "Fire (Hearts)", "Ocean (Hearts)", "Monochrome (Hearts)",
     "Custom Sequence"
], group="Emoji Styling", tooltip="Choose a pre-defined color gradient and shape for the footprint blocks.")
cusEm = input.string("🟥,🟧,🟨,🟩,🟦", "Custom Sequence", group="Emoji Styling", tooltip="Only used when Theme = Custom Sequence. Define your own comma-separated emojis from left to right.")
 
// POC Settings
shwPc = input.bool(true, "Show POC Line", group="Display", tooltip="Toggle the display of the Point of Control (the price level with the highest traded volume).")
pcMod = input.string("Rolling", "POC Mode", options=["Static", "Rolling"], group="Display", tooltip="Static draws a single horizontal line across the session. Rolling draws a step-line tracking how the POC developed bar-by-bar.")
pcCol = input.color(color.yellow, "POC Color", group="Display", tooltip="Color of the POC line and its volume label.")
pcWid = input.int(2, "POC Line Width", minval=1, maxval=4, group="Display", tooltip="Thickness of the POC line.")
pcTxS = input.string("normal", "POC Text Size", options=["auto", "tiny", "small", "normal", "large", "huge"], group="Display", tooltip="Text size for the POC volume label.")
 
prCol = input.color(color.teal, "Solid Profile Color (If Emojis OFF)", group="Display", tooltip="Color of the footprint blocks if emojis are turned off.")
bxBgC = input.color(color.new(color.teal, 95), "Box Background", group="Display", tooltip="Background color of the session range box.")
bxBrC = input.color(color.new(color.teal, 50), "Box Border", group="Display", tooltip="Border color of the session range box.")
txSiz = input.string("tiny", "Profile Text Size", options=["auto", "tiny", "small", "normal", "large", "huge"], group="Display", tooltip="Text size for the volume footprint blocks.")
shwLv = input.bool(true, "Show Live Profile During Session", group="Display", tooltip="If true, the footprint updates in real-time. If false, it only draws after the session ends.")
kpHis = input.bool(true, "Keep Previous Session Drawings", group="Display", tooltip="Keep boxes, profiles, and POC lines from previous sessions on the chart.")
shwNm = input.bool(true, "Show Session Name Above Box", group="Display", tooltip="Show the name of the session (e.g., 'NY' or 'London') centered above the box.")
 
prOff = input.int(0, "Profile X Offset (bars)", group="Display", tooltip="Shift the footprint horizontally. Positive values move it right (outside the box), negative values move it left (inside).")
prAln = input.string("Right", "Profile Alignment", options=["Left", "Right"], group="Display", tooltip="Direction the footprint extends from its anchor point. 'Right' extends outwards, 'Left' extends inwards.")
 
// Bins
atrLn = 100
atrDv = input.float(5.0, "ATR Divisor (binHeight = ATR / divisor)", minval=0.5, step=0.5, group="Bin Calculation", tooltip="Controls the height of each volume bin. Higher values = thinner bins = more detail. Lower values = thicker bins = smoother profile.")
ltfRs = input.timeframe("1", "Lower Timeframe (Footprint)", group="Bin Calculation", tooltip="The lower timeframe used to calculate the volume footprint. Must be LOWER than the chart timeframe.")
mxChr = input.int(30, "Profile Max Width (chars)", minval=5, maxval=60, group="Bin Calculation", tooltip="Maximum width of the longest footprint bar in characters.")
mxBns = input.int(50, "Max Bins Per LTF Bar", minval=5, maxval=200, group="Bin Calculation", tooltip="Maximum number of price bins a single lower-timeframe candle can span. Prevents performance issues during extreme volatility.")
// }
 
 
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
// Theme Selection Logic
string fnEmS = cusEm
 
if emoTh == "Heatmap (Squares)"
    fnEmS := "🟦,🟩,🟨,🟧,🟥"
else if emoTh == "Fire (Squares)"
    fnEmS := "🟥,🟧,🟨"
else if emoTh == "Ocean (Squares)"
    fnEmS := "🟦,🟩"
else if emoTh == "Monochrome (Squares)"
    fnEmS := "⬛,⬜"
    
else if emoTh == "Heatmap (Circles)"
    fnEmS := "🔵,🟢,🟡,🟠,🔴"
else if emoTh == "Fire (Circles)"
    fnEmS := "🔴,🟠,🟡"
else if emoTh == "Ocean (Circles)"
    fnEmS := "🔵,🟢"
else if emoTh == "Monochrome (Circles)"
    fnEmS := "⚫,⚪"
    
else if emoTh == "Heatmap (Hearts)"
    fnEmS := "💙,💚,💛,🧡,❤️"
else if emoTh == "Fire (Hearts)"
    fnEmS := "❤️,🧡,💛"
else if emoTh == "Ocean (Hearts)"
    fnEmS := "💙,💚"
else if emoTh == "Monochrome (Hearts)"
    fnEmS := "🖤,🤍"
 
// Parse the emoji sequence into an array
var array<string> emojs = str.split(fnEmS, ",")
if array.size(emojs) == 0
    array.push(emojs, "▇")
 
// Session Definition
string sesSt = ""
string sesTz = ""
 
if sesTy == "NY"
    sesSt := "0800-1700"
    sesTz := "America/New_York"
else if sesTy == "London"
    sesSt := "0800-1700"
    sesTz := "Europe/London"
else if sesTy == "Tokyo"
    sesSt := "0900-1800"
    sesTz := "Asia/Tokyo"
else if sesTy == "Sydney"
    sesSt := "0700-1600"
    sesTz := "Australia/Sydney"
else
    sesSt := cusSe
    sesTz := cusTz
 
flSes = sesSt + ":1234567"
 
// Session State
inSes = not na(time(timeframe.period, flSes, sesTz))
bool pvSes = bar_index > 0 ? inSes[1] : false
stSes = inSes and not pvSes
enSes = not inSes and pvSes
 
// State Variables
var int    stBar   = na
var float  sHi     = na
var float  sLo     = na
var int    hBar    = na
var int    lBar    = na
var float  bHgt    = na   // LOCKED at session start!
var map<int, float> vMap = na
var box    cBox    = na
var label  nmLbl   = na
var line   pcLin   = na
var label  pcLbl   = na
var array<chart.point> rPts = array.new<chart.point>()
var polyline rPoly = na
var array<label> lLbls = array.new<label>()
var array<label> hLbls = array.new<label>()
var label  hiLbl   = na
var label  loLbl   = na
 
// FIX: Made these 'var' so they retain values on the sessionEnd bar when showLive = false
var float mVol = 0.0
var int pocK = na
var int mnBin = na
var int mxBin = na
var int nBins = na
var float pcPrc = na
var string pTxt = ""
 
// Map size strings to Pine size constants
strSz = txSiz == "auto" ? size.auto : txSiz == "tiny" ? size.tiny : txSiz == "small" ? size.small : txSiz == "normal" ? size.normal : txSiz == "large" ? size.large : size.huge
pcSz  = pcTxS == "auto" ? size.auto : pcTxS == "tiny" ? size.tiny : pcTxS == "small" ? size.small : pcTxS == "normal" ? size.normal : pcTxS == "large" ? size.large : size.huge
lblSt = prAln == "Right" ? label.style_label_left : label.style_label_right
 
// LTF request
[lVol, lH, lL] = request.security_lower_tf(syminfo.tickerid, ltfRs, [volume, high, low])
 
// Session Start
atr = ta.atr(atrLn)
if stSes
    if kpHis
        for l in lLbls
            array.push(hLbls, l)
        array.clear(lLbls)
        cBox := na
        nmLbl := na
        pcLin := na
        pcLbl := na
        rPoly := na
        array.clear(rPts)
        hiLbl := na
        loLbl := na
    else
        for l in lLbls
            label.delete(l)
        array.clear(lLbls)
        if not na(cBox)
            box.delete(cBox)
            cBox := na
        if not na(nmLbl)
            label.delete(nmLbl)
            nmLbl := na
        if not na(pcLin)
            line.delete(pcLin)
            pcLin := na
        if not na(pcLbl)
            label.delete(pcLbl)
            pcLbl := na
        if not na(rPoly)
            polyline.delete(rPoly)
            rPoly := na
        array.clear(rPts)
        if not na(hiLbl)
            label.delete(hiLbl)
            hiLbl := na
        if not na(loLbl)
            label.delete(loLbl)
            loLbl := na
 
    stBar  := bar_index
    sHi    := high
    sLo    := low
    hBar   := bar_index
    lBar   := bar_index
    
    // Reset var variables at start of new session
    mVol := 0.0
    pocK := na
    mnBin := na
    mxBin := na
    nBins := na
    pcPrc := na
    pTxt := ""
    
    // Lock the bin height for the entire session so absolute bins don't drift
    bHgt := nz(atr / atrDv, syminfo.mintick * 10)
    if bHgt <= 0
        bHgt := syminfo.mintick * 10
        
    vMap := map.new<int, float>()
 
// During Session — aggregate LTF volume into price bins
if inSes and not na(vMap)
    if na(sHi) or high > sHi
        sHi := high
        hBar := bar_index
    if na(sLo) or low < sLo
        sLo := low
        lBar := bar_index
 
    // Distribute LTF volume across ABSOLUTE price bins
    if array.size(lVol) > 0 and not na(bHgt) and bHgt > 0
        for i = 0 to array.size(lVol) - 1
            float v = array.get(lVol, i)
            float h = array.get(lH,   i)
            float l = array.get(lL,   i)
            if not na(v) and not na(h) and not na(l) and v > 0
                int loB = int(math.floor(l / bHgt))
                int hiB = int(math.floor(h / bHgt))
                if hiB < loB
                    hiB := loB
                
                int span = math.min(int(math.floor(mxBns / 2)), hiB - loB)
                int midB = int(math.round((loB + hiB) / 2.0))
                int loB2 = midB - span
                int hiB2 = midB + span
                int nBns = hiB2 - loB2 + 1
                float vPer = v / nBns
                for b = loB2 to hiB2
                    float cur = nz(map.get(vMap, b), 0.0)
                    map.put(vMap, b, cur + vPer)
 
    // Calculate POC
    mVol := 0.0
    for [k, v] in vMap
        if v > mVol
            mVol := v
            pocK := k
 
    if mVol > 0 and not na(bHgt) and bHgt > 0
        mnBin := int(math.floor(sLo / bHgt))
        mxBin := int(math.floor(sHi / bHgt))
        nBins := mxBin - mnBin + 1
        if nBins < 1
            nBins := 1
        
        if not na(pocK)
            int sPocK = math.max(mnBin, math.min(mxBin, pocK))
            pcPrc := nBins > 1 ? sLo + (sPocK - mnBin) * ((sHi - sLo) / (nBins - 1)) : (sLo + sHi) / 2
            pTxt := "POC: " + str.tostring(mVol, format.volume) + " ▶"
// }
 
 
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
if inSes and not na(vMap)
    // Update Session Name Label position (Middle X, Top Y)
    if shwNm
        if na(nmLbl)
            nmLbl := label.new(bar_index, sHi, sesTy, color=color.new(color.black, 100), textcolor=prCol, style=label.style_label_down, size=size.normal)
        else
            int mBar = int(math.round((stBar + bar_index) / 2.0))
            label.set_xy(nmLbl, mBar, sHi)
 
    // Draw Box
    if na(cBox)
        cBox := box.new(stBar, sHi, bar_index, sLo, bgcolor=bxBgC, border_color=bxBrC, border_width=1)
    else
        box.set_right(cBox, bar_index)
        box.set_top(cBox,   sHi)
        box.set_bottom(cBox, sLo)
 
    // High / Low Range Labels — placed at the exact touch bar
    if na(hiLbl)
        hiLbl := label.new(hBar, sHi, "▼",
             color     = color.new(color.black, 100),
             textcolor = bxBrC,
             style     = label.style_label_down,
             tooltip   = str.tostring(sHi, format.mintick))
    else
        label.set_xy(hiLbl, hBar, sHi)
        label.set_tooltip(hiLbl, str.tostring(sHi, format.mintick))
 
    if na(loLbl)
        loLbl := label.new(lBar, sLo, "▲",
             color     = color.new(color.black, 100),
             textcolor = bxBrC,
             style     = label.style_label_up,
             tooltip   = str.tostring(sLo, format.mintick))
    else
        label.set_xy(loLbl, lBar, sLo)
        label.set_tooltip(loLbl, str.tostring(sLo, format.mintick))
 
    // POC Line Drawing
    if shwPc and not na(pcPrc)
        if pcMod == "Static"
            if na(pcLin)
                pcLin := line.new(stBar, pcPrc, bar_index, pcPrc, color=pcCol, style=line.style_solid, width=pcWid)
            else
                line.set_x2(pcLin, bar_index)
                line.set_y1(pcLin, pcPrc)
                line.set_y2(pcLin, pcPrc)
        else
            // Rolling mode with polyline
            bool prChg = false
            float lPrc = na
            if array.size(rPts) > 0
                chart.point lPt = array.get(rPts, array.size(rPts) - 1)
                lPrc := lPt.price
                if lPrc != pcPrc
                    prChg := true
                    
            if prChg and not na(lPrc)
                array.push(rPts, chart.point.from_index(bar_index, lPrc))
            array.push(rPts, chart.point.from_index(bar_index, pcPrc))
            
            if not na(rPoly)
                rPoly.delete()
            rPoly := polyline.new(rPts, line_color=pcCol, line_width=pcWid)
 
        // Label at the start
        if na(pcLbl)
            pcLbl := label.new(stBar, pcPrc, pTxt, color=color.new(color.black, 100), textcolor=pcCol, style=label.style_label_right, size=pcSz)
        else
            if pcMod == "Static"
                label.set_xy(pcLbl, stBar, pcPrc)
            else
                label.set_x(pcLbl, stBar)
            label.set_text(pcLbl, pTxt)
 
    // Live footprint redraw
    if shwLv and map.size(vMap) > 0
        for l in lLbls
            label.delete(l)
        array.clear(lLbls)
 
        for [k, v] in vMap
            int chrs = math.max(1, int(math.round(v / mVol * mxChr)))
            string txt = ""
            
            // Build the text with gradient emojis or solid blocks
            if useEm
                int nEmj = array.size(emojs)
                for c = 1 to chrs
                    int cIdx = math.min(nEmj - 1, math.max(0, int((c - 1) / chrs * nEmj)))
                    txt := txt + array.get(emojs, cIdx)
            else
                for c = 1 to chrs
                    txt := txt + "▇"
                    
            // Safety clamp to keep labels strictly inside the box bounds
            int sK = math.max(mnBin, math.min(mxBin, k))
            float prc = nBins > 1 ? sLo + (sK - mnBin) * ((sHi - sLo) / (nBins - 1)) : (sLo + sHi) / 2
            
            // Draw using the offset and style
            lbl = label.new(bar_index + prOff, prc, txt,
                 color=color.new(color.black, 100), textcolor=prCol,
                 style=lblSt, size=strSz)
            array.push(lLbls, lbl)
 
// Session End — draw final profile if live mode is off
if enSes and not shwLv and not na(vMap) and map.size(vMap) > 0
    for l in lLbls
        label.delete(l)
    array.clear(lLbls)
 
    if mVol > 0 and not na(bHgt) and bHgt > 0
        int eBar = bar_index - 1
        
        // Static POC line final update
        if shwPc and not na(pocK) and pcMod == "Static"
            int sPocK = math.max(mnBin, math.min(mxBin, pocK))
            float pEnd = nBins > 1 ? sLo + (sPocK - mnBin) * ((sHi - sLo) / (nBins - 1)) : (sLo + sHi) / 2
            if na(pcLin)
                pcLin := line.new(stBar, pEnd, eBar, pEnd, color=pcCol, style=line.style_solid, width=pcWid)
            else
                line.set_x2(pcLin, eBar)
                line.set_y1(pcLin, pEnd)
                line.set_y2(pcLin, pEnd)
 
        // Draw final footprint labels
        for [k, v] in vMap
            int chrs = math.max(1, int(math.round(v / mVol * mxChr)))
            string txt = ""
            
            // Build the text with gradient emojis or solid blocks
            if useEm
                int nEmj = array.size(emojs)
                for c = 1 to chrs
                    int cIdx = math.min(nEmj - 1, math.max(0, int((c - 1) / chrs * nEmj)))
                    txt := txt + array.get(emojs, cIdx)
            else
                for c = 1 to chrs
                    txt := txt + "▇"
            
            int sK = math.max(mnBin, math.min(mxBin, k))
            float prc = nBins > 1 ? sLo + (sK - mnBin) * ((sHi - sLo) / (nBins - 1)) : (sLo + sHi) / 2
            
            lbl = label.new(eBar + prOff, prc, txt,
                 color=color.new(color.black, 100), textcolor=prCol,
                 style=lblSt, size=strSz)
            array.push(lLbls, lbl)
// }

← 返回列表