OPEN-SOURCE SCRIPT

Gap Trend Lines by @eyemaginative

Actualizado
Summary:
The "Gap Trend Lines" script is designed to identify and visualize gaps between the close of one candle and the opening of the next on a TradingView chart. It draws extended trend lines to visually connect these gaps, helping traders to identify significant price movements between consecutive candles.

Functionality:
Indicator Setup:
The script is set as an overlay indicator on the main chart.
It includes settings for maximum line and label counts, ensuring efficient performance.
Parameter Customization:
Gap Threshold: Defines the minimum gap size considered significant.
Line Colors: Allows customization of colors for small and large gaps.
Line Thickness and Style: Provides options to adjust the thickness and style (solid, dotted, dashed) of the trend lines.

Drawing Extended Trend Lines:

For each bar (candlestick) on the chart, the script checks if there is a gap between the previous candle's close and the current candle's open.
If a gap is detected (i.e., close[1] != open), it determines the size of the gap.
Depending on the size relative to the defined threshold, it selects the appropriate color (small or large gap).
It then draws an extended trend line that starts from the close of the previous candle (bar_index[1], close[1]) and extends to the open of the current candle (bar_index, open).
The trend line is drawn with the specified thickness, color, and style.

Dynamic Line Attribute Changes:

The script includes a function (changeLineAttributes()) that periodically changes the color and style of the trend lines.
By default, it changes the color every 4 hours (adjustable), alternating between green and the original color.

Enhanced Functionality:

Handles both small and large gaps with different visual cues (colors).
Supports extended trend lines that span both past and future directions (extend=extend.both), ensuring visibility across the entire chart.

Usage:

Traders can use the "Gap Trend Lines" script to:
Identify and analyze gaps between candlesticks.
Visualize significant price movements or breaks in continuity.
Customize the appearance of trend lines for better clarity and analysis.

By utilizing this script, traders can gain insights into price gap dynamics directly on TradingView charts, aiding in decision-making and strategy development.
Notas de prensa
Update: This script now includes input parameters for customizing the color and style of lines that change every 4 hours. The default values for these parameters are green for color and dotted for style, but you can customize them as needed.

Summary:
The "Gap Trend Lines" script is designed to identify and visualize gaps between the close of one candle and the opening of the next on a TradingView chart. It draws extended trend lines to visually connect these gaps, helping traders to identify significant price movements between consecutive candles.

Functionality:
Indicator Setup:
The script is set as an overlay indicator on the main chart.
It includes settings for maximum line and label counts, ensuring efficient performance.
Parameter Customization:
Gap Threshold: Defines the minimum gap size considered significant.
Line Colors: Allows customization of colors for small and large gaps.
Line Thickness and Style: Provides options to adjust the thickness and style (solid, dotted, dashed) of the trend lines.

Drawing Extended Trend Lines:

For each bar (candlestick) on the chart, the script checks if there is a gap between the previous candle's close and the current candle's open.
If a gap is detected (i.e., close != open), it determines the size of the gap.
Depending on the size relative to the defined threshold, it selects the appropriate color (small or large gap).
It then draws an extended trend line that starts from the close of the previous candle (bar_index, close) and extends to the open of the current candle (bar_index, open).
The trend line is drawn with the specified thickness, color, and style.

Dynamic Line Attribute Changes:

The script includes a function (changeLineAttributes()) that periodically changes the color and style of the trend lines.
By default, it changes the color every 4 hours (adjustable), alternating between green and the original color.

Enhanced Functionality:

Handles both small and large gaps with different visual cues (colors).
Supports extended trend lines that span both past and future directions (extend=extend.both), ensuring visibility across the entire chart.

Usage:

Traders can use the "Gap Trend Lines" script to:
Identify and analyze gaps between candlesticks.
Visualize significant price movements or breaks in continuity.
Customize the appearance of trend lines for better clarity and analysis.

By utilizing this script, traders can gain insights into price gap dynamics directly on TradingView charts, aiding in decision-making and strategy development.
Notas de prensa
Updated Indicator Image, No Script Update
Notas de prensa
Changed Sample Image, No Script Change
Notas de prensa
NOTE: There still exists an issue when refreshing browser where the script will need removed and readded due to a compile error. It compiles initially and will function until a refresh. I will resolve this in a future update.

