This paper characterizes the equilibrium in a continuous time financial market populated by heterogeneous agents who differ in their rate of relative risk aversion and face convex portfolio constraints. The model is studied in an application to margin constraints and found to match real world observations about financial variables and leverage cycles. It is shown how margin constraints increase the market price of risk and decrease the interest rate by forcing more risk averse agents to hold more risky assets, producing a higher equity risk premium. In addition, heterogeneity and margin constraints are shown to produce both pro- and counter-cyclical leverage cycles. Beyond two types, it is shown how constraints can cascade and how leverage can exhibit highly non-linear dynamics. Finally, empirical results are given, documenting a novel stylized fact which is predicted by the model, namely that the leverage cycle is both pro- and counter-cyclical.