Code: Select all
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © malk1903
//@version=5
indicator(title="ICT Opening Gaps [MK]", shorttitle = 'Opening Gaps [MK]',overlay=true, max_boxes_count = 500, max_labels_count = 500)
volimba = input.bool(defval=true, title='Daily Volume Imbalance', group='Enable/Disable Section---------------------------------------------', inline = '01')
WOG = input.bool(defval=true, title='New Week Opening Gap', group='Enable/Disable Section---------------------------------------------', inline = '02')
sessiongaps = input.bool(defval=true, title='New Day Opening Gap', group='Enable/Disable Section---------------------------------------------', inline = '02')
DOG = input.bool(defval=true, title='RTH Opening Gap - USE 1min Timeframe', group='Enable/Disable Section---------------------------------------------', inline = '03')
timeZone = input.string("GMT-5", title="Time Zone", options=["GMT-10","GMT-9","GMT-8","GMT-7","GMT-6","GMT-5","GMT-4","GMT-3","GMT-2","GMT-1","GMT","GMT+1","GMT+2","GMT+3","GMT+4","GMT+5","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10"], group="Timezone")
//show930 = input.bool(true, "0930 Opening Line", group='Enable/Disable Section---------------------------------------------', inline = '03')
/////////////////////////////////////////////Volume Imbalances
boxlength = 1
viMaxBoxSet = input.int(defval=6, title="Maximum VI Box Displayed", group='Daily Volume Imbalances-----------------------------------------', inline='1')
mitig_typeVI = "Body"
UseBodyForMitigationVI = mitig_typeVI == "Body" ? true : false
VOLimb_extend = true
tfvi = "D"
color c_box = input.color(color.new(color.orange,70), 'Volume Imbalance Box', group='Daily Volume Imbalances-----------------------------------------', inline='3')
color c_bord = input.color(color.new(color.white,100), 'Volume Imbalance Border', group='Daily Volume Imbalances-----------------------------------------', inline='3')
color mit_c = input.color(color.new(color.orange,100), title = 'Fully Mitigated Box', group='Daily Volume Imbalances-----------------------------------------', inline='4')
color mitbord_c = input.color(color.new(color.orange,100), title = 'Fully Mitigated Border', group='Daily Volume Imbalances-----------------------------------------', inline='4', tooltip ="Full mitigation is when imbalance has been passed through with buy side + sell side")
i_BoxBordervi = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group='Daily Volume Imbalances-----------------------------------------', inline="5")
BoxBorderw = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group='Daily Volume Imbalances-----------------------------------------', inline='5')
novolsun = input.bool(defval=true, title="Dont Show Daily Volume Imbalance on top of New Week Open Gap", tooltip="Daily Volume Imbalances and New Week Opening Gaps will both be displayed in the same area, Enabling this checkbox will remove the Daily volume imbalance so it does not overlap the New Week Opening Gap", inline="6")
plotBoxLabelvi = input.bool(defval=true, title="Show Symbol ", tooltip="Helps to identify very small volume imblance boxes. Plots asterix symbol at right side of box", inline="7")
BoxLabelColorvi = color.new(color.orange,0)
BoxLabelSizevi = size.large
var box[] greenboxes = array.new_box()
var box[] redboxes = array.new_box()
line_style_function(input_var) =>
switch input_var
"dotted (┈)" => line.style_dotted
"dashed (╌)" => line.style_dashed
=> line.style_solid
BoxBordervi = line_style_function(i_BoxBordervi)
//Data
open0 = request.security(syminfo.tickerid, tfvi, open[0], barmerge.gaps_on, barmerge.lookahead_on)
open1 = request.security(syminfo.tickerid, tfvi, open[1], barmerge.gaps_on, barmerge.lookahead_on)
high0 = request.security(syminfo.tickerid, tfvi, high[0], barmerge.gaps_on, barmerge.lookahead_on)
low0 = request.security(syminfo.tickerid, tfvi, low[0], barmerge.gaps_on, barmerge.lookahead_on)
close0 = request.security(syminfo.tickerid, tfvi, close[0], barmerge.gaps_on, barmerge.lookahead_on)
close1 = request.security(syminfo.tickerid, tfvi, close[1], barmerge.gaps_on, barmerge.lookahead_on)
UpVoid = close1 < open0
UpVoidsize = open0 - close1
//is candle and precandle both red or both green
bothred = false
bothgreen = false
if close0 > open0 and close1 > open1
bothgreen := true
if close0 < open0 and close1 < open1
bothred := true
//timeframe good
tfgoodwk = false
if not timeframe.isweekly
tfgoodwk := true
tfgoodmth = false
if not timeframe.ismonthly
tfgoodmth :=true
VIMB_SUN = time('1', "1800-1801",timeZone)//Time input used for removing D volume imbalance/Sunday Opening Gap overlap
if UpVoid and UpVoidsize > 0 and bothgreen and volimba and tfgoodwk and tfgoodmth //and novimb
BOX1 = box.new(left=bar_index[1], top=open0, right=bar_index[0] + boxlength, bottom=close1, text=plotBoxLabelvi?"*":na,border_style=BoxBordervi, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSizevi, text_color=BoxLabelColorvi, border_width = BoxBorderw)
if VIMB_SUN and novolsun
box.set_bgcolor(BOX1, color.new(color.black,100))
box.set_border_color(BOX1, color.new(color.black,100))
else
box.set_bgcolor(BOX1, c_box)
box.set_border_color(BOX1, c_bord)
if array.size(greenboxes) > viMaxBoxSet
box.delete(array.shift(greenboxes))
array.push(greenboxes, BOX1)
BottomVoid = close1 > open0
BottomVoidsize = close1 - open0
if BottomVoid and BottomVoidsize > 0 and bothred and volimba and tfgoodwk and tfgoodmth //and novimb
BOX2 = box.new(left=bar_index[1], top=close1, right=bar_index[0] + boxlength, bottom=open0, text=plotBoxLabelvi?"*":na,border_style=BoxBordervi, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSizevi, text_color=BoxLabelColorvi, border_width = BoxBorderw)
if VIMB_SUN and novolsun
box.set_bgcolor(BOX2, color.new(color.black,100))
box.set_border_color(BOX2, color.new(color.black,100))
else
box.set_bgcolor(BOX2, c_box)
box.set_border_color(BOX2, c_bord)
if array.size(redboxes) > viMaxBoxSet
box.delete(array.shift(redboxes))
array.push(redboxes, BOX2)
if barstate.isconfirmed //and tfgood3
if array.size(greenboxes) > 0
for i = array.size(greenboxes) - 1 to 0 by 1
tbox2 = array.get(greenboxes, i)
top2 = box.get_top(tbox2)
bottom2 = box.get_bottom(tbox2)
ago2 = box.get_left(tbox2)
if VOLimb_extend
box.set_right(tbox2, bar_index + 1)
//////////////////Use body for mitigation
//greenboxes+red candle
redc = open0-close0
greenc = close0-open0
if UseBodyForMitigationVI
//greenboxes+red candle
if open0 > top2 and close0 < bottom2 and redc>0
box.set_bgcolor(tbox2,c_box)
box.set_border_color(tbox2, c_bord)
box.set_right(tbox2, last_bar_index)
//greenboxes+green candle
if open0 < bottom2 and close0 > top2 and greenc>0
box.set_bgcolor(tbox2,mit_c)
box.set_border_color(tbox2, mitbord_c)
box.set_text_color(tbox2, mitbord_c)
box.set_right(tbox2, last_bar_index)
array.remove(greenboxes, i)
if not UseBodyForMitigationVI
//greenboxes+red candle
if high0 > top2 and low0 < bottom2 and redc>0
box.set_bgcolor(tbox2,c_box)
box.set_border_color(tbox2, c_bord)
box.set_right(tbox2, last_bar_index)
//greenboxes+green candle
if low0 < bottom2 and high0 > top2 and greenc>0
box.set_bgcolor(tbox2,mit_c)
box.set_border_color(tbox2, mitbord_c)
box.set_text_color(tbox2, mitbord_c)
box.set_right(tbox2, last_bar_index)
array.remove(greenboxes, i)
if array.size(redboxes) > 0 //and UseBodyForMitigationVI
for i = array.size(redboxes) - 1 to 0 by 1
tbox3 = array.get(redboxes, i)
top3 = box.get_top(tbox3)
bottom3 = box.get_bottom(tbox3)
ago3 = box.get_left(tbox3)
redc = open0-close0
greenc = close0-open0
if VOLimb_extend
box.set_right(tbox3, bar_index + 1)
//redboxes+green candle
if UseBodyForMitigationVI
if open0 < bottom3 and close0 > top3 and greenc>0 //and UseBodyForMitigationVI
box.set_bgcolor(tbox3,c_box)
box.set_border_color(tbox3, c_bord)
box.set_right(tbox3, last_bar_index)
//redboxes+red candle
if open0 > top3 and close0 < bottom3 and redc>0 //and UseBodyForMitigationVI
box.set_bgcolor(tbox3,mit_c)
box.set_border_color(tbox3,mit_c)
box.set_text_color(tbox3, mitbord_c)
box.set_right(tbox3, last_bar_index)
array.remove(redboxes, i)
if not UseBodyForMitigationVI
//redboxes+green candle
if low0 < bottom3 and high0 > top3 and greenc>0
box.set_bgcolor(tbox3,c_box)
box.set_border_color(tbox3, c_bord)
box.set_right(tbox3, last_bar_index)
//redboxes+red candle
if high0 > top3 and low0 < bottom3 and redc>0
box.set_bgcolor(tbox3,mit_c)
box.set_border_color(tbox3,mit_c)
box.set_text_color(tbox3, mitbord_c)
box.set_right(tbox3, last_bar_index)
array.remove(redboxes, i)
////////////////////////////////////////////////////////////////////////////////////Weekly Opening Gap
isNewbar(res) =>
t = time(res)
not na(t) and (na(t[1]) or t > t[1])
type gap
float high
float low
int start
int end
prevgaps = input.int(10, "Previous Weeks to Show", group = "New Week Opening Gap----------------------------------------------", inline="2")
bool showOnHistory = input.bool(true, "Show Previous Week Gaps", group = "New Week Opening Gap----------------------------------------------", inline="2")
color boxBg_curr = input.color(color.new(color.lime,70), "Current", group = "New Week Opening Gap----------------------------------------------", inline="3")
color boxBg = input.color(color.new(color.blue,80), "Box", group = "New Week Opening Gap----------------------------------------------", inline="3")
color boxBc = input.color(color.new(color.white,100), "Border", group = "New Week Opening Gap----------------------------------------------", inline="3")
i_BoxBordernw = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Week Opening Gap----------------------------------------------", inline="3")
BoxBorderwnw = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group = "New Week Opening Gap----------------------------------------------", inline='3')
color boxMl = input.color(color.new(color.silver,0), "Midline", group = "New Week Opening Gap----------------------------------------------", inline="4")
i_mid_line_style = input.string(defval="dotted (┈)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Week Opening Gap----------------------------------------------", inline="4")
mid_line_width = input.int(2, "Width", group = "New Week Opening Gap----------------------------------------------", inline="4")
showhlprices = input.bool(defval=false, title="Show H/L price labels")
BoxBordernw = line_style_function(i_BoxBordernw)
mid_line_style = line_style_function(i_mid_line_style)
getGapData() =>
gap.new(math.max(close[1], open), math.min(close[1], open), time, time_close)
gap weeklyOpenGap = request.security(syminfo.tickerid, "1W", getGapData(), lookahead = barmerge.lookahead_on)
int currentDayTimeClose = request.security(syminfo.tickerid, "1D", time_close, lookahead = barmerge.lookahead_on)
var box[] sgbox = array.new_box()
var line[] sgline = array.new_line()
var label[] p_labelsh = array.new_label()
var label[] p_labelsl = array.new_label()
var line gapmid = na
var label plabelsh = na
linegap = 0.0
linegap := (weeklyOpenGap.high+weeklyOpenGap.low)/2
if isNewbar("W") and (timeframe.isdaily or timeframe.isintraday) and WOG
weeklyOpenGapBox = box.new(bar_index, weeklyOpenGap.high, last_bar_index, weeklyOpenGap.low, xloc = xloc.bar_index, bgcolor = boxBg_curr, border_width = 1, border_color = boxBc)
if showhlprices
plabelsh :=label.new(last_bar_index,linegap, text="high: " + str.tostring(weeklyOpenGap.high) + " low: " + str.tostring(weeklyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_left, size=size.normal)
//plabelsl :=label.new(last_bar_index,linegap, text=str.tostring(weeklyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_up)
if array.size(sgbox) > prevgaps
box.delete(array.shift(sgbox))
box.set_bgcolor(weeklyOpenGapBox[1],color.white)
//box.set_right(sgbox, last_bar_index)
array.push(sgbox, weeklyOpenGapBox)
if array.size(p_labelsh) > prevgaps
label.delete(array.shift(p_labelsh))
array.push(p_labelsh, plabelsh)
gapmid := line.new(x1=bar_index, y1=linegap, x2= last_bar_index, y2=linegap, xloc=xloc.bar_index, color=boxMl, style=mid_line_style, width=mid_line_width)
if array.size(sgline) > prevgaps
line.delete(array.shift(sgline))
array.push(sgline, gapmid)
if not showOnHistory
box.delete(weeklyOpenGapBox[1])
line.delete(gapmid[1])
highlight=true
if highlight
box.set_bgcolor(weeklyOpenGapBox[1],boxBg)
///////////////////////////////////////////////////////////////////////////////////////////////////////////1700 to 1800 Gap
GPL = 'New Day Opening Gap -----------------------------------------------'
prevNDOG = input.int(0, "Previous NDOG to Show", group = GPL, inline="2")
bool DOGhist = input.bool(true, "Show Previous NDOG", group = GPL, inline="2")
i_maxtf = input.int (60, "Max Timeframe", 1, 240, inline="x1", group=GPL)
disp = timeframe.isintraday and timeframe.multiplier <= i_maxtf
i_sessiongaps_color = input.color(defval=color.new(color.white, 100), title="Fill Gap", group=GPL, inline="x1")
i_sessionline_color = input.color(defval=color.new(color.red, 0), title="Close+Open Lines", group=GPL, inline="x2")
i_eight_thirty_line_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=GPL, inline="x2")
i_eight_thirty_line_width = input.int(defval=4, title="Width", minval = 1, maxval = 4, group=GPL, inline="x2")
TDOG_sym = input.bool(defval=true,title="⭐ Symbol for Current NDOG", group=GPL, inline="x3")
i_eight_thirty_text_color = "Black"
// Function that converts the words black/white to their code equivalents
text_color_function(string_color) =>
switch string_color
"Black" => color.black
"White" => color.white
=> color.black
timePeriod(tf, period) => request.security(syminfo.tickerid, tf, time[period])
highPeriod(tf, period) => request.security(syminfo.tickerid, tf, high[period])
lowPeriod(tf, period) => request.security(syminfo.tickerid, tf, low[period])
openPeriod(tf, period) => request.security(syminfo.tickerid, tf, open[period])
line_style_eith_thirty_open = line_style_function(i_eight_thirty_line_style)
yestclose17 = (hour(time, timeZone) == 16) and (minute(time) == 59)
yestopen18 = (hour(time, timeZone) == 18) and (minute(time) == 00)
var line line_yestclose17 = na
var line line_yestopen18 = na
var label labelninethirty = na
var label labelyestclose = na
var box daygapbox = na
var line[] line17 = array.new_line()
var line[] line18 = array.new_line()
if sessiongaps and disp
if (yestclose17)
line_yestclose17 := line.new(x1=bar_index, y1=close, x2=last_bar_index, y2=close, color=i_sessionline_color, style=line_style_eith_thirty_open, width=i_eight_thirty_line_width)
if array.size(line17) > prevNDOG
line.delete(array.shift(line17))
array.push(line17, line_yestclose17)
//if not DOGhist
//line.delete(line_yestclose17[1])
if (yestopen18)
line_yestopen18 := line.new(x1=bar_index, y1=open, x2=last_bar_index, y2=open, color=i_sessionline_color, style=line_style_eith_thirty_open, width=i_eight_thirty_line_width)
open18 = line.get_y1(line_yestopen18)
if TDOG_sym
labelyestclose := label.new(x=last_bar_index, y=open, text='⭐', xloc=xloc.bar_index, color=color.new(color.yellow,100), textcolor=color.white,size=size.huge, textalign = text.align_center, style=label.style_label_left)
label.set_y(labelyestclose, open)
label.delete(labelyestclose[1])
if array.size(line18) > prevNDOG
line.delete(array.shift(line18))
array.push(line18, line_yestopen18)
//if not DOGhist
//line.delete(line_yestopen18[1])
linefill.new(line_yestclose17, line_yestopen18,i_sessiongaps_color)//1700 close to 1800 open
if not DOGhist
line.delete(line_yestclose17[2])
line.delete(line_yestopen18[2])
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////New York Open RTH Gap Box
group930 = "RTH Opening Gap-----------------------------------------------------"
i_maxtf2 = input.int (5, "Max Timeframe", 1, 240, group=group930, inline="l",tooltip="Prices will vary on different timeframes! the script uses the first bars close and the last bars open within the session from the chart timeframe. ONLY 1min CHART TF WILL SHOW ABSOLUTE CORRECT PRICE LEVEL!")
disp2 = timeframe.isintraday and timeframe.multiplier <= i_maxtf2
//GMToff = input.string(defval="GMT-5",title="Timzone", options=["GMT-5", "GMT-4"], group=group930, inline="o")
DOG_cu = input.color(defval=color.new(color.green, 70), title="Box Up", group=group930, inline="m")
DOG_cd = input.color(defval=color.new(color.red, 70), title="Box Down", group=group930, inline="m")
DOG_bc = input.color(defval=color.new(color.white, 100), title="Border", group=group930, inline="m")
DOG_tc = input.color(defval=color.new(color.white, 50), title="Text", group=group930, inline="m")
i_BoxRTH_s = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=group930, inline="n")
i_BoxRTH_w = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group=group930, inline='n')
BorderRTH = line_style_function(i_BoxRTH_s)
IsSessionStart(sessionTime, sessionTimeZone=syminfo.timezone) =>
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
inSess and not inSess[1]
// Create inputs
session = input.session("1614-0931", title="RTH Opening Gap")
//rembox = input.bool(false, title="Remove RTH gap box before 0930 open")
sessionforbox = input.session("0930-1614", title="Only show RTH Gap during this period")
labelos = input.int(defval=0, title="Price Offset", minval= -30, maxval = 100, step=1, inline="1")
showlabs = input.bool(defval=false, title="Price Labels", tooltip="Prices will vary on different timeframes! the script uses the first bars close and the last bars open within the session from the chart timeframe", inline="1")
///0930 Line
line_nine30 = " 0930 Opening Line----------------------------------------------------"
//on0930 = input.bool(defval=true, title="", group=line_nine30, inline="1")
ln_0930 = input.color(defval=color.new(color.aqua, 0), title="Line", group=line_nine30, inline="n")
i_line0930 = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=line_nine30, inline="n")
w_0930 = input.int(defval=4, title="Width", minval=1, maxval=4,group=line_nine30, inline="n")
lbl_0930 = input.color(defval=color.new(color.aqua, 0), title="Text", group=line_nine30, inline="n")
xtnd903 = input.bool(defval=true, title="Extend to right", group=line_nine30, inline="0")
line0930 = line_style_function(i_line0930)
///RTH Closing Line
line_RTH = " RTH Closing Line----------------------------------------------------"
//onRTH = input.bool(defval=true, title="", group=line_RTH, inline="1")
ln_RTH = input.color(defval=color.new(color.silver, 0), title="Line", group=line_RTH, inline="n")
i_lineRTH = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=line_RTH, inline="n")
w_RTH = input.int(defval=2, title="Width", minval=1, maxval=4,group=line_RTH, inline="n")
lbl_RTH = input.color(defval=color.new(color.silver, 0), title="Text", group=line_RTH, inline="n")
xtndclose = input.bool(defval=true, title="Extend to right", group=line_RTH, inline="0")
lineRTH = line_style_function(i_lineRTH)
nround(x) =>
n = math.round(x / syminfo.mintick) * syminfo.mintick
n
// Verify if the bar happened first in the specified session
isFirstOfSession = IsSessionStart(session, timeZone)
var line closel = na
var line openl = na
var label labelopen = na
var label labelclose = na
var box gapbox = na
if isFirstOfSession and DOG and disp2
closel := line.new(bar_index, close, last_bar_index, close, xloc=xloc.bar_index, extend=xtndclose ? extend.right : extend.none, color=ln_RTH, style=lineRTH, width=w_RTH)
line.delete(closel[1])
closeprice = line.get_y1(closel)
labelclose:= not showlabs ? na : label.new(x=last_bar_index + labelos, y=closeprice, text='Close: ' + str.tostring(nround(closeprice)), xloc=xloc.bar_index, color=color.new(color.black,100), textcolor=lbl_RTH,size=size.normal, textalign = text.align_center)
label.delete(labelclose[1])
IsLastBarSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
var int lastBarHour = na
var int lastBarMinute = na
var int lastBarSecond = na
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
if not inSess and inSess[1]
lastBarHour := hour[1]
lastBarMinute := minute[1]
lastBarSecond := second[1]
hour == lastBarHour and minute == lastBarMinute and second == lastBarSecond
// See if current bar is last of session
lastSessionBar = IsLastBarSession(session, timeZone)
InSession(sessionTimes) =>
not na(time(timeframe.period, sessionTimes,timeZone))
if lastSessionBar //and on0930
openl := line.new(bar_index, open, last_bar_index + 20, open, xloc=xloc.bar_index, extend=xtnd903 ? extend.right : extend.none, color=ln_0930, style= line0930, width = w_0930)
line.delete(openl[1])
openprice = line.get_y1(openl)
labelopen:= not showlabs ? na : label.new(x=last_bar_index + labelos, y=openprice, text='0930: ' + str.tostring(nround(openprice)), xloc=xloc.bar_index, color=color.new(color.black,100), textcolor=lbl_0930,size=size.normal, textalign = text.align_center)
label.delete(labelopen[1])
if DOG and disp2
closeprice = line.get_y1(closel)
openprice = line.get_y2(openl)
//price1 = array.get(min1_c,1)
gapbox := InSession(sessionforbox) ? box.new(last_bar_index + 10, closeprice < openprice ? openprice : closeprice, last_bar_index + 20, closeprice < openprice ? closeprice : openprice, text= closeprice < openprice ? "RTH\n GAP\n UP" : "RTH\n GAP\n DOWN", border_color=DOG_bc , bgcolor = closeprice < openprice ? DOG_cu : DOG_cd, border_style=BorderRTH, xloc=xloc.bar_index, border_width = i_BoxRTH_w, text_color = DOG_tc, text_size=size.small) :na
box.delete(gapbox[1])
//tradeTimes = input.session("0700-1300", title="Trading Times")
// InSession() returns 'true' when the current bar happens inside
// the specified session, and 'false' when the bar isn't inside
// that time period (or when the chart's time frame is 1 day or higher).
// Make the background orange for bars inside the session
//bgcolor(InSession(sessionforbox) ? color.new(color.orange, 80) : na)