Changes Made in this Release:
Added Independent Customization:

Added separate customization for small and large gaps, including line thickness and style.
Parameters for old gap customization included color, thickness, and style.
Dynamic Deletion of Lines:

Ensured delete_all_lines() function only deletes lines when necessary to avoid compilation issues upon refresh.
Error Handling:

Checked if trendLines array is empty before performing operations on it, preventing out-of-bounds errors.
Refined Periodic Change Function:

Added more granular control over the appearance of lines older than 4 hours, allowing customization from the input parameters.

This version ensures the script dynamically updates on changes and prevents issues with array bounds, providing robust functionality for identifying and visualizing price gaps.

Key Features:
Gap Detection: Identifies gaps where the close price of one candle is different from the open price of the next.

Line Drawing: Draws extended trend lines between these gaps, with customizable colors, thicknesses, and styles based on the gap size.

Periodic Change: Changes the color and style of lines that are older than 4 hours, with customizable parameters.

Dynamic Updates: Ensures the script updates dynamically when the gap threshold changes.

Customization Parameters:
Gap Threshold: Determines the size of gaps to be considered.

Small Gaps: Customizable color, thickness, and style for lines representing small gaps.
Large Gaps: Customizable color, thickness, and style for lines representing large gaps.
Old Gaps: Customizable color, thickness, and style for lines older than 4 hours.

Functions:
get_line_style(): Converts string input to the corresponding line style.
delete_all_lines(): Deletes all existing lines to prevent accumulation on parameter changes.
changeLineAttributes(): Changes the appearance of lines older than 4 hours based on specified intervals.
Notas de prensa
Preventing Large Gap Lines from Becoming Old Gap Lines:

Initially, the script was treating all gap lines that persisted beyond a certain age as old gap lines. This included both small and large gap lines.

To differentiate between large and old gap lines, we modified the condition in the changeLineAttributes function. Now, only lines categorized as small gaps are allowed to age into old gap lines. Large gap lines retain their original attributes without turning into old gap lines.

Adding Hidden Line Options in Input Settings:

Three checkboxes were added to the input settings: Hide Small Gaps, Hide Large Gaps, and Hide Old Gaps.

Each checkbox allows the user to toggle the visibility of specific types of gap trend lines independently.

The update_visibility function was updated to read these settings and adjust the visibility of lines accordingly. Lines are hidden based on their type (small, large, or old) and whether the respective checkbox is checked.

These changes enable more flexible control over which types of gap trend lines are displayed on the chart, improving usability and customization options for the indicator.
Notas de prensa
Summary of Changes to the Gap Trend Lines Script
1. Added Below Threshold Gap Lines:

Implemented logic to identify and plot gap trend lines that are below the defined gap threshold.

Added input parameters for customization of these below threshold gap lines, including color, line thickness, and line style.

2. Renamed Gap Lines:

Renamed the line types for better clarity:
Large gap lines are now referred to as Above Threshold lines.
Small gap lines are now referred to as Threshold lines.
New lines are introduced for Below Threshold gaps.

3. Customization Parameters for All Line Types:

Added input parameters for each type of gap line:

Above Threshold Gaps:
line_color_above_threshold
line_thickness_above_threshold
line_style_above_threshold

Threshold Gaps:
line_color_threshold
line_thickness_threshold
line_style_threshold
Below Threshold Gaps:

line_color_below_threshold
line_thickness_below_threshold
line_style_below_threshold
Old Gaps:

line_color_old_gap
line_thickness_old_gap
line_style_old_gap

4. Input Parameters for Hiding Lines:

Added options to hide specific types of gap lines:
hide_threshold_gaps
hide_above_threshold_gaps
hide_below_threshold_gaps
hide_old_gaps

5. Debugging Labels:

Implemented debugging labels to show gap sizes and threshold values on the chart.
Added an input parameter hide_debug_labels to toggle the visibility of these debugging labels.

6. Note on 30-Minute Timescale Loading Issue:

