Both indices spent much of the morning in negative territory before a sharp buying surge after 2 pm turned the tide