In the smart grid, real-time pricing policy is an important mechanism for incentivizing the consumers to dynamically change or shift their electricity consumption, thereby improving the reliability of the grid. Retailers are incorporated to the smart grid with distributed control mechanism in order to reduce the amount of communication overhead associated with the direction interaction between utility companies and consumers. The retailer procures electricity from both traditional and renewable energy sources, and sells it to its consumers. The consumers include residential users that can only consume power, and plug-in electric vehicles (PEVs) that can either consume power or supply power stored in its battery to the grid. In this work, a novel four-stage nested game model is proposed to model the interaction of the electricity retailer, utility companies, and consumers. The objective of the retailer is to maximize its overall profit as well as perform frequency regulation, whereas the goal of each consumer is to maximize a predefined utility function. In the game theoretic framework, the retailer should decide the amounts of electricity purchased from the renewable and traditional energy sources, respectively, as well as the real-time pricing scheme for its consumers. The consumers will react to the pricing mechanism and maximize their utility functions by adjusting the electricity demand. The optimal solution of the nested game is provided through: (i) finding the subgame perfect equilibrium (SPE) of all the consumers, and (ii) optimizing the retailer's action using the backward induction method. Experimental results demonstrate the effectiveness of the proposed game theoretic modeling and optimization framework.