In response to the Global Financial Crisis of 2007–2009, by now, most of the financial transactions must be cleared through central counterparties operating a dynamic margin setting mechanism. High margin calls can reduce counterparty risk in a turbulent market, but at the same time, increase liquidity risk and escalate systemic risk. In this paper, we construct a theoretical model to address this challenge, deriving an optimal margin setting policy framed as a stochastic control problem. Our analysis reveals that an adaptive, countercyclical approach is superior to a purely risk-sensitive strategy, primarily by minimizing the expected loss for the clearing institution.