ProRealCode - Trading & Coding with ProRealTime™
I saw this “gap fill indicator” by Roberto gozzi. I wonder how can we make the screener for it and be able to screen only when the current price is really close to the available gap. here is the indicator code:
// 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
//IF BarIndex <= 9999999 THEN
//
// 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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarIDE[Elements] = 0
ENDIF
// store the new element in the first available slot
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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarID[Elements] = 0
ENDIF
// store the new element in the first available slot
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
//
//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
//
RETURN
so no solution?
You can try this code, setting the distance you consider close enough:
// GAP Monitoring till Filled
//
// https://www.prorealcode.com/topic/gap-fill-indicator/
//
//DEFPARAM DrawOnLastBarOnly = True
//
//ONCE GapLineSize = 4
ONCE GapDepth = 0.3
ONCE Distance = 2 * PipSize //detect when the current price is as close as DISTANCE pips
//
//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
//IF BarIndex <= 9999999 THEN
//
// 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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarIDE[Elements] = 0
ENDIF
// store the new element in the first available slot
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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarID[Elements] = 0
ENDIF
// store the new element in the first available slot
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
//
//ENDIF
//
// draw lines
//
//FOR i = Elements DOWNTO 1
IF $Gap[Elements] <> 0 THEN
a = $BarID[Elements]
x = $Gap[Elements] //gapfillzoneLOW
y = $Gap2[Elements] //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
c1 = (abs(close - x) <= Distance)
c2 = (abs(close - y) <= Distance)
ENDIF
//NEXT
//
SCREENER[c1 OR c2]
Thank you and can you tell me what is pipsize and elements defined to please?
DISTANCE, line 9.
can you please change it to only bullish gap so I can define distance between the gap and current low not the close?
There you go:
// GAP Monitoring till Filled
//
// https://www.prorealcode.com/topic/gap-fill-indicator/
//
//DEFPARAM DrawOnLastBarOnly = True
//
//ONCE GapLineSize = 4
ONCE GapDepth = 0.3
ONCE Distance = 2 * PipSize //detect when the current price is as close as DISTANCE pips
//
//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
//IF BarIndex <= 9999999 THEN
//
// 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
//// move next element to the current position (so it's overwritten)
//$Gap[j] = $Gap[j + 1]
//$Gap2[j] = $Gap2[j + 1]
//$Direction[j] = $Direction[j + 1]
//$BarID[j] = $BarID[j + 1]
//// clear the next element
//$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 $Direction[i] = DOWNwards THEN //bullish gaps
IF low[1] > $Gap[i] AND low <= $Gap[i] THEN
FOR j = i TO Elements - 1
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarIDE[Elements] = 0
ENDIF
// store the new element in the first available slot
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
//// make room for one more array element, if needed
//IF $Gap[Elements] <> 0 THEN
//Elements = Elements + 1
//$Gap[Elements] = 0
//$Gap2[Elements] = 0
//$Direction[Elements] = 0
//$BarID[Elements] = 0
//ENDIF
//// store the new element in the first available slot
//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
//
//ENDIF
//
// draw lines
//
//FOR i = Elements DOWNTO 1
IF $Gap[Elements] <> 0 THEN
a = $BarID[Elements]
x = $Gap[Elements] //gapfillzoneLOW
y = $Gap2[Elements] //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
//c1 = (abs(close - x) <= Distance)
//c2 = (abs(close - y) <= Distance)
c1 = (abs(low - x) <= Distance)
c2 = (abs(low - y) <= Distance)
ENDIF
//NEXT
//
SCREENER[c1 OR c2]
thank you but line 134 to 137 instead of [elements] shouldn’t be [ i ] ? because results on nasdaq don’t make sense.
i want results like attached photo.
Yes, you are right, uncomment line 133 and line 150, then replace [elements] with [ i ].
Unfortunately I cannot test it.
ok those changes worked but I still have results that don’t match like in some of them the gap was already been filled before.
I modified it a bit, it sounds better now:
// GAP Monitoring till Filled
//
// https://www.prorealcode.com/topic/gap-fill-indicator/
//
//DEFPARAM DrawOnLastBarOnly = True
//
//ONCE GapLineSize = 4
ONCE GapDepth = 0.35
ONCE Distance = 2 * PipSize //detect when the current price is as close as DISTANCE pips
//
//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
//IF BarIndex <= 9999999 THEN
//
// 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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// move next element to the current position (so it's overwritten)
$Gap[j] = $Gap[j + 1]
$Gap2[j] = $Gap2[j + 1]
$Direction[j] = $Direction[j + 1]
$BarID[j] = $BarID[j + 1]
// clear the next element
$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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarIDE[Elements] = 0
ENDIF
// store the new element in the first available slot
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
// make room for one more array element, if needed
IF $Gap[Elements] <> 0 THEN
Elements = Elements + 1
$Gap[Elements] = 0
$Gap2[Elements] = 0
$Direction[Elements] = 0
$BarID[Elements] = 0
ENDIF
// store the new element in the first available slot
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
//
//ENDIF
//
// draw lines
//
MinDistance = 999999
FOR i = 1 TO Elements
IF ($Gap[i] <> 0) AND ($Direction[i] = DOWNwards) 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
c1 = (abs(high - x))// <= Distance) //or HIGH instead of CLOSE
c2 = (abs(high - y))// <= Distance) //or HIGH instead of CLOSE
cx = min(c1,c2)
c3 = (abs(low - x))// <= Distance) //or LOW instead of CLOSE
c4 = (abs(low - y))// <= Distance) //or LOW instead of CLOSE
cy = min(c3,c4)
MinDistance = min(MinDistance,min(cx,cy))
ENDIF
NEXT
//
SCREENER[MinDistance <= Distance]
Thank you so much.
gap fill screener
This topic contains 11 replies,
has 2 voices, and was last updated by rob.es
2 years, 10 months ago.
| Forum: | ProScreener: Market Scanners & Detection |
| Language: | English |
| Started: | 04/02/2023 |
| Status: | Active |
| Attachments: | 1 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.