Randomly in my algorithm, I noticed when I'm closing my long call option (sell to close), it's getting exercised and my algo is forced to purchase the shares.

This makes no sense because I'm selling my long call to close the trade, so how is it being exercised when I should no longer have an open position?

If I'm using a real broker, this will never happen. When selling to close a call option position in a real environment, it will never get exercised. Why is this so hard to find information about, and is there any way I can work around this issue?

In my attached backtest, notice that I am selling to close my option position but then suddenly acquire the maximum amount of shares my account can hold (Around April 21). This shouldn't be happening.