Optimal Algorithms for Energy Markets