This paper proposes a probabilistic optimization method that produces optimal bidding curves to be submitted by an aggregator to the day-ahead electricity market and the intraday market, considering the flexible demand of his customers (based in time dependent resources such as batteries and shiftable demand) and taking into account the possible imbalance costs as well as the uncertainty of forecasts (market prices, demand, and renewable energy sources (RES) generation). The optimization strategy aims to minimize the total cost of the traded energy over a whole day, taking into account the intertemporal constraints. The proposed formulation leads to the solution of different linear optimization problems, following the natural temporal sequence of electricity spot markets. Intertemporal constraints regarding time dependent resources are fulfilled through a scheduling process performed after the day-ahead market clearing. Each of the different problems is of moderate dimension and requires short computation times. The benefits of the proposed strategy are assessed comparing the payments done by an aggregator over a sample period of one year following different deterministic and probabilistic strategies. Results show that probabilistic strategy reports better benefits for aggregators participating in power markets.