Investors often fear bear markets. But as much as we enjoy bull markets, we also need to embrace the bumpy periods too as part and parcel of how the markets work. We can learn to appreciate these periods by having a solidly diversified portfolio of stocks that work for us in different investing environments -- and by using bear markets to identify companies with strong, long-term opportunities that should soar when the bull comes back. I want to recommend four stocks to buy in the next bear market. Two of them can help shield your portfolio when times are rough, and two of them are trading at high valuations and can become better values if prices fall. The first two are Realty Income (NYSE: O) and Costco Wholesale (NASDAQ: COST), and the second two are Square (NYSE: SQ) and Shopify (NYSE: SHOP). Image source: Getty Images. Realty Income: A stable and growing dividend For income investors, there's a lot to love about Realty Income. Most notably, it pays a monthly dividend instead of the more common quarterly dividend, and it has paid it for 615 months in a row. What's more, the company raises the dividend quarterly, and it's done so for 96 consecutive quarters. Realty Income operates as a REIT, or real estate investment trust, and it owns more than 6,700 properties globally. Its largest tenants, such as Walgreens and FedEx, are mostly companies that sell essentials -- and so do well even in economic downturns. That gives Realty Income stability, and it means you can trust its dividend. Realty Income is merging with VEREIT, a similar property owner, which will bring its holdings to more than 10,000 properties and make it one of the biggest REITs in the world. That's even more security for shareholders. Costco: The case for low prices Big-box retailer Costco has had a steady and growing stock price for years, nearly doubling the S&P 500's gains over the past 10 years. It barely registered the March 2020 market crash, and it's performed similarly well during other market pullbacks. That's because its core model of selling products with razor-thin margins means great deals for members, who flock to the company's warehouses in all types of economic environments. Sour times often work in its favor since members (who pay an annual fee of $60 for a standard membership or $120 for an executive membership) shop at Costco to get the most out of their memberships and take advantage of its prices. Costco's sales shot up when the pandemic started, growing in low double-digits year over year, and they remain elevated even as other pandemic winners have seen their growth return to normal levels. Its stock price is up 20% year to date, and it pays a growing dividend that yields 0.68% at the current price. Image source: Getty Images. Square: High-growth fintech Digital payments processor Square has seen its stock gain more than 2,000% over the past five years, but it still has massive growth prospects. In the past year alone it's launched several new products, including trading in Bitcoin and stocks as well as a savings account, all on its Cash App. It's also expanding its sellers business to target more medium-size and large businesses, a step away from its core small-business customers, and it announced a recent deal with TikTok to integrate with the Square sellers platform. Its growth strategy is paying off with $4.7 billion in second-quarter sales -- up 143% over last year -- and $250 million in net income vs. a loss in the second quarter of 2020. Square is a fintech leader that's only just beginning. It's also super-expensive, with its stock trading at 225 trailing 12-month earnings. That premium might be justified, but Square stock is definitely one to consider if the price comes down. Shopify: Powering e-commerce The stock of Shopify has been a fantastic investment, gaining more than 3,000% over the past five years. But it's always been expensive and currently trades at 70 times trailing 12-month earnings. But it still has so much opportunity ahead, and it has demonstrated that it has the tools and team to keep growing. The company, which sells customizable e-commerce platforms, sees a total addressable market of $153 billion. It works with an extensive range of companies, from small ones (for a $29 monthly fee) to large ones (which can cost more than $2,000 a month), giving it a far reach. It also offers several products, giving it a broader growth runway. Shopify has grown to become the second-largest e-commerce company in the U.S. with almost 9% of the market, behind only Amazon. As you might imagine, 2020 was a huge year for Shopify as e-commerce became much more important under lockdowns. It posted an 86% year-over-year sales increase. But it's not suffering in 2021. Sales increased 57% in the second quarter. And although its merchant count jumped last year, it's still growing, albeit at more typical rates. Shopify is a great company with strong growth prospects. Investors should consider buying its stock -- and particularly if the price goes down. Find out why Shopify is one of the 10 best stocks to buy now Our award-winning analyst team has spent more than a decade beating the market. After all, the newsletter they have run for over a decade, Motley Fool Stock Advisor, has tripled the market.* They just revealed their ten top stock picks for investors to buy right now. Shopify is on the list -- but there are nine others you may be overlooking. Click here to get access to the full list! *Stock Advisor returns as of September 17, 2021 John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Jennifer Saibil has no position in any of the stocks mentioned. The Motley Fool owns shares of and recommends Amazon, Bitcoin, Costco Wholesale, FedEx, Shopify, and Square. The Motley Fool recommends the following options: long January 2022 $1,920 calls on Amazon, long January 2023 $1,140 calls on Shopify, short January 2022 $1,940 calls on Amazon, and short January 2023 $1,160 calls on Shopify. The Motley Fool has a disclosure policy.Source