本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// [Abaco-Ind] Fair Value Gap MTF - FVG 4H, Daily, Weekly - the gaps of three timeframes you choose (the 4h, the day and the week to begin with), the SAME
// boxes on every chart timeframe.
//
// THE RULE
// bullish gap low of the third candle above the high of the first zone = [high of 1, low of 3]
// bearish gap high of the third candle below the low of the first zone = [high of 3, low of 1]
// 1 the candles are the gap's own timeframe's - the 4h, the UTC day, the week from Monday - whatever the chart: a 5m
// chart and a 4h chart show the same gaps
// 2 a gap is born at the close of its third candle and known from the next bar on: a candle still forming makes no
// gap, so nothing is redrawn
// 3 a gap is filled by the first print that reaches its far edge - a low at or under the bottom of a bullish gap, a
// high at or over the top of a bearish one - read on the chart's own bars; a filled gap leaves the chart
// 4 the box starts at the open of the gap's first candle. Boxes run: Fixed, for the three candles of the pattern plus
// the chosen number of bars of the gap's own timeframe; or Until filled
//
// HOW TO USE IT
// Three timeframes, each with its own checkbox and its own menu. Green is a gap left by a rise, red one left by a
// fall; the colour's own transparency is the same for every timeframe. Label: a micro label with the timeframe, in the middle of each
// box, at its right end.
// Each timeframe looks back a number of its own bars (Bars Back); a gap older than that is not followed.
// The box starts on the first candle although the gap is only known at the close of the third: it is drawn back for
// the eye, while the plots below only say what was true on each bar.
// Plots (hidden): Open gaps (how many are open, all timeframes); In gap (+1 inside a bullish gap's zone, -1 inside a
// bearish one's); Touch, the Abaco touch standard v1 (touch-standard.md): one NUMBER, 0 is none, otherwise its size is the edge
// touched plus the number of edges touched on the bar divided by 100, and its sign the direction the price came from (positive
// from below, negative from above). The edges are numbered by timeframe slot: 1 the first timeframe's first edge (the one the
// price meets first: the top of a bullish gap, the bottom of a bearish one), 2 its opposite edge, which fills the gap, 3 and 4
// the second timeframe's, 5 and 6 the third's. An edge ARRIVES when its band (the edge plus or minus Touch Tolerance, in % of
// the close) enters the bar's range and was not inside the bar before's. With several edges on one bar, the one of the longest
// timeframe is reported, then the nearest to the previous close, then the lowest number.
// Alert conditions: In a gap, Touch (an edge). Any other script can read the plots as sources.
// Inputs and plots keep their order: TradingView stores a copy's values by position. New ones go at the end.
// THE ALERT speaks: create it on this indicator with "Any alert() function call". Its message names every level that arrived,
// each once, as it arrives at the tick. It is not tied to the Touch plot's one level.
indicator("[Abaco-Ind] Fair Value Gap MTF - FVG 4H, Daily, Weekly", overlay = true, max_boxes_count = 500, max_labels_count = 500)
G = "Timeframes"
on1 = input.bool(true, "Timeframe 1", inline = "1", group = G, display = display.none)
tf1 = input.timeframe("240", "", inline = "1", group = G, options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
on2 = input.bool(true, "Timeframe 2", inline = "2", group = G, display = display.none)
tf2 = input.timeframe("D", "", inline = "2", group = G, options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
on3 = input.bool(true, "Timeframe 3", inline = "3", group = G, display = display.none)
tf3 = input.timeframe("W", "", inline = "3", group = G, options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
lookIn = input.int(100, "Bars Back", minval = 10, maxval = 2000, tooltip = "Bars of each gap's own timeframe, not chart bars.\nGaps older than this are dropped.\nExample: 100 on W = about 2 years.", group = G, display = display.none)
labelIn = input.bool(false, "Label", tooltip = "Timeframe name on each box (4H, 1D, 1W).\nFixed: inside the box, at its right edge.\nUntil filled: beside the last bar.", group = "Style", display = display.none)
extendIn = input.string("Fixed", "Boxes Run", options = ["Until filled", "Fixed"], tooltip = "Fixed: box length set by Fixed Length.\nUntil filled: box runs right while the gap is open.\nA filled gap is removed in both.", group = "Style", display = display.none)
lengthIn = input.int(20, "Fixed Length", minval = 0, maxval = 500, tooltip = "Used with Boxes Run = Fixed.\nBars of the gap's own timeframe after the 3 pattern candles.\n0: the 3 pattern candles only.", group = "Style", display = display.none)
cUp = input.color(color.new(#6a9f66, 80), "Up / Down", inline = "c", group = "Style", tooltip = "Up: gap left by a rise.\nDown: gap left by a fall.\nThe transparency you set applies to every timeframe.", display = display.none)
cDn = input.color(color.new(#965254, 80), "", inline = "c", group = "Style", display = display.none)
tolIn = input.float(0.1, "Touch Tolerance", minval = 0.0, step = 0.05, tooltip = "In % of the price.\nA level is touched when the bar's range, low to high, comes within this distance of it.\nUsed by the Touch output and its alert only.\nIn gap does not use it.", group = "Alerts", display = display.none)
// ── on the gap's own timeframe: every open gap, from closed candles only ────
// at candle i the third candle is i-1, the first i-3: what the output says at i is known at i's open
f_gaps() =>
var array<float> gTop = array.new_float(0)
var array<float> gBot = array.new_float(0)
var array<float> gT0 = array.new_float(0)
var array<float> gSide = array.new_float(0)
var array<int> gBar = array.new_int(0)
// the candle that closed fills what it reached
if not na(low[1])
int r = 0
while r < gTop.size()
bool hit = gSide.get(r) > 0 ? low[1] <= gBot.get(r) : high[1] >= gTop.get(r)
if hit or gBar.get(r) < bar_index - lookIn
gTop.remove(r), gBot.remove(r), gT0.remove(r), gSide.remove(r), gBar.remove(r)
else
r += 1
// and may be the third of a new gap
if not na(high[3])
if low[1] > high[3]
gTop.push(low[1]), gBot.push(high[3]), gT0.push(time[3]), gSide.push(1), gBar.push(bar_index - 3)
else if high[1] < low[3]
gTop.push(low[3]), gBot.push(high[1]), gT0.push(time[3]), gSide.push(-1), gBar.push(bar_index - 3)
// packed: top, bottom, first candle's open, side
array<float> out = array.new_float(0)
if gTop.size() > 0
for q = 0 to gTop.size() - 1
out.push(gTop.get(q)), out.push(gBot.get(q)), out.push(gT0.get(q)), out.push(gSide.get(q))
out
f_frame(simple string tf) =>
request.security(syminfo.tickerid, tf, f_gaps(),
lookahead = timeframe.in_seconds(tf) >= timeframe.in_seconds() ? barmerge.lookahead_on : barmerge.lookahead_off)
array<float> g1 = on1 ? f_frame(tf1) : array.new_float(0)
array<float> g2 = on2 ? f_frame(tf2) : array.new_float(0)
array<float> g3 = on3 ? f_frame(tf3) : array.new_float(0)
// a timeframe lower than the chart's (lookahead off) hands over na on the first bars: no gaps yet
if na(g1)
g1 := array.new_float(0)
if na(g2)
g2 := array.new_float(0)
if na(g3)
g3 := array.new_float(0)
// ── on the chart: one box per gap, by its timeframe, side and first candle ──
var map<string, box> live = map.new<string, box>()
var map<string, label> tags = map.new<string, label>()
var map<string, float> sides = map.new<string, float>()
var map<string, bool> done = map.new<string, bool>() // filled on the chart: not drawn again
var map<string, int> slotOf = map.new<string, int>() // the timeframe slot of the gap: 0, 1, 2
// "15m", "4H", "1D", "1W", "1M" from the seconds of the timeframe
nameOf(int sec) =>
sec < 3600 ? str.tostring(sec / 60, "#") + "m" : sec < 86400 ? str.tostring(sec / 3600, "#") + "H" : sec < 604800 ? str.tostring(sec / 86400, "#") + "D" : sec < 2592000 ? str.tostring(sec / 604800, "#") + "W" : str.tostring(sec / 2592000, "#") + "M"
array<int> secs = array.from(timeframe.in_seconds(tf1), timeframe.in_seconds(tf2), timeframe.in_seconds(tf3))
f_drop(string key) =>
if tags.contains(key)
tags.get(key).delete()
tags.remove(key)
map<string, bool> listed = map.new<string, bool>()
int openAll = 0
for f = 0 to 2
array<float> g = g3
if f == 0
g := g1
else if f == 1
g := g2
int sec = secs.get(f)
string nm = nameOf(sec)
int slots = int(g.size() / 4)
openAll += slots
if slots > 0
for s = 0 to slots - 1
float top = g.get(4 * s)
float bot = g.get(4 * s + 1)
int t0 = int(g.get(4 * s + 2))
float side = g.get(4 * s + 3)
string key = nm + "|" + str.tostring(t0) + "|" + str.tostring(side)
listed.put(key, true)
if not live.contains(key) and not done.contains(key)
color col = side > 0 ? cUp : cDn
bool fixed = extendIn == "Fixed"
int right = fixed ? t0 + (3 + lengthIn) * sec * 1000 : time
// Fixed: the name is the box's own text, against its right border. Until filled: the box has no right border, so
// the name follows the last candle
live.put(key, box.new(t0, top, right, bot, xloc = xloc.bar_time, border_color = color(na), bgcolor = col,
extend = fixed ? extend.none : extend.right, text = labelIn and fixed ? nm : "", text_size = size.tiny,
text_color = color.new(col, 15), text_halign = text.align_right, text_valign = text.align_center))
if labelIn and not fixed
tags.put(key, label.new(time, (top + bot) / 2, nm, xloc = xloc.bar_time,
style = label.style_label_right, color = color(na), textcolor = color.new(col, 15),
size = size.tiny))
sides.put(key, side)
slotOf.put(key, f)
// the price against every open gap on this bar; a filled gap leaves the chart, a gap the timeframe no longer hands over too
float tol = close * tolIn / 100
// a level ARRIVES: its band enters this bar's range and was not inside the bar before's
f_arrives(float y) =>
not na(y) and low <= y + tol and high >= y - tol and not (low[1] <= y + tol and high[1] >= y - tol)
// one number for the bar: the reported edge plus the count / 100, signed by where the price came from. The reported edge is
// the one with the longest span, then the nearest to the previous close, then the lowest number
f_pack(array<int> id, array<float> lv, array<bool> hit, array<float> span) =>
int n = 0
int best = -1
for i = 0 to lv.size() - 1
if lv.size() > 0 and hit.get(i)
n += 1
if best < 0
best := i
else if span.get(i) > span.get(best) or (span.get(i) == span.get(best) and math.abs(lv.get(i) - close[1]) < math.abs(lv.get(best) - close[1])) or (span.get(i) == span.get(best) and math.abs(lv.get(i) - close[1]) == math.abs(lv.get(best) - close[1]) and id.get(i) < id.get(best))
best := i
float out = 0.0
if n > 0
out := (close[1] < lv.get(best) ? 1.0 : -1.0) * (id.get(best) + math.min(n, 99) / 100.0)
out
int inGap = 0
array<int> eId = array.new_int(0)
array<float> eLv = array.new_float(0)
array<bool> eHit = array.new_bool(0)
array<float> eSpan = array.new_float(0)
array<string> eNm = array.new_string(0)
array<string> eK = array.new_string(0)
for key in live.keys()
box bx = live.get(key)
float side = sides.get(key)
float top = bx.get_top()
float bot = bx.get_bottom()
// a bullish gap lies under the price: its first edge is the top, its far edge the bottom; a bearish one the other way round
int slot = slotOf.get(key)
float first = side > 0 ? top : bot
float far = side > 0 ? bot : top
eId.push(2 * slot + 1), eLv.push(first), eHit.push(f_arrives(first)), eSpan.push(secs.get(slot))
eId.push(2 * slot + 2), eLv.push(far), eHit.push(f_arrives(far)), eSpan.push(secs.get(slot))
string gapNm = nameOf(secs.get(slot)) + (side > 0 ? " bullish gap " : " bearish gap ")
eNm.push(gapNm + "first edge"), eK.push(key + "|1")
eNm.push(gapNm + "far edge"), eK.push(key + "|2")
bool filled = side > 0 ? low <= bot : high >= top
if filled
done.put(key, true)
live.remove(key)
slotOf.remove(key)
f_drop(key)
bx.delete()
else if not listed.contains(key)
live.remove(key)
slotOf.remove(key)
f_drop(key)
bx.delete()
else
if labelIn and extendIn != "Fixed" and tags.contains(key)
tags.get(key).set_x(time)
if low <= top and high >= bot and inGap == 0
inGap := side > 0 ? 1 : -1
float touch = f_pack(eId, eLv, eHit, eSpan)
// ── causal outputs, for alerts and for other scripts (their order is their identity) ──
plot(openAll, "Open gaps", display = display.none, editable = false)
plot(inGap, "In gap", display = display.none, editable = false)
alertcondition(inGap != 0, "In a gap", "{{ticker}}: the price is inside an open Fair Value Gap")
plot(touch, "Touch", display = display.none, editable = false)
// ── the alert speaks: every edge that arrived by name, each told once as it arrives (at the tick, not at the close) ──
varip string toldS = ""
if barstate.isnew
toldS := ""
string said = ""
for i = 0 to eLv.size() - 1
if eLv.size() > 0 and eHit.get(i) and not str.contains(toldS, "[" + eK.get(i) + "]")
toldS += "[" + eK.get(i) + "]"
said += (said == "" ? "" : ", ") + eNm.get(i) + " " + str.tostring(eLv.get(i), format.mintick) + (close[1] < eLv.get(i) ? " from below" : " from above")
if said != ""
alert(syminfo.ticker + " " + timeframe.period + ": " + said, alert.freq_all)