By studying the mechanism of bilateral contract trading considering the renewable energy generation, an optimization model is developed to maximize the benefits of power grid companies and power generation companies. And the model is solved through an improved dynamic programming method with reverse and forward algorithm. Finally, the rationality of the proposed optimization model and the effectiveness of the algorithm are verified through a numerical example.