US stock markets close week with gains