There is no straight answer for this. As per my little exp, we need to make habit of booking some profit when market is euphoric and put those back in when market is pessimistic. As far as investing is concern, going out of market fully does not make sense and force us to take wrong decision later( Except end of bull market cycle end.)
There is a good post by Traderji about different phases of bull market and bear market, one can read that to have better understanding of current market situation/cycle.
Happy Trading!