# Optimal trading strategy in toy world of simple Hidden Markov model with Gaussians

I want to solve the following optimization problem: What is the optimal general trading strategy (in the sense of the highest Sharpe ratio) on a time series which is the result of a Hidden Markov model with two states and two Gaussians with a known transition matrix and known mean and variance parameters?

The trading strategy should ideally be based only on easily observable technical indicators like intrinsic momentum or SMAs and volatility. My gut feeling is that the optimal lookback period for calculating these must depend on the transition matrix.

I am grateful (as usual) for ideas, literature, code (ideally R :-) and the like

-
Don't you need to specify what optimal means? –  Bob Jansen Dec 11 '13 at 9:04
@BobJansen: Fair question. Let's stick with the industry standard, i.e. Sharpe ratio. I will edit the question accordingly. Thank you. –  vonjd Dec 11 '13 at 10:23

I don't understand how technical indicators are at all relevant to the question. State probabilities can be generated directly from the returns if the model is known. There is no need to guess at heuristic trading rules based on technical indicators.

Let $r_t$ be the return at time $t$. Your model is

1. $E\{r_t | s_t=i\} \sim N(\mu_i,\sigma^2_i), i=0,1$
2. $P\{s_t=i | s_{t-1}, s_{t-2}, ...\} = P\{s_t=i|s_{t-1}\}$

In other words, the state is Markov and returns are normal with known mean, variance in either state. Suppose we are standing at time $t$. We need to first determine $P\{s_t = 0 | r_{t-1}, ..., r_0\} = p_t$. Use the forward backward algorithm aka dynamic programming and implemented in most HMM packages.

Now $$E\{r_t\} = p_t\mu_0 + (1-p_t)\mu_1$$ and $$Var\{r_t\} = p_t\sigma_0^2 + (1-p_t)\sigma_1^2.$$

Now we need to choose our position $x$ to maximize the Sharpe

$$\frac{E\{x'r\}}{\sqrt{Var\{x'r\}}}$$

This is equivalent (up to a scaling factor) to the mean-variance problem

$$\min_x \{\lambda x' \Sigma x - x'\bar{r} \},$$ where $\Sigma$ is a diagonal matrix with $Var\{r_t\}, t= 0,1,...,T$ on the diagonal and $\bar{r} = E\{r\}$. The proof of this fact is by contradiction. Suppose there is an $x$ with higher Sharpe that isn't the solution to the mean-variance problem, $x^*$. We can scale $x$ by a positive constant $\alpha$ so that we have $\alpha \bar{r}'x = \bar{r}'x^*$ . At the same time, we know that $$\alpha\sqrt{x'\Sigma x} < \sqrt{(x^*) ' \Sigma x^*}$$ because $x^*$ isn't Sharpe-optimal. Squaring both sides gives

$$\alpha^2 x'\Sigma x < (x^*) ' \Sigma x^*.$$

The fact that $\alpha x$ has the same mean and strictly lower variance contradicts the assumption that $x^*$ is the solution. We thus conclude that the mean variance solution is always Sharpe optimal. Note that in more general problems (e.g. with constraints) this equivalence doesn't necessarily hold. Now the solution to the mean-variance problem (take derivative and set to zero) is just $\frac{1}{2 \lambda}\Sigma^{-1} \bar{r}$. However, at time $t$ we don't have to worry about anything other than $x_t$. It is possible to compute $x$ for future times that are optimal with respect to current expectations, but in practice it is better to re-run the forward-backward algorithm after we observe the next return and then re-compute $x_{t+1}$. The optimal solution therefore is to bet proportionally to $\frac{E\{r_t\}}{Var\{r_t\}}$. This has an intuitive interpretation as $$\frac{E\{r_t\}}{Var\{r_t\}} = \frac{E\{r_t\}}{\sqrt{Var\{r_t\}}} \frac{1}{\sqrt{Var\{r_t\}}}$$ so that $$x_t\sqrt{Var\{r_t\}} = \frac{E\{r_t\}}{\sqrt{Var\{r_t\}}},$$ i.e. take risk proportional to expected Sharpe at each time.

If you have transaction costs then you need to consider future means and variances, which makes the problem more difficult, but doable.

-
nice answer but can't we specify $p_t$ more exactly using the densities? Moreover above I would not write $E\{\dots\}$ for the distribution. –  Richard Jun 12 at 7:00
Note that if $\mu_0 = \mu_1$ then you get zero autocorrelation of returns - which is often the case (look here in Rogers and Zhang (2.2) –  Richard Jun 12 at 7:03
Thank you and +1. You say "It is easy to show" - could you please do so or give a reference? –  vonjd Jun 12 at 7:44
Added detailed proof of Sharpe ratio <> mean-variance equivalence. This is a standard result though. –  MichaelJ Jun 12 at 14:19
Richard- I didn't write $E\{...\}$ for any distributions. Also, I don't understand what your point is RE: $\mu_0 = \mu_1$. In that case the solution is a constant, but this is just a special case. In any event, since the OP is trading based on an HMM, it is quite unlikely that $\mu_0 = \mu_1$ here. –  MichaelJ Jun 12 at 14:24