Saving for retirement is a good way to ensure that you're able to pay your living costs once you stop working. After all, Social Security will only replace about 40% of your preretirement paycheck if you're an average earner, and most seniors need roughly 70% to 80% of their former income to keep up with their expenses. In the absence of a robust pension, saving in an IRA or 401(k) is your next best bet. And the good thing about these accounts is that they allow you to invest your money for added growth.

But an alarming percentage of older workers might be investing their retirement savings too aggressively. A recent analysis from Fidelity found that almost 38% of baby boomers have a higher percentage of stocks in their portfolios than recommended. And 7% are 100% invested in stocks, which is a dangerous mistake that should be corrected.

Pen hovering over a pie chart with asset allocation spelled out under it.

IMAGE SOURCE: GETTY IMAGES.

Don't take on needless risk

Loading up on stocks in your retirement plan during your 20s, 30s, 40s, and even early to mid-50s can be a very wise move. That way, you give your savings a chance to generate some solid returns.

Imagine you're able to set aside $300 a month in a retirement account between the ages of 25 and 50. If you were to invest that money primarily in stocks and generate an 8% average annual return as a result (which is just below the stock market's average), you'd grow your savings to $263,000. If you were to play it safe by sticking mostly to bonds, and therefore limit yourself to an average annual 3% return, you'd wind up with only $131,000 over that 25-year span. As such, younger workers are often advised to invest in stocks for maximum growth.

The rules change, however, as you get closer to ending your career, and for one key reason: At that point, you don't have a lot of time to ride out potential market downturns, and the last thing you want is for your portfolio value to take a massive hit in the year leading up to your retirement. If that happens and you're forced to start withdrawing from your savings at a time when your portfolio is worth less, it could produce a ripple effect that leaves you with far less income during your golden years than expected.

In fact, as a general rule, you shouldn't invest money in stocks that you expect to need or access within 7 to 10 years. Therefore, if you're within a decade of retirement, you should really start thinking about shifting toward safer investments, like bonds. Bonds tend to be far less volatile than stocks, so they're appropriate for near-retirees.

But don't dump your stocks, either

That said, stocks should still very much have a place in your portfolio as retirement nears and also during your golden years. That way, your retirement account can continue to generate decent growth, even once you reach the point that you're starting to take withdrawals from it.

How much of your portfolio should you keep invested in stocks as an almost-retiree or actual retiree? Well, a classic rule of thumb is to subtract your age from 110 to arrive at your ideal allocation. If you're 65, for example, you'd put 45% of your portfolio into stocks and the rest in safer investments, like bonds. But that's not a perfect rule, and there's wiggle room with it, depending on your personal tolerance for risk.

But even if you have an extremely healthy appetite for risk, keeping your nest egg 100% invested in stocks is probably a bit too extreme for a near-retiree. If you're going to insist on keeping that allocation, be prepared to be flexible about when you retire -- because if the market tanks at the wrong time, you might need to quickly adjust your plans to avoid taking major losses.