after reasearching all troops do you still need the building required to buid them
No you don't. Unless it's noble then yes.
Ofc, you need the training building, so horses need stable, but once you have researched HC you can continue building HC with a level 2 stable.
"optimum" hq levels depends on building levels. There is no general level at which hq's become "optimum", that's just silly.
Technically isn't the optimum level dependant on what you want?
Adjective: Most conducive to a favorable outcome; best.
Noun: The most favorable conditions or level for growth, reproduction, or success.
On the frontline where you want your walls to be rebuilt the fastest possible that would be level 30. Backline the optimum balance is a matter of opinion more than anything, one person might like level 22 and another might prefer 24.
Im being relative in terms of keeping a reasonable farm level, as well as rebuilding quickly, a level 30 HQ is a waste of farm space.