This year has been a pleasant surprise in the stock market for many investors. While many stock market projections for 2023 included a continued down period, this hasn't been the case though we're almost seven months through the year.

The contrast has even left some investors skeptical. On one end, who doesn't like gains (even if they're unrealized)? On the other end, many people shy away from investing after rallies because they fear they're investing at the market's peak.

I always refer to Warren Buffett's timeless advice in these situations: "It's far better to buy a wonderful company at a fair price than a fair company at a wonderful price."

Someone sitting at a table typing on a laptop with a dog beside them.

Image source: Getty Images.

Many top companies have been leading the way

A lot of the rallies in the major indexes like the S&P 500 and Nasdaq Composite can be attributed to the spikes of a handful of top companies.

The Vanguard S&P 500 (VOO 1.00%) is up over 19% this year. Apple and Microsoft -- which combine for over 13% of that fund -- are up over 54% and 43% year to date, respectively.

The Invesco QQQ ETF (QQQ 1.54%) tracks the Nasdaq-100, which contains the largest 100 non-financial companies trading on the Nasdaq stock exchange. It's currently up over 42% this year, and here's how the top five non-Apple or Microsoft companies have performed:

  • Nvidia: 213%
  • Amazon: 50%
  • Meta Platforms: 133%
  • Tesla: 146%
  • Alphabet: 36%

When in doubt, go the quality route

The most important part of Buffett's quote is the "wonderful company" portion. While wonderful is subjective, there are characteristics that separate good companies from great companies whose longevity you'd never have to question.

Buffett, in particular, is a fan of businesses with a sustainable competitive advantage, strong leadership, and a track record of great profits. If a company has all three, there's a good chance it will return value to its shareholders over time, even if you have to pay a little more than preferred.

Top companies don't get there accidentally; it comes from years of innovation, maintaining a competitive edge, and not becoming complacent. Most wonderful companies have this recipe for success built into their DNA, which is why investors are willing to pay more for their stocks.

There are no guarantees in the stock market, but there are companies whose long-term success is far more likely than comparable companies.

Stock price should be one factor, not the deciding factor

Just because a company is a blue chip stock doesn't mean you should ignore stock prices altogether. Notice that Buffett said a "fair" price, not any price. However, you don't want to hold off on investing while assuming that a stock's price is due for a correction just because of a recent rally.

You can use numerous metrics to find the relative value of a stock, and in many cases, great companies will be overvalued by these metrics. Take Apple, for example, whose P/E ratio of 32.7 is considerably higher than the S&P 500 average of 23.4. Nonetheless, you don't want that to be the ultimate deciding factor, especially if you have time on your side.

Many undervalued companies are in the stock market, but that doesn't make them great long-term options. Plenty of fair companies grow into their intrinsic value and then fail to produce value afterward.

Don't lose sight of what really matters

Investing should largely be about securing your financial future, so you don't want to become too short-sighted.

Don't be discouraged from investing in a wonderful company just because its stock has recently rallied. If a company's financials are solid, its competitive advantage is sustainable, and it's available at a fair price, it's likely to yield impressive returns in the long run.

It's not about timing the market for the best moment to invest (spoiler alert: there isn't a "best" moment). It's about being consistent and trusting the long-term process.