Repurchase agreements (repos) are one of the most important sources of funding liquidity for many financial investors and intermediaries. In a repo, some assets are given by a borrower as collateral in exchange of funding. The capital given to the borrower is the market value of the collateral, reduced by an amount termed as haircut (or margin). The haircut protects the capital lender from loss of value of the collateral contingent on the borrower׳s default. For this reason, the haircut is typically calculated with a simple Value at Risk estimation of the collateral for the purpose of preventing the risk associated to volatility. However, other risk factors should be included in the haircut and a severe undervaluation of them could result in a significant loss of value of the collateral if the borrower defaults. In this paper we present a stylized model of the financial system, which allows us to compute the haircut incorporating the liquidity risk of the collateral and, most important, possible systemic effects. These are mainly due to the similarity of bank portfolios, excessive leverage of financial institutions, and illiquidity of assets. The model is analytically solvable under some simplifying assumptions and robust to the relaxation of these assumptions, as shown through Monte Carlo simulations. We also show which are the most critical model parameters for the determination of haircuts.