This is issue is due to low speed optocoupler .

Replace Direction and Pulse(CLock) optocoupler(p817) with 6n137 optocoupler.

This will solve problem and not loss steps.