Pop quiz, hotshot: The S&P is up 48% from its closing low in March. If the market continues to go up, you want to capture those gains. But if it reverses course, you want to protect your nest egg. So what do you do?

The answer seems obvious, right? Unemployment is closing in on 10%, companies like United Technologies and Coca-Cola (NYSE:KO) are posting better-than-expected earnings numbers but missing revenue expectations, and government spending is propping up the economy. It's time to sell!

But wait just a second there. Financial companies like Goldman Sachs (NYSE:GS) and JPMorgan Chase (NYSE:JPM) seem to be getting their footing back, home sales appear to finally be showing signs of stabilization, and the chief economist at the International Monetary Fund just said that the global economy will resume growth by the end of the year. For Pete's sake, buy!

Don't mind the 48%
Stock market commentators tend to be a rather fearful bunch. When times get bad and the market is sinking, many of them seem to suffer from taphephobia -- a fear of getting buried alive -- and they want to run for the hills as quickly as possible. On the flip side, when the market is shooting up, acrophobia -- the fear of heights -- seems to come into play and, well, they're off running for the hills again.

But if you ask me -- or really any fundamentals-oriented investor -- our concern shouldn't be over how much the market has gone up or down, but rather whether its valuation is attractive or unattractive. If a hot market with sky-high valuations rises 48% in a few months, that acrophobia is probably warranted. On the other hand, if a sorely undervalued market tacks on that same 48%, there could still be good reason to buy.

Do mind the valuation
There are many ways to track the market's valuation, but I'm a big fan of the work that Robert Shiller of Yale has done. He maintains a spreadsheet that tracks the S&P's price and earnings going all the way back to 1871 and tracks the market's valuation by comparing price to the average earnings over the past 10 years.

The long-term average for the P/E number that Shiller tracks is around 16.3. The current readout is 17.5, which is up from 13.3 in March and down from 27 prior to the market's crash. So today's valuation isn't nearly as attractive as March's, but it's much more attractive than what was available to us pre-recession.

What's concerning though, is that earnings estimates for the S&P index from Standard & Poor's suggest that earnings will remain well below their pre-recession levels. This means that even if the market doesn't move up much more, its valuation will rise as average earnings fall. And if you want to try and escape this escalating valuation by moving back to a P/E based on a single year of earnings, don't bother -- earnings are expected to be low enough that the single-year P/E may be double its historical average until midway through 2010.

So what do you do?
Earlier in the year, when the market's valuation was undeniably low, it was difficult to go wrong with buying an index fund and calling it a day. Sure, you could have done better buying certain individual stocks such as Research In Motion (NASDAQ:RIMM) (up 123%) or Starbucks (NASDAQ:SBUX) (up 163%), but you would have done quite well with an index and wouldn't have had to break much of a mental sweat.

If you're a long-term retirement investor and you want to keep it simple, you're still probably fine grabbing those index funds today. For investors who like to put on a hard hat and go prospecting for the best stock opportunities, though, now is a good time to start eschewing the index funds in favor of some individual stocks that are still undervalued.

IBM (NYSE:IBM), for example, is still trading at just 11 times its expected 2009 earnings -- which is much more attractive than the broad S&P index. Health-care giant UnitedHealth Group (NYSE:UNH), meanwhile, is sitting at an attractive forward P/E of 8.

And while I think the stocks of those blue-chip candidates will perform quite well, there may be even more opportunity for investors willing to consider small-cap stocks. The world of small caps is a largely uncharted sea of stocks that most institutional types avoid because they're too small to make an impact on huge mutual fund portfolios. For individual investors, though, they can be a great way to find hidden value -- particularly at the outset of an economic recovery.

If you're looking for some stock ideas, the advisors at Motley Fool Hidden Gems are a great resource for new or veteran small-cap hunters. You can see their six "buy first" stocks and read the research on all of their recommendations free for 30 days. Click here to get started. 

Fool contributor Matt Koppenheffer owns shares of Coca-Cola, but does not own shares of any of the other companies mentioned in this article. Starbucks and UnitedHealth Group are Motley Fool Stock Advisor selections and Fool holdings. Coca-Cola, Starbucks, and UnitedHealth Group are Motley Fool Inside Value selections. Coca-Cola is a Motley Fool Income Investor recommendation. The Fool's disclosure policy enjoyed Speed in spite of Keanu Reeves.