don't want to see "FVG" text on chart, and bias label over/under a swing point
Assistant
// --- True range for label positioning ---
tr = ta.tr(true)
// --- Bias Detection and Label ---
var label biasLabel = na
bullishBias = (emaValue > emaValue2) and (emaValue2 > emaValue3)
bearishBias = (emaValue < emaValue2) and (emaValue2 < emaValue3)
if not na(biasLabel)
label.delete(biasLabel)
if bullishBias
biasLabel := label.new(bar_index, low - tr1.5, "Only Buy", xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_up, color=color.new(color.teal, 0), textcolor=color.white, size=size.large)
else if bearishBias
biasLabel := label.new(bar_index, high + tr1.5, "Only Sell", xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.large)
else
// No label if no clear bias, do nothing
na
// === FVG SETTINGS ===
isFvgToShow = input(true, title='Display FVG', group="Fair Value Gap")
bullishFvgColor = input.color(color.new(color.green, 50), 'Bullish FVG Color', group="Fair Value Gap")
bearishFvgColor = input.color(color.new(color.red, 50), 'Bearish FVG Color', group="Fair Value Gap")
mitigatedFvgColor = input.color(color.new(color.gray, 50), 'Mitigated FVG Color', group="Fair Value Gap")
fvgHistoryNbr = input.int(5, 'Number of FVG to show', minval=1, maxval=50, group="Fair Value Gap")
isMitigatedFvgToReduce = input(false, title='Reduce mitigated FVG', group="Fair Value Gap")
// === DATA STRUCTURES ===
// For Liquidity
type liquidity
float value
int barStart
int barEnd
line liquidityLine
bool broken
label sweep
var array bullishLiquidity = array.new()
var array bearishLiquidity = array.new()
// For FVG
var array fvgBoxes = array.new_box(0)
var array fvgTypes = array.new_bool(0)
var array fvgLabels = array.new_label(0)
var array isFvgMitigated = array.new_bool(0)
// === FVG FUNCTIONS ===
FVGDraw(_boxes, _fvgTypes, _isFvgMitigated, _fvgLabels) =>
for [index, value] in _boxes
// Processing bullish FVG
if array.get(_fvgTypes, index)
if low <= box.get_bottom(value)
array.remove(_boxes, index)
array.remove(_fvgTypes, index)
array.remove(_isFvgMitigated, index)
label.delete(array.get(_fvgLabels, index))
array.remove(_fvgLabels, index)
box.delete(value)
else
if low < box.get_top(value)
box.set_bgcolor(value, mitigatedFvgColor)
if not array.get(_isFvgMitigated, index)
alert("FVG has been mitigated", alert.freq_once_per_bar)
array.set(_isFvgMitigated, index, true)
if isMitigatedFvgToReduce
box.set_top(value, low)
box.set_right(value, bar_index)
label.set_x(array.get(_fvgLabels, index), (box.get_left(value) + box.get_right(value)) / 2 )
label.set_y(array.get(_fvgLabels, index), box.get_top(value) - (box.get_top(value) - box.get_bottom(value)) / 2)
// Processing bearish FVG
else
if high >= box.get_top(value)
array.remove(_boxes, index)
array.remove(_fvgTypes, index)
array.remove(_isFvgMitigated, index)
label.delete(array.get(_fvgLabels, index))
array.remove(_fvgLabels, index)
box.delete(value)
else
if high > box.get_bottom(value)
box.set_bgcolor(value, mitigatedFvgColor)
if not array.get(_isFvgMitigated, index)
alert("FVG has been mitigated", alert.freq_once_per_bar)
array.set(_isFvgMitigated, index, true)
if isMitigatedFvgToReduce
box.set_bottom(value, high)
box.set_right(value, bar_index)
label.set_x(array.get(_fvgLabels, index), (box.get_left(value) + box.get_right(value)) / 2 )
label.set_y(array.get(_fvgLabels, index), box.get_top(value) - (box.get_top(value) - box.get_bottom(value)) / 2)
// === UPDATE VISUALS ===
// Update Liquidity Lines and Sweeps
var bool sweepAlert = false
if array.size(bearishLiquidity) > 0
liquidity testLiquidity = na
for i = array.size(bearishLiquidity) - 1 to 0 by 1
testLiquidity := array.get(bearishLiquidity, i)
if high > testLiquidity.value
testLiquidity.liquidityLine.set_x2(time)
testLiquidity.liquidityLine.set_style(line.style_dashed)
array.remove(bearishLiquidity, i)
if close < testLiquidity.value
testLiquidity.sweep := label.new(x = time, y = high, text = 'x', xloc = xloc.bar_time, style = label.style_label_down, size = size.normal, textcolor = color.new(color.purple, 0), color = color.new(color.white, 100))
sweepAlert := true
else
testLiquidity.liquidityLine.set_x2(time)
if array.size(bullishLiquidity) > 0
liquidity testLiquidity = na
for i = array.size(bullishLiquidity) - 1 to 0 by 1
testLiquidity := array.get(bullishLiquidity, i)
if low < testLiquidity.value
testLiquidity.liquidityLine.set_x2(time)
testLiquidity.liquidityLine.set_style(line.style_dashed)
array.remove(bullishLiquidity, i)
if close > testLiquidity.value
testLiquidity.sweep := label.new(x = time, y = low, text = 'x', xloc = xloc.bar_time, style = label.style_label_up, size = size.normal, textcolor = color.new(color.teal, 0), color = color.new(color.white, 100))
sweepAlert := true
else
testLiquidity.liquidityLine.set_x2(time)
// === ALERTS ===
alertcondition(sweepAlert, title = "Liquidity Sweep", message = "A liquidity zone has been swept.")
sweepAlert := false
// === PREVIOUS DAY HIGH/LOW SETTINGS ===
showPrevDayRange = input.bool(true, 'Show Previous Day High/Low', group='Previous Day Range')
prevDayColorHigh = input.color(color.new(color.blue, 0), 'Previous Day High Color', group='Previous Day Range')
prevDayColorLow = input.color(color.new(color.orange, 0), 'Previous Day Low Color', group='Previous Day Range')
prevDayLineWidth = input.int(2, 'Previous Day Line Width', minval=1, maxval=5, group='Previous Day Range')
prevDayLineStyle = input.string('Dashed', 'Previous Day Line Style', options=['Dashed', 'Dotted'], group='Previous Day Range')
// Convert line style string to line.style constant
lineStyle = prevDayLineStyle == 'Dotted' ? line.style_dotted : line.style_dashed
// Get previous day timeframe string or use 'D'
dailyTF = "D" // fixed daily timeframe
// Request previous daily data (high/low) and previous day start timestamp (session start)
prevDayHigh = request.security(syminfo.tickerid, dailyTF, high[1], lookahead=barmerge.lookahead_on)
prevDayLow = request.security(syminfo.tickerid, dailyTF, low[1], lookahead=barmerge.lookahead_on)
prevDayTime = request.security(syminfo.tickerid, dailyTF, time[1], lookahead=barmerge.lookahead_on) // Timestamp of previous daily bar start
// Variables to hold lines just once (persistent)
var line prevHighLine = na
var line prevLowLine = na
else
if not na(prevHighLine)
line.delete(prevHighLine)
prevHighLine := na
if not na(prevLowLine)
line.delete(prevLowLine)
prevLowLine := na
Your script looks sophisticated and well-structured for detecting liquidity lines, sweeps, fair value gaps (FVG), EMAs, and plotting previous day levels with customizable input options.
Here are a few observations, possible improvements, and clarifications you may find useful:
1. Version Usage and Syntax
You are using //@version=6, which is the newest Pine Script version as of June 2024, good for latest features.
The usage of type to define the liquidity structure is a nice touch, appropriate for managing complex objects in arrays.
request.security() calls with timeframe option are handled correctly.
2. Liquidity Detection & Updating
Liquidity structures are correctly updated and lines created over time.
You correctly remove entries when the lines become broken (swept liquidity):
For bearish liquidity, if current high > liquidity.value.
For bullish liquidity, if current low < liquidity.value.
You generate sweep labels properly with color coding.
You cap liquidity arrays at 7 elements to avoid excessive resource use.
Note:
In your sweeping detection loops:
pinescript
for i = array.size(bearishLiquidity) - 1 to 0 by 1
you iterate backward which is correct because you remove elements inside the loop. This prevents skipping items or out-of-range errors.
3. Fair Value Gap (FVG) Detection & Drawing
Your bullish FVG condition: high[3] < low[1]
Bearish FVG condition: low[3] > high[1]
These conditions align with common FVG definitions between bar 3 and bar 1.
You use arrays of boxes, types, labels, and mitigation flags for managing FVG objects.
Mitigation logic: If price closes into the gap zone, color and alert trigger, with option to shrink the boxes.
4. EMA Calculation & Bias Label
You correctly allow custom EMA inputs with multi-timeframe support.
Bias detection is based on slope/direction of EMAs:
pinescript
bullishBias = (emaValue > emaValue2) and (emaValue2 > emaValue3)
bearishBias = (emaValue < emaValue2) and (emaValue2 < emaValue3)
You create a single label at each bar (delete previous label before creating a new one).
Use of tr for vertical placement of the label is practical.
5. Previous Day High/Low Lines
You calculate previous day high, low, and timestamp via request.security of daily timeframe.
Draw lines extending into the future (approx 500 bars) so the lines persist across the intraday chart.
lineStyle string input is converted to line style constants correctly.
You responsibly delete lines if showPrevDayRange is toggled off.
or swap if your intent was different, but the top should be the max (higher price) and bottom the min (lower price).
Line.X2 update on liquidity lines:
You are extending lines using time which is valid using xloc.bar_time for horizontal axes, but be aware of chart timezones/time alignment if you want to be exact in visual expiry.
Alert conditions:
Your alert condition uses a variable sweepAlert declared as a var bool, set inside loops — this can potentially raise multiple alerts in the same bar if you add more liquidity zones. You might want to explicitly reset it right after the alertcondition() call (which you do), or else consider counting multiple alerts if that's preferred.
Minor Cleanup:
Your code uses line and boxes with auto-delete when arrays exceed sizes, which is good.
Summary
Your script is very thorough and should work well. As a next step, test these aspects:
Confirm FVG box coordinates (top/bottom) are as expected (especially for bearish cases).
Test alerts firing correctly without multiple repeats on single bars.
Validate previous day lines appear and extend properly irrespective of intraday timeframe.
Optional: Add some user instructions or comments for first-time users on how to interpret liquidity sweeps and FVG boxes.
If you'd like, I can also help you with any particular enhancement, bug fix, or translating this into a specific feature. Just let me know!
Got it! You want:
Remove the "FVG" text label on the Fair Value Gap boxes (so only the shaded boxes appear).
Make the bias label ("Only Buy" / "Only Sell") show positioned over/under the closest recent swing point (pivot high for bearish, pivot low for bullish) instead of relative to the current bar's low/high.
Just remove or comment out this label creation. That means remove these lines from both bullish and bearish FVG creation:
pinescript
// Remove this line:
label _label = label.new(math.ceil((_box.get_left() + _box.get_right()) / 2), _box.get_top() - (_box.get_top() - _box.get_bottom()) / 2, text = "FVG", style = label.style_none, textcolor = color.white)
// And remove all array.push(fvgLabels, _label) for these labels.
// Instead push na to keep array sizes consistent
array.push(fvgLabels, na)
Modify the related parts like this:
pinescript
// For bullish FVG:
array.push(fvgBoxes, _box)
array.push(fvgTypes, true)
array.push(isFvgMitigated, false)
array.push(fvgLabels, na) // no label for FVG
// Do the same for bearish FVG.
If you want, you can even remove the fvgLabels array entirely and related label management inside FVGDraw() but for minimal changes, just keep pushing na instead of labels.
We can try to identify the most recent pivot high or pivot low (the closest swing on the left), then place bias labels there.
Suggestion:
Find the last pivot low index for bullish bias and place the label there just below low.
Find the last pivot high index for bearish bias and put label just above high.
Implementation examples
Add these helper variables near bias code:
pinescript
// Find most recent pivot low/bar index for bullish label
var int lastSwingLowIndex = na
var float lastSwingLowPrice = na
for i = 0 to liquidity_len*2 // search backward within some range
int idx = bar_index - i
if not na(plLiquidity[i])
lastSwingLowIndex := bar_index - i // i since plLiquidity is shifted by liquidity_len bars
lastSwingLowPrice := low[i]
break
// Find most recent pivot high/bar index for bearish label
var int lastSwingHighIndex = na
var float lastSwingHighPrice = na
for i = 0 to liquidity_len*2
int idx = bar_index - i
if not na(phLiquidity[i])
lastSwingHighIndex := bar_index - i
lastSwingHighPrice := high[i]
break
But since plLiquidity and phLiquidity are only non-na at pivot bars (with look forward and back equal to liquidity_len), and they shift time, it's easier to capture last known swing pivot bar's bar_index and price by a helper function.
More robust way (using ta.valuewhen()):
pinescript
// Get the bar_index of last pivot low
lastSwingLowBarIndex = ta.valuewhen(not na(plLiquidity), bar_index[liquidity_len], 0)
lastSwingLowPrice = ta.valuewhen(not na(plLiquidity), low[liquidity_len], 0)
// Get the bar_index of last pivot high
lastSwingHighBarIndex = ta.valuewhen(not na(phLiquidity), bar_index[liquidity_len], 0)
lastSwingHighPrice = ta.valuewhen(not na(phLiquidity), high[liquidity_len], 0)
(We use [liquidity_len] offset to match how pivot functions work — pivots are identified liquidity_len bars ago.)
Then update the bias label block to:
pinescript
if not na(biasLabel)
label.delete(biasLabel)
if bullishBias and not na(lastSwingLowBarIndex)
biasLabel := label.new(lastSwingLowBarIndex, lastSwingLowPrice - tr*1.5, "Only Buy", xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_up, color=color.new(color.teal, 0), textcolor=color.white, size=size.large)
else if bearishBias and not na(lastSwingHighBarIndex)
biasLabel := label.new(lastSwingHighBarIndex, lastSwingHighPrice + tr*1.5, "Only Sell", xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.large)
else
na
Summary of changes
Remove FVG text labels, replace with na in fvgLabels array.
Use ta.valuewhen() to grab last detected swing pivot bar index and price.
Place bias label over that swing point with an offset of tr * 1.5 for vertical placement.
If you want me to provide a fully patched snippet just for these parts, let me know!