Large scale electricity storage is set to play an increasingly important role in the management of future energy networks. A major aspect of the economics of such projects is captured in arbitrage, i.e. buying electricity when it is cheap and selling it when it is expensive. We consider a mathematical model which may account for nonlinear---and possibly stochastically evolving---cost functions, market impact, input and output rate constraints and both time-dependent and time-independent inefficiencies or losses in the storage process. We develop an algorithm which is maximally efficient in the sense that it incorporates the result that, at each point in time, the optimal management decision depends only a finite, and typically short, time horizon. We give examples related to the management of a real-world system. Finally we consider a model in which the associated costs evolve stochastically in time. Our results are formulated in a perfectly general setting which permits their application to other commodity storage problems.