Implementing Gap Detection and Visualization in Trading Charts

06 Apr 2023
0 comment
0 attachment

This code snippet is designed to detect and visualize gaps in trading charts. Gaps occur when there is a noticeable difference between the closing price of one period and the opening price of the next period. This script identifies these gaps, checks if they have been filled, and draws indicators on the chart for visual reference.

// GAP Monitoring till Filled
// https://www.prorealcode.com/topic/gap-fill-indicator/
DEFPARAM DrawOnLastBarOnly = True

//ONCE GapLineSize = 4
//ONCE GapDepth = 0.35
// ONCE t155 = 135
ONCE t255 = 255
ONCE Elements = 10
ONCE UPwards = 1
ONCE DOWNwards = -1

IF BarIndex = 0 THEN
    FOR i = 0 TO Elements
        $Gap[i] = 0
        $Gap2[i] = 0
        $Direction[i] = 0
        $BarID[i] = 0
    NEXT
ENDIF

// Calculate average range
AverageHIGH = AVERAGE[20](HIGH)
AverageLOW = AVERAGE[20](LOW)
AverageRANGE = AverageHIGH - AverageLOW

// check whether any previous gap has been filled
FOR i = 1 TO Elements
    IF $Direction[i] = UPwards THEN
        //bearish gaps
        IF high[1] < $Gap[i] AND high >= $Gap[i] THEN
            FOR j = i TO Elements - 1
                $Gap[j] = $Gap[j + 1]
                $Gap2[j] = $Gap2[j + 1]
                $Direction[j] = $Direction[j + 1]
                $BarID[j] = $BarID[j + 1]
                $Gap[j + 1] = 0
                $Gap2[j + 1] = 0
                $Direction[j + 1] = 0
                $BarID[j + 1] = 0
            NEXT
            $Gap[Elements] = 0
            $Gap2[Elements] = 0
            $Direction[Elements] = 0
            $BarID[Elements] = 0
        ENDIF
    ELSIF $Direction[i] = DOWNwards THEN
        //bullish gaps
        IF low[1] > $Gap[i] AND low <= $Gap[i] THEN
            FOR j = i TO Elements - 1
                $Gap[j] = $Gap[j + 1]
                $Gap2[j] = $Gap2[j + 1]
                $Direction[j] = $Direction[j + 1]
                $BarID[j] = $BarID[j + 1]
                $Gap[j + 1] = 0
                $Gap2[j + 1] = 0
                $Direction[j + 1] = 0
                $BarID[j + 1] = 0
            NEXT
            $Gap[Elements] = 0
            $Gap2[Elements] = 0
            $Direction[Elements] = 0
            $BarID[Elements] = 0
        ENDIF
    ENDIF
NEXT

// Detect new Gaps
// Find bullish gaps
IF LOW > HIGH[1] AND (LOW - HIGH[1]) > GapDepth * AverageRANGE THEN
    gapfillzoneHIGH = LOW
    gapfillzoneLOW = HIGH[1]
    a = BARINDEX-1
    IF $Gap[Elements] <> 0 THEN
        Elements = Elements + 1
        $Gap[Elements] = 0
        $Gap2[Elements] = 0
        $Direction[Elements] = 0
        $BarIDE[Elements] = 0
    ENDIF
    FOR i = 1 TO Elements
        IF $Gap[i] = 0 THEN
            $Gap[i] = gapfillzoneLOW
            $Gap2[i] = gapfillzoneHIGH
            $Direction[i] = DOWNwards
            $BarID[i] = a
            break
        ENDIF
    NEXT
ENDIF

// Find bearish gaps
IF HIGH < LOW[1] AND (LOW[1] - HIGH) > GapDepth * AverageRANGE THEN
    gapfillzoneHIGH = HIGH
    gapfillzoneLOW = LOW[1]
    a = BARINDEX-1
    IF $Gap[Elements] <> 0 THEN
        Elements = Elements + 1
        $Gap[Elements] = 0
        $Gap2[Elements] = 0
        $Direction[Elements] = 0
        $BarID[Elements] = 0
    ENDIF
    FOR i = 1 TO Elements
        IF $Gap[i] = 0 THEN
            $Gap[i] = gapfillzoneLOW
            $Gap2[i] = gapfillzoneHIGH
            $Direction[i] = UPwards
            $BarID[i] = a
            break
        ENDIF
    NEXT
ENDIF

// draw lines
FOR i = 1 TO Elements
    IF $Gap[i] <> 0 THEN
        a = $BarID[i]
        x = $Gap[i] //gapfillzoneLOW
        y = $Gap2[i] //gapfillzoneHIGH
        IF $Direction[i] = DOWNwards THEN
            DRAWSEGMENT(a,y,a+GapLineSize,y) COLOURED("Green",t155) style(Line,2)
            DRAWSEGMENT(a,x,a+GapLineSize,x) COLOURED("Green",t255) style(Line,2)
        ELSIF $Direction[i] = UPwards THEN
            DRAWSEGMENT(a,x,a+GapLineSize,x) COLOURED("Red",t155) style(Line,2)
            DRAWSEGMENT(a,y,a+GapLineSize,y) COLOURED("Red",t255) style(Line,2)
        ENDIF
    ENDIF
NEXT

Explanation of the Code:

  • Initialization: Sets up variables and arrays to store information about detected gaps and their characteristics.
  • Average Range Calculation: Computes the average high and low over the last 20 bars to determine the typical price range, which helps in identifying significant gaps.
  • Gap Detection: Checks for gaps between consecutive bars that are larger than a defined threshold based on the average range.
  • Gap Filling: Monitors if previously detected gaps have been filled by subsequent price movements.
  • Visualization: Draws lines on the chart to visually represent the detected gaps, using different colors to distinguish between bullish and bearish gaps.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/gap-fill-indicator/#post-203551

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...