Hey there,
I am in the process of making a gap-on-open trading algorithm and I wanted to start (obviosly) with the gap-datection part. I wrote a class for detecting and evaluating a gap between two TradeBars, which works just fine. In the actual algorithm, I schedule two events, one at market close, one after market open.

So far, so good. As a universe, I chose the S&P500, but now there is the follwing problem:

First the AtMarketClose. It gets one 1-hour Tradebar from every security in the universe and stores it. This takes a huge amount of time while backtesting, around one minute per simlutated day, only with 150 securities. With all 500, the backtest crashes after a few days of simulation because of the 1GB memory limit (even if I call the Garbage Collector with GC.Collect()). If I only use 150 securities, the backtest runs a bit longer and then crashes after 11 minutes of runtime, because of a timeout by the lean backend.

The detection of the gaps works just fine, its only the large amount of memory and time, that is cosumed by the algorithm, for no apearent reason. Maybe it is the history access, but does anybody know for sure?

Here is the backtest, I cant embedd it, because of the error:

https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_ed5912a51688c5386d13fd1ecb4241b6.html

Hopefully someone is able to help me figure it out...
Greatings,
Hendrik