FvgPanelโโOVERVIEW
This library provides functionalities for creating and managing a display panel within a Pine Scriptโข indicator. Its primary purpose is to offer a structured way to present Fair Value Gap (FVG) information, specifically the nearest bullish and bearish FVG levels across different timeframes (Current, MTF, HTF), directly on the chart. The library handles the table's structure, header initialization, and dynamic cell content updates.
โโCONCEPTS
The core of this library revolves around presenting summarized FVG data in a clear, tabular format. Key concepts include:
FVG Data Aggregation and Display
The panel is designed to show at-a-glance information about the closest active FVG mitigation levels. It doesn't calculate these FVGs itself but relies on the main script to provide this data. The panel is structured with columns for timeframes (TF), Bullish FVGs, and Bearish FVGs, and rows for "Current" (LTF), "MTF" (Medium Timeframe), and "HTF" (High Timeframe).
The `panelData` User-Defined Type (UDT)
To facilitate the transfer of information to be displayed, the library defines a UDT named `panelData`. This structure is central to the library's operation and is designed to hold all necessary values for populating the panel's data cells for each relevant FVG. Its fields include:
Price levels for the nearest bullish and bearish FVGs for LTF, MTF, and HTF (e.g., `nearestBullMitLvl`, `nearestMtfBearMitLvl`).
Boolean flags to indicate if these FVGs are classified as "Large Volume" (LV) (e.g., `isNearestBullLV`, `isNearestMtfBearLV`).
Color information for the background and text of each data cell, allowing for conditional styling based on the FVG's status or proximity (e.g., `ltfBullBgColor`, `mtfBearTextColor`).
The design of `panelData` allows the main script to prepare all display-related data and styling cues in one object, which is then passed to the `updatePanel` function for rendering. This separation of data preparation and display logic keeps the library focused on its presentation task.
Visual Cues and Formatting
Price Formatting: Price levels are formatted to match the instrument's minimum tick size using an internal `formatPrice` helper function, ensuring consistent and accurate display.
Large FVG Icon: If an FVG is marked as a "Large Volume" FVG in the `panelData` object, a user-specified icon (e.g., an emoji) is prepended to its price level in the panel, providing an immediate visual distinction.
Conditional Styling: The background and text colors for each FVG level displayed in the panel can be individually controlled via the `panelData` object, enabling the main script to implement custom styling rules (e.g., highlighting the overall nearest FVG across all timeframes).
Handling Missing Data: If no FVG data is available for a particular cell (i.e., the corresponding level in `panelData` is `na`), the panel displays "---" and uses a specified background color for "Not Available" cells.
โโCALCULATIONS AND USE
Using the `FvgPanel` typically involves a two-stage process: initialization and dynamic updates.
Step 1: Panel Creation
First, an instance of the panel table is created once, usually during the script's initial setup. This is done using the `createPanel` function.
Call `createPanel()` with parameters defining its position on the chart, border color, border width, header background color, header text color, and header text size.
This function initializes the table with three columns ("TF", "Bull FVG", "Bear FVG") and three data rows labeled "Current", "MTF", and "HTF", plus a header row.
Store the returned `table` object in a `var` variable to persist it across bars.
// Example:
var table infoPanel = na
if barstate.isfirst
infoPanel := panel.createPanel(
position.top_right,
color.gray,
1,
color.new(color.gray, 50),
color.white,
size.small
)
Step 2: Panel Updates
On each bar, or whenever the FVG data changes (typically on `barstate.islast` or `barstate.isrealtime` for efficiency), the panel's content needs to be refreshed. This is done using the `updatePanel` function.
Populate an instance of the `panelData` UDT with the latest FVG information. This includes setting the nearest bullish/bearish mitigation levels for LTF, MTF, and HTF, their LV status, and their desired background and text colors.
Call `updatePanel()`, passing the persistent `table` object (from Step 1), the populated `panelData` object, the icon string for LV FVGs, the default text color for FVG levels, the background color for "N/A" cells, and the general text size for the data cells.
The `updatePanel` function will then clear previous data and fill the table cells with the new values and styles provided in the `panelData` object.
// Example (inside a conditional block like 'if barstate.islast'):
var panelData fvgDisplayData = panelData.new()
// ... (logic to populate fvgDisplayData fields) ...
// fvgDisplayData.nearestBullMitLvl = ...
// fvgDisplayData.ltfBullBgColor = ...
// ... etc.
if not na(infoPanel)
panel.updatePanel(
infoPanel,
fvgDisplayData,
"๐ฅ", // LV FVG Icon
color.white,
color.new(color.gray, 70), // NA Cell Color
size.small
)
This workflow ensures that the panel is drawn only once and its cells are efficiently updated as new data becomes available.
โโNOTES
Data Source: This library is solely responsible for the visual presentation of FVG data in a table. It does not perform any FVG detection or calculation. The calling script must compute or retrieve the FVG levels, LV status, and desired styling to populate the `panelData` object.
Styling Responsibility: While `updatePanel` applies colors passed via the `panelData` object, the logic for *determining* those colors (e.g., highlighting the closest FVG to the current price) resides in the calling script.
Performance: The library uses `table.cell()` to update individual cells, which is generally more efficient than deleting and recreating the table on each update. However, the frequency of `updatePanel` calls should be managed by the main script (e.g., using `barstate.islast` or `barstate.isrealtime`) to avoid excessive processing on historical bars.
`series float` Handling: The price level fields within the `panelData` UDT (e.g., `nearestBullMitLvl`) can accept `series float` values, as these are typically derived from price data. The internal `formatPrice` function correctly handles `series float` for display.
Dependencies: The `FvgPanel` itself is self-contained and does not import other user libraries. It uses standard Pine Scriptโข table and string functionalities.
โโEXPORTED TYPES
panelData
โโRepresents the data structure for populating the FVG information panel.
โโFields:
โโโโ nearestBullMitLvl (series float) : The price level of the nearest bullish FVG's mitigation point (bottom for bull) on the LTF.
โโโโ isNearestBullLV (series bool) : True if the nearest bullish FVG on the LTF is a Large Volume FVG.
โโโโ ltfBullBgColor (series color) : Background color for the LTF bullish FVG cell in the panel.
โโโโ ltfBullTextColor (series color) : Text color for the LTF bullish FVG cell in the panel.
โโโโ nearestBearMitLvl (series float) : The price level of the nearest bearish FVG's mitigation point (top for bear) on the LTF.
โโโโ isNearestBearLV (series bool) : True if the nearest bearish FVG on the LTF is a Large Volume FVG.
โโโโ ltfBearBgColor (series color) : Background color for the LTF bearish FVG cell in the panel.
โโโโ ltfBearTextColor (series color) : Text color for the LTF bearish FVG cell in the panel.
โโโโ nearestMtfBullMitLvl (series float) : The price level of the nearest bullish FVG's mitigation point on the MTF.
โโโโ isNearestMtfBullLV (series bool) : True if the nearest bullish FVG on the MTF is a Large Volume FVG.
โโโโ mtfBullBgColor (series color) : Background color for the MTF bullish FVG cell.
โโโโ mtfBullTextColor (series color) : Text color for the MTF bullish FVG cell.
โโโโ nearestMtfBearMitLvl (series float) : The price level of the nearest bearish FVG's mitigation point on the MTF.
โโโโ isNearestMtfBearLV (series bool) : True if the nearest bearish FVG on the MTF is a Large Volume FVG.
โโโโ mtfBearBgColor (series color) : Background color for the MTF bearish FVG cell.
โโโโ mtfBearTextColor (series color) : Text color for the MTF bearish FVG cell.
โโโโ nearestHtfBullMitLvl (series float) : The price level of the nearest bullish FVG's mitigation point on the HTF.
โโโโ isNearestHtfBullLV (series bool) : True if the nearest bullish FVG on the HTF is a Large Volume FVG.
โโโโ htfBullBgColor (series color) : Background color for the HTF bullish FVG cell.
โโโโ htfBullTextColor (series color) : Text color for the HTF bullish FVG cell.
โโโโ nearestHtfBearMitLvl (series float) : The price level of the nearest bearish FVG's mitigation point on the HTF.
โโโโ isNearestHtfBearLV (series bool) : True if the nearest bearish FVG on the HTF is a Large Volume FVG.
โโโโ htfBearBgColor (series color) : Background color for the HTF bearish FVG cell.
โโโโ htfBearTextColor (series color) : Text color for the HTF bearish FVG cell.
โโEXPORTED FUNCTIONS
createPanel(position, borderColor, borderWidth, headerBgColor, headerTextColor, headerTextSize)
โโCreates and initializes the FVG information panel (table). Sets up the header rows and timeframe labels.
โโParameters:
โโโโ position (simple string) : The position of the panel on the chart (e.g., position.top_right). Uses position.* constants.
โโโโ borderColor (simple color) : The color of the panel's border.
โโโโ borderWidth (simple int) : The width of the panel's border.
โโโโ headerBgColor (simple color) : The background color for the header cells.
โโโโ headerTextColor (simple color) : The text color for the header cells.
โโโโ headerTextSize (simple string) : The text size for the header cells (e.g., size.small). Uses size.* constants.
โโReturns: The newly created table object representing the panel.
updatePanel(panelTable, data, lvIcon, defaultTextColor, naCellColor, textSize)
โโUpdates the content of the FVG information panel with the latest FVG data.
โโParameters:
โโโโ panelTable (table) : The table object representing the panel to be updated.
โโโโ data (panelData) : An object containing the FVG data to display.
โโโโ lvIcon (simple string) : The icon (e.g., emoji) to display next to Large Volume FVGs.
โโโโ defaultTextColor (simple color) : The default text color for FVG levels if not highlighted.
โโโโ naCellColor (simple color) : The background color for cells where no FVG data is available ("---").
โโโโ textSize (simple string) : The text size for the FVG level data (e.g., size.small).
โโReturns: _void