There is an outstanding issue where the script fails to load on the 30-minute timescale due to execution timeouts. This issue needs to be addressed, but the focus should be on other functionalities for now.
Notas de prensa
Summary of Changes
Restored Original Functionality:
Reverted to the last known working version that correctly identified and drew gap trends.

Customization Parameters:
Added input parameters for customizing the appearance (color, thickness, style) of different types of gap lines, including threshold gaps, above threshold gaps, below threshold gaps, and old gaps.

Debug Labels:
Re-added debugging labels to assist in identifying gaps and their sizes. These can be toggled on or off with a setting.

Line Management:
Managed line arrays to keep track of the gaps, their sizes, ages, and original colors.

Line Attribute Update:
Added a function to periodically update the attributes of old gaps based on their ages and gap sizes.

Visibility Update Function:
Implemented a function to update the visibility of lines based on the input settings, allowing users to hide or show threshold gaps, above threshold gaps, below threshold gaps, and old gaps.

Redraw on Parameter Change:
Ensured that all lines are redrawn when parameters are changed to maintain the correct display of gap trends.
Notas de prensa
Added New Settings for Gap Size Labels:
Introduced settings to show or hide labels for threshold gap sizes (show_threshold_gap_sizes) and non-threshold gap sizes (show_non_threshold_gap_sizes).

These labels display the size of the gaps on the chart.

Added Debug Labels:
Introduced a new setting (show_debug_labels) to show or hide debug labels.

Debug labels provide detailed information about each gap, including bar index, close[1] price, open price, gap size, and the type of gap (Above Threshold, Below Threshold, or Threshold).

Enhanced Label Management:
Ensured that labels for gap sizes are distinct and can be controlled separately through the new settings.

Debug labels provide more detailed information useful for troubleshooting and understanding the script's behavior.
Refactored Line and Label Creation:

Ensured that gap size labels are created and managed separately from debug labels.

Ensured that debug labels display detailed information about each gap.

Retained Existing Functionality:
Preserved existing functionalities for drawing and managing trend lines.

Preserved existing functionalities for periodically changing attributes of old gaps and updating visibility based on input settings.

New Input Settings:
Show Threshold Gap Sizes: Toggle to show or hide labels for gaps that match the threshold size.

Show Non-Threshold Gap Sizes: Toggle to show or hide labels for gaps that do not match the threshold size.

Show Debug Labels: Toggle to show or hide debug labels that display detailed information about each gap.

Debug labels now display:
Bar Index
Close[1] Price
Open Price
Gap Size
Type of Gap (Above Threshold, Below Threshold, or Threshold)
Notas de prensa
Summary of Changes
Gap Threshold and Customization:

Added input parameters for defining gap threshold and customizing the appearance of lines for gaps that meet or exceed this threshold. This includes colors, line thickness, and line styles.

Line Management:

Implemented functions to handle the creation, updating, and deletion of lines based on detected gaps.

Added functionality to periodically change the appearance of lines representing older gaps.

Gap Size Labels:

Added options to show or hide labels for gap sizes.
Included a feature to show detailed debug labels for each gap, displaying bar information and gap details.

Visibility Settings:

Introduced input parameters to hide lines based on their type (threshold, above threshold, below threshold, old gaps).

Gap Filtering:

Added input parameters to filter gaps based on minimum gap size and the number of decimal places.

Enabled or disabled gap filtering based on user preference.

Most Common Gaps Calculation:

Implemented a function to calculate the most common gap sizes and their counts.
Added a feature to display a label showing the top three most common gap sizes along with their counts.

Dynamic Label Update:

Added logic to dynamically update the label showing the most common gap sizes at the last bar.
Included an option to hide this label if desired.

Usage Instructions
Gap Threshold and Customization:

Gap Threshold: Set the minimum gap size to be considered significant.

Line Colors and Styles: Customize the appearance of lines for different gap types (threshold, above threshold, below threshold, old gaps) using color, thickness, and style options.

Visibility Settings:

Hide Threshold Gaps: Enable this option to hide lines for gaps that meet the threshold.
Hide Above/Below Threshold Gaps: Enable these options to hide lines for gaps above or below the threshold.

Hide Old Gaps: Enable this option to hide lines representing older gaps.

