本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Pereira & Co. - FVG / IFVG tracker + MNQ sizer + position plan + named levels - version 4.2
//@version=6
indicator("Pereira FVG + Sizer v4", shorttitle="P&Co FVG", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// ------------------------------------------------------------ 1. which timeframes (checkboxes)
gt = "1. Timeframes To Draw Gaps From"
s0on = input.bool(true, "The Chart Timeframe (Whatever You Are Looking At)", group=gt)
s1on = input.bool(true, "5 Minute", group=gt)
s2on = input.bool(true, "15 Minute", group=gt)
s3on = input.bool(true, "1 Hour", group=gt)
s4on = input.bool(false, "4 Hour", group=gt)
s5on = input.bool(false, "Daily", group=gt)
s1tf = "5"
s2tf = "15"
s3tf = "60"
s4tf = "240"
s5tf = "D"
// ------------------------------------------------------------ 2. which gap types (checkboxes)
gg = "2. Gap Types To Show"
showFvgAll = input.bool(true, "FVG (Open Or Touched)", group=gg)
showIfvgAll = input.bool(true, "IFVG (Inverted)", group=gg)
showDeadAll = input.bool(false, "Invalidated IFVG", group=gg)
showTouchLbl = input.bool(true, "Say 'Touched' On The Label Once Price Has Entered A Gap", group=gg)
// ------------------------------------------------------------ 3. minimum gap size per timeframe (points)
gm = "3. Minimum Gap Size (Points)"
s0min = input.float(3.0, "Chart Timeframe", minval=0, group=gm)
s1min = input.float(8.0, "5 Minute", minval=0, group=gm)
s2min = input.float(15.0, "15 Minute", minval=0, group=gm)
s3min = input.float(30.0, "1 Hour", minval=0, group=gm)
s4min = input.float(60.0, "4 Hour", minval=0, group=gm)
s5min = input.float(100.0, "Daily", minval=0, group=gm)
// ------------------------------------------------------------ rules and housekeeping
gr = "4. Rules And Text"
invByClose = input.bool(true, "Inversion needs a body close through the far edge", group=gr, tooltip="On: a gap becomes an IFVG only when a candle CLOSES beyond its far edge. Off: a wick through is enough.")
maxAgeBars = input.int(400, "Delete a gap after this many chart bars", minval=20, group=gr)
keepDead = input.int(40, "Keep an invalidated gap for (chart bars)", minval=0, group=gr)
showCE = input.bool(true, "Show the midpoint (CE) line", group=gr)
showLabels = input.bool(true, "Show labels", group=gr)
textSize = input.string("normal", "Text Size (Everything: Labels And Table)", options=["tiny", "small", "normal", "large", "huge"], group=gr)
sizeOf(string x) => x == "tiny" ? size.tiny : x == "small" ? size.small : x == "normal" ? size.normal : x == "large" ? size.large : size.huge
lblSize = sizeOf(textSize)
tblSize = sizeOf(textSize)
extendRight = input.bool(true, "Extend live gaps to the right", group=gr)
// ------------------------------------------------------------ colours
gc = "5. Colours"
fvgOpac = input.int(80, "Gap Fill Transparency (0 = Solid, 100 = Invisible)", minval=0, maxval=100, group=gc)
c0b = input.color(#2E5E4E, "Chart TF - Bullish FVG", group=gc)
c0s = input.color(#C0402A, "Chart TF - Bearish FVG", group=gc)
c1b = input.color(#2E5E4E, "5m - Bullish FVG", group=gc)
c1s = input.color(#C0402A, "5m - Bearish FVG", group=gc)
c2b = input.color(#3C8C6E, "15m - Bullish FVG", group=gc)
c2s = input.color(#D86A4A, "15m - Bearish FVG", group=gc)
c3b = input.color(#1F7A8C, "1H - Bullish FVG", group=gc)
c3s = input.color(#B5312F, "1H - Bearish FVG", group=gc)
c4b = input.color(#146356, "4H - Bullish FVG", group=gc)
c4s = input.color(#8B2A1C, "4H - Bearish FVG", group=gc)
c5b = input.color(#0B4F3A, "Daily - Bullish FVG", group=gc)
c5s = input.color(#6E1F12, "Daily - Bearish FVG", group=gc)
cNowBull = input.color(#2C4A6E, "IFVG Now Bullish (Was Bearish)", group=gc)
cNowBear = input.color(#D08A1E, "IFVG Now Bearish (Was Bullish)", group=gc)
cDead = input.color(#808080, "Invalidated", group=gc)
cText = input.color(#1A1A1A, "Label Text", group=gc)
// ------------------------------------------------------------ per-slot settings as arrays (index = slot)
chartSec = timeframe.in_seconds(timeframe.period)
secOf(string tf) => timeframe.in_seconds(tf)
slotOn(int k, bool userOn, string tf) =>
sec = k == 0 ? chartSec : secOf(tf)
userOn and (k == 0 or sec > chartSec) // a higher-timeframe slot only counts if it really is higher than the chart
var slotTag = array.from("", "5m", "15m", "1H", "4H", "D")
var slotMin = array.from(s0min, s1min, s2min, s3min, s4min, s5min)
var slotShow = array.from(slotOn(0, s0on, ""), slotOn(1, s1on, s1tf), slotOn(2, s2on, s2tf), slotOn(3, s3on, s3tf), slotOn(4, s4on, s4tf), slotOn(5, s5on, s5tf))
var slotBull = array.from(c0b, c1b, c2b, c3b, c4b, c5b)
var slotBear = array.from(c0s, c1s, c2s, c3s, c4s, c5s)
// ------------------------------------------------------------ gap storage (parallel arrays)
var boxes = array.new<box>()
var ceLines = array.new<line>()
var labels = array.new<label>()
var tops = array.new<float>()
var bots = array.new<float>()
var dirs = array.new<int>() // 1 bullish gap, -1 bearish gap (original direction)
var states = array.new<int>() // 0 open, 1 touched, 2 inverted (IFVG), 3 invalidated
var births = array.new<int>()
var deaths = array.new<int>()
var slots = array.new<int>()
hidden = color.new(color.white, 100)
// paint one gap according to its state and the switches
paint(int i) =>
st = array.get(states, i)
d = array.get(dirs, i)
k = array.get(slots, i)
b = array.get(boxes, i)
l = array.get(ceLines, i)
lb = array.get(labels, i)
tag = array.get(slotTag, k)
pre = tag == "" ? "" : tag + " "
show = st <= 1 ? showFvgAll : st == 2 ? showIfvgAll : showDeadAll
fill0 = st <= 1 ? (d == 1 ? array.get(slotBull, k) : array.get(slotBear, k)) : st == 2 ? (d == 1 ? cNowBear : cNowBull) : cDead
fill = color.new(fill0, fvgOpac)
txt = st == 0 ? pre + (d == 1 ? "Bull FVG" : "Bear FVG") : st == 1 ? pre + (d == 1 ? "Bull FVG" : "Bear FVG") + (showTouchLbl ? " - Touched" : "") : st == 2 ? pre + (d == 1 ? "IFVG - Now Bearish" : "IFVG - Now Bullish") : pre + "Invalidated"
isHtf = k > 0
box.set_bgcolor(b, show ? fill : hidden)
box.set_border_color(b, show ? color.new(color.gray, isHtf ? 20 : 55) : hidden)
box.set_border_width(b, k >= 4 ? 2 : 1)
box.set_border_style(b, isHtf ? line.style_dashed : line.style_solid)
line.set_color(l, show and showCE and st < 3 ? color.new(color.gray, 35) : hidden)
label.set_text(lb, show and showLabels ? txt : "")
label.set_textcolor(lb, show and showLabels ? cText : hidden)
addGap(float top, float bot, int dir, int leftBar, int k) =>
b = box.new(leftBar, top, bar_index, bot, border_color=hidden, bgcolor=hidden, extend=extendRight ? extend.right : extend.none)
l = line.new(leftBar, (top + bot) / 2, bar_index, (top + bot) / 2, color=hidden, style=line.style_dotted, extend=extendRight ? extend.right : extend.none)
lb = label.new(bar_index + 1, (top + bot) / 2, "", style=label.style_label_left, color=hidden, textcolor=cText, size=lblSize, text_formatting=text.format_italic)
array.push(boxes, b)
array.push(ceLines, l)
array.push(labels, lb)
array.push(tops, top)
array.push(bots, bot)
array.push(dirs, dir)
array.push(states, 0)
array.push(births, bar_index)
array.push(deaths, -1)
array.push(slots, k)
paint(array.size(boxes) - 1)
// ------------------------------------------------------------ detection: chart timeframe (third candle confirmed)
newBull = barstate.isconfirmed and low > high[2] and (low - high[2]) >= s0min
newBear = barstate.isconfirmed and high < low[2] and (low[2] - high) >= s0min
if array.get(slotShow, 0)
if newBull
addGap(low, high[2], 1, bar_index - 2, 0)
if newBear
addGap(low[2], high, -1, bar_index - 2, 0)
// ------------------------------------------------------------ detection: higher timeframes (previous closed HTF bar, non-repainting)
[h1b, h1s, h1t, h1l] = request.security(syminfo.tickerid, s1tf, [low[1] > high[3], high[1] < low[3], low[1] > high[3] ? low[1] : low[3], low[1] > high[3] ? high[3] : high[1]], lookahead=barmerge.lookahead_on)
[h2b, h2s, h2t, h2l] = request.security(syminfo.tickerid, s2tf, [low[1] > high[3], high[1] < low[3], low[1] > high[3] ? low[1] : low[3], low[1] > high[3] ? high[3] : high[1]], lookahead=barmerge.lookahead_on)
[h3b, h3s, h3t, h3l] = request.security(syminfo.tickerid, s3tf, [low[1] > high[3], high[1] < low[3], low[1] > high[3] ? low[1] : low[3], low[1] > high[3] ? high[3] : high[1]], lookahead=barmerge.lookahead_on)
[h4b, h4s, h4t, h4l] = request.security(syminfo.tickerid, s4tf, [low[1] > high[3], high[1] < low[3], low[1] > high[3] ? low[1] : low[3], low[1] > high[3] ? high[3] : high[1]], lookahead=barmerge.lookahead_on)
[h5b, h5s, h5t, h5l] = request.security(syminfo.tickerid, s5tf, [low[1] > high[3], high[1] < low[3], low[1] > high[3] ? low[1] : low[3], low[1] > high[3] ? high[3] : high[1]], lookahead=barmerge.lookahead_on)
n1 = ta.change(time(s1tf)) != 0
n2 = ta.change(time(s2tf)) != 0
n3 = ta.change(time(s3tf)) != 0
n4 = ta.change(time(s4tf)) != 0
n5 = ta.change(time(s5tf)) != 0
htfAdd(int k, bool isNew, bool bull, bool bear, float top, float bot) =>
if array.get(slotShow, k) and isNew and (top - bot) >= array.get(slotMin, k)
if bull
addGap(top, bot, 1, bar_index - 1, k)
if bear
addGap(top, bot, -1, bar_index - 1, k)
htfAdd(1, n1, h1b, h1s, h1t, h1l)
htfAdd(2, n2, h2b, h2s, h2t, h2l)
htfAdd(3, n3, h3b, h3s, h3t, h3l)
htfAdd(4, n4, h4b, h4s, h4t, h4l)
htfAdd(5, n5, h5b, h5s, h5t, h5l)
// ------------------------------------------------------------ life cycle: open -> touched -> inverted -> invalidated
if array.size(boxes) > 0
for i = array.size(boxes) - 1 to 0
st = array.get(states, i)
d = array.get(dirs, i)
top = array.get(tops, i)
bot = array.get(bots, i)
b = array.get(boxes, i)
born = array.get(births, i)
changed = false
if bar_index > born
if st <= 1
if d == 1
if st == 0 and low <= top
array.set(states, i, 1)
changed := true
if invByClose ? close < bot : low < bot
array.set(states, i, 2)
changed := true
alert("Bullish FVG inverted (now bearish IFVG) " + str.tostring(bot) + " to " + str.tostring(top), alert.freq_once_per_bar)
else
if st == 0 and high >= bot
array.set(states, i, 1)
changed := true
if invByClose ? close > top : high > top
array.set(states, i, 2)
changed := true
alert("Bearish FVG inverted (now bullish IFVG) " + str.tostring(bot) + " to " + str.tostring(top), alert.freq_once_per_bar)
else if st == 2
dead = d == 1 ? (invByClose ? close > top : high > top) : (invByClose ? close < bot : low < bot)
if dead
array.set(states, i, 3)
array.set(deaths, i, bar_index)
box.set_extend(b, extend.none)
box.set_right(b, bar_index)
line.set_extend(array.get(ceLines, i), extend.none)
line.set_x2(array.get(ceLines, i), bar_index)
changed := true
if changed
paint(i)
if array.get(states, i) < 3
label.set_x(array.get(labels, i), bar_index + 1)
if array.get(states, i) < 3 and not extendRight
box.set_right(b, bar_index)
line.set_x2(array.get(ceLines, i), bar_index)
// housekeeping
st2 = array.get(states, i)
tooOld = bar_index - born > maxAgeBars
deadLong = st2 == 3 and bar_index - array.get(deaths, i) > keepDead
if tooOld or deadLong
box.delete(b)
line.delete(array.get(ceLines, i))
label.delete(array.get(labels, i))
array.remove(boxes, i)
array.remove(ceLines, i)
array.remove(labels, i)
array.remove(tops, i)
array.remove(bots, i)
array.remove(dirs, i)
array.remove(states, i)
array.remove(births, i)
array.remove(deaths, i)
array.remove(slots, i)
// ------------------------------------------------------------ session highs and lows (Eastern time)
gsh = "6. Session Highs And Lows"
shOn = input.bool(true, "Show Session Highs And Lows", group=gsh)
shAsia = input.bool(true, " Asia 6:00 PM - 2:00 AM", group=gsh)
shLdn = input.bool(true, " London 2:00 AM - 5:00 AM", group=gsh)
shPre = input.bool(false, " Pre-Market 5:00 AM - 9:30 AM", group=gsh)
shNy = input.bool(true, " New York 9:30 AM - 4:00 PM", group=gsh)
shOn2 = input.bool(true, " Overnight 6:00 PM - 9:30 AM", group=gsh)
shPd = input.bool(true, " Previous Day High And Low", group=gsh)
shLbl = input.bool(true, " Labels On The Lines", group=gsh)
cAsia = input.color(color.new(#8A7D6B, 0), " Asia Colour", group=gsh)
cLdn = input.color(color.new(#2C4A6E, 0), " London Colour", group=gsh)
cPre = input.color(color.new(#6B6B66, 0), " Pre-Market Colour", group=gsh)
cNy = input.color(color.new(#1A1A1A, 0), " New York Colour", group=gsh)
cOn = input.color(color.new(#2E5E4E, 0), " Overnight Colour", group=gsh)
cPd = input.color(color.new(#C0402A, 0), " Previous Day Colour", group=gsh)
sessOpac = input.int(0, " Line Transparency (0 = Solid)", minval=0, maxval=100, group=gsh)
// ------------------------------------------------------------ killzones (shaded, labelled, Eastern time)
gkz = "7. Killzones"
kzOn = input.bool(true, "Show Killzones", group=gkz)
kzAsia = input.bool(false, " Asia 6:00 PM - 2:00 AM", group=gkz)
kzLdn = input.bool(true, " London KZ 2:00 AM - 5:00 AM", group=gkz)
kzNyAm = input.bool(true, " New York AM KZ 8:30 AM - 11:00 AM", group=gkz)
kzLunch = input.bool(true, " Lunch 12:00 PM - 1:00 PM", group=gkz)
kzNyPm = input.bool(true, " New York PM KZ 1:30 PM - 4:00 PM", group=gkz)
kzSb = input.bool(false, " Silver Bullet Windows 3-4 AM, 10-11 AM, 2-3 PM", group=gkz)
kzLbl = input.bool(true, " Labels", group=gkz)
kzOpac = input.int(90, " Shading Transparency (0 = Solid, 100 = Invisible)", minval=0, maxval=100, group=gkz)
cKz = input.color(color.new(#2C4A6E, 0), " Killzone Colour", group=gkz)
cLunch = input.color(color.new(#8A7D6B, 0), " Lunch Colour", group=gkz)
cSb = input.color(color.new(#C0402A, 0), " Silver Bullet Colour", group=gkz)
tz = "America/New_York"
// one running high/low pair per session; lines extend right until the next instance of that session starts
sessHL(string name, string sessStr, bool show, color col) =>
var float h = na
var float l = na
var line hl = na
var line ll = na
var label hb = na
var label lb = na
var bool live = false
inS = not na(time(timeframe.period, sessStr, tz))
if show
if inS and not inS[1]
if not na(hl)
line.set_extend(hl, extend.none)
line.set_x2(hl, bar_index)
line.set_extend(ll, extend.none)
line.set_x2(ll, bar_index)
h := high
l := low
live := true
c = color.new(col, sessOpac)
hl := line.new(bar_index, h, bar_index + 1, h, color=c, style=line.style_dashed, extend=extend.right)
ll := line.new(bar_index, l, bar_index + 1, l, color=c, style=line.style_dashed, extend=extend.right)
if shLbl
hb := label.new(bar_index + 1, h, name + " High " + str.tostring(h, "#,##0.00"), style=label.style_label_left, color=color.new(color.white, 100), textcolor=c, size=lblSize, text_formatting=text.format_italic)
lb := label.new(bar_index + 1, l, name + " Low " + str.tostring(l, "#,##0.00"), style=label.style_label_left, color=color.new(color.white, 100), textcolor=c, size=lblSize, text_formatting=text.format_italic)
else if inS and not na(hl)
h := math.max(h, high)
l := math.min(l, low)
line.set_y1(hl, h)
line.set_y2(hl, h)
line.set_y1(ll, l)
line.set_y2(ll, l)
if shLbl and not na(hb)
label.set_y(hb, h)
label.set_x(hb, bar_index + 1)
label.set_text(hb, name + " High " + str.tostring(h, "#,##0.00"))
label.set_y(lb, l)
label.set_x(lb, bar_index + 1)
label.set_text(lb, name + " Low " + str.tostring(l, "#,##0.00"))
else if not inS and live and shLbl and not na(hb)
label.set_x(hb, bar_index + 1)
label.set_x(lb, bar_index + 1)
[h, l]
[asiaH, asiaL] = sessHL("Asia", "1800-0200:1234567", shOn and shAsia, cAsia)
[ldnH, ldnL] = sessHL("London", "0200-0500:1234567", shOn and shLdn, cLdn)
[preH, preL] = sessHL("Pre-Market", "0500-0930:1234567", shOn and shPre, cPre)
[nyH, nyL] = sessHL("New York", "0930-1600:1234567", shOn and shNy, cNy)
[onH, onL] = sessHL("Overnight", "1800-0930:1234567", shOn and shOn2, cOn)
// previous day high and low (from the daily, previous closed day; drawn from the 6 PM reopen)
[pdH, pdL] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead=barmerge.lookahead_on)
var line pdHl = na
var line pdLl = na
var label pdHb = na
var label pdLb = na
newDay = not na(time(timeframe.period, "1800-1801:1234567", tz)) and na(time(timeframe.period, "1800-1801:1234567", tz)[1])
if shOn and shPd and (newDay or (barstate.isfirst))
if not na(pdHl)
line.set_extend(pdHl, extend.none)
line.set_x2(pdHl, bar_index)
line.set_extend(pdLl, extend.none)
line.set_x2(pdLl, bar_index)
c = color.new(cPd, sessOpac)
pdHl := line.new(bar_index, pdH, bar_index + 1, pdH, color=c, style=line.style_solid, extend=extend.right)
pdLl := line.new(bar_index, pdL, bar_index + 1, pdL, color=c, style=line.style_solid, extend=extend.right)
if shLbl
pdHb := label.new(bar_index + 1, pdH, "PDH " + str.tostring(pdH, "#,##0.00"), style=label.style_label_left, color=color.new(color.white, 100), textcolor=c, size=lblSize, text_formatting=text.format_italic)
pdLb := label.new(bar_index + 1, pdL, "PDL " + str.tostring(pdL, "#,##0.00"), style=label.style_label_left, color=color.new(color.white, 100), textcolor=c, size=lblSize, text_formatting=text.format_italic)
if shOn and shPd and not na(pdHb) and shLbl
label.set_x(pdHb, bar_index + 1)
label.set_x(pdLb, bar_index + 1)
// killzone boxes: one per instance, sized to the session's running high and low
kzBox(string name, string sessStr, bool show, color col) =>
var box bx = na
var float h = na
var float l = na
inS = not na(time(timeframe.period, sessStr, tz))
if show
if inS and not inS[1]
h := high
l := low
bx := box.new(bar_index, h, bar_index, l, border_color=color.new(col, 60), bgcolor=color.new(col, kzOpac), text=kzLbl ? name : "", text_color=color.new(col, 0), text_size=lblSize, text_halign=text.align_left, text_valign=text.align_top, text_formatting=text.format_italic)
else if inS and not na(bx)
h := math.max(h, high)
l := math.min(l, low)
box.set_top(bx, h)
box.set_bottom(bx, l)
box.set_right(bx, bar_index)
true
kzBox("Asia", "1800-0200:1234567", kzOn and kzAsia, cKz)
kzBox("London KZ", "0200-0500:1234567", kzOn and kzLdn, cKz)
kzBox("New York AM KZ", "0830-1100:1234567", kzOn and kzNyAm, cKz)
kzBox("Lunch", "1200-1300:1234567", kzOn and kzLunch, cLunch)
kzBox("New York PM KZ", "1330-1600:1234567", kzOn and kzNyPm, cKz)
kzBox("SB 3-4", "0300-0400:1234567", kzOn and kzSb, cSb)
kzBox("SB 10-11", "1000-1100:1234567", kzOn and kzSb, cSb)
kzBox("SB 2-3", "1400-1500:1234567", kzOn and kzSb, cSb)
// ------------------------------------------------------------ sizer
gs = "8. Sizer (Uses This Symbol's Point Value: MNQ = 2, NQ = 20)"
showTable = input.bool(true, "Show The Table", group=gs)
account = input.float(50000, "Account ($)", minval=1, group=gs)
riskPct = input.float(1.0, "Risk Per Trade (%)", minval=0.01, step=0.1, group=gs)
commSide = input.float(0.52, "Commission Per Side, Per Contract ($)", minval=0, group=gs)
tablePos = input.string("top_right", "Table Position", options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"], group=gs)
tableOpac = input.int(5, "Table Background Transparency", minval=0, maxval=100, group=gs)
// ------------------------------------------------------------ position plan (type it in, or click the price on the chart from the settings)
gp = "9. Position Plan (Type It, Or Click The Price On The Chart From This Dialog)"
posOn = input.bool(false, "Show The Position Plan", group=gp, tooltip="Pine cannot read your broker. Type the order you placed (or will place) here; the script draws it and does the arithmetic.")
posLong = input.bool(true, "Long (Off = Short)", group=gp)
posEntry = input.price(0.0, "Entry Price (0 = Last Close)", group=gp)
posStop = input.price(0.0, "Stop Price", group=gp)
posQty = input.int(0, "Contracts (0 = Use The Sizer's Number)", minval=0, group=gp)
tp1P = input.price(0.0, "Take Profit 1 Price (0 = Off)", group=gp)
tp1Q = input.int(1, "Take Profit 1 Contracts", minval=0, group=gp)
tp2P = input.price(0.0, "Take Profit 2 Price (0 = Off)", group=gp)
tp2Q = input.int(1, "Take Profit 2 Contracts", minval=0, group=gp)
tp3P = input.price(0.0, "Take Profit 3 Price (0 = Off)", group=gp)
tp3Q = input.int(1, "Take Profit 3 Contracts", minval=0, group=gp)
stopDist = input.float(50.0, "Stop Distance If No Stop Price (Points)", minval=0.25, group=gp)
drawPlan = input.bool(true, "Draw The Entry, Stop And Take-Profit Lines", group=gp)
// ------------------------------------------------------------ named levels (support, resistance, anything): drawn and listed in the table
gl = "10. Named Levels (Resistance, Support, Anything)"
lvOn = input.bool(true, "Show Named Levels", group=gl, tooltip="Set a price (or 0 to hide). The name is what appears on the line and in the table, with the distance from the last close.")
l1n = input.string("Resistance 1", "Level 1 Name", group=gl)
l1p = input.price(0.0, "Level 1 Price", group=gl)
l2n = input.string("Resistance 2", "Level 2 Name", group=gl)
l2p = input.price(0.0, "Level 2 Price", group=gl)
l3n = input.string("Resistance 3", "Level 3 Name", group=gl)
l3p = input.price(0.0, "Level 3 Price", group=gl)
l4n = input.string("Support 1", "Level 4 Name", group=gl)
l4p = input.price(0.0, "Level 4 Price", group=gl)
l5n = input.string("Support 2", "Level 5 Name", group=gl)
l5p = input.price(0.0, "Level 5 Price", group=gl)
l6n = input.string("Support 3", "Level 6 Name", group=gl)
l6p = input.price(0.0, "Level 6 Price", group=gl)
l7n = input.string("PDH", "Level 7 Name", group=gl)
l7p = input.price(0.0, "Level 7 Price", group=gl)
l8n = input.string("PDL", "Level 8 Name", group=gl)
l8p = input.price(0.0, "Level 8 Price", group=gl)
cLevel = input.color(color.new(#2C4A6E, 0), "Level Line Colour", group=gl)
cPlan = input.color(color.new(#1A1A1A, 0), "Entry Line Colour", group=gl)
cStop = input.color(color.new(#C0402A, 0), "Stop Line Colour", group=gl)
cTp = input.color(color.new(#2E5E4E, 0), "Take-Profit Line Colour", group=gl)
// ------------------------------------------------------------ arithmetic
pv = syminfo.pointvalue
e = posEntry > 0 ? posEntry : close
dist = posStop > 0 ? math.abs(e - posStop) : stopDist
perContract = dist * pv + 2 * commSide
riskDollars = account * riskPct / 100
sizerN = int(math.floor(riskDollars / perContract))
qty = posQty > 0 ? posQty : sizerN
dirSign = posLong ? 1 : -1
f2(float x) => str.tostring(x, "#,##0.00")
f0(float x) => str.tostring(x, "#,##0")
usd(float x) => (x < 0 ? "-$" : "$") + f0(math.abs(x))
rr(float price) => dist > 0 ? str.tostring(dirSign * (price - e) / dist, "0.00") + "R" : ""
tpDol(float price, int q) => dirSign * (price - e) * pv * q - 2 * commSide * q
// ------------------------------------------------------------ drawing: plan lines and named levels (redrawn on the last bar)
var line lnE = na
var line lnS = na
var line lnT1 = na
var line lnT2 = na
var line lnT3 = na
var label lbE = na
var label lbS = na
var label lbT1 = na
var label lbT2 = na
var label lbT3 = na
var lvLines = array.new<line>()
var lvLabels = array.new<label>()
hLine2(float price, color col, string txt, string sty) =>
ln = line.new(bar_index - 20, price, bar_index, price, color=col, style=sty == "dash" ? line.style_dashed : sty == "dot" ? line.style_dotted : line.style_solid, extend=extend.both, width=1)
lb = label.new(bar_index + 1, price, txt, style=label.style_label_left, color=color.new(color.white, 100), textcolor=col, size=lblSize, text_formatting=text.format_italic)
[ln, lb]
if barstate.islast
// clear and redraw the plan
line.delete(lnE)
line.delete(lnS)
line.delete(lnT1)
line.delete(lnT2)
line.delete(lnT3)
label.delete(lbE)
label.delete(lbS)
label.delete(lbT1)
label.delete(lbT2)
label.delete(lbT3)
if posOn and drawPlan
[a1, b1] = hLine2(e, cPlan, "Entry " + f2(e) + " - " + str.tostring(qty) + " MNQ", "solid")
lnE := a1
lbE := b1
sp = posStop > 0 ? posStop : e - dirSign * dist
[a2, b2] = hLine2(sp, cStop, "Stop " + f2(sp) + " - " + f2(dist) + " Pts - " + usd(-qty * perContract), "dash")
lnS := a2
lbS := b2
if tp1P > 0
[a3, b3] = hLine2(tp1P, cTp, "TP1 " + f2(tp1P) + " - " + str.tostring(tp1Q) + " Off - " + rr(tp1P) + " - " + usd(tpDol(tp1P, tp1Q)), "dot")
lnT1 := a3
lbT1 := b3
if tp2P > 0
[a4, b4] = hLine2(tp2P, cTp, "TP2 " + f2(tp2P) + " - " + str.tostring(tp2Q) + " Off - " + rr(tp2P) + " - " + usd(tpDol(tp2P, tp2Q)), "dot")
lnT2 := a4
lbT2 := b4
if tp3P > 0
[a5, b5] = hLine2(tp3P, cTp, "TP3 " + f2(tp3P) + " - " + str.tostring(tp3Q) + " Off - " + rr(tp3P) + " - " + usd(tpDol(tp3P, tp3Q)), "dot")
lnT3 := a5
lbT3 := b5
// clear and redraw the named levels
if array.size(lvLines) > 0
for i = 0 to array.size(lvLines) - 1
line.delete(array.get(lvLines, i))
label.delete(array.get(lvLabels, i))
array.clear(lvLines)
array.clear(lvLabels)
if lvOn
names = array.from(l1n, l2n, l3n, l4n, l5n, l6n, l7n, l8n)
prices = array.from(l1p, l2p, l3p, l4p, l5p, l6p, l7p, l8p)
for i = 0 to 7
p = array.get(prices, i)
if p > 0
[ln, lb] = hLine2(p, cLevel, array.get(names, i) + " " + f2(p), "solid")
array.push(lvLines, ln)
array.push(lvLabels, lb)
// ------------------------------------------------------------ the table
pos = tablePos == "top_left" ? position.top_left : tablePos == "top_center" ? position.top_center : tablePos == "top_right" ? position.top_right : tablePos == "middle_left" ? position.middle_left : tablePos == "middle_center" ? position.middle_center : tablePos == "middle_right" ? position.middle_right : tablePos == "bottom_left" ? position.bottom_left : tablePos == "bottom_center" ? position.bottom_center : position.bottom_right
var table t = table.new(pos, 2, 32, bgcolor=color.new(color.white, tableOpac), border_color=color.new(color.gray, 60), border_width=1, frame_color=color.new(color.black, 0), frame_width=1)
inkCol = color.new(#1A1A1A, 0)
accCol = color.new(#C0402A, 0)
row = 0
cellPair(int r, string a, string b, color colB, bool header) =>
table.cell(t, 0, r, a, text_color=header ? color.white : inkCol, bgcolor=header ? inkCol : color(na), text_halign=text.align_left, text_size=tblSize, text_formatting=text.format_italic)
table.cell(t, 1, r, b, text_color=header ? color.white : colB, bgcolor=header ? inkCol : color(na), text_halign=text.align_right, text_size=tblSize, text_formatting=text.format_italic)
r + 1
if showTable and barstate.islast
table.clear(t, 0, 0, 1, 31)
row := 0
row := cellPair(row, syminfo.ticker + " - $" + f2(pv) + " Per Point", "Last " + f2(close), inkCol, true)
row := cellPair(row, "Risk Budget", usd(riskDollars) + " (" + str.tostring(riskPct) + "%)", inkCol, false)
row := cellPair(row, "Stop Distance", f2(dist) + " Pts - " + usd(-perContract) + " Per Contract", inkCol, false)
row := cellPair(row, "Contracts By The Rule", str.tostring(sizerN) + (sizerN == 0 ? " - No Trade" : ""), accCol, false)
row := cellPair(row, "1R / 2R / 3R", f2(e + dirSign * dist) + " / " + f2(e + dirSign * 2 * dist) + " / " + f2(e + dirSign * 3 * dist), inkCol, false)
if posOn
sp = posStop > 0 ? posStop : e - dirSign * dist
row := cellPair(row, "Position", (posLong ? "Long " : "Short ") + str.tostring(qty) + " MNQ", inkCol, true)
row := cellPair(row, "Entry", f2(e), inkCol, false)
row := cellPair(row, "Stop", f2(sp) + " - " + usd(-qty * perContract), accCol, false)
left = qty
total = 0.0
if tp1P > 0
row := cellPair(row, "TP1 - " + str.tostring(tp1Q) + " Off", f2(tp1P) + " - " + rr(tp1P) + " - " + usd(tpDol(tp1P, tp1Q)), inkCol, false)
left -= tp1Q
total += tpDol(tp1P, tp1Q)
if tp2P > 0
row := cellPair(row, "TP2 - " + str.tostring(tp2Q) + " Off", f2(tp2P) + " - " + rr(tp2P) + " - " + usd(tpDol(tp2P, tp2Q)), inkCol, false)
left -= tp2Q
total += tpDol(tp2P, tp2Q)
if tp3P > 0
row := cellPair(row, "TP3 - " + str.tostring(tp3Q) + " Off", f2(tp3P) + " - " + rr(tp3P) + " - " + usd(tpDol(tp3P, tp3Q)), inkCol, false)
left -= tp3Q
total += tpDol(tp3P, tp3Q)
row := cellPair(row, "Still Open After Targets", str.tostring(left) + (left < 0 ? " - Targets Exceed The Position" : ""), left < 0 ? accCol : inkCol, false)
row := cellPair(row, "If Every Target Hits", usd(total) + (dist > 0 and qty > 0 ? " - " + str.tostring(total / (qty * dist * pv), "0.00") + "R" : ""), inkCol, false)
row := cellPair(row, "Open P&L At Last", usd(dirSign * (close - e) * pv * qty) + " - " + rr(close), dirSign * (close - e) >= 0 ? color.new(#2E5E4E, 0) : accCol, false)
if lvOn
names = array.from(l1n, l2n, l3n, l4n, l5n, l6n, l7n, l8n)
prices = array.from(l1p, l2p, l3p, l4p, l5p, l6p, l7p, l8p)
row := cellPair(row, "Named Levels", "Points From Last", inkCol, true)
for i = 0 to 7
p = array.get(prices, i)
if p > 0
row := cellPair(row, array.get(names, i), f2(p) + " - " + (p >= close ? "+" : "-") + f2(math.abs(p - close)), inkCol, false)
// ------------------------------------------------------------ alerts
alertcondition(newBull, "New bullish FVG", "New bullish FVG on {{ticker}} {{interval}}")
alertcondition(newBear, "New bearish FVG", "New bearish FVG on {{ticker}} {{interval}}")