1
$\begingroup$

Suppose there is one asset that you can buy and sell, and that you know what was the selling price and buying price at all times between times t0 and t1, both in the past. If you would have had an initial amount of money, how would you find which would have been the ideal trading strategy? I.e. the set of times at which to buy and sell the asset to maximize the income. (Assuming perfect market liquidity and instant transactions.)

The following two plots show the ask and bid price in the total period of time and in a zoomed view at the beginning.

enter image description here enter image description here

My original approach was to just use brute force, i.e. compute the amount of money earned through all possible strategies combining the "Buy" and "Sell" points as in the following plot:

enter image description here

Though this works, it is extremely expensive computationally. Is there a more efficient algorithm to perform this kind of calculation? If so, details or references would be appreciated, I am an outsider from physics and engineering.

$\endgroup$
3
  • $\begingroup$ Hi: Brute force is not a bad idea IMHO. you can cut down the number of cases you are looking at by buying or selling into trends. Since you know the data beforehand, you can stop before the trend reverses and bites you. That should cut down on a great deal of possibilities, atleast in the case, where there is trending. $\endgroup$
    – mark leeds
    Commented Feb 25 at 1:57
  • $\begingroup$ Hi: I just re-visited this question and had another possible idea. You can try dynamic programming where you start from the last trade of the day and work backwards. That too would reduce the number of possibilities to consider. If you are not familiar with it, the concept of dynamic programming will be explained in any decent operations research text. $\endgroup$
    – mark leeds
    Commented Feb 25 at 12:13
  • $\begingroup$ Is the strategy displayed in your final graph the optimal strategy arrived at by brute force? There seems to be several instances where it buys stock shortly before a drop and sells afterwards, e.g. at time 50. $\endgroup$
    – Giskard
    Commented Feb 25 at 14:31

1 Answer 1

1
$\begingroup$

Let $b_1,\ldots, b_T$ and $s_1, \ldots, s_T$ be the buying and selling prices. If we know the prices, it will be optimal to hold at every point in time either the entire wealth in stocks or liquid. Assume that we cannot buy and sell at the same point in time.

Assume we start at period 1 where all wealth is liquid and end at period $T$ where all wealth is again liquid. Then the optimal trade will exists of a number of periods $$ 1 \le t_1 < t_2 < \ldots < t_{2k - 1}< t_{2k} \le T. $$ where at every odd period, the entire wealth is invested in stocks and at every even period, wealth is converted to liquid. If $w$ is the initial wealth, the final wealth is given by: $$ w \times \frac{s_{t_2}}{b_{t_1}}\times \frac{s_{t_4}}{b_{t_3}} \times \ldots \times \frac{s_{t_{2k}}}{b_{2k-1}}. $$

This value is maximized by maximizing $$ \frac{s_{t_2}}{b_{t_1}} \times \frac{s_{t_4}}{b_{t_3}} \times \ldots \times \frac{s_{t_{2k}}}{b_{2k-1}}. $$

Let $V(t, v)$ be the maximal value over all such sequences that satisfy the contraint $t \le t_1 < t_{2k} \le v$.

Then we have the recursion (for $t \le v$):$$ V(t,v) = \max\left\{\begin{array}{l}1,\\ \dfrac{s_{v}}{b_{t}},\\ \max_{t < t_1 < t_2 \le v} \left(V(t, t_1 - 1) \times \dfrac{s_{t_2}}{b_{t_1}} \right),\\ \max_{t \le t_1 < t_2 < v} \left( \dfrac{s_{t_2}}{b_{t_1}} \times V(t_2 + 1, v)\right),\\ \max_{t < t_1 < t_2 < v} \left( V(t,t_1 - 1) \times \dfrac{s_{t_2}}{b_{t_1}} \times V(t_2 + 1, v) \right) \end{array}\right\} $$ The first argument captures the possibility that there is no buying and selling between periods $t$ and $v$. The second argument looks at the case where one buys at $t$, sells at $v$ and there is no intermediate buying and selling between these periods. The other parts capture the recursion. The second case is only relevant if $t > v$. The 3rd and 4th case are only relevant if $v > t+1$ while the last one is only relevant if $v > t+2$.

The running time is $O(T^4)$ (we need to compute no more than $T^2$ values $V(t,v)$ each optimizing over no more than $T^2$ possible intermediary values).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.