Distributed renewable power generators, such as solar cells and wind turbines are difficult to predict, making the demand-supply problem more complex than in the traditional energy production scenario. They also introduce bidirectional energy flows in the low-voltage power grid, possibly causing voltage violations and grid instabilities. In this article we describe a distributed algorithm for residential energy management in smart power grids. This algorithm consists of a market-oriented multi-agent system using virtual energy prices, levels of renewable energy in the real-time production mix, and historical price information, to achieve a shifting of loads to periods with a high production of renewable energy. Evaluations in our smart grid simulator for three scenarios show that the designed algorithm is capable of improving the self consumption of renewable energy in a residential area and reducing the average and peak loads for externally supplied power.