Using an Oscillator in a program, I noticed that the lower and the higher frequencies are less loud than the middle ones. I suspect there is a relation between pitch and loudness but can it be calculated? e.g. can I amplify the amplitude of the oscillator to keep the loudness level equal to all frequencies?
It's human physiology based and I am not aware of any easily implementable numerical correction. The relationship is not linear or anyhow nice. The keyword for you would be equal-loudness contour. Read more about that.
Actually, if you do e.g. psychoacoustical listening tests for timbre perception, the samples are adjusted to the same "psychological loudness", i.e. measured by an ear only and amplified based on that "measurement".
If you want to model that, then you need to read about nonlinear behavior of ear drum, cochlea and basilar membrane among others. If you just want to do some sound engineering task, then move this question to SE Sound Design.