market-sessions-viewer-panel-by-muqwishi.pine
Copy
// 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_lines_count = 500, max_boxes_count = 500, max_labels_count = 500)
import n00btraders/Timezone/1 as tz
import MUQWISHI/colorLab/2 as cl
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 = input.bool(true, "Session №1", group = G1, display = display.none, inline = "1")
sess01 = sessions.new(SESS01,
input.string("Tokyo", "", group = G1, display = display.none, inline = "1", active = SESS01),
input.color(color.new(cl.DarkMagenta, 80), "", group = G1, display = display.none, inline = "1", active = SESS01),
input.session("0000-0900", sp, group = G1, display = display.none, active = SESS01))
// Session No2
SESS02 = input.bool(true, "Session №2", group = G1, display = display.none, inline = "2")
sess02 = sessions.new(SESS02,
input.string("London", "", group = G1, display = display.none, inline = "2", active = SESS02),
input.color(color.new(cl.OrangeRed, 80), "", group = G1, display = display.none, inline = "2", active = SESS02),
input.session("0700-1500", "", group = G1, display = display.none, active = SESS02))
// Session No3
SESS03 = input.bool(true, "Session №3", group = G1, display = display.none, inline = "3")
sess03 = sessions.new(SESS03,
input.string("New York", "", group = G1, display = display.none, inline = "3", active = SESS03),
input.color(color.new(cl.DarkTurquoise , 80), "", group = G1, display = display.none, inline = "3", active = SESS03),
input.session("1300-2000", "", group = G1, display = display.none, active = SESS03))
// Session No4
SESS04 = input.bool(false, "Session №4", group = G1, display = display.none, inline = "4"),
sess04 = sessions.new(SESS04,
input.string("Sydney", "", group = G1, display = display.none, inline = "4", active = SESS04),
input.color(color.new(cl.LightSlateGray , 80), "", group = G1, display = display.none, inline = "4", active = SESS04),
input.session("2100-0600", "", group = G1, display = display.none, active = SESS04))
// ++++++++++++++++++++++ {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", active = snpChk)
snpCl1 = input.color(cl.Green, " ", group = G2, display = display.none, inline = "2", active = snpChk)
snpCl2 = input.color(cl.Red, " ", group = G2, display = display.none, inline = "2", active = snpChk)
snpWd = input.float(1.5, " W x H ", 0.01, step = 0.1,
group = G2, display = display.none, inline = "3", active = snpChk)
snpHi = input.float(2 , "x", 0.01, step = 0.1, group = G2, display = display.none, inline = "3", active = snpChk)/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", active = snpChk)
snpSrt = input.string( "Right to Left" , " Sort ",
["Default", "Right to Left" , "Left to Right"], group = G2, display = display.none, inline = "5", active = snpChk)
// ++++++++++++++++++++++ {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", active = drwChk)
drwSyl = input.string("-----------", " Style | Size ", ["________", "-----------", "..........."],
group = G3, display = display.none, inline = "3", active = drwChk)
drwSiz = input.int(1, "", 0, group = G3, display = display.none, inline = "3", active = drwChk)
labLoc = input.string( "Above" , " Label Point ",
["Above", "Below"], group = G3, display = display.none, inline = "4", active = drwChk)
labSiz = input.int(12, "", 0, group = G3, display = display.none, inline = "4", active = drwChk)
nmeLab = input.bool(true, "Show Name ", group = G3, display = display.none, inline = "6", active = drwChk)
prcLab = input.bool(true, "Show Last", group = G3, display = display.none, inline = "6", active = drwChk)
chgLab = input.bool(true, "Show Change ", group = G3, display = display.none, inline = "7", active = drwChk)
chpLab = input.bool(true, "Show Change(%)", group = G3, display = display.none, inline = "7", active = drwChk)
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | CALCULATION |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
type data
string status = na
int tdDay = na
int sBr = na
int eBr = na
float op = na
float cs = na
float hi = na
float lw = na
float vol = na
sessions input = na
type chartVisual
box highlight = na
box body = na
line upWick = na
line dnWick = na
label info = na
// +++++++++++++++ Round to mintick
toStr(x) =>
str.tostring(x, str.replace(str.tostring(syminfo.mintick), "1", "0"))
// +++++++++++++++ Current Bar Time in HHmm format
barTime = 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, cl.cNone, style, txtClr, labSiz)
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, cl.cNone, 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
itsTime = time(timeframe.period, timZon)
dayWeek = dayofweek(itsTime)
var tradeDay = 0, tradeDay := dayWeek != dayWeek[1] ? tradeDay + 1 : tradeDay
var rawCollect = 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)
// Main
if enable and key < 5
// Chart Plot
var chartDraw = chartVisual.new()
// Define Variable To Record Number of bars per sesseion per day
var day = dayWeek - 1
var maxBarsArray = array.new<int>(7, 0)
var maxBars = int(na)
var limBars = int(na)
// Detect Time
var sSessTime = str.tonumber(str.split(this.session, "-").get(0))
var eSessTime = str.tonumber(str.split(this.session, "-").get(1))
timeCond = (barTime >= sSessTime and barTime[1] <= eSessTime and barTime < barTime[1])
// [3] Wrap-up Drawing
if not na(prc.sBr) and ((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"
rawCollect.remove(key)
maxBarsArray.set(day, nz(math.max(maxBarsArray.get(day), prc.eBr - prc.sBr)))
day := dayWeek - 1
maxBars := maxBarsArray.get(day)
limBars := maxBars > 500 ? 500 : maxBars
prc := data.new("🟢", tradeDay, bar_index, bar_index, 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)
rawCollect.put(key, prc)
// +++++++++ Remove Values From Map
if tradeDay > prc.tdDay + 1
rawCollect.remove(key)
// Call Sessions
sess01.main(1), sess02.main(2), sess03.main(3), sess04.main(4)
// Remove NA Values
collect = rawCollect.copy()
for [key, value] in collect
if na(collect.get(key))
collect.remove(key)
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | 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)
40 - math.round(40/(maxHi - minLw) * (x - minLw)) + 2
// ++++++++++ Create Table Plot
var szBd = snpTyp == "Bar" ? snpWd/4 : (2 * snpWd)/5
mxSz = nz(collect.size())
nCol = 5 * mxSz
nRow = 44
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, cl.cNone, chart.fg_color, 1, cl.cNone, 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
if j == nRow - 1
pltCell(i, j, 0.1, 0.5, colour, "")
else
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, ""), plt.cell_set_height(k, 1, 0.01)
infoCell(k + 4, 1, " " , color2, color1, ""), plt.cell_set_height(k + 4, 1, 0.01)
// Info Market Session
infoCell(k, 0, status + "\n" + name + "\n" + toStr(cs) + "\n("
+ str.tostring((cs - op)/op * 100, format.percent) + ")", 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
Show all 452 lines