First of all, "quitting while you are ahead" only has relevance if
you will NEVER PLAY AGAIN. Ever. Otherwise, you are just lengthening
the span of time between now and the next time you will gamble.
The above said, for psychological comfort, if you find yourself a
significant amount ahead, you can establish a "stop loss" amount
wherein you will preserve a win. Let's say you're up $1000--you
might say, "I will keep playing, but quit for this (day, trip,
month) if I drop down to only $500 ahead." And if you keep winning,
you can move the stop-loss up to $1000 ahead, and so forth.
This only works if you consider the remaining amount as ABSOLUTELY
NO LONGER PART OF YOUR BANKROLL. In the past, I once even took the
extreme precaution of going down to Western Union and wiring myself
the $5000 I had just won, leaving myself with $200 to gamble with.
It all comes down to the psychological effects of winning and losing
(presuming that the amount you have won doesn't represent some vital
life-changing event, like now you have enough for that sex-change
operation, or something). For most people, being up $1000 and
finishing dead even feels MUCH worse than being DOWN $1000, then
finishing even--even though the exact same result is achieved. The
simple fact is that once we've won, losing $X feels MUCH worse than
winning $X more feels good. So---stop-loss when you're ahead, reduce
your bet size (drastically, if necessary) to accomodate your new,
smaller bankroll, and gamble with a smile knowing that no matter
what, they're not gonna get you THIS time 
(... at the risk of further objection, I'll assure