Gap Size Labels:

Show Threshold Gap Sizes: Enable this option to display labels for gaps that meet the threshold.
Show Non-Threshold Gap Sizes: Enable this option to display labels for gaps that do not meet the threshold.

Show Debug Labels: Enable this option to display detailed debug information for each gap.

Gap Filtering:

Minimum Gap Size: Set the minimum gap size to be considered in the calculation.
Number of Decimal Places: Define the maximum number of decimal places for gap sizes to be considered.

Enable Gap Filtering: Enable or disable gap filtering based on the above criteria.

Most Common Gaps Label:

Show Most Common Gaps Label: Enable this option to display a label showing the top three most common gap sizes and their counts at the last bar.

By configuring these input settings, users can customize the behavior of the script to suit their specific needs, making it easier to analyze and visualize significant gaps in their data.
Notas de prensa
Added Input Parameters for Ascending and Descending Gap Colors:

line_color_ascending_gap: Color for ascending gaps.
line_color_descending_gap: Color for descending gaps.
Modified Gap Detection Logic:

Included a condition to check if a gap is ascending or descending based on the previous close and current open prices.

Set the line color for ascending gaps to line_color_ascending_gap.
Set the line color for descending gaps to line_color_descending_gap.
Updated Line Drawing Logic:

Updated the logic to set line color based on whether the gap is ascending or descending.
Kept the existing logic to set line thickness and style based on gap size.
Maintained Other Functionalities:

Kept the logic for line thickness, style, and visibility settings based on gap size.
Preserved the functionality to display gap size labels, manage old gaps, and periodically update line attributes.

Retained the feature to calculate and display the most common gaps.

These changes ensure that the script can differentiate between ascending and descending gaps and customize their colors accordingly while maintaining the overall functionality of gap detection, line drawing, and visualization.
Notas de prensa
Image Update
Notas de prensa
Conditional Logic Update for Threshold Gaps:

Updated the script so that the color of threshold gaps is determined based on whether they are ascending or descending.

Separate Logic for Non-Threshold Gaps:

Ensured that gaps outside of the threshold have their colors set independently of whether they are ascending or descending.

Threshold Gaps Color Determination:

If gap size equals the threshold: Set the color based on whether the gap is ascending (line_color_threshold_ascending) or descending (line_color_threshold_descending).

Non-Threshold Gaps Color Determination:

If gap size does not equal the threshold: Set the color based on whether the gap is above or below the threshold without considering if it is ascending or descending.

Notas de prensa
Alert Notifications Added
Alert Notification Setup:

Alert Conditions: Added alertcondition statements to the global scope to set up alerts for new ascending and descending gap trends.

New Ascending Gap Alert: Triggers when a new ascending gap is detected.

New Descending Gap Alert: Triggers when a new descending gap is detected.

Flag Variables for Alert Triggers:

Introduced boolean flag variables (new_ascending_gap and new_descending_gap) to control when the alerts are triggered. These flags are set based on whether the gap is ascending or descending, ensuring the correct alert is triggered when a new gap line is drawn.

Correct Placement of alertcondition:

Global Scope: Moved the alertcondition declarations to the global scope, which is a requirement in Pine Script. This ensures that alerts can be properly configured and triggered based on the script’s logic.

Triggering Alerts: The actual triggering of alerts is managed within the main logic of the script using the boolean flags, ensuring that the alerts correspond to the detection of new gaps.

Function Definitions:

delete_all_lines() Function: Added this function to delete all existing lines when needed, ensuring that the script starts fresh under certain conditions (e.g., when parameters change or on the first bar). This helps in maintaining a clean state and preventing issues with line management.

General Script Enhancements:

Line Management: The script maintains the existing functionality of drawing, managing, and updating trend lines based on gap detection, with enhancements to support alerting without disrupting the original logic.

Debug Labels and Line Customization: Retained all original features like debug labels, customizable line styles, colors, and visibility options, ensuring the script remains fully customizable.

Key Benefits:
Real-Time Notifications: The script now provides real-time notifications for new trends, allowing users to react promptly to significant market movements.

Enhanced Usability: By integrating alert functionality without altering the core logic of the script, it enhances usability while preserving the original features.

