How did 炒 semantically shift to mean 'speculate' and 'perform badly (in a test or exam)'?
炒魷魚 is a Cantonese slang for 'fire' (from a job)

In olden time, many employees would live in the employer's place. When an employee was fired, He had to 執包袱 (roll up his belongings in a bundle to pack them) and leave.

炒魷魚 got the meaning of 'fired' because when you stir fry squid, they would roll up like a bundled package and that resembling the action of '執包袱' (pack up and leave) . Nowadays, we use 炒 (fire from job) as a shorthand for 炒魷魚

As for 炒 in 炒股票 (to speculate in stocks) and 炒樓 (to speculate in property). It is short for  炒買，炒賣

Meaning quickly acquire and quickly sell off stock or property to make a quick profit (different from normal investment) like making a quick stir fired dish. Since 炒 means 'to stir'  The act of 炒股票 and 炒樓 also imply 'stir up the market'


  (Hong Kong Cantonese) to perform badly (in a test or exam)


This meaning of '炒' came from another slang '炒車' (having a car accident) You can imagine cars being threw around in an accident like food being stirred in a wok. 炒 therefore, took on the meaning of 'to ruin' extended to --> perform badly (in a test or exam)