Your first goal as an investor is to identify stock market opportunities. That's the point of investing, after all. If you can earn 15% to 20% annualized returns, you're elated.

But investing doesn't end there.

Taxes, fees, and trading costs add up over time. If you're not smart about your asset allocation, trading practices, withdrawal rates, and tax-advantaged vehicles, they will take a bite out of your real returns. And the returns from those after-tax, after-trading costs are, after all, what matter most. They may not be the numbers you brag to your friends about, but they sure are the numbers that pay the bills.

Get ahead, stay ahead
Let's say you were a shareholder of one or more of the runaway market successes of the past year: Titanium Metals (NYSE:TIE), NutriSystem (NASDAQ:NTRI), and Knight Capital Group (NASDAQ:NITE). (And if you held a position in any or all, congratulations are in order.)

Each of those stocks is up more than 100% since July 31, 2005. And while you should be toasting to those returns, a few bad moves can make your super paper returns a little more average.

Now let's say you want to take some of your Titanium Metals off the table (maybe a prudent move in the face of recent volatility). If you'd bought in September of last year and sold today, you would have to pay capital gains at your personal income tax level (up to 35%!). If, however, you wait a month, you qualify for long-term gains and would likely pay only 15%. On a $5,000 investment, those 30 days could save you more than $2,500.

Sure, that's a straightforward example, but let me assure you that the tax code is as complicated as it is long. If you've dollar-cost averaged into your positions, or if you want to withdraw funds from your Roth IRA, you've got a pile of math and legalese to wade through.

Profit from your losses
As we all know and have experienced, stocks don't always go up. The stock market is full of disasters. From the Great Depression to the tech bubble, investors in every era have experienced the pain of financial loss.

But the good news is that a few stock market duds don't signal the end of your financial future. In fact, a few losers can actually help your returns, because you can use stock market losses to write off stock market gains. Want to unload NutriSystem or Knight Capital? Maybe you also own Sirius Satellite Radio (NASDAQ:SIRI) or Yahoo! (NASDAQ:YHOO); these two stocks are down significantly over the past 12 months. It's only prudent to use your losses there to keep your gains out of the grubby hands of the tax man.

Or maybe you only own shares of Sirius and Yahoo! If so, then this has been a rough year so far. But don't fret: Even those who have been crippled by such losses can get started on a plan to rebuild their savings and investments and achieve their retirement dreams.

One of the first steps in such a plan is to make sure your investments match your investing timeline. Sirius could be hugely successful in the future. Unfortunately, its current state of affairs isn't so rosy. It might be years before the company is consistently profitable and ready to wow analysts. If you don't have years -- read: you're in or very near retirement -- then Sirius is not the place for your money. Sell your shares to someone many years your junior.

If, however, you're a younger investor with some risk tolerance, consider taking a chance on a growth company such as PDL BioPharma (NASDAQ:PDLI), which is not profitable right now, but can point to a stacked pipeline. There will be more bumps along the way with PDL than if you invest in a more proven health-care stock such as Pfizer (NYSE:PFE), but you've still got some years in you, so why not swing for the fences?

Listen to legends
The world's greatest investors can't predict the future. Jack Bogle, founder of the hugely successful Vanguard Group, told Fool co-founder David Gardner, "I am a nervous investor. Nobody knows what will happen tomorrow or next year. ... But [with] four things -- diversification, tax efficiency, lower costs, a long time horizon -- you will capture the returns that this uncertain stock market is kind enough to deliver."

Bogle's words should guide us all. While investing is the foundation of wealth building, there's always work to be done to perfect your portfolio, balance your investments, and allocate your resources among taxable and tax-advantaged accounts in a way that meets your timeline and protects you from financial stumbles. By being smart with your gains, you can benefit most from your stock-picking successes.

The goal of our Motley Fool Rule Your Retirement service is to help you do just that. Former Wall Street financial planner Robert Brokamp will help make sure you don't get blindsided by arcane government regulations -- so that you earn the best real returns possible -- and plan for the perfect retirement. He'll even highlight specific stock picks from the Fool's best analysts along the way. To see exactly what Robert can offer, click here for a free 30-day trial. There is no obligation to subscribe.

This article was originally published on June 27, 2006. It has been updated.

Tim Hanson does not own shares of any company mentioned in this article. PDL BioPharma is a Rule Breakers recommendation. Pfizer is an Inside Value recommendation. No Fool is too cool fordisclosure.