Streamlined Code: The script has been refined to handle alerts efficiently, keeping the code clean and focused on its primary purpose.
Notas de prensa
Separate Alert for Non-Threshold Gaps:

Added a new alertcondition called "New Non-Threshold Gap".

This alert triggers when a gap is detected that does not equal the defined threshold value.

Trigger Condition:

The script now includes a boolean flag new_non_threshold_gap which is set to true whenever a gap is detected that does not match the threshold value. This flag is used to determine when the new non-threshold gap alert should be triggered.

Comprehensive Alert System:

The script now supports three distinct alerts:
New Ascending Gap
New Descending Gap
New Non-Threshold Gap
Notas de prensa
Old Gaps Separation: The script now separates old gaps into ascending or descending categories. The lines are customized separately for ascending and descending gaps using the new input settings.

Customization Inputs: Added input settings for customizing the color, thickness, and style of lines for old ascending and descending gaps.
Notas de prensa
Ascending and Descending Flip Detection:

Ascending Flip: Identified when the price is close to the moving averages and there is a positive change in price (compression followed by an upward move). This is visualized with an upward-facing green arrow.

Descending Flip: Identified when the price is close to the moving averages and there is a negative change in price (compression followed by a downward move). This is visualized with a downward-facing red arrow.

How It Works:

Green Arrows: Appear below the bars when an ascending manipulation flip is detected, indicating a potential upward move following a period of compression.

Red Arrows: Appear above the bars when a descending manipulation flip is detected, indicating a potential downward move following a period of compression.
Notas de prensa
Dynamically adjusts thresholds based on the timeframe and identifies and marks both ascending and descending manip flips on the chart.
Notas de prensa
Reverted back to original gap trend lines script without manip flip detector. Still bugs to work out before readding it.
Notas de prensa
Added the manip flip detector back as an indicator, there is also an ability to hide the indicator in input settings.

Manual Thresholds:

Flip Threshold: This is a manually set level that determines how close the current price must be to the moving averages (e.g., 50, 100, 200 periods) to suggest that a potential price reversal or "flip" may occur.

Compression Threshold: This is another manually set level that determines the magnitude of price change needed to trigger a signal. It captures moments when the price shows a sudden shift, indicating a potential flip.

Moving Averages:

The detector uses moving averages as benchmarks for identifying potential flips. When the price approaches these averages closely enough, it may indicate interaction with critical support or resistance levels.

Signal Detection:

Ascending Manip Flip: Detected when the price is close to a moving average and shows a sudden upward change, indicating a potential bullish reversal.

Descending Manip Flip: Detected when the price is close to a moving average and shows a sudden downward change, indicating a potential bearish reversal.
Notas de prensa
Show/Hide Ascending and Descending Gaps:

Added a checkbox in the settings to independently show or hide ascending and descending gaps. This allows users to selectively view only one group of gaps or both, depending on their preference.

Old Gaps Matching Ascending/Descending Colors:

Old gaps now automatically match their corresponding ascending or descending colors. This provides a clearer visual distinction for gaps based on their direction.

Manip Flip Indicators Changed:

The manip flip indicators have been updated from small arrows to tiny circles. The colors of these circles can be customized through the settings, allowing for more personalized chart visualization.
Notas de prensa
Customizable Colors: Each of the SMA-based flips (SMA1, SMA2, and SMA3) has its own ascending and descending flip color, which can be customized in the settings.

Show/Hide Options: You can choose to display or hide the flip indicators for each of the three SMAs individually.

Independent Flip Thresholds: Each SMA can have its own Flip Threshold, giving you finer control over how close the price must be to trigger a flip for each individual SMA.
Notas de prensa
1. Manip Flip Detector Improvements:

Manip Flip Detection Logic:

Improved the logic for detecting Manip Flips using the three SMAs with user-defined thresholds:
flipThreshold1 for SMA1 flips.
flipThreshold2 for SMA2 flips.
flipThreshold3 for SMA3 flips.

Added a shared compressionThreshold to detect when price compression triggers a flip.

Customizable Colors for Manip Flips:

