Last week we took a look at what investors should do when one of their holdings undergoes a sharp decline -- examining what they should consider when deciding whether they should cut their losses, take advantage of the discount or merely hold tight.

There's a similar calculus that should be undertaken when investors are enjoying good times instead of bad, when an investment thesis plays out and you find yourself sitting on top of solid gains. In a situation like that, should you secure profits by taking some money off the table, add to your position in anticipation of even further gains, or simply step back?

As with the question of what you should do with losses, this depends on the specifics of the company. If your holding has benefitted from a trend you expect to continue -- whether something broad like an improving economy or something company-specific like improved market share -- that's a reason to stay put. No one has ever lost money taking profits, but some caution and forethought is required. First, recognize that just because you took some profits at one point, it isn't necessarily bad to buy the stock again, even at a higher price. Furthermore, if you are contemplating a sell, consider a partial sell as opposed to selling all your shares. This will give you some cash in the short-term and lessen the potential tax bite, while allowing you to remain focused on the stock's long-term outlook.

Moreover, remember that a significant part of an investor's total returns come from reinvesting dividends, so even if you've already made a tidy profit, years from now you'll want as many shares generating dividends for you as possible. Unless the company's fundamental story has changed for the worse, you're generally wise to not reduce your holdings, and the income you get from the company's dividend should always be a major part of your considerations.

If you have both a sizable cash position and a long-term investment horizon, you might look to increase your position. Viewed over a period of several years -- which is how long you should look to hold your positions, all things considered -- the cost of the trade and the current valuation will be minimal. As Warren Buffett is fond of saying, "it's far better to buy a wonderful company at a fair price than a fair company at a wonderful price."

Now, if you're a few years out from retirement or are unsure about the company's long-term prospects, you might be wise to take profits while you still have them. Similarly, if the recent rally was steep or the company's valuation seems stretched, the stock could be vulnerable to a swing in the opposite direction. Look at how much cash the company has on hand and compare its price-to-earnings ratio (P/E) to that of its peers. If it doesn't have a lot of cash, the dividend may not be sustainable, and if the P/E ratio is elevated, that could indicate the stock has little room to continue growing, at least in the near term.

Long-term investors will see both gains and losses over their life, so the important thing is to be sure about the fundamentals of your investments. If you are, you can feel confident that staying in is the right thing to do - no matter whether the stock is rising or falling.

This article is commentary by an independent contributor.