Algorithm for optimal bidding of energy storage into energy markets