The reason you will be losing position in this manner is 99% likely to be because your Step pulse is falling on the wrong side of the slope. This happens often and often goes unnoticed because it only shows on longer jobs or jobs with lots of direction changes. On Profile jobs or short jobs, etc it often goes unnoticed.

The solution to this is an easy fix.! In ports-n-pins / motor outputs toggle the "Step Low Active" to the opposite of whatever it is set to now.

If you want to test for this the write a G-code file which moves the axis back n forth a few 100 times then ends by coming back to zero, you don't need to move far 10mm will be enough and you can use G0 moves so travels at rapid speeds. Just make sure the last move is back zero. Something like this repeated 200 times will do.
G0 z-10
z0


Before you run the G-code Zero the axis and mark a point on the axis. When the code is finished it should return to the mark. If it doesn't then change the Step Low active and repeat the test. (don't forget to Zero and remark)

Do this test on each axis individually.