In most modern energy markets, electricity is traded in pay-as-clear auctions. Usually, multiple sequential markets with daily auctions, in which each hourly product is traded separately, coexist. In each market and for each traded hour, each power producer and consumer submits multiple price and volume combinations, called bids. After all bids are submitted by the market participants, the market-clearing price for each hour is published, and the market participants must fulfill their accepted commitments. The corresponding decision problem is particularly difficult to solve for market participants with stochastic supply or demand. We formulate the energy trading problem as a dynamic program and derive the optimal bidding functions analytically via backward recursion. We demonstrate that, for each hour and market, the optimal bidding function is completely defined by two bids. While we focus on power producers with stochastic supply (e.g., wind or solar), our model is applicable to power consumers with stochastic demand, as well. The optimal policy is applicable in most liberalized energy markets, virtually independent of the structure of the underlying electricity price process.