Wall Street capped 2017 with a loss, weighed down by a broad slide in light trading ahead of the New Year’s holiday. Technology companies, banks, and health care stocks accounted for much of the market’s decline. Energy stocks also fell, even as the price of US crude oil surged to its highest level in more than two years. Despite the downbeat end to the week, the US stock market finished 2017 with its strongest year since 2013. The Standard & Poor’s 500 index, the broadest measure of the stock market, gained 19.4 percent for the year, more than double its gain in 2016. Including dividends, the total return was 22.5 percent, as of late Thursday. The Dow Jones industrial average ended the year with a 25.1 percent gain, setting 71 all-time highs along the way. The Nasdaq composite notched the biggest gain, an increase of 28.2 percent, while the Russell 2000 index of smaller-company stocks closed out 2017 with a gain of 13.1 percent. ‘‘It’s been the year that surprised everybody,’’ said J.J. Kinahan, chief market strategist at TD Ameritrade. ‘‘It was truly buy-on-the-dip, and that paid off better than anyone possibly expected.’’ On Friday, many investors opted to pocket some of their gains, especially in technology stocks, which led the market with a gain of 36.9 percent.