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