Necessary and sufficient conditions for existence of an optimal portfolio