Solution (source code)

= Solution

Let $B_t=e^{rt}$ and let $\psi_t$ be the bank-account holding. Then $X_t=\psi_tB_t+\theta_tS_t$. The <self-financing portfolio> condition gives
$$
\boxed{dX_t=\psi_t\,dB_t+\theta_t\,dS_t
=r(X_t-\theta_tS_t)\,dt+\theta_t\,dS_t.}
$$