I got a message from my future self. Rather than a sports almanac, it contained minute-by-minute data for tomorrow's prices on a few select stocks. I want to use this to earn a bit of money. Is there an efficient way to decide when to sell and buy which stocks in order to maximize profits?
An example: Say I got the following price data: $$ \begin{array}{|c|rr|rr|rr|}\hline \text{Time}& \text{Company 1}&&\text{Company 2}&&\text{Company 3}\\ &\text{Sell}&\text{Buy}&\text{Sell}&\text{Buy}&\text{Sell}&\text{Buy}\\\hline 00:00:00&1.23&1.25&12.48&12.57&7.93&8.01\\ \vdots&\vdots&\vdots&\vdots&\vdots&\vdots&\vdots&\\ 23:59:59&1.03&1.12&15.93&15.97&10.30&10.39\\\hline \end{array} $$ Now, assuming we can actually buy and sell for these exact prices at the given times (which is an approximation), how can one most efficiently navigate these data to end the day with as much money as possible? Are there any known algorithms for these? What computational complexity do you get?
I think for the case of a single company I can do linear in the number of minutes: search forward for first local minimum of buy at $b_0$. Between there and the next time the buy price is lower ($b_1$), if there is ever a sell price higher than $b_0$, then buy at $b_0$, sell as high as you can before $b_1$, and if no sell price in that time interval is higher than $b_0$, then don't buy. Then continue searching for local buy minimums from $b_1$ onward.
But for more than one company I'm not even sure how I would systematically look for the optimal solution, other than full brute force. And that quickly becomes unwieldy and exponential. Is there a more efficient algorithm?
Specifics:
- I don't have to buy whole stocks. I can buy parts of one. Let's say any positive real number is valid, as long as I can afford it.
- No shorting. I can't sell stocks I haven't bought earlier in the day.
- I have to end up with money, not stocks. Sell before the day is over, at the latest using the $23:59:59$ prices.
- I can sell for a given sell price, and then buy for a buy price within the same minute.