Hey - can't comment on the error with the original algorithm as I'm running a C# env and can't replicate.However, I see a few potential problems with the code you pasted.
Line 23: the way windows work is that the latest entry is actually at index [0] and not [5] as you have written. Also, since you defined your window to be 5 entries long, the keys will be [0], [1], ... [4]. Key [5] does not exist - this is probably why you're getting the error you're getting.
Also, even if you referenced [0], you would have to be careful in how you used it. At this line (23), the window has not been populated yet, so you'll be referencing an empty location in memory which could cause all kinds of problems. I can't confirm, but most likely Python won't let you do that in the first place.
Solution: rewrite line 23 to reference index [0] and move the entire line inside your OnData method instead.