Capital market equilibrium with externalities, production, and heterogeneous agents