  The Markowitz problem consists of finding in a financial market a
self-financing trading strategy whose final wealth has maximal mean and minimal
variance. We study this in continuous time in a general semimartingale model
and under cone constraints: Trading strategies must take values in a (possibly
random and time-dependent) closed cone. We first prove existence of a solution
for convex constraints by showing that the space of constrained terminal gains,
which is a space of stochastic integrals, is closed in L^2. Then we use
stochastic control methods to describe the local structure of the optimal
strategy, as follows. The value process of a naturally associated constrained
linear-quadratic optimal control problem is decomposed into a sum with two
opportunity processes L^{\pm} appearing as coefficients. The martingale
optimality principle translates into a drift condition for the semimartingale
characteristics of L^{\pm} or equivalently into a coupled system of backward
stochastic differential equations for L^{\pm}. We show how this can be used to
both characterise and construct optimal strategies. Our results explain and
generalise all the results available in the literature so far. Moreover, we
even obtain new sharp results in the unconstrained case.
