Every implementation of SPH I’ve seen, in various articles and academic papers, uses two separate loops. One loop that includes the current particle to calculate the density and pressure, followed by a second loop that excludes the current particle to calculate the forces. You need to calculate the density and pressure first.