We consider the problem of optimally placing market orders so as to minimize the expected liquidity costs from buying a given amount of shares. The liquidity price impact of market orders is described by an extension of a model for a limit order book with resilience that was proposed by Obizhaeva and Wang (2006). We extend their model by allowing for a time-dependent resilience rate, arbitrary trading times, and general equilibrium dynamics for the unaffected bid and ask prices. Our main results solve the problem of minimizing the expected liquidity costs within a given convex set of predictable trading strategies by reducing it to a deterministic optimization problem. This deterministic problem is explicitly solved for the case in which the convex set of strategies is defined via finitely many linear constraints. A detailed study of optimal portfolio liquidation in markets with opening and closing call auctions is provided as an illustration. We also obtain closed-form solutions for the unconstrained portfolio liquidation problem in our time-inhomogeneous setting and thus extend a result from our earlier paper .