I have a backtest that analyzes the SP 500 for the past 10 years and finds the top 5 gainers and losers for each month. It buys the top 5 gains and shorts the top 5 losers at the beginning of the month and the liquidates at the end of the month. It works but very, very slowly. Is there any way optimize my algorithm?

Right now it checks the date and if it's the first of the month it buys/shorts and then does another check to see if it's the end of the month and liquidates if it is. It doesn't need anything better than daily resolution but I believe that's not an option currently? Also, I initially tried this so that the algo was analyzing the sp500 and finding the top/worst 5. But I thought the slowness was due to this so I did a bunch of data crunching and hardcoded the top/worst 5 in my algo. Unfortunately, I didn't see an improvement in speed.