The unpredictable nature of renewable energies is drawing attention to lithium-ion batteries. In order to make full utilization of these batteries, some research works are focused on the management of existing systems, while others propose sizing techniques based on business models. However, in order to optimise the global system, a comprehensive methodology that considers both battery sizing and management at the same time is needed. This paper proposes a new optimisation algorithm based on a combination of dynamic programming and a region-elimination technique that makes it possible to address both problems at the same time. This is of great interest, since the optimal size of the storage system depends on the management strategy and, in turn, the design of this strategy needs to take account of the battery size. The method is applied to a real installation consisting of a 100 kWp rooftop photovoltaic plant and a Li-ion battery system connected to a grid with variable electricity price. Results show that, unlike conventional optimisation methods, the proposed algorithm reaches an optimised energy dispatch plan that leads to a higher net present value. Finally, the tool is used to provide a sensitivity analysis that identifies key informative variables for decision makers.