This paper presents an energy management algorithm to be embedded for a residential application disconnected from the power grid. The multisource system studied includes solar panels as a renewable energy source, a fuel cell as a secondary source, three batteries and a bank of supercapacitors as storage systems. The proposed algorithm uses as input data, future estimates obtained from power consumption and meteorological forecasting data, and historical of the load power and the renewable power obtained from measurements. It is assumed that these two input powers are imposed and uncontrollable. In the case where the future estimates have errors compared to what has actually been measured, a mathematical approach shows that the algorithm is able to compensate these forecasting errors by sharing them between the different sources of the system while respecting their different characteristics. In addition to the optimal energy distribution, the algorithm gives the optimal size of each source and storage devices. In this work, the total cost of the system is chosen as the criteria to be optimized. The simulations are carried out over a year, with a time step of 1 second and in the presence of significant forecasting errors. The results obtained are particularly convincing and make it possible to validate this energy management strategy.