a bit of info from Stacktrace...
I know that Stacktrace is not exactly scientific, but according to their stats, the average software developer is about 28.5 years old. Half of all developers leave the field before age 40. (!!!!!) Just three percent of developers are aged 50-60, and just 0.8% of developers are over age 60.
I am in a small company(now, from HP ten years ago where I was ageism laid off) where a few of us in the over-55 age range know a LOT of the underlying system and the subject matter. Younger guys have come and gone, and never learned the underlying business logic. Thus, they were relegated to UI and other areas where business logic was unnecessary. We are in a place where our knowledge (over the new and shiny) is valued.
The most important thing is to not take a position that pigeon-holes you in a dying technology. Find a shop open to new technology, and keep learning.