I used to, as a child/teen always think the saying referred to quitting while one was ahead at something. The idea being eventually we all get our bad game, rainy day etc and pushing our luck is asking for disaster.
However, having now had experience at gambling and seeing a different context for the saying even in other scenarios outside of gambling, I am seeing truth in those who would wrongly quote it as quitting when one is already behind, to avoid further problems and harsher downswings.
What I cannot seem to get to the bottom of is if it just that I was raised with the 'ahead' variant being normal or if it really is the original.