The S&P 500 is essentially flat for the year, back from a low of 2,237 on March 23. However, the index is heavily divided. Just five companies -- Amazon (NASDAQ:AMZN), Apple (NASDAQ:AAPL), Facebook (NASDAQ:FB), Alphabet (NASDAQ:GOOGL) (NASDAQ:GOOG), and Microsoft (NASDAQ:MSFT) -- account for roughly 20% of the index. These stocks have all posted double-digit gains for the year.

According to Goldman Sachs, the median stock in the index is 28% below its high. That means these five stocks are pulling the index up significantly.

Tabletop scale, showing balanced financial reward on the left and risk on the right, with an umbrella over top of both sides of the scale.

Image source: Getty Images

It's easy to get caught up in the excitement when a stock or sector is performing well the way tech is now, and buy more. Before you jump in and buy, let's take a look at what's causing the rise in tech stocks, and what that might mean for your portfolio.

What's driving tech stock performance?

Across the board, tech companies have outperformed other sectors of the market during the coronavirus pandemic. 

On the whole, tech companies had the ability to continue operations via remote work in a locked-down virtual environment. In addition, consumers have had increased downtime at home, which has provided more demand for digital services -- meaning there's been uninterrupted operations and increased demand for these companies' products and services.

For example, Amazon sales were up 40% in Q2. Facebook also saw a 12% increase in users, which drove ad revenue and profits up.

If you're wondering why this is, think of your own behaviors during the pandemic. Have you ordered more online? Paid for more digital services like streaming video? Now imagine all your friends you know that do the same. Investors have seen the good position many of these companies are in. This is helping fuel the rise in tech stocks.

Should I concentrate my portfolio in tech stocks?

If you're watching tech stocks over the past few months and getting excited, you're not alone. Since most other sectors are struggling, it can feel more exciting to invest only in the seemingly more resilient tech stocks. But investing a significant portion of your money in tech stocks is a risky move. That's not to say that tech stocks are bad investments -- we simply can't know that the current economic trends will continue. 

Tech stocks are performing well now because these companies are well-suited for the current economic environment. Any of these stocks, or the tech sector as a whole, could fall out of favor as the economy improves, competitors adapt to virtual environments and tech loses its competitive edge, or consumer preferences change. 

The importance of diversification

When a handful of companies produce exciting stock gains, it's tempting to want to jump in and buy. 

Remember, though, that concentrated positions mean concentrated risk. 

Any hot stock or sector today may be the mediocre or lackluster sector tomorrow. Remember the dot-com bubble? If you had concentrated your portfolio in tech stocks alone, you would have lost all the gains you made when the bubble popped

That's a dramatic example driven by a speculative bubble, but it serves to illustrate how quickly a stock, sector, or the market can turn. It also illustrates how quickly gains can be erased in the short-run too.

Investing beyond the pandemic

Long-term returns are what matter to long-term investors. Investing during the pandemic shouldn't be about chasing hot stocks or sectors in the short run. 

The real issue for a long term investor is what their investments will do in the long run. When the pandemic finally ends and life returns to normal, will tech stocks still be leading the market? Maybe, but maybe not. That will depend on future market conditions. That's true of any sector, whether it is currently hot or out of favor.

If you concentrate your stock position in tech because it's performing well, you are betting that it will continue to do so in the future. You are also exposing yourself to significant risk if you're mistaken.

It's OK to buy into a sector with good fundamentals in expectation of long-term growth, but remember the downside, too. Be realistic about your expectations, and take time to assess how you would react to price movements over different time periods. How would you react if your investments didn't perform the way you expected? If you think you'd feel pressured to sell, that's a strong sign you are holding too much. 

Diversify appropriately, and only hold stocks -- and sectors -- in your portfolio that fit your long-term strategy.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.