Custom Colors for Each SMA's Ascending/Descending Flips:
Added inputs for custom colors for both ascending and descending flips for each SMA.

SMA1:
circle_color_ascending1 (default: green) for ascending flips.
circle_color_descending1 (default: red) for descending flips.
SMA2:
circle_color_ascending2 (default: blue) for ascending flips.
circle_color_descending2 (default: orange) for descending flips.
SMA3:
circle_color_ascending3 (default: purple) for ascending flips.
circle_color_descending3 (default: pink) for descending flips.

These colors are fully customizable, allowing users to define their own preferred colors for better visualization.

2. Congestion Zone Detection & Alerts:

Congestion Zone Detection:
Added an input to customize the number of consecutive gaps required to detect a congestion zone: consecutive_gaps_for_congestion (default 2).

Added an option to choose between two congestion zone detection methods:
Use the predefined gap threshold (use_gap_threshold_size).
Use the distance between consecutive gaps (congestion_zone_distance).

Congestion Zone Alerts:
Implemented logic to detect when price enters a congestion zone based on the gap detection methods described above.

Added an alert condition (alertcondition) that notifies when the price has entered a congestion zone with the message: "Price has entered a congestion zone."

Alerts can be enabled or disabled using the enable_congestion_alerts input.
Notas de prensa
Introducing Congestion Zone Detection Logic:

The script now detects congestion zones based on gaps formed between consecutive bars. Specifically, it checks whether the size of the gaps meets certain criteria, which are defined through inputs such as the gap threshold size or the distance between consecutive gap sizes.

Congestion is detected when a defined number of consecutive gaps (set by consecutive_gaps_for_congestion) meet the criteria for being close enough together, either in absolute terms or relative to a threshold.

Gap Tolerance and Threshold:

The congestion zone logic allows you to define a tolerance (gap_tolerance) for how much the gaps can deviate from a threshold size before they are considered part of a congestion zone. If the gaps stay within this tolerance range, the bars are flagged as being in a congestion zone.
The option use_gap_threshold_size determines whether the script checks the gap sizes against a specific threshold or just the distance between consecutive gaps.

Congestion Zone Types:

The script identifies three types of congestion zones:
Ascending Congestion Zone: All gaps in the zone are ascending (price increases).
Descending Congestion Zone: All gaps are descending (price decreases).
Mixed Congestion Zone: The gaps are a mix of ascending and descending.

The type of congestion zone is determined by checking whether consecutive gaps are all in one direction or a combination of both.
Displaying Congestion Zones:

The script has options to show or hide specific types of congestion zones, such as ascending or descending zones. These are controlled by show_ascending_congestion_zones and show_descending_congestion_zones.

Congestion zones are represented visually on the chart as lines. The color, thickness, and style of these lines can be customized for ascending and descending zones. For mixed zones (if handled), a default color/style can be applied.

Congestion Zone Alerts:

If the criteria for a congestion zone are met, and the user has enabled congestion alerts (enable_congestion_alerts), the script triggers an alert informing the user that the price has entered a congestion zone.

These alerts can be tied to specific types of congestion zones (ascending, descending, or mixed).

Congestion Zone Lines:

When a congestion zone is detected, lines are drawn on the chart to visually represent the zone. These lines extend over the bars where congestion occurs.

The line's color, thickness, and style are determined based on whether the congestion zone is ascending, descending, or mixed, allowing for visual differentiation between the types of congestion zones on the chart.

In essence, the congestion zone logic helps to identify areas on the chart where price movements are tightly clustered, either moving consistently up or down, or fluctuating in both directions. These zones are highlighted with customizable lines and can trigger alerts based on user-defined settings.
Notas de prensa
Changed Default Settings Parameters to make initial display more user friendly.
Chart patternsforecastingTrend Analysis

Script de código abierto

Siguiendo fielmente el espíritu de TradingView, el autor de este script lo ha publicado en código abierto, permitiendo que otros traders puedan entenderlo y verificarlo. ¡Olé por el autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la reutilización de este código en la publicación se rige por las Normas internas. Puede añadir este script a sus favoritos y usarlo en un gráfico.

¿Quiere utilizar este script en un gráfico?


También en:

Exención de responsabilidad