Market Sessions & Viewer Panel [By MUQWISHI]

MUQWISHI · study · 431 行 · 点赞 2,454 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MUQWISHI
 
//@version=6
indicator("Market Sessions & Viewer Panel [By MUQWISHI]", "Sessions Viewer [MUQWISHI]", overlay = true, 
          max_boxes_count = 500, max_labels_count = 500)
import n00btraders/Timezone/1 as tz
var sp = "                                "
 
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                   INPUT                                    |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
var timZon = input.enum(tz.TimezoneDST.UTC, "Timezone", display = display.none).tostring()
 
// ++++++++++++++++++++++ {SESSIONS}
type sessions
    bool   enable
    string name
    color  colour 
    string session
 
var G1 = "Sessions"
// Session No1
sess01 = sessions.new(
     input.bool(true, "Session №1",            group = G1, display = display.none, inline = "1"),
     input.string("Tokyo", "",                 group = G1, display = display.none, inline = "1"),
     input.color(color.new(#009688, 80), "", group = G1, display = display.none, inline = "1"),
     input.session("0000-0900", sp,            group = G1, display = display.none))
 
// Session No2
sess02 = sessions.new(
     input.bool(true, "Session №2",            group = G1, display = display.none, inline = "2"),
     input.string("London", "",                group = G1, display = display.none, inline = "2"),
     input.color(color.new(#FFC107, 80), "", group = G1, display = display.none, inline = "2"),
     input.session("0700-1500", "",            group = G1, display = display.none))
 
// Session No3
sess03 = sessions.new(
     input.bool(true, "Session №3",             group = G1, display = display.none, inline = "3"),
     input.string("New York", "",               group = G1, display = display.none, inline = "3"),
     input.color(color.new(#AB47BC , 80), "", group = G1, display = display.none, inline = "3"),
     input.session("1300-2000", "",             group = G1, display = display.none))
 
// Session No4
sess04 = sessions.new(
     input.bool(false, "Session №4",            group = G1, display = display.none, inline = "4"),
     input.string("Sydney", "",                 group = G1, display = display.none, inline = "4"),
     input.color(color.new(#FF5722 , 80), "", group = G1, display = display.none, inline = "4"),
     input.session("2100-0600", "",             group = G1, display = display.none))
 
 
// ++++++++++++++++++++++ {SESSIONS TABLE}
var G2 = "Session Panel"
snpChk = input.bool(true, "Enable Session Panel?",           group = G2, display = display.none, inline = "1")
 
snpTyp = input.string("Candle", "            Chart Types", ["Candle", "Bar"],  
                                                             group = G2, display = display.none, inline = "2")
snpCl1 = input.color(color.new(#00FF00 , 00), "   ",       group = G2, display = display.none, inline = "2")
snpCl2 = input.color(color.new(#FF0000 , 00), "  ",        group = G2, display = display.none, inline = "2")
 
snpWd  = input.float(1.5, "            W x H             ", 0.01, step = 0.1,
                                                             group = G2, display = display.none, inline = "3")
snpHi  = input.float(2 , "x", 0.01, step = 0.1,              group = G2, display = display.none, inline = "3")/10
 
snpLoc = input.string(  "Bottom Right"  , "            Location       ", 
          ["Top Right" , "Middle Right" , "Bottom Right"   , 
           "Top Center", "Middle Center", "Bottom Center"  , 
           "Top Left"  , "Middle Left"  , "Bottom Left" ]  , group = G2, display = display.none, inline = "4")
 
snpSrt = input.string( "Right to Left"  , "            Sort                 ", 
          ["Default", "Right to Left" , "Left to Right"],    group = G2, display = display.none, inline = "5")
 
 
// ++++++++++++++++++++++ {SESSIONS CHART}
var G3 = "Chart Visualization"
drwChk = input.bool(true, "Enable Chart Visualization?",     group = G3, display = display.none, inline = "1")
drwTyp = input.string("Box", "            Draw Shape", ["Box", "Candle"],  
                                                             group = G3, display = display.none, inline = "2")
 
drwSyl = input.string("-----------", "            Style | Size ",  ["________", "-----------", "..........."],      
                                                             group = G3, display = display.none, inline = "3")
drwSiz = input.int(1, "", 0,                                 group = G3, display = display.none, inline = "3")
 
labLoc = input.string(  "Above"     , "            Label Point  ", 
                                         ["Above", "Below"], group = G3, display = display.none, inline = "4")
labSiz = input.int(10, "", 0,                                group = G3, display = display.none, inline = "4")
 
nmeLab = input.bool(true, "Show Name             ",          group = G3, display = display.none, inline = "6")
prcLab = input.bool(true, "Show Last",                       group = G3, display = display.none, inline = "6")
chgLab = input.bool(true, "Show Change         ",            group = G3, display = display.none, inline = "7")
chpLab = input.bool(true, "Show Change(%)",                  group = G3, display = display.none, inline = "7")
 
 
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                CALCULATION                                 |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
type data
    string status   = na // Sesssion: Open/Closed
    int    sBr      = na // Inital Bar
    int    eBr      = na // Final Bar
    float  op       = na // Open
    float  cs       = na // Close
    float  hi       = na // High
    float  lw       = na // Low
    float  vol      = na // Volume
    sessions input  = na
 
type chartVisual
    box   highlight = na // Highlighted Box
    box   body      = na // Candle's Body 
    line  upWick    = na // Candle's Upper Wick
    line  dnWick    = na // Candle's Lower Wick 
    label info      = na // Session Chart Label Info
 
 
// +++++++++++++++ Round to mintick 
toStr(x) =>
    str.tostring(x, str.replace(str.tostring(syminfo.mintick), "1", "0"))
 
 
// +++++++++++++++ Current Time in HHmm format
nowTime = str.tonumber(str.format_time(time, "HHmm", timZon))
 
 
// +++++++++++++++ Drawing Tools
// Line Style Function
styLine(s) =>
    switch s
        "..........."  => line.style_dotted   
        "-----------"  => line.style_dashed   
        "________"     => line.style_solid
 
// ++++++++++ Box
newBox(tp, x2, bt, brClr, bgClr) =>
    box.new(bar_index, tp, x2, bt, brClr, drwSiz, styLine(drwSyl), extend.none, xloc.bar_index, bgClr)
 
// ++++++++++ Line 
newLine(x, y1, y2, brClr) =>
    line.new(x, y1, x, y2, xloc.bar_index, extend.none, brClr, styLine(drwSyl), drwSiz)
 
// ++++++++++ Label 
newLab(x, y, txtClr, txt) =>
    style = labLoc == "Above" ? label.style_label_down : label.style_label_up
    label.new(x, y, txt, xloc.bar_index, yloc.price, color(na), style, txtClr, labSiz, 
     text_formatting = text.format_bold)
 
chartLable(name, cs, op) =>
    txt01 =  nmeLab ? name : ""
    txt02 = (txt01 != "" ? "\n" : "") 
             + (prcLab ? toStr(cs)      + "\n" : "")
             + (chgLab ? toStr(cs - op) +  " " : "")
             + (chpLab ? "(" + str.tostring((cs - op)/op * 100, format.percent) + ")" : "")
 
    txt01 + txt02
    
 
// +++++++++++++++ Drawing Stages
// New Draw
method newDraw(chartVisual this, data prc, xLimit) =>
    // Chart Sketch 
    if drwChk
        avgBar = int(math.avg(prc.sBr, prc.sBr + xLimit))
        color1 = prc.input.colour
        color2 = color.new(color1, 0)
        txtLab = chartLable(prc.input.name, prc.cs, prc.op)
        
        // Highlight
        this.highlight := newBox(prc.hi, prc.sBr + xLimit, prc.lw, color(na), color1)
 
        // Box
        if drwTyp == "Box" 
            this.highlight.set_border_color(color2)
 
        // Candle
        else if drwTyp == "Candle" 
            this.body   := newBox(prc.op, prc.sBr + xLimit, prc.cs, color2,   color1)
            this.upWick := newLine(avgBar, prc.hi, math.max(prc.op, prc.cs), color2)
            this.dnWick := newLine(avgBar, prc.lw, math.min(prc.op, prc.cs), color2)
 
        // Chart Label 
        if txtLab != ""
            loc = labLoc == "Above" ? prc.hi : prc.lw
            this.info := newLab(avgBar, loc, color2, txtLab) 
 
// Update Draw Function
method updateDraw(chartVisual this, data prc, xLimit) =>
    // Chart Sketch 
    if drwChk
        avgBar = int(math.avg(prc.sBr, (prc.sBr + xLimit)))
        txtLab = chartLable(prc.input.name, prc.cs, prc.op)
 
        // Box
        this.highlight.set_right(prc.sBr + xLimit)
        this.highlight.set_top(prc.hi)
        this.highlight.set_bottom(prc.lw)
 
        // Candle
        if drwTyp == "Candle" 
            // Candle Body
            this.body.set_right(prc.sBr + xLimit)
            this.body.set_bottom(prc.cs)
            // Upper Wick
            this.upWick.set_xy1(avgBar, math.max(prc.op, prc.cs))
            this.upWick.set_xy2(avgBar, prc.hi)
            // Lower Wick
            this.dnWick.set_xy1(avgBar, math.min(prc.op, prc.cs))
            this.dnWick.set_xy2(avgBar, prc.lw)
 
        // Chart Label 
        if txtLab != ""
            this.info.set_text(txtLab)
            this.info.set_x(avgBar)
            this.info.set_y(labLoc == "Above" ? prc.hi : prc.lw)
 
// Wrap-up Draw
method wrapDraw(chartVisual this, data prc) =>
    // Chart Sketch 
    if drwChk
        avgBar = int(math.avg(prc.sBr, prc.eBr))
        txtLab = chartLable(prc.input.name, prc.cs, prc.op)
 
        // Box
        this.highlight.set_right(prc.eBr)
 
        // Candle
        if drwTyp == "Candle" 
            // Candle Body
            this.body.set_right(prc.eBr)
            // Upper Wick
            this.upWick.set_x1(avgBar)
            this.upWick.set_x2(avgBar)
            // Lower Wick
            this.dnWick.set_x1(avgBar)
            this.dnWick.set_x2(avgBar)
 
        // Chart Label 
        if txtLab != ""
            this.info.set_text(txtLab)
            this.info.set_x(avgBar)
 
 
// +++++++++++++++ Main
var collect = map.new<int, data>()
method main(sessions this, int key) =>
    var prc = data.new()
    if timeframe.isintraday
        enable  = this.enable
        session = time(timeframe.period, this.session, timZon)
        
        if enable
            var chartDraw = chartVisual.new()
 
            var maxBarsArray = array.new<int>(7, 0)
            var day = dayofweek(time(timeframe.period, timZon)) - 1
 
            var int   maxBars = na
            var int   limBars = na 
 
            // Adjust Time 
            var sSessTime = str.tonumber(str.split(this.session, "-").get(0))
            var eSessTime = str.tonumber(str.split(this.session, "-").get(1))
            timeCond = (nowTime >= sSessTime and nowTime[1] <= eSessTime and nowTime < nowTime[1])
 
            // [3] Wrap-up Drawing
            if (na(session) and not na(session[1])) or timeCond
                prc.status := "🔴"
                chartDraw.wrapDraw(prc)
 
            // [1] Create New Values & Drawing
            if (not na(session) and na(session[1])) or timeCond
                if snpSrt != "Default"
                    collect.remove(key)
                maxBarsArray.set(day, nz(math.max(maxBarsArray.get(day), prc.eBr - prc.sBr)))
 
                day := dayofweek(time(timeframe.period, timZon)) - 1
                maxBars := maxBarsArray.get(day)
                limBars := maxBars > 500 ? 500 : maxBars
 
                prc := data.new("🟢", bar_index, na, open, close, high, low, volume, this)
 
                if (timeframe.isseconds ? timeframe.multiplier >= 5 : true) and not(timeframe.isticks)
                    chartDraw.newDraw(prc, limBars)
 
            // [2] Uodate Values & Drawing
            else if not na(session)
                prc.eBr := bar_index 
                prc.cs := close 
                prc.hi := math.max(prc.hi, high)
                prc.lw := math.min(prc.lw,  low)
                prc.vol += volume
 
                limBars := limBars < maxBars ? limBars + 1 : limBars
                chartDraw.updateDraw(prc, limBars)
 
 
            // ++++++++++ Fill Map 
            if snpChk 
                if not na(prc.sBr)
                    collect.put(key, prc)
                else 
                    collect.put(key, na)
// Call Sessions 
sess01.main(1), sess02.main(2), sess03.main(3), sess04.main(4)
 
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                             PANEL - TABLE PLOT                             |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// ++++++++++ Get Tbale Location & Size
locNsze(x) => 
    y   = str.split(str.lower(x), " ")
    out = ""
    for i = 0 to y.size() - 1
        out += y.get(i)
        if i != y.size() - 1
            out += "_"
    out
 
 
// ++++++++++ Convert Prices unit into Cell points  
point(x) => 
    maxHi  = float(na), minLw = float(na)
    for value in map.values(collect)
        maxHi := na(maxHi) ? value.hi : math.max(maxHi, value.hi)
        minLw := na(minLw) ? value.lw : math.min(minLw, value.lw)
 
    33 - math.round(33/(maxHi - minLw) * (x - minLw)) + 1
 
 
// ++++++++++ Create Table Plot 
var szBd = snpTyp == "Bar" ? snpWd/4 : (2 * snpWd)/5
mxSz = nz(collect.size())
nCol = 5 * mxSz
nRow = 35
var table plt =  na
 
 
// ++++++++++ Cells Functions
// Info Cell
infoCell(col, row, txt, txColour, bgColour, tip) =>
    plt.cell(col, row, txt, text_color = txColour, bgcolor = bgColour, tooltip = tip)
 
// Draw Cell
pltCell(col, row, w, h, color, tip) =>
    plt.cell(col, row, width = w, height = h, bgcolor = color, tooltip = tip)
 
// To Draw Candle 
drawCndl(k, op, hp, lp, cp, colour, tip) =>
    // Body 
    for w = math.max(cp, op) to math.min(cp, op)
        pltCell(k + 1, w, szBd  , snpHi, colour, tip) 
        pltCell(k + 2, w, szBd/3, snpHi, colour, tip)
        pltCell(k + 3, w, szBd  , snpHi, colour, tip)  
    // Up Wick
    for w = math.max(cp, op) to hp
        pltCell(k + 2, w, szBd/3, snpHi, colour, tip)
    // Down Wick
    for w = math.min(cp, op) to lp
        pltCell(k + 2, w, szBd/3, snpHi, colour, tip)
 
// To Draw Bar
drawBar(k, op, hp, lp, cp, colour, tip) =>
    // Body
    for w = lp to hp
        pltCell(k + 2, w, szBd, snpHi, colour, tip)
    // Open
    pltCell(k + 1, op, szBd, snpHi, colour, tip) 
    pltCell(k + 1, op < 17 ? op + 1 : op - 1, szBd, snpHi, colour, tip) 
 
    // Close
    pltCell(k + 3, cp, szBd, snpHi, colour, tip) 
    pltCell(k + 3, cp < 17 ? cp + 1 : cp - 1, szBd, snpHi, colour, tip) 
 
 
// ++++++++++ Create Table Plot
if barstate.islast
    plt.delete()
    if snpChk and mxSz > 0 
        plt := table.new(locNsze(snpLoc), nCol, nRow, color(na), chart.fg_color, 1, color(na), 0)
 
        // Reset Cells
        values  = map.values(collect)
        u = 0, r = snpSrt == "Left to Right" ? values.size() - 1 : 0
        for i = 0 to nCol - 1
            u += 1
            colour = collect.values().get(r).input.colour
            if u >= 5 
                r := snpSrt == "Left to Right" ? r - 1 : r + 1
                u := 0
 
            for j = 0 to nRow - 1
                pltCell(i, j, 0.1, 0.1, colour, "")
 
        // Fill Cells 
        k = 0
        for i  = (snpSrt == "Left to Right" ? values.size() - 1 : 0) to 
                 (snpSrt == "Left to Right" ? 0 : values.size() - 1)
 
            // Get Values
            name   = values.get(i).input.name
            status = values.get(i).status
            sess   = values.get(i).input.session
            color1 = values.get(i).input.colour
            color2 = color.new(color1, 0)
 
            op = values.get(i).op, hi = values.get(i).hi, 
            lw = values.get(i).lw, cs = values.get(i).cs, 
 
            // Bar Color 
            barColor  = cs >= op ? snpCl1 : snpCl2
 
            // Tool Tips 
            statusTip = (status == "🟢" ? "Session Open" : "Session Closed") + "\n   " + sess
            priceTip  =  "𝐎: " + toStr(op) + "\n𝐇: " + toStr(hi) + "\n𝐋: " + toStr(lw) + "\n𝐂: " + toStr(cs) 
                         + "\n\n𝐕𝐨𝐥: " + str.tostring(values.get(i).vol, format.volume)
 
            // Space for Organizing  
            infoCell(k,     1, "      " , color2, color1, "")
            infoCell(k + 4, 1, "      " , color2, color1, "")
 
            // Info Market Session 
            infoCell(k, 0, status + "\n" + name + "\n" +  toStr(cs) + "\n(" 
                         + str.tostring((cs - op)/op * 100, format.percent) + ")\n", color2, color1, statusTip)
            plt.merge_cells(k , 0, k + 4,  0)
 
            // Draw Bar
            if snpTyp == "Candle"
                drawCndl(k, point(op), point(hi), point(lw), point(cs), barColor, priceTip) 
            else 
                drawBar (k, point(op), point(hi), point(lw), point(cs), barColor, priceTip) 
 
            k += 5

← 返回列表