<p>Let <span class="math-container">$b_1,\ldots, b_T$</span> and <span class="math-container">$s_1, \ldots, s_T$</span> 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.</p>
<p>Assume we start at period 1 where all wealth is liquid and end at period <span class="math-container">$T$</span> where all wealth is again liquid. Then the optimal trade will exists of a number of periods
<span class="math-container">$$
1 \le t_1 &lt; t_2 &lt; \ldots &lt; t_{2k - 1}&lt; t_{2k} \le T.
$$</span>
where at every odd period, the entire wealth is invested in stocks and at every even period, wealth is converted to liquid. If <span class="math-container">$w$</span> is the initial wealth, the final wealth is given by:
<span class="math-container">$$
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}}.
$$</span></p>
<p>This value is maximized by maximizing
<span class="math-container">$$
\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}}.
$$</span></p>
<p>Let <span class="math-container">$V(t, v)$</span> be the maximal value over all such sequences that satisfy the contraint <span class="math-container">$t \le t_1 &lt; t_{2k} \le v$</span>.</p>
<p>Then we have the recursion (for <span class="math-container">$t \le v$</span>):<span class="math-container">$$
V(t,v) = \max\left\{\begin{array}{l}1,\\
\dfrac{s_{v}}{b_{t}},\\
 \max_{t &lt; t_1 &lt; t_2 \le v} \left(V(t, t_1 - 1) \times \dfrac{s_{t_2}}{b_{t_1}} \right),\\
\max_{t \le t_1 &lt; t_2 &lt; v} \left( \dfrac{s_{t_2}}{b_{t_1}} \times V(t_2 + 1, v)\right),\\ 
\max_{t &lt; t_1 &lt; t_2 &lt; 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\}
$$</span>
The first argument captures the possibility that there is no buying and selling between periods <span class="math-container">$t$</span> and <span class="math-container">$v$</span>. The second argument looks at the case where one buys at <span class="math-container">$t$</span>, sells at <span class="math-container">$v$</span> and there is no intermediate buying and selling between these periods. The other parts capture the recursion. The second case is only relevant if <span class="math-container">$t &gt; v$</span>. The 3rd and 4th case are only relevant if <span class="math-container">$v &gt; t+1$</span> while the last one is only relevant if <span class="math-container">$v &gt; t+2$</span>.</p>
<p>The running time is <span class="math-container">$O(T^4)$</span> (we need to compute no more than <span class="math-container">$T^2$</span> values <span class="math-container">$V(t,v)$</span> each optimizing over no more than <span class="math-container">$T^2$</span> possible intermediary values).</p>

CC BY-SA 4.0
2024-02-26T11:42:48.093Z
2024-02-26T11:42:48.093Z
tdm
<p>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.)</p>
<p>The following two plots show the ask and bid price in the total period of time and in a zoomed view at the beginning.</p>
<p><a href="https://i.sstatic.net/aLhv2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aLhv2.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/pKtR4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pKtR4.png" alt="enter image description here" /></a></p>
<p>My original approach was to just use brute force, i.e. compute the amount of money earned through all possible strategies combining the &quot;Buy&quot; and &quot;Sell&quot; points as in the following plot:</p>
<p><a href="https://i.sstatic.net/9QHVq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9QHVq.png" alt="enter image description here" /></a></p>
<p>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.</p>

CC BY-SA 4.0
2024-02-25T01:57:08.763Z
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.
mark leeds
CC BY-SA 4.0
2024-02-25T12:13:43.123Z
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.
mark leeds
CC BY-SA 4.0
2024-02-25T14:31:02.140Z
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.
Giskard
<optimization><trading>
Finding the optimal trading strategy
user171780
# Finding the optimal trading strategy

(Asked by: username_0 on 2024-02-24)

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](https://i.sstatic.net/aLhv2.png)](https://i.sstatic.net/aLhv2.png)
[![enter image description here](https://i.sstatic.net/pKtR4.png)](https://i.sstatic.net/pKtR4.png)

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](https://i.sstatic.net/9QHVq.png)](https://i.sstatic.net/9QHVq.png)

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.

**Tags:** optimization, trading

> **Comments:**
> **username_1**: 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.  
> **username_1**: 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.  
> **username_2**: 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.  

---

## 1 answers

### Answer by username_3 on 2024-02-26. Score: 1

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).


---

