Liquidation via API does not work 5 minutes before market close

I have a project in live deployment with QuantConnect (local LEAN CLI) trading a $30,000 margin paper trading account at Alpaca, which takes positions intra-day only, both long and short. I’m using 1 minute data, but my code only takes action at 5 minute intervals.

I have a scheduled event setup that is supposed to close all open positions exactly 5 minutes before market close every trading day. In backtesting everything works as intended, and open positions are closed exactly 5 minutes before market close. However, during live deployment, the liquidate function does not work at the 5 minutes before market close mark. 20 minutes, 15 minutes, 10 minutes, and 6 minutes before market close all work as intended in live deployment, as do other times throughout the trading day, but 5 minutes before close does not.

The instrument I’m trading is extremely liquid, so there’s no issue there. Additionally, I am able to liquidate my open positions manually via the Alpaca dashboard any time between 5 and 0 minutes prior to market close. Why am I not allowed to liquidate my positions via API at 5 minutes or less before market close?

This turned out to be an error on my part. My code was trying to perform multiple operations at the same time and was tripping over itself.

@fred_pickles Thank you for the update and glad to hear everything’s working for you now. Good luck!