What is the meaning of [Get out while the going (getting) is good.]

To leave a place or situation before conditions worsen and it becomes difficult to leave.

Example: With the stock market at an all-time high and further upside looking difficult, we decided to sell our shares and get out while the going was good.