Stocks alternated between gains and losses, but ended the day higher.