Generally, a buyer's appetite for something would decline as the price rises. But that's one of the many ways the stock market defies normal conventions.

Often, demand for a stock increases after a rally -- and falls following a period of underperformance. In other words, a stock gets less popular as it becomes cheaper, and more popular as its price increases.

That oddness is partly because investors have a habit of chasing performance, which isn't a great strategy if you're looking to boost your long-term returns. Still, some stocks are rallying for excellent reasons, and their prices don't fully reflect all the positive operating momentum in the business.

Let's look at two good examples. I'd buy Amazon (AMZN 0.75%) and Costco Wholesale (COST -0.39%) right now, even though they have trounced the market over the past year.

1. Amazon

Amazon shares are up 80% in the past full year and have gained over 20% in 2024 alone. Yet the stock could still be undervalued.

Sure, this member of the "Magnificent Seven" has been caught up in the market euphoria around artificial intelligence (AI). Wall Street loves the tech giant's exposure to enterprise cloud services growth, especially given that it is backed up by a large but barely profitable e-commerce segment -- at least compared to the Amazon Web Services (AWS) unit. This segment generated $7.2 billion of operating profit last quarter, up from $5.2 billion a year ago.

Shares don't seem expensive given the strong sales and earnings growth that Amazon is putting up. You can buy the stock for 3.3x revenue, which is still well below the pandemic-high valuation that investors saw in 2021. Brace for volatility ahead, but consider keeping Amazon in your portfolio for the long term.

2. Costco

It's a retailer, yet it's rallying like a tech stock right now. Costco, the big-box retailing titan, is up sharply in the past year and trouncing industry rivals like Walmart and Target. I'm still bullish on this popular stock following its recent rally, though.

That's because Costco is attracting more customers to its stores, thanks to its price-leadership selling approach. Customer traffic was up a healthy 4% last quarter.

Shoppers are flocking to the company's online sales channel that tilts toward more discretionary purchases (like consumer electronics and home furnishings). Wins in both areas allowed second-quarter sales to rise 6% to $57 billion.

Costco won't thrill you with huge sales gains in any short-term period, and most Wall Street pros expect revenue to rise by less than 5% this fiscal year. Investors should temper their expectations around earnings gains, as well. Costco routinely converts just 3% of sales into operating profit, compared to Walmart's 4% and Target's 5%.

But the business is primed for long-term success. Its customers are highly engaged, as you can see by Costco's membership renewal rate that's sitting at a record high of 92%. This elevated level will also make it easier for the company to pass along its next membership-fee hike at some point this year.

Watch for most of the financial gains from this move to flow into Costco's price cuts rather than spark an immediate earnings improvement. Yet the chain will still get major financial benefits from the increased fees, which constitute most of its annual earnings haul.

That selling approach means Costco's profits are far more stable than those of its peers and can grow through the ups and downs of the economy. That stability is valuable -- and why investors should consider owning this consumer staples stock, even after its recent rally.