Hi,
in case you are still using this and would still want to gain a bit of speed, I had a look at it as part of my late unread summer posts. By the way as Roberto said above, many needed help. This topic came the day after I had to be offline for a few weeks for personal reasons and I thank Nicolas and Roberto for their offline support during these few weeks, and even now as I still have to be offline sometimes.
Basically, your loop ends up being very big with gapsindex continuously growing, but as importantly, you do lots of unnecessary calculations inside it, you can avoid most of them (perhaps all, would require a closer look) with the if statements structure modified as follows.
Also, this is a drawonlastbaronly=true code, so why would you keep graphic commands done on any bar not being the last one? At code launch you wouldn’t see any of the graphics asked during previous bars of history anyway as only the latest graphic output would appear, but you would still “pay” the time spent on executing the graphics commands (done many more times than you see it written because of the loop avg size multiplied by the number of bars the loop is executed). An “if islastbarupdate” can reduce this by not spending time on graphics not displayed. All together, early test show time reduced by more than 2 when applying these modifications on code from post #217882:
DEFPARAM DrawOnLastBarOnly = true
DEFPARAM CalculateOnLastBars = 2000
ONCE GapsIndex = 0
// Identify and store high gaps
IF High > Max(High[1], High[2]) THEN
$HighGapsTop[GapsIndex] = High
$HighGapsBottom[GapsIndex] = Max(High[1], High[2])
$GapStartHigh[GapsIndex] = BarIndex
$HighGapStatus[GapsIndex] = 0
$GapEndHigh[GapsIndex] = barindex
$GapFilledHigh[GapsIndex] = 0
GapsIndex = GapsIndex + 1
ENDIF
// Identify and store low gaps
IF Low < Min(Low[1], Low[2]) THEN
$LowGapsTop[GapsIndex] = Min(Low[1], Low[2])
$LowGapsBottom[GapsIndex] = Low
$GapStartLow[GapsIndex] = BarIndex
$LowGapStatus[GapsIndex] = 0
$GapEndLow[GapsIndex] = barindex
$GapFilledLow[GapsIndex] = 0
GapsIndex = GapsIndex + 1
ENDIF
// GAP status
// 0 for not filled
// 1 for filled upwards (High crosses over $HighGapsTop[i] and Low < $HighGapsBottom[i])
// 2 for filled downwards (Low crosses under $HighGapsBottom[i] and High > $HighGapsTop[i])
// 3 for partially filled upwards (High > $HighGapsBottom[i] and High < $HighGapsTop[i])
// 4 for partially filled downwards (Low > $HighGapsBottom[i] and Low < $HighGapsTop[i])
// 5 for filled upwards (Low crosses under $LowGapsBottom[i] and High > $LowGapsTop[i])
// 6 for filled downwards (High crosses over $LowGapsTop[i] and Low < $LowGapsBottom[i])
// 7 for partially filled upwards (Low > $LowGapsBottom[i] and Low < $LowGapsTop[i])
// 8 for partially filled downwards (High > $LowGapsBottom[i] and High < $LowGapsTop[i])
// Loop over all identified gaps and draw rectangles if they have not been filled
FOR i = 1 TO GapsIndex-1 DO
// High gaps
if $GapFilledHigh[i]=0 then
IF High > $HighGapsTop[i] AND Low < $HighGapsBottom[i] THEN // Gap Status 1
$GapFilledHigh[i] = 1
$HighGapStatus[i] = 1
$GapEndHigh[i] = BarIndex
ELSIF Low < $HighGapsBottom[i] AND High > $HighGapsTop[i] THEN // Gap Status 2
$GapFilledHigh[i] = 1
$HighGapStatus[i] = 2
$GapEndHigh[i] = BarIndex
ELSIF High > $HighGapsBottom[i] AND High < $HighGapsTop[i] THEN // Gap Status 3
$HighGapsBottom[i] = high
$HighGapStatus[i] = 3
ELSIF Low > $HighGapsBottom[i] AND Low < $HighGapsTop[i] THEN // Gap Status 4
$HighGapsTop[i] = low
$HighGapStatus[i] = 4
ENDIF
endif
// Low gaps
if $GapFilledLow[i]=0 then
IF Low < $LowGapsBottom[i] AND High > $LowGapsTop[i] THEN // Gap Status 5
$LowGapStatus[i] = 5
$GapEndLow[i] = BarIndex
$GapFilledLow[i] = 1
ELSIF High > $LowGapsTop[i] AND Low < $LowGapsBottom[i] THEN // Gap Status 6
$LowGapStatus[i] = 6
$GapEndLow[i] = BarIndex
$GapFilledLow[i] = 1
ELSIF Low > $LowGapsBottom[i] AND Low < $LowGapsTop[i] THEN // Gap Status 7
$LowGapsTop[i] = low
$LowGapStatus[i] = 7
ELSIF High > $LowGapsBottom[i] AND High < $LowGapsTop[i] THEN // Gap Status 8
$LowGapsBottom[i] = high
$LowGapStatus[i] = 8
ENDIF
endif
NEXT
if islastbarupdate then
FOR i = 1 TO GapsIndex-1 DO
IF $HighGapStatus[i] <> 0 then
if $GapFilledHigh[i] = 1 THEN
DRAWRECTANGLE($GapStartHigh[i], $HighGapsTop[i], $GapEndHigh[i], $HighGapsBottom[i]) coloured("red", 20) bordercolor("black", 20)
ELSe
DRAWRECTANGLE($GapStartHigh[i], $HighGapsTop[i], barindex, $HighGapsBottom[i]) coloured("red", 20) bordercolor("black", 20)
ENDIF
endif
IF $LowGapStatus[i] <> 0 then
if $GapFilledLow[i] = 1 THEN
DRAWRECTANGLE($GapStartLow[i], $LowGapsTop[i], $GapEndLow[i], $LowGapsBottom[i]) coloured("blue", 20) bordercolor("black", 20)
else
DRAWRECTANGLE($GapStartLow[i], $LowGapsTop[i], barindex, $LowGapsBottom[i]) coloured("blue", 20) bordercolor("black", 20)
ENDIF
endif
NEXT
endif
RETURN