Behind a record surge in cocoa prices this year, a corner of financial markets that drives the cost of chocolate underwent a seismic shift: The hedge funds that oiled its workings headed for the exit.

Confectionery prices, from candy bars to hot chocolate, are heavily influenced by futures contracts for cocoa beans. These financial instruments, traded in London and New York, allow cocoa buyers and sellers to determine a price for the commodity, forming a benchmark for sales across the world.

In the middle of last year, hedge funds — a class of investors that use privately pooled money to make speculative bets — started pulling back from trading cocoa futures because price swings in the market were raising their cost of trading and making it harder to make profits.