本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fadizeidan
//@version=5
indicator("ICT Open Range Gap & 1st FVG (fadi)", overlay=true, dynamic_requests = true, max_lines_count = 500 )
type Settings
bool show_fvg
bool show_openrange
bool show_fvg_bg
color fvg_bg
color fvg_o
string fvg_o_style
color fvg_c
string fvg_c_style
color fvg_ce
string fvg_ce_style
color fvg_link
string fvg_link_style
bool fvg_show_label
string fvg_label_size
color fvg_label_color
color fvg_label_bg
bool fvg_extend_monday
bool fvg_extend_all
bool show_org_bg
bool show_org_fill
color org_fill1
color org_fill2
color org_bg1
color org_bg2
color org_bg3
color org_bg4
color org_o
string org_o_style
color org_o_trace
string org_o_trace_style
color org_c
string org_c_style
color org_ce
string org_ce_style
bool org_show_qdr
color org_qdr
string org_qdr_style
bool org_extend
int org_factor
string hide_above
bool show_labels
string label_size
color label_color
color label_bg
int max
int buffer
type OpenRange
float close
float open
int close_time
int open_time
bool is_Monday
int woy
line line_open
line line_open_trace
line line_close
line line_q1
line line_q2
line line_ce
linefill lf_org1
linefill lf_org2
linefill lf_org3
linefill lf_org4
line line_fvg_o
line line_fvg_c
line line_fvg_ce
line line_fvg_link
label label_fvg
label label_org1
label label_org2
label label_org3
label label_org4
label label_org5
linefill lf_fvg
box box_fill1
box box_fill2
var Settings settings = Settings.new()
var OpenRange[] ranges = array.new<OpenRange>()
settings.hide_above := input.timeframe("15", "Hide above")
settings.max := input.int(5, "Limit to last X days", minval=1, maxval=100)
settings.buffer := input.int(10, "Extend to the right by", minval=1, maxval=100)
settings.show_fvg := input.bool(true, "1m FVG", group="First 1m FVG", inline="fvg")
settings.fvg_o := input.color(color.new(color.green,10), "Open ", group="First 1m FVG", inline="fvg_o")
settings.fvg_o_style := input.string('····', '', options = ['⎯⎯⎯', '----', '····'], group="First 1m FVG", inline="fvg_o")
settings.fvg_c := input.color(color.new(color.green,10), "Close ", group="First 1m FVG", inline="fvg_c")
settings.fvg_c_style := input.string('····', '', options = ['⎯⎯⎯', '----', '····'], group="First 1m FVG", inline="fvg_c")
settings.fvg_ce := input.color(color.new(color.green,10), "C.E. ", group="First 1m FVG", inline="fvg_ce")
settings.fvg_ce_style := input.string('····', '', options = ['⎯⎯⎯', '----', '····'], group="First 1m FVG", inline="fvg_ce")
settings.fvg_link := input.color(color.new(color.green,10), "Link ", group="First 1m FVG", inline="fvg_link")
settings.fvg_link_style := input.string('⎯⎯⎯', '', options = ['⎯⎯⎯', '----', '····'], group="First 1m FVG", inline="fvg_link")
settings.fvg_show_label := input.bool(true, "Label ", group="First 1m FVG", inline="fvg_label")
settings.fvg_label_size := input.string(size.small, "", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group="First 1m FVG", inline="fvg_label")
settings.fvg_label_color := input.color(color.gray, "", group="First 1m FVG", inline="fvg_label")
settings.fvg_label_bg := input.color(color.new(color.black, 100), "", group="First 1m FVG", inline="fvg_label")
settings.show_fvg_bg := input.bool(true, "Background ", group="First 1m FVG", inline="fvg_bg")
settings.fvg_bg := input.color(color.new(color.green,95), "", group="First 1m FVG", inline="fvg_bg")
settings.fvg_extend_monday := input.bool(true, "Extend Monday's FVG", group="First 1m FVG")
settings.fvg_extend_all := input.bool(true, "Extend All", group="First 1m FVG")
settings.show_openrange := input.bool(true, "Open Range Gap", group="Open Range Gap", inline="org")
settings.org_o := input.color(color.new(color.gray,10), "Open ", group="Open Range Gap", inline="org_o")
settings.org_o_style := input.string('⎯⎯⎯', '', options = ['⎯⎯⎯', '----', '····'], group="Open Range Gap", inline="org_o")
settings.org_c := input.color(color.new(color.gray,10), "Close ", group="Open Range Gap", inline="org_c")
settings.org_c_style := input.string('⎯⎯⎯', '', options = ['⎯⎯⎯', '----', '····'], group="Open Range Gap", inline="org_c")
settings.org_ce := input.color(color.new(color.red,10), "C.E. ", group="Open Range Gap", inline="org_ce")
settings.org_ce_style := input.string('----', '', options = ['⎯⎯⎯', '----', '····'], group="Open Range Gap", inline="org_ce")
settings.org_o_trace := input.color(color.new(color.gray,10), "Trace Open ", group="Open Range Gap", inline="org_o_trace")
settings.org_o_trace_style := input.string('⎯⎯⎯', '', options = ['⎯⎯⎯', '----', '····'], group="Open Range Gap", inline="org_o_trace")
settings.org_show_qdr := input.bool(true, "Quadrants ", group="Open Range Gap", inline="org_qdr")
settings.org_qdr := input.color(color.new(color.gray,10), "", group="Open Range Gap", inline="org_qdr")
settings.org_qdr_style := input.string('····', '', options = ['⎯⎯⎯', '----', '····'], group="Open Range Gap", inline="org_qdr")
settings.show_org_bg := input.bool(true, "Background ", group="Open Range Gap", inline="org_bg")
settings.org_bg1 := input.color(color.new(color.blue,90), "", group="Open Range Gap", inline="org_bg")
settings.org_bg2 := input.color(color.new(color.green,90), "", group="Open Range Gap", inline="org_bg")
settings.org_bg3 := input.color(color.new(color.red,90), "", group="Open Range Gap", inline="org_bg")
settings.org_bg4 := input.color(color.new(color.orange,90), "", group="Open Range Gap", inline="org_bg")
settings.show_org_fill := input.bool(true, "Fill ", group="Open Range Gap", inline="org_fill")
settings.org_fill1 := input.color(color.new(color.blue,95), "", group="Open Range Gap", inline="org_fill")
settings.org_fill2 := input.color(color.new(color.blue,80), "", group="Open Range Gap", inline="org_fill")
settings.org_extend := input.bool(false, "Extend to end of day", group="Open Range Gap")
settings.show_labels := input.bool(true, "Labels", group="labels", inline="label")
settings.label_size := input.string(size.small, "", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group="labels", inline="label")
settings.label_color := input.color(color.gray, "", group="labels", inline="label")
settings.label_bg := input.color(color.new(color.black, 100), "", group="labels", inline="label")
settings.org_factor := input.int(5, "Minimum Size", minval=1, tooltip="Hide Open Range Gaps smaller than this size.")
int tc = (time-time[1])*settings.buffer
int ts = (time-time[1])*3
lineStyle(string style) =>
out = switch style
'----' => line.style_dashed
'····' => line.style_dotted
=> line.style_solid
out
validtimeframe() =>
n1 = timeframe.in_seconds()
n2 = timeframe.in_seconds(settings.hide_above)
n1 <= n2
get_ltf_values(o, c, t) =>
float _o = na
float _c = na
int _t = na
int index = na
if o.size() > 1
for i = 0 to o.size() - 1
if not na(o.get(i))
index := i
break
if not na(index)
if o.size() >= index
_o := o.get(index)
if c.size() >= index
_c := c.get(index)
if t.size() >= index
_t := t.get(index)
[_o, _c, _t]
sessionStarted() =>
bool started = false
if timeframe.isseconds
insession = request.security(syminfo.tickerid, "1", (not na(time("1", "1614-0930", "America/New_York")) and na(time("1", "1614-0930", "America/New_York")[1])), lookahead=barmerge.lookahead_on)
started := insession
else
if (timeframe.isminutes and timeframe.multiplier == 1)
insession = (not na(time("1", "1614-0930", "America/New_York")) and na(time("1", "1614-0930", "America/New_York")[1]))
started := insession
if (timeframe.isminutes and timeframe.multiplier > 1)
insession = request.security_lower_tf(syminfo.tickerid, "1", (not na(time("1", "1614-0930", "America/New_York")) and na(time("1", "1614-0930", "America/New_York")[1])))
started := insession.includes(true)
started
sessionEnded() =>
bool ended = false
if timeframe.isseconds
insession = request.security(syminfo.tickerid, "1", (na(time("1", "1614-0930", "America/New_York")) and not na(time("1", "1614-0930", "America/New_York")[1])), lookahead=barmerge.lookahead_on)
ended := insession
else
if (timeframe.isminutes and timeframe.multiplier == 1)
insession = (na(time("1", "1614-0930", "America/New_York")) and not na(time("1", "1614-0930", "America/New_York")[1]))
ended := insession
if (timeframe.isminutes and timeframe.multiplier > 1)
insession = request.security_lower_tf(syminfo.tickerid, "1", (na(time("1", "1614-0930", "America/New_York")) and not na(time("1", "1614-0930", "America/New_York")[1])))
ended := insession.includes(true)
ended
test1mFVG(l1, h1, l3, h3) =>
(l1 > h3 or h1 < l3)
get1mFVG(l1, l2, l3, h1, h2, h3, min1, max1, min2, max2, min3, max3, t3) =>
float o = na
float c = na
log.info("validate time")
if (hour(t3, "America/New_York") == 9 and minute(t3, "America/New_York") >= 31) or (hour(t3, "America/New_York") > 9 and hour(t3, "America/New_York")<= 12)
log.info("valid time")
if l1 > h3
if min2 > max3
o := max3
else
o := h3
if min1 > max2
c := min1
else
c := l1
log.info("FVG: o: {0} c: {1} h1: {2} {3}", h1 , l3, max1 < max2, max1, c)
if h1 < l3
if max1 < min2
c := max1
log.info("assigned: {0}", c)
else
c := h1
if min3 > max2
o := min3
else
o := l3
[o, c, t3]
firstFVG() =>
float fvg_o = na
float fvg_c = na
int fvg_t = na
if ta.change(time("1"))
if timeframe.isseconds
[_o, _c, _t] = request.security(syminfo.tickerid, "1", get1mFVG(low[1], low[2], low[3], high[1], high[2], high[3], math.min(open[1], close[1]), math.max(open[1], close[1]), math.min(open[2], close[2]), math.max(open[2], close[2]), math.min(open[3], close[3]), math.max(open[3], close[3]), time[2]), lookahead = barmerge.lookahead_on)
fvg_o := _o
fvg_c := _c
fvg_t := _t
else
if (timeframe.isminutes and timeframe.multiplier == 1)
if barstate.isconfirmed
[_o, _c, _t] = get1mFVG(low, low[1], low[2], high, high[1], high[2], math.min(open, close), math.max(open, close), math.min(open[1], close[1]), math.max(open[1], close[1]), math.min(open[2], close[2]), math.max(open[2], close[2]), time[1])
fvg_o := _o
fvg_c := _c
fvg_t := _t
if (timeframe.isminutes and timeframe.multiplier >= 2)
[o, c, t] = request.security_lower_tf(syminfo.tickerid, "1", get1mFVG(low[1], low[2], low[3], high[1], high[2], high[3], math.min(open[1], close[1]), math.max(open[1], close[1]), math.min(open[2], close[2]), math.max(open[2], close[2]), math.min(open[3], close[3]), math.max(open[3], close[3]), time[2]))
[_o, _c, _t] = get_ltf_values(o, c, t)
fvg_o := _o
fvg_c := _c
fvg_t := time
[fvg_o, fvg_c, fvg_t]
method clear(OpenRange this) =>
this.line_open.delete()
this.line_open_trace.delete()
this.line_close.delete()
this.line_q1.delete()
this.line_ce.delete()
this.line_q2.delete()
this.line_fvg_o.delete()
this.line_fvg_c.delete()
this.line_fvg_ce.delete()
this.line_fvg_link.delete()
this.lf_org1.delete()
this.lf_org2.delete()
this.lf_org3.delete()
this.lf_org4.delete()
this.lf_fvg.delete()
this.label_fvg.delete()
this.label_org1.delete()
this.label_org2.delete()
this.label_org3.delete()
this.label_org4.delete()
this.label_org5.delete()
this.box_fill1.delete()
this.box_fill2.delete()
method terminateRange(OpenRange this) =>
//if (settings.org_extend and (hour(time, "America/New_York") == 16 and minute(time("1"), "America/New_York")== 14)) or (not settings.org_extend and (hour(time, "America/New_York") == 10 and minute(time, "America/New_York")== 0))
this.line_open.set_x2(time)
this.line_close.set_x2(time)
this.line_q1.set_x2(time)
this.line_ce.set_x2(time)
this.line_q2.set_x2(time)
this.label_org1.set_x(time)
this.label_org2.set_x(time)
this.label_org3.set_x(time)
this.label_org4.set_x(time)
this.label_org5.set_x(time)
this.box_fill1.delete()
this.box_fill2.delete()
this.label_org1.delete()
this.label_org2.delete()
this.label_org3.delete()
this.label_org4.delete()
this.label_org5.delete()
method updateFill(OpenRange this) =>
if this.open < this.close
if this.box_fill2.get_bottom() >= this.open and low < this.box_fill2.get_bottom()
this.box_fill2.set_bottom(math.max(low, this.open))
this.box_fill1.set_bottom(this.box_fill2.get_bottom())
else
if this.box_fill2.get_bottom() <= this.open and high > this.box_fill2.get_bottom()
this.box_fill2.set_bottom(math.min(high, this.open))
this.box_fill1.set_bottom(this.box_fill2.get_bottom())
method render(OpenRange[] this) =>
if this.size() > 0
OpenRange org = this.first()
if not na(org.close)
if settings.show_openrange
if math.abs(org.open - org.close) > settings.org_factor
if na(org.line_open)
float q = math.abs(org.open - org.close)/4
float q1 = org.close > org.open ? org.close-q : org.open-q
float q2 = org.close > org.open ? org.close-(q*2) : org.open-(q*2)
float q3 = org.close > org.open ? org.close-(q*3) : org.open-(q*3)
org.line_open := line.new(org.close_time, org.open, time+tc, org.open, xloc=xloc.bar_time, color=settings.org_o, style=lineStyle(settings.org_o_style))
org.line_ce := line.new(org.close_time, q2, time+tc, q2, xloc=xloc.bar_time, color=settings.org_ce, style=lineStyle(settings.org_ce_style))
org.line_close := line.new(org.close_time, org.close, time+tc, org.close, xloc=xloc.bar_time, color=settings.org_c, style=lineStyle(settings.org_c_style))
if settings.org_show_qdr
org.line_q1 := line.new(org.close_time, q1, time+tc, q1, xloc=xloc.bar_time, color=settings.org_qdr, style=lineStyle(settings.org_qdr_style))
org.line_q2 := line.new(org.close_time, q3, time+tc, q3, xloc=xloc.bar_time, color=settings.org_qdr, style=lineStyle(settings.org_qdr_style))
if settings.show_org_bg
if org.open < org.close
if settings.org_show_qdr
org.lf_org1 := linefill.new(org.line_open, org.line_q2, color=settings.org_bg1)
org.lf_org2 := linefill.new(org.line_q2, org.line_ce, color=settings.org_bg2)
org.lf_org3 := linefill.new(org.line_ce, org.line_q1, color=settings.org_bg3)
org.lf_org4 := linefill.new(org.line_q1, org.line_close, color=settings.org_bg4)
else
org.lf_org1 := linefill.new(org.line_open, org.line_ce, color=settings.org_bg1)
org.lf_org2 := linefill.new(org.line_ce, org.line_close, color=settings.org_bg2)
else
if settings.org_show_qdr
org.lf_org1 := linefill.new(org.line_open, org.line_q1, color=settings.org_bg1)
org.lf_org2 := linefill.new(org.line_q1, org.line_ce, color=settings.org_bg2)
org.lf_org3 := linefill.new(org.line_ce, org.line_q2, color=settings.org_bg3)
org.lf_org4 := linefill.new(org.line_q2, org.line_close, color=settings.org_bg4)
else
org.lf_org1 := linefill.new(org.line_open, org.line_ce, color=settings.org_bg1)
org.lf_org2 := linefill.new(org.line_ce, org.line_close, color=settings.org_bg2)
if settings.show_org_fill
org.box_fill1 := box.new(time+tc, org.open, time+tc-ts, org.close, xloc=xloc.bar_time, border_color=settings.org_fill1, bgcolor=settings.org_fill1)
org.box_fill2 := box.new(time+tc, org.close, time+tc-ts, org.close, xloc=xloc.bar_time, border_color=settings.org_fill2, bgcolor=settings.org_fill2)
if settings.show_labels
//org.label_org1 := label.new(time+tc, org.open, str.tostring(org.open, "#,##0.00") + " (open)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
if settings.org_show_qdr
if org.open < org.close
org.label_org2 := label.new(time+tc, q1, str.tostring(q1, "#,##0.00") + " (1/4)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
org.label_org4 := label.new(time+tc, q3, str.tostring(q3, "#,##0.00") + " (3/4)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
else
org.label_org2 := label.new(time+tc, q1, str.tostring(q1, "#,##0.00") + " (3/4)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
org.label_org4 := label.new(time+tc, q3, str.tostring(q3, "#,##0.00") + " (1/4)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
org.label_org3 := label.new(time+tc, q2, str.tostring(q2, "#,##0.00") + " (1/2)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
org.label_org5 := label.new(time+tc, org.close, str.tostring(org.close, "#,##0.00") + " (close)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
else
if settings.org_extend or (not settings.org_extend and (hour(time, "America/New_York") < 10 or (hour(time, "America/New_York") == 10 and minute(time, "America/New_York")== 0)))
org.line_open.set_x2(time+tc)
org.line_open_trace.set_x2(org.close_time)
org.line_close.set_x2(time+tc)
org.line_q1.set_x2(time+tc)
org.line_ce.set_x2(time+tc)
org.line_q2.set_x2(time+tc)
org.label_org1.set_x(time+tc)
org.label_org2.set_x(time+tc)
org.label_org3.set_x(time+tc)
org.label_org4.set_x(time+tc)
org.label_org5.set_x(time+tc)
org.box_fill1.set_right(time+tc)
org.box_fill1.set_left(time+tc-ts)
org.box_fill2.set_right(time+tc)
org.box_fill2.set_left(time+tc-ts)
org.updateFill()
if not na(org.line_fvg_o)
org.line_fvg_o.set_x2(time+tc)
org.line_fvg_c.set_x2(time+tc)
org.line_fvg_ce.set_x2(time+tc)
org.line_fvg_link.set_x1(time+tc)
org.line_fvg_link.set_x2(time+tc)
org.label_fvg.set_x(time+tc)
else
if not na(org.open) and settings.show_openrange // we have open only, we can draw traceline
if na(org.line_open_trace)
if settings.show_labels
org.label_org1 := label.new(time+tc, org.open, str.tostring(org.open, "#,##0.00") + " (open)", xloc=xloc.bar_time, textcolor=settings.label_color, color=settings.label_bg, size=settings.label_size, style=label.style_label_left)
org.line_open_trace := line.new(org.open_time, org.open, time+tc, org.open, xloc=xloc.bar_time, color=settings.org_o_trace, style=lineStyle(settings.org_o_trace_style))
else
org.line_open_trace.set_x2(time+tc)
org.label_org1.set_x(time+tc)
if settings.fvg_extend_monday or settings.fvg_extend_all
for org in this
if (settings.fvg_extend_monday and org.is_Monday and org.woy == weekofyear(time, "America/New_York")) or (settings.fvg_extend_all and org.woy == weekofyear(time_tradingday, "America/New_York"))
if not na(org.line_fvg_o)
org.line_fvg_o.set_x2(time+tc)
org.line_fvg_c.set_x2(time+tc)
org.line_fvg_ce.set_x2(time+tc)
org.line_fvg_link.set_x1(time+tc)
org.line_fvg_link.set_x2(time+tc)
org.label_fvg.set_x(time+tc)
if (not settings.org_extend and (hour(time, "America/New_York") == 10 and minute(time, "America/New_York")== 0))
org.terminateRange()
this
if validtimeframe()
if sessionStarted()
if settings.org_extend
if ranges.size() > 0
OpenRange old = ranges.first()
old.terminateRange()
OpenRange org = OpenRange.new()
org.open := close
org.open_time := time
ranges.unshift(org)
if sessionEnded()
if ranges.size() >0
OpenRange org = ranges.first()
org.close := open
org.close_time := time
if settings.show_fvg
if ranges.size() > 0
OpenRange org = ranges.first()
if na(org.line_fvg_o)
[o, c, t] = firstFVG()
if not na(o)
org.is_Monday := dayofweek(time, "America/New_York") == dayofweek.monday
org.woy := weekofyear(time, "America/New_York")
org.line_fvg_o := line.new(t, o, (t+tc), o, xloc=xloc.bar_time, color=settings.fvg_o, style=lineStyle(settings.fvg_o_style))
org.line_fvg_c := line.new(t, c, (t+tc), c, xloc=xloc.bar_time, color=settings.fvg_c, style=lineStyle(settings.fvg_c_style))
org.line_fvg_ce := line.new(t, (o+c)/2, (t+tc), (o+c)/2, xloc=xloc.bar_time, color=settings.fvg_ce, style=lineStyle(settings.fvg_ce_style))
org.line_fvg_link := line.new((t+tc), o, (t+tc), c, xloc=xloc.bar_time, color=settings.fvg_link, style=lineStyle(settings.fvg_link_style), width=3)
if settings.fvg_show_label
org.label_fvg := label.new(time+tc, (o+c)/2, "1st FVG", xloc=xloc.bar_time, textcolor=settings.fvg_label_color, color=settings.fvg_label_bg, size=settings.fvg_label_size, style=label.style_label_left)
if settings.show_fvg_bg
org.lf_fvg := linefill.new(org.line_fvg_o, org.line_fvg_c, settings.fvg_bg)
if ranges.size() > settings.max
OpenRange temp = ranges.pop()
temp.clear()
ranges.render()