FvgTypesโโOVERVIEW
This library serves as a foundational module for Pine Scriptโข projects focused on Fair Value Gaps (FVGs). Its primary purpose is to define and centralize custom data structures (User-Defined Types - UDTs) and enumerations that are utilized across various components of an FVG analysis system. By providing standardized types for FVG characteristics and drawing configurations, it promotes code consistency, readability, and easier maintenance within a larger FVG indicator or strategy.
โโCONCEPTS
The library introduces several key data structures (User-Defined Types - UDTs) and an enumeration to organize Fair Value Gap (FVG) related data logically. These types are central to the functioning of FVG analysis tools built upon this library.
Timeframe Categorization (`tfType` Enum)
To manage and differentiate FVGs based on their timeframe of origin, the `tfType` enumeration is defined. It includes:
`LTF`: Low Timeframe (typically the current chart).
`MTF`: Medium Timeframe.
`HTF`: High Timeframe.
This allows for distinct logic and visual settings to be applied depending on the FVG's source timeframe.
FVG Data Encapsulation (`fvgObject` UDT)
The `fvgObject` is a comprehensive UDT designed to encapsulate all pertinent information and state for an individual Fair Value Gap throughout its lifecycle. Instead of listing every field, its conceptual structure can be understood as holding:
Core Definition: The FVG's fundamental price levels (top, bottom) and its formation time (`startTime`).
Classification Attributes: Characteristics such as its direction (`isBullish`) and whether it qualifies as a Large Volume FVG (`isLV`), along with its originating timeframe category (`tfType`).
Lifecycle State: Current status indicators including full mitigation (`isMitigated`, `mitigationTime`), partial fill levels (`currentTop`, `currentBottom`), midline interaction (`isMidlineTouched`), and overall visibility (`isVisible`).
Drawing Identifiers: References (`boxId`, `midLineId`, `mitLineLabelId`, etc.) to the actual graphical objects drawn on the chart to represent the FVG and its components.
Optimization Cache: Previous-bar state values (`prevIsMitigated`, `prevCurrentTop`, etc.) crucial for optimizing drawing updates by avoiding redundant operations.
This comprehensive structure facilitates easy access to all FVG-related information through a single object, reducing code complexity and improving manageability.
Drawing Configuration (`drawSettings` UDT)
The `drawSettings` UDT centralizes all user-configurable parameters that dictate the visual appearance of FVGs across different timeframes. It's typically populated from script inputs and conceptually groups settings for:
General Behavior: Global FVG classification toggles (e.g., `shouldClassifyLV`) and general display rules (e.g., `shouldHideMitigated`).
FVG Type Specific Colors: Colors for standard and Large Volume FVGs, both active and mitigated (e.g., `lvBullColor`, `mitigatedBearBoxColor`).
Timeframe-Specific Visuals (LTF, MTF, HTF): Detailed parameters for each timeframe category, covering FVG boxes (visibility, colors, extension, borders, labels), midlines (visibility, style, color), and mitigation lines (visibility, style, color, labels, persistence after mitigation).
Contextual Information: The current bar's time (`currentTime`) for accurate positioning of time-dependent drawing elements and timeframe display strings (`tfString`, `mtfTfString`, `htfTfString`).
This centralized approach allows for extensive customization of FVG visuals and simplifies the management of drawing parameters within the main script. Such centralization also enhances the maintainability of the visual aspects of the FVG system.
โโNOTES
User-Defined Types (UDTs): This library extensively uses UDTs (`fvgObject`, `drawSettings`) to group related data. This improves code organization and makes it easier to pass complex data between functions and libraries.
Mutability and Reference Behavior of UDTs: When UDT instances are passed to functions or methods in other libraries (like `fvgObjectLib`), those functions might modify the fields of the passed object if they are not explicitly designed to return new instances. This is because UDTs are passed by reference and are mutable in Pine Scriptโข. Users should be aware of this standard behavior to prevent unintended side effects.
Optimization Fields: The `prev_*` fields in `fvgObject` are crucial for performance optimization in the drawing logic. They help avoid unnecessary redrawing of FVG elements if their state or relevant settings haven't changed.
No Direct Drawing Logic: `FvgTypes` itself does not contain any drawing logic. It solely defines the data structures. The actual drawing and manipulation of these objects are handled by other libraries (e.g., `fvgObjectLib`).
Centralized Definitions: By defining these types in a separate library, any changes to the structure of FVG data or settings can be made in one place, ensuring consistency across all dependent scripts and libraries.
โโEXPORTED TYPES
fvgObject
โโfvgObject Represents a Fair Value Gap (FVG) object.
Fields:
โโโโ top (series float) : The top price level of the FVG.
โโโโ bottom (series float) : The bottom price level of the FVG.
โโโโ startTime (series int) : The start time (timestamp) of the bar where the FVG formed.
โโโโ isBullish (series bool) : Indicates if the FVG is bullish (true) or bearish (false).
โโโโ isLV (series bool) : Indicates if the FVG is a Large Volume FVG.
โโโโ tfType (series tfType) : The timeframe type (LTF, MTF, HTF) to which this FVG belongs.
โโโโ isMitigated (series bool) : Indicates if the FVG has been fully mitigated.
โโโโ mitigationTime (series int) : The time (timestamp) when the FVG was mitigated.
โโโโ isVisible (series bool) : The current visibility status of the FVG, typically managed by drawing logic based on filters.
โโโโ isMidlineTouched (series bool) : Indicates if the price has touched the FVG's midline (50% level).
โโโโ currentTop (series float) : The current top level of the FVG after partial fills.
โโโโ currentBottom (series float) : The current bottom level of the FVG after partial fills.
โโโโ boxId (series box) : The drawing ID for the main FVG box.
โโโโ mitigatedBoxId (series box) : The drawing ID for the box representing the partially filled (mitigated) area.
โโโโ midLineId (series line) : The drawing ID for the FVG's midline.
โโโโ mitLineId (series line) : The drawing ID for the FVG's mitigation line.
โโโโ boxLabelId (series label) : The drawing ID for the FVG box label.
โโโโ mitLineLabelId (series label) : The drawing ID for the mitigation line label.
โโโโ testedBoxId (series box) : The drawing ID for the box of a fully mitigated (tested) FVG, if kept visible.
โโโโ keptMitLineId (series line) : The drawing ID for a mitigation line that is kept after full mitigation.
โโโโ prevIsMitigated (series bool) : Stores the isMitigated state from the previous bar for optimization.
โโโโ prevCurrentTop (series float) : Stores the currentTop value from the previous bar for optimization.
โโโโ prevCurrentBottom (series float) : Stores the currentBottom value from the previous bar for optimization.
โโโโ prevIsVisible (series bool) : Stores the visibility status from the previous bar for optimization (derived from isVisibleNow passed to updateDrawings).
โโโโ prevIsMidlineTouched (series bool) : Stores the isMidlineTouched status from the previous bar for optimization.
drawSettings
โโdrawSettings A structure containing settings for drawing FVGs.
Fields:
โโโโ shouldClassifyLV (series bool) : Whether to classify FVGs as Large Volume (LV) based on ATR.
โโโโ shouldHideMitigated (series bool) : Whether to hide FVG boxes once they are fully mitigated.
โโโโ currentTime (series int) : The current bar's time, used for extending drawings.
โโโโ lvBullColor (series color) : Color for Large Volume Bullish FVGs.
โโโโ mitigatedLvBullColor (series color) : Color for mitigated Large Volume Bullish FVGs.
โโโโ lvBearColor (series color) : Color for Large Volume Bearish FVGs.
โโโโ mitigatedLvBearColor (series color) : Color for mitigated Large Volume Bearish FVGs.
โโโโ shouldShowBoxes (series bool) : Whether to show FVG boxes for the LTF.
โโโโ bullBoxColor (series color) : Color for LTF Bullish FVG boxes.
โโโโ mitigatedBullBoxColor (series color) : Color for mitigated LTF Bullish FVG boxes.
โโโโ bearBoxColor (series color) : Color for LTF Bearish FVG boxes.
โโโโ mitigatedBearBoxColor (series color) : Color for mitigated LTF Bearish FVG boxes.
โโโโ boxLengthBars (series int) : Length of LTF FVG boxes in bars (if not extended).
โโโโ shouldExtendBoxes (series bool) : Whether to extend LTF FVG boxes to the right.
โโโโ shouldShowCurrentTfBoxLabels (series bool) : Whether to show labels on LTF FVG boxes.
โโโโ shouldShowBoxBorder (series bool) : Whether to show a border for LTF FVG boxes.
โโโโ boxBorderWidth (series int) : Border width for LTF FVG boxes.
โโโโ boxBorderStyle (series string) : Border style for LTF FVG boxes (e.g., line.style_solid).
โโโโ boxBorderColor (series color) : Border color for LTF FVG boxes.
โโโโ shouldShowMidpoint (series bool) : Whether to show the midline (50% level) for LTF FVGs.
โโโโ midLineWidthInput (series int) : Width of the LTF FVG midline.
โโโโ midpointLineStyleInput (series string) : Style of the LTF FVG midline.
โโโโ midpointColorInput (series color) : Color of the LTF FVG midline.
โโโโ shouldShowMitigationLine (series bool) : Whether to show the mitigation line for LTF FVGs.
(Line always extends if shown)
โโโโ mitLineWidthInput (series int) : Width of the LTF FVG mitigation line.
โโโโ mitigationLineStyleInput (series string) : Style of the LTF FVG mitigation line.
โโโโ mitigationLineColorInput (series color) : Color of the LTF FVG mitigation line.
โโโโ shouldShowCurrentTfMitLineLabels (series bool) : Whether to show labels on LTF FVG mitigation lines.
โโโโ currentTfMitLineLabelOffsetX (series float) : The horizontal offset value for the LTF mitigation line's label.
โโโโ shouldKeepMitigatedLines (series bool) : Whether to keep showing mitigation lines of fully mitigated LTF FVGs.
โโโโ mitigatedMitLineColor (series color) : Color for kept mitigation lines of mitigated LTF FVGs.
โโโโ tfString (series string) : Display string for the LTF (e.g., "Current TF").
โโโโ shouldShowMtfBoxes (series bool) : Whether to show FVG boxes for the MTF.
โโโโ mtfBullBoxColor (series color) : Color for MTF Bullish FVG boxes.
โโโโ mtfMitigatedBullBoxColor (series color) : Color for mitigated MTF Bullish FVG boxes.
โโโโ mtfBearBoxColor (series color) : Color for MTF Bearish FVG boxes.
โโโโ mtfMitigatedBearBoxColor (series color) : Color for mitigated MTF Bearish FVG boxes.
โโโโ mtfBoxLengthBars (series int) : Length of MTF FVG boxes in bars (if not extended).
โโโโ shouldExtendMtfBoxes (series bool) : Whether to extend MTF FVG boxes to the right.
โโโโ shouldShowMtfBoxLabels (series bool) : Whether to show labels on MTF FVG boxes.
โโโโ shouldShowMtfBoxBorder (series bool) : Whether to show a border for MTF FVG boxes.
โโโโ mtfBoxBorderWidth (series int) : Border width for MTF FVG boxes.
โโโโ mtfBoxBorderStyle (series string) : Border style for MTF FVG boxes.
โโโโ mtfBoxBorderColor (series color) : Border color for MTF FVG boxes.
โโโโ shouldShowMtfMidpoint (series bool) : Whether to show the midline for MTF FVGs.
โโโโ mtfMidLineWidthInput (series int) : Width of the MTF FVG midline.
โโโโ mtfMidpointLineStyleInput (series string) : Style of the MTF FVG midline.
โโโโ mtfMidpointColorInput (series color) : Color of the MTF FVG midline.
โโโโ shouldShowMtfMitigationLine (series bool) : Whether to show the mitigation line for MTF FVGs.
(Line always extends if shown)
โโโโ mtfMitLineWidthInput (series int) : Width of the MTF FVG mitigation line.
โโโโ mtfMitigationLineStyleInput (series string) : Style of the MTF FVG mitigation line.
โโโโ mtfMitigationLineColorInput (series color) : Color of the MTF FVG mitigation line.
โโโโ shouldShowMtfMitLineLabels (series bool) : Whether to show labels on MTF FVG mitigation lines.
โโโโ mtfMitLineLabelOffsetX (series float) : The horizontal offset value for the MTF mitigation line's label.
โโโโ shouldKeepMtfMitigatedLines (series bool) : Whether to keep showing mitigation lines of fully mitigated MTF FVGs.
โโโโ mtfMitigatedMitLineColor (series color) : Color for kept mitigation lines of mitigated MTF FVGs.
โโโโ mtfTfString (series string) : Display string for the MTF (e.g., "MTF").
โโโโ shouldShowHtfBoxes (series bool) : Whether to show FVG boxes for the HTF.
โโโโ htfBullBoxColor (series color) : Color for HTF Bullish FVG boxes.
โโโโ htfMitigatedBullBoxColor (series color) : Color for mitigated HTF Bullish FVG boxes.
โโโโ htfBearBoxColor (series color) : Color for HTF Bearish FVG boxes.
โโโโ htfMitigatedBearBoxColor (series color) : Color for mitigated HTF Bearish FVG boxes.
โโโโ htfBoxLengthBars (series int) : Length of HTF FVG boxes in bars (if not extended).
โโโโ shouldExtendHtfBoxes (series bool) : Whether to extend HTF FVG boxes to the right.
โโโโ shouldShowHtfBoxLabels (series bool) : Whether to show labels on HTF FVG boxes.
โโโโ shouldShowHtfBoxBorder (series bool) : Whether to show a border for HTF FVG boxes.
โโโโ htfBoxBorderWidth (series int) : Border width for HTF FVG boxes.
โโโโ htfBoxBorderStyle (series string) : Border style for HTF FVG boxes.
โโโโ htfBoxBorderColor (series color) : Border color for HTF FVG boxes.
โโโโ shouldShowHtfMidpoint (series bool) : Whether to show the midline for HTF FVGs.
โโโโ htfMidLineWidthInput (series int) : Width of the HTF FVG midline.
โโโโ htfMidpointLineStyleInput (series string) : Style of the HTF FVG midline.
โโโโ htfMidpointColorInput (series color) : Color of the HTF FVG midline.
โโโโ shouldShowHtfMitigationLine (series bool) : Whether to show the mitigation line for HTF FVGs.
(Line always extends if shown)
โโโโ htfMitLineWidthInput (series int) : Width of the HTF FVG mitigation line.
โโโโ htfMitigationLineStyleInput (series string) : Style of the HTF FVG mitigation line.
โโโโ htfMitigationLineColorInput (series color) : Color of the HTF FVG mitigation line.
โโโโ shouldShowHtfMitLineLabels (series bool) : Whether to show labels on HTF FVG mitigation lines.
โโโโ htfMitLineLabelOffsetX (series float) : The horizontal offset value for the HTF mitigation line's label.
โโโโ shouldKeepHtfMitigatedLines (series bool) : Whether to keep showing mitigation lines of fully mitigated HTF FVGs.
โโโโ htfMitigatedMitLineColor (series color) : Color for kept mitigation lines of mitigated HTF FVGs.
โโโโ htfTfString (series string) : Display string for the HTF (e.g., "HTF").
Settings
Tunable SWMADissected the standard SWMA function and added options for user to change just about every part of it. Weights ,Lookback ,Source can all be changed in the settings.
Green is the standard SWMA, Using the Input value selected.(MAs/LRC/VWAP)
Red is the tuned SWMA, with the option of applying a final Output filter (MAs/LRC/VWAP). Uses 8 datapoints instead of 4 for the default.
Customization can really help expand upon the standard SWMA I find. Enjoy tuning to your hearts content

