The Nasdaq, S&P 500 and government bonds rallied on the final day of July, capping a topsy-turvy month for global markets.