ICT Premium/Discount Zones with Advanced Fair Value Gaps in TradingView
January 3, 2025 | by admin

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Giovanni-1-
//@version=5
indicator('ICT Premium/Discount' , overlay=true , max_bars_back=500 , max_lines_count = 500)
Timezone = 'America/New_York'
DOM_240 = (timeframe.multiplier >= 240)
DOM240 = (timeframe.multiplier <= 240)
DOM60 = (timeframe.multiplier <= 60)
DOM40 = (timeframe.multiplier <= 40)
DOM30 = (timeframe.multiplier <= 30)
DOM15 = (timeframe.multiplier <= 15)
DOM5 = (timeframe.multiplier <= 5)
DOM1 = (timeframe.multiplier <= 1)
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
// Start Range
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------
// -- Functions
backtestt(int _start) =>
backtestRange = time >= _start and time <= timestamp(year, month, dayofmonth, 23, 59) ? true : false
backtestRange
//-------------------------------------------------------------
start_test = input.time(defval = timestamp('01 Dec 2023 00:00 +0000') , title = 'Start Date ' , inline = '0' , group = 'Start')
col_start_test = input.color(defval = color.rgb(141, 122, 122, 73) , title = '' , inline = '0' , group = 'Start')
show_range = input.bool(defval = true , title = 'Show High - Low Range' , inline = '1' , group = 'Start')
show_txt_HL = input.bool(defval = true , title = 'Price' , inline = '1' , group = 'Start')
BG_range = input.bool(defval = false , title = 'BG' , inline = '1' , group = 'Start')
col_Premium = input.color(defval = color.red , title = 'Premium / Discount' , inline = '2' , group = 'Start')
col_Discoun = input.color(defval = color.green , title = '' , inline = '2' , group = 'Start')
style_PD = input.string(defval = line.style_solid , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '2' , group = 'Start')
size_ln_PD = input(defval = 2 , title = ' Width' , inline = '2' , group = 'Start')
show_Midlin = input(defval = true , title = 'Show Midline' , inline = '3' , group = 'Start')
col_Midline = input.color(defval = color.blue , title = '' , inline = '3' , group = 'Start')
style_Mid = input.string(defval = line.style_solid , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '3' , group = 'Start')
size_ln_Mid = input(defval = 1 , title = 'Width' , inline = '3' , group = 'Start')
show_txt_Mid = input.bool(defval = true , title = 'Price' , inline = '3' , group = 'Start')
show_Quarti = input(defval = false , title = 'Show 75/25 ' , inline = '4' , group = 'Start')
col_Quartil = input.color(defval = color.black , title = '' , inline = '4' , group = 'Start')
style_Quart = input.string(defval = line.style_dotted , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '4' , group = 'Start')
size_ln_Qua = input(defval = 1 , title = 'Width' , inline = '4' , group = 'Start')
show_txt_Qua = input.bool(defval = false , title = 'Price' , inline = '4' , group = 'Start')
//-------------------------------------------------------------
is_line_start = year == year(start_test) and month == month(start_test) and dayofmonth == dayofmonth(start_test) and hour == hour(start_test) and minute == minute(start_test)
var line start_line = na
if is_line_start
start_line := line.new(bar_index , bar_index , bar_index , bar_index + 1 , extend = extend.both , color = col_start_test , style = line.style_dotted)
plotshape(series = is_line_start , title = '' , style = shape.circle , location = location.bottom , color = color.rgb(120, 123, 134, 100) , text = 'Start Line' , textcolor = color.new(col_start_test , 80) , size = size.small)
//-------------------------------------------------------------
//-------------------------------------------------------------
start_range = backtestt(start_test)
start_day = time('D') == time('D')[1]
start_track = start_range
reset_range = start_range and not(start_range[1])
reset_day = time('D') != time('D')[1]
reset_res = reset_range
var _hh = float(na)
_hh := reset_res ? high : start_track? high > _hh ? high : nz(_hh[1]) : na
var _ll = float(na)
_ll := reset_res ? low : start_track? low < _ll ? low : nz(_ll[1]) : na
var line lineVarHighs = line(na)
var line lineVarLows = line(na)
var label Price_high = label(na)
var label Price_low = label(na)
var line lineMid = line(na)
var label Price_5 = label(na)
var line line_75 = line(na)
var line line_25 = line(na)
var label Price_75 = label(na)
var label Price_25 = label(na)
if show_range
if reset_res
lineVarHighs := line.new(bar_index[1] , _hh , bar_index , _hh , xloc.bar_index , extend = extend.none , color = col_Premium , width = size_ln_PD , style = style_PD)
lineVarLows := line.new(bar_index[1] , _ll , bar_index , _ll , xloc.bar_index , extend = extend.none , color = col_Discoun , width = size_ln_PD , style = style_PD)
if show_txt_HL
Price_high := label.new(x = last_bar_index , y = _hh , text = show_txt_HL ? str.tostring(_hh) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Premium, textalign = text.align_right , tooltip = 'Premium High' )
Price_low := label.new(x = last_bar_index , y = _ll , text = show_txt_HL ? str.tostring(_ll) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_center , textcolor = col_Discoun , textalign = text.align_right , tooltip = 'Discount Low')
if show_Midlin
midValue = (_hh + _ll) / 2
lineMid := line.new(bar_index[1] , midValue , bar_index , midValue , xloc.bar_index , extend = extend.none , color = col_Midline , width = size_ln_Mid , style = style_Mid)
if show_txt_Mid
Price_5 := label.new(x = last_bar_index , y = midValue , text = show_txt_HL ? str.tostring(midValue) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Midline , textalign = text.align_right , tooltip = 'Midline High' )
if show_Quarti
Value25 = _hh - 0.25 * (_hh - _ll)
line_25 := line.new(bar_index[1] , Value25, bar_index , Value25 , xloc.bar_index , extend = extend.none , color = col_Quartil , width = size_ln_Qua , style = style_Quart)
Value75 = _hh - 0.75 * (_hh - _ll)
line_75 := line.new(bar_index[1] , Value75, bar_index , Value75 , xloc.bar_index , extend = extend.none , color = col_Quartil , width = size_ln_Qua , style = style_Quart)
if show_txt_Qua
Price_75 := label.new(x = last_bar_index , y = Value75 , text = show_txt_HL ? str.tostring(Value75) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Quartil , textalign = text.align_right)
Price_25 := label.new(x = last_bar_index , y = Value25 , text = show_txt_HL ? str.tostring(Value25) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Quartil , textalign = text.align_right)
if BG_range
linefill.new(lineVarHighs , line_25 , color = color.new(col_Premium,90))
linefill.new(line_25 , lineMid , color = color.new(col_Premium,95))
linefill.new(lineVarLows , line_75 , color = color.new(col_Discoun,90))
linefill.new(line_75 , lineMid , color = color.new(col_Discoun,95))
if start_track
line.set_x2(lineVarHighs, bar_index)
line.set_y1(lineVarHighs, _hh)
line.set_y2(lineVarHighs, _hh)
line.set_x2(lineVarLows, bar_index)
line.set_y1(lineVarLows, _ll)
line.set_y2(lineVarLows, _ll)
line.set_x2(lineMid, bar_index)
line.set_y1(lineMid, (_hh + _ll) / 2)
line.set_y2(lineMid, (_hh + _ll) / 2)
line.set_x2(line_25, bar_index)
line.set_y1(line_25, _hh - 0.25 * (_hh - _ll))
line.set_y2(line_25, _hh - 0.25 * (_hh - _ll))
line.set_x2(line_75, bar_index)
line.set_y1(line_75, _hh - 0.75 * (_hh - _ll))
line.set_y2(line_75, _hh - 0.75 * (_hh - _ll))
label.set_x(Price_high, bar_index+2)
label.set_y(Price_high, _hh)
label.set_x(Price_low, bar_index+2)
label.set_y(Price_low, _ll)
label.set_x(Price_5, bar_index+2)
label.set_y(Price_5, (_hh + _ll) / 2)
label.set_x(Price_75, bar_index+2)
label.set_y(Price_75, _hh - 0.75 * (_hh - _ll))
label.set_x(Price_25, bar_index+2)
label.set_y(Price_25, _hh - 0.25 * (_hh - _ll))
//-------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
// FVG
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
type Settings
bool CE_show
string CE_style
color CE_color
bool fra_show
string fra_style
color fra_color
bool Border_show
bool mitigated_show
string mitigated_type
color mitigated_color
bool mitigated_CE_show
color mitigated_CE_color
string mitigated_CE_style
bool mitigated_fra_show
color mitigated_fra_color
string mitigated_fra_style
bool ltf_hide
bool label_show
color label_color
color label_bgcolor
string label_size
int padding
int buffer
type Imbalance_Settings
bool show
string htf
color color_bull
color color_bear
int max_count
type Imbalance
int open_time
int close_time
float open
float middle
float mid_25
float mid_75
float close_up_75
float close_dn_75
float close
bool mitigated
int mitigated_time
line line_middle
line line_25
line line_75
label lbl
box box
type ImbalanceStructure
Imbalance[] imbalance
Imbalance_Settings settings
type Helper
string name = 'Helper'
//+------------------------------------------------------------------------------------------------------------+//
//+--- Settings ---+//
//+------------------------------------------------------------------------------------------------------------+//
Settings settings = Settings.new()
Imbalance_Settings HTF_1_Settings = Imbalance_Settings.new()
Imbalance_Settings HTF_2_Settings = Imbalance_Settings.new()
Imbalance_Settings HTF_3_Settings = Imbalance_Settings.new()
Imbalance_Settings HTF_4_Settings = Imbalance_Settings.new()
Imbalance_Settings HTF_5_Settings = Imbalance_Settings.new()
Imbalance_Settings HTF_6_Settings = Imbalance_Settings.new()
string tooltip1 = 'HTF FVG Settings:\n\tShow/Hide timeframe\n\tTimeframe to display\n\tBullish FVG Color\n\tBearish FVG Color\n\tMaximum number of FVGs to display'
string tooltip2 = 'Mitigated FVG Settings:\n\tShow/Hide mitigated (Applies to all).\n\tBullish FVG Color\n\tBearish FVG Color\n\tWhen to mark it as mitigated (Based on HTF timeframe, not current timeframe)'
settings.ltf_hide := input.bool(defval = false , title = 'Hide Lower Timeframes' , inline = '0' , group = 'FVG')
settings.Border_show := input.bool(defval = false , title = 'Show Border' , inline = '0' , group = 'FVG')
HTF_1_Settings.show := input.bool(defval = true , title = '' , inline = 'htf1', group = 'FVG')
htf_1 = input.timeframe(defval = '' , title = '', inline='htf1' , group = 'FVG')
HTF_1_Settings.htf := htf_1
HTF_1_Settings.color_bull := input.color(defval = color.new(#089981 , 80) , title = '', inline = 'htf1' , group = 'FVG')
HTF_1_Settings.color_bear := input.color(defval = color.new(color.white , 80) , title = '', inline = 'htf1' , group = 'FVG')
HTF_1_Settings.max_count := input.int(defval = 5 , title = '' , inline = 'htf1' , group = 'FVG' , tooltip = tooltip1)
HTF_2_Settings.show := input.bool(defval = false , title = '', inline = 'htf2' , group = 'FVG')
htf_2 = input.timeframe(defval = '60' , title = '' , inline = 'htf2' , group = 'FVG')
HTF_2_Settings.htf := htf_2
HTF_2_Settings.color_bull := input.color(defval = color.new(#089981,90) , title = '', inline='htf2' , group = 'FVG')
HTF_2_Settings.color_bear := input.color(defval = color.new(color.white,90) , title = '', inline='htf2' , group = 'FVG')
HTF_2_Settings.max_count := input.int(defval = 5 , title = '', inline = 'htf2' , group = 'FVG' , tooltip = tooltip1)
HTF_3_Settings.show := input.bool(defval = false , title = '', inline = 'htf3' , group = 'FVG')
htf_3 = input.timeframe(defval = '240' , title = '' , inline = 'htf3' , group = 'FVG')
HTF_3_Settings.htf := htf_3
HTF_3_Settings.color_bull := input.color(defval = color.new(#089981,90) , title = '', inline='htf3' , group = 'FVG')
HTF_3_Settings.color_bear := input.color(defval = color.new(color.white,90) , title = '', inline='htf3' , group = 'FVG')
HTF_3_Settings.max_count := input.int(defval = 5 , title = '', inline = 'htf3' , group = 'FVG' , tooltip = tooltip1)
settings.CE_show := input.bool(defval = true , title = 'Show C.E.' , inline='3' , group = 'FVG')
settings.CE_color := input.color(defval = color.new(color.white , 0) , title = '' , inline = '3' , group='FVG')
settings.CE_style := input.string(defval = '----', title = '', options = ['⎯⎯⎯', '----', '····'] , inline='3' , group = 'FVG')
settings.fra_show := input.bool(defval = true , title = 'Show 75-25%' , inline = '4' , group = 'FVG')
settings.fra_color := input.color(defval = color.new(color.black,60) , title = '' , inline = '4' , group = 'FVG')
settings.fra_style := input.string(defval = '····' , title = '' , options = ['⎯⎯⎯', '----', '····'] , inline = '4' , group = 'FVG')
settings.mitigated_show := input.bool(defval = true , title = 'Show Inversion FVG' , inline = '0', group = 'Inversion')
settings.mitigated_color := input.color(defval = color.new(color.orange , 90) , title = '' , inline = '0' , group = 'Inversion')
settings.mitigated_CE_show := input.bool(defval = true , title = 'C.E.' , inline = '1' , group = 'Inversion')
settings.mitigated_CE_color := input.color(defval = color.new(color.orange,0) , title = '' , inline = '1' , group = 'Inversion')
settings.mitigated_CE_style := input.string(defval = '⎯⎯⎯', title = '', options = ['⎯⎯⎯', '----', '····'] , inline = '1' , group = 'Inversion')
settings.mitigated_fra_show := input.bool(defval = false , title = '75-25%' , inline = '2' , group = 'Inversion')
settings.mitigated_fra_color := input.color(defval = color.new(color.black,60) , title = '' , inline = '2' , group = 'Inversion')
settings.mitigated_fra_style := input.string(defval = '····' , title = '' , options = ['⎯⎯⎯', '----', '····'] , inline = '2' , group = 'Inversion')
settings.mitigated_type := 'Body filled'
settings.label_show := input.bool(defval = true , title = 'Show Label' , inline = '0' , group = 'Label')
settings.label_color := input.color(defval = color.new(color.black, 10) , title = '' , inline = '0' , group = 'Label')
settings.label_bgcolor := input.color(defval = color.new(color.white, 100) , title = '' , inline = '0' , group = 'Label')
settings.label_size := input.string(defval = size.small , title = '' , options = [size.tiny , size.small , size.normal , size.large , size.huge] , inline = '0' , group = 'Label')
settings.padding := input.int(defval = 0 , title = 'Distance from current candle' , minval = 0 , inline = '1' , group = 'Label')
settings.buffer := input.int(defval = 1 , title = 'Spacing between timeframes' , minval = 0 , inline = '2' , group = 'Label')
//+------------------------------------------------------------------------------------------------------------+//
//+--- Variables ---+//
//+------------------------------------------------------------------------------------------------------------+//
color color_transparent = #ffffff00
Helper helper = Helper.new()
var ImbalanceStructure FVG_1 = ImbalanceStructure.new()
var ImbalanceStructure FVG_2 = ImbalanceStructure.new()
var ImbalanceStructure FVG_3 = ImbalanceStructure.new()
var Imbalance[] FVGs_1 = array.new<Imbalance>()
var Imbalance[] FVGs_2 = array.new<Imbalance>()
var Imbalance[] FVGs_3 = array.new<Imbalance>()
FVG_1.imbalance := FVGs_1
FVG_1.settings := HTF_1_Settings
FVG_2.imbalance := FVGs_2
FVG_2.settings := HTF_2_Settings
FVG_3.imbalance := FVGs_3
FVG_3.settings := HTF_3_Settings
//Used internally for padding
var int TF_1 = 0
var int TF_2 = 0
var int TF_3 = 0
//+------------------------------------------------------------------------------------------------------------+//
//+--- Methods ---+//
//+------------------------------------------------------------------------------------------------------------+//
method LineStyle(Helper helper, string style) =>
helper.name := style
out = switch style
'----' => line.style_dashed
'····' => line.style_dotted
=> line.style_solid
out
method Gethtftext(Helper helper, string htf) =>
helper.name := htf
formatted = htf
seconds = timeframe.in_seconds(htf)
if seconds < 60
formatted := str.tostring(seconds) + 's'
else if (seconds / 60) < 60
formatted := str.tostring((seconds/60)) + 'm'
else if (seconds/60/60) < 24
formatted := str.tostring((seconds/60/60)) + 'H'
formatted
method Validtimeframe(Helper helper, tf) =>
helper.name := tf
n1 = timeframe.in_seconds()
n2 = timeframe.in_seconds(tf)
n1 <= n2
//+------------------------------------------------------------------------------------------------------------+//
//+--- Imbalances Methods ---+//
//+------------------------------------------------------------------------------------------------------------+//
// AddZone is used to display and manage imbalance related boxes
method AddZone(ImbalanceStructure IS, Imbalance imb) =>
if IS.settings.show and time >= start_test
int buffer = time+((time-time[1])*(settings.padding+1+(settings.buffer)))
if na(imb.box)
imb.box := box.new(imb.open_time, imb.open, buffer, imb.close, settings.Border_show ? imb.open < imb.close ? color.new(IS.settings.color_bull, color.t(IS.settings.color_bull)/ 3) : color.new(IS.settings.color_bear, color.t(IS.settings.color_bear)/ 3) : color_transparent, settings.Border_show ? 1 : 0, bgcolor = imb.open < imb.close ? IS.settings.color_bull : IS.settings.color_bear, xloc=xloc.bar_time)
if settings.label_show
imb.lbl := label.new(buffer, imb.middle, text=helper.Gethtftext(IS.settings.htf), xloc=xloc.bar_time, textcolor=settings.label_color, style=label.style_label_left, color=settings.label_bgcolor, size = settings.label_size)
if settings.CE_show
imb.line_middle := line.new(imb.open_time, imb.middle, buffer, imb.middle, xloc=xloc.bar_time, style=helper.LineStyle(settings.CE_style), color=settings.CE_color)
if settings.fra_show
imb.line_25 := line.new(imb.open_time, imb.mid_25, buffer, imb.mid_25, xloc=xloc.bar_time, style=helper.LineStyle(settings.fra_style), color=settings.fra_color)
imb.line_75 := line.new(imb.open_time, imb.mid_75, buffer, imb.mid_75, xloc=xloc.bar_time, style=helper.LineStyle(settings.fra_style), color=settings.fra_color)
else
//if mitigated
box.set_right(imb.box, imb.mitigated ? imb.mitigated_time : buffer)
box.set_bgcolor(imb.box, imb.open < imb.close ? imb.mitigated ? settings.mitigated_color : IS.settings.color_bull : imb.mitigated ? settings.mitigated_color : IS.settings.color_bear)
box.set_border_color(imb.box, settings.Border_show ? imb.open < imb.close ? color.new(settings.mitigated_color, color.t(settings.mitigated_color)/ 3) : color.new(settings.mitigated_color, color.t(settings.mitigated_color)/ 3) : color_transparent)
label.delete(imb.lbl)
if settings.CE_show
line.set_color(imb.line_middle , color.new(settings.mitigated_color , 100))
if settings.mitigated_CE_show
line.set_x2(imb.line_middle, imb.mitigated ? imb.mitigated_time : buffer)
line.set_color(imb.line_middle , settings.mitigated_CE_color)
line.set_style(imb.line_middle , helper.LineStyle(settings.mitigated_CE_style))
if settings.fra_show
line.set_color(imb.line_75 , color.new(settings.mitigated_color , 100))
line.set_color(imb.line_25 , color.new(settings.mitigated_color , 100))
if settings.mitigated_fra_show
line.set_x2(imb.line_25, imb.mitigated ? imb.mitigated_time : buffer)
line.set_x2(imb.line_75, imb.mitigated ? imb.mitigated_time : buffer)
line.set_color(imb.line_25 , settings.mitigated_fra_color)
line.set_color(imb.line_75 , settings.mitigated_fra_color)
if imb.mitigated and time >= start_test and not settings.mitigated_show
box.delete(imb.box)
line.delete(imb.line_middle)
line.delete(imb.line_25)
line.delete(imb.line_75)
label.delete(imb.lbl)
IS
// AddImbalance adds a newly discovered imbalance. this applies for both FVG and Volume Imbalance
method AddImbalance(ImbalanceStructure IS, float o, float c, int o_time) =>
Imbalance imb = Imbalance.new()
imb.open_time := o_time
imb.open := o
imb.middle := (o+c)/2
imb.mid_25 := c+(o-c)*0.25
imb.mid_75 := c+(o-c)*0.75
imb.close := c
IS.imbalance.unshift(imb)
IS.AddZone(imb)
if time >= start_test and IS.imbalance.size() > IS.settings.max_count
temp = IS.imbalance.pop()
box.delete(temp.box)
line.delete(temp.line_middle)
line.delete(temp.line_25)
line.delete(temp.line_75)
label.delete(temp.lbl)
IS
// CheckMitigated checks if the imbalance has been mitigated based on the settings
method CheckMitigated(ImbalanceStructure IS, o, h, l, c) =>
if IS.imbalance.size() > 0
for i = IS.imbalance.size() - 1 to 0
imb = IS.imbalance.get(i)
if not imb.mitigated
switch settings.mitigated_type
'Body filled' =>
imb.mitigated := imb.open < imb.close ? math.min(o, c) <= imb.open : math.max(o, c) >= imb.open
if imb.mitigated
if not settings.mitigated_show
box.delete(imb.box)
line.delete(imb.line_middle)
line.delete(imb.line_25)
line.delete(imb.line_75)
label.delete(imb.lbl)
IS.imbalance.remove(i)
else
if IS.settings.max_count
imb.mitigated_time := time
IS.AddZone(imb)
IS
method AdjustMargins(ImbalanceStructure IS, int step) =>
if time >= start_test and IS.imbalance.size() > 0
int buffer = time+((time-time[1])*(settings.padding+1+(settings.buffer*(step-1))))
for i = IS.imbalance.size() - 1 to 0
imb = IS.imbalance.get(i)
box.set_right(imb.box, buffer)
label.set_x(imb.lbl, buffer)
if settings.CE_show
line.set_x2(imb.line_middle, buffer)
if settings.fra_show
line.set_x2(imb.line_25, buffer)
line.set_x2(imb.line_75, buffer)
IS
// FindImbalance looks for imbalances and, if found, adds it to the list
method FindImbalance(ImbalanceStructure IS, o, h, l, c, t, o1, h1, l1, c1, t1, o2, h2, l2, c2, t2) =>
if time >= start_test and IS.settings.show and (h < l2 or l > h2)
o = h < l2 ? l2 : h2
c = h < l2 ? h : l
IS.AddImbalance(o, c, t2)
IS
method Process(ImbalanceStructure IS, float o, float h, float l, float c, int t, float o1, float h1, float l1, float c1, int t1, float o2, float h2, float l2, float c2, int t2, bool confirmed) =>
var int visible = 0
if time >= start_test and IS.settings.show and confirmed
if not settings.ltf_hide or (settings.ltf_hide and helper.Validtimeframe(IS.settings.htf))
if ta.change(time(IS.settings.htf)) and IS.settings.show
IS.FindImbalance(o, h, l, c, t, o1, h1, l1, c1, t1, o2, h2, l2, c2, t2)
visible := 1
IS.CheckMitigated(o, h, l, c)
visible
//+------------------------------------------------------------------------------------------------------------+//
//+--- Main call to start the process ---+//
//+------------------------------------------------------------------------------------------------------------+//
[o_1, h_1, l_1, c_1, t_1, confirmed] = request.security(syminfo.tickerid, htf_1, [open, high, low, close, time, barstate.isconfirmed])
[o1_1, h1_1, l1_1, c1_1, t1_1] = request.security(syminfo.tickerid, htf_1, [open[1], high[1], low[1], close[1], time[1]])
[o2_1, h2_1, l2_1, c2_1, t2_1] = request.security(syminfo.tickerid, htf_1, [open[2], high[2], low[2], close[2], time[2]])
TF_1 := FVG_1.Process(o_1, h_1, l_1, c_1, t_1, o1_1, h1_1, l1_1, c1_1, t1_1, o2_1, h2_1, l2_1, c2_1, t2_1, confirmed)
if barstate.isconfirmed and time >= start_test
FVG_1.AdjustMargins(TF_1)
[o_2, h_2, l_2, c_2, t_2, confirmed_2] = request.security(syminfo.tickerid, htf_2, [open, high, low, close, time, barstate.isconfirmed])
[o1_2, h1_2, l1_2, c1_2, t1_2] = request.security(syminfo.tickerid, htf_2, [open[1], high[1], low[1], close[1], time[1]])
[o2_2, h2_2, l2_2, c2_2, t2_2] = request.security(syminfo.tickerid, htf_2, [open[2], high[2], low[2], close[2], time[2]])
TF_2 := TF_1 + FVG_2.Process(o_2, h_2, l_2, c_2, t_2, o1_2, h1_2, l1_2, c1_2, t1_2, o2_2, h2_2, l2_2, c2_2, t2_2, confirmed_2)
if barstate.isconfirmed and time >= start_test
FVG_2.AdjustMargins(TF_2)
[o_3, h_3, l_3, c_3, t_3, confirmed_3] = request.security(syminfo.tickerid, htf_3, [open, high, low, close, time, barstate.isconfirmed])
[o1_3, h1_3, l1_3, c1_3, t1_3] = request.security(syminfo.tickerid, htf_3, [open[1], high[1], low[1], close[1], time[1]])
[o2_3, h2_3, l2_3, c2_3, t2_3] = request.security(syminfo.tickerid, htf_3, [open[2], high[2], low[2], close[2], time[2]])
TF_3 := TF_1 + FVG_3.Process(o_3, h_3, l_3, c_3, t_3, o1_3, h1_3, l1_3, c1_3, t1_3, o2_3, h2_3, l2_3, c2_3, t2_3, confirmed_3)
if barstate.isconfirmed and time >= start_test
FVG_3.AdjustMargins(TF_3)
How to Apply Pine Script in TradingView:
- Open TradingView and log in.
- Navigate to the Pine Script Editor at the bottom of the screen.
- Copy the provided Pine Script code.
- Paste it into the editor and click Save.
- Name the script, e.g., “ICT Premium/Discount Zones.”
- Click Add to Chart to apply the script.
- Customize settings to align with your trading strategy via the indicator panel.
Key Features of the Script:
- Premium and Discount Zones:
- Automatically identifies high and low ranges for price movement.
- Highlights midlines, 25%, and 75% levels to pinpoint potential premium and discount zones.
- Fair Value Gap (FVG) Detection:
- Detects bullish and bearish Fair Value Gaps across multiple timeframes.
- Visualizes FVG zones with customizable colors, borders, and labels.
- Mitigated Zone Highlighting:
- Marks mitigated FVGs with unique styles and colors for better trend identification.
- Provides options to hide mitigated zones if not required.
- Dynamic Range Updates:
- Real-time updates of high/low levels, midlines, and quartiles.
- Adjustable spacing and padding for optimal chart visibility.
- Customizable Visualization:
- Offers flexible settings for line styles, colors, and label display.
- Allows toggle options for background fills, labels, and multi-timeframe visibility.
- Multi-Timeframe Analysis:
- Supports up to three customizable higher timeframes for detailed market analysis.
- Automatically adjusts and updates zones based on chosen timeframes.
Recommended Usage:
- Range-Based Trading:
- Utilize premium and discount zones to identify ideal trade entry and exit points.
- Combine midlines and quartiles to refine price level expectations.
- Fair Value Gap Analysis:
- Spot bullish and bearish gaps for potential reversal or continuation trades.
- Use mitigated FVG zones to confirm market trends.
- Intraday and Swing Trading:
- Apply the script for intraday scalping with short timeframes or swing trading with higher timeframes.
- Leverage multi-timeframe FVGs for a comprehensive market view.
- Custom Strategies:
- Adjust settings to align with specific trading strategies and preferences.
- Hide lower timeframe zones when focusing on macro-level market movements.
Script Evaluation:
- Functionality: 4.8/5
The script offers a robust combination of premium/discount zone identification and FVG analysis, making it highly versatile. - Ease of Use: 4.5/5
While the settings panel is extensive, the intuitive layout ensures accessibility for traders at all levels. - Accuracy: 4.7/5
Provides reliable and actionable zones and FVGs when applied with proper configurations. - Repainting:
After detailed review, this script does not repaint. It bases its calculations on confirmed bar data and updates zones dynamically without altering past signals, ensuring dependable real-time and historical analysis. - Overall Score: 4.8/5
A well-designed tool that blends key trading concepts into a cohesive framework for technical analysis.
Final Verdict:
The ICT Premium/Discount Zones script is a powerful tool for traders focusing on price levels and Fair Value Gap strategies. Its non-repainting nature, combined with extensive customization options, makes it an invaluable resource for both novice and experienced traders. With precise visualizations and multi-timeframe compatibility, this script enhances market analysis and decision-making.
RELATED POSTS
View all