This paper investigates the optimal capacity of distributed renewable generators and battery storage system for a grid-tied residential microgrid under real-time pricing (RTP). Three system configurations by combination of solar photovoltaic (PV), wind turbine (WT) and battery energy storage system (BESS) are optimally sized by minimizing microgrid's cost of electricity (COE) as the objective function. The residential microgrid imports the electricity from the main grid under an RTP program and exports the electricity under a flat rate. Real annual data set of electricity consumption of a residential microgrid, RTP electricity rate, wind speed, temperature and insolation in South Australia is incorporated in the optimization model. An uncertainty analysis based on the stochastic behaviour of RTP rates and load consumption is provided to validate the optimization results. It is found that, under an RTP rate, the PV-WT-BESS microgrid is the cost-effective system with 26% and 2% COE reductions compared to WT-BESS and PV-BESS systems, respectively.