Solution (source code)

= Solution

Applying the <Itô product rule> to $YB$ makes its drift vanish automatically. For $YS$, the drift is
$$
YS(\mu-r-\lambda\sigma)\,dt.
$$
Thus both deflated prices are local martingales when
$$
\lambda=\frac{\mu-r}{\sigma}.
$$
Since self-financing gives $dX=\phi\,dB+\pi\,dS$, another application of the product rule, including $d[X,Y]$, cancels the drift and yields
$$
d(X_tY_t)
=Y_t(\pi_tS_t\sigma-X_t\lambda)\,dW_t.
$$