I notice that Psalm 18 is very similar to the song sung by David in 2 Samuel 22 at the end of his life. Maybe he wrote this Psalm as a young man and returned to it with gratitude at the end of his life.