We address a problem of stochastic optimal control motivated by portfolio optimization in mathematical finance, the goal of which is to minimize the expected value of a general quadratic loss function of the wealth at close of trade when there is a specified convex constraint on the portfolio, together with a specified almost-sure lower-bound on intertemporal wealth over the full trading interval. A precursor to the present work, by Heunis (Ann Financ 11:243–282, 2015), addressed the simpler problem of minimizing a general quadratic loss function with a convex portfolio constraint and a stipulated almost-sure lower-bound on the wealth only at close of trade. In the parlance of optimal control the problem that we shall address here exhibits the combination of a control constraint (i.e. the portfolio constraint) together with an almost-sure intertemporal state constraint (on the wealth over the full trading interval). Optimal control problems with this combination of constraints are well known to be quite challenging even in the deterministic case, and of course become still more so when one deals with these same constraints in a stochastic setting. We nevertheless find that an ingenious variational approach of Rockafellar (Conjugate duality and optimization, CBMS-NSF series no. 16, SIAM, 1974), which played a key role in the precursor work noted above, is fully equal to the challenges posed by this problem, and leads naturally to an appropriate vector space of dual variables, together with a dual functional on the space of dual variables, such that the dual problem of maximizing the dual functional is guaranteed to have a solution (or Lagrange multiplier) when the problem constraints satisfy a simple and natural Slater condition. We then establish necessary and sufficient conditions for the optimality of a candidate wealth process in terms of the Lagrange multiplier, and use these conditions to construct an optimal portfolio. © 2017, Springer-Verlag GmbH Germany.