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:
Check out this related content for more information:
https://www.prorealcode.com/topic/gap-fill-indicator/#post-203551
Visit Link