If You Knew Tomorrow’s Stock Prices—— Dynamic Programming in Trading
Introduction
Imagine a world where the future is no longer a mystery. Before the market opens, you already know the exact stock prices of every day in the following months. Would becoming a perfect trader finally become easy?
Surprisingly, the answer is still no.
Knowing the future removes uncertainty, but it does not remove the difficulty of decision-making. When every choice changes the possibilities of future choices, finding the optimal strategy becomes an algorithmic problem.
This article shows how dynamic programming turns an overwhelming number of possible strategies into a tractable optimization problem.
From Trading Decisions to Mathematical Models
Suppose we know the stock prices for the next days.
On day :
- the buying price of one stock is ;
- the selling price of one stock is ;
- we can buy at most stocks;
- we can sell at most stocks.
There are also two restrictions:
-
The number of stocks we hold cannot exceed .
-
After completing a transaction, we must wait for days before making another transaction.
The goal is to maximize the final profit.
A straightforward idea is to enumerate every possible sequence of buying and selling. However, the number of possible strategies grows exponentially with time, making brute force impossible.
The key observation is that many different trading histories can lead to exactly the same situation.
For example, suppose two strategies arrive at day while holding the same number of stocks. From this moment onward, they face exactly the same choices. Therefore, if one strategy has already earned less profit than the other, it will never become better in the future.
This leads to the central idea of dynamic programming:
We do not need to remember every path that happened before. We only need to remember the information that affects future decisions.
Defining the State
To describe a trading situation, we only need two pieces of information:
- the current day;
- the number of stocks currently held.
Let represent the maximum profit after day , when we own exactly stocks.
Initially, we have no stocks and no profit:
All unreachable states are assigned:
because they cannot contribute to any valid solution.
Now the original problem is transformed into a process of continuously updating these states.
State Transition
On each day, there are three possible actions.
1. Do Nothing
If we do not trade today, the number of stocks remains unchanged:
2. Buying Stocks
Because of the waiting period, if we buy on day , the previous transaction must have happened before day:
Assume that before buying we owned stocks. After buying stocks, our profit becomes:
Therefore:
where:
3. Selling Stocks
Similarly, if we sell stocks on day :
where:
These transitions completely describe all possible decisions.
However, directly enumerating every possible requires:
time, which is too slow for large constraints.
Optimizing with Monotonic Queue
The reason the transition is slow is that we repeatedly search for the maximum value inside a moving range.
Consider the buying transition:
It can be rewritten as:
For a fixed day , the term:
is constant.
Therefore, we only need to find the maximum value of:
inside a valid interval of .
As increases, this interval moves continuously, which creates a sliding window maximum problem.
A monotonic queue maintains the best candidates efficiently:
- outdated states are removed from the front;
- states that can never become optimal are removed from the back;
- the maximum candidate is always kept at the front.
Since every state enters and leaves the queue at most once, each transition is processed in linear time.
The final complexity is reduced to:
which makes the problem solvable even for much larger inputs.
The Bigger Picture
The stock market scenario is only a metaphor for a much broader class of problems in computer science.
The real challenge is not always the lack of information. Sometimes, even when all information is available, the number of possible decisions is simply too large to explore directly.
Dynamic programming provides a way to handle this complexity. It divides a difficult problem into smaller states, identifies repeated structures, and stores only the information that can influence future decisions.
In this problem, the algorithm does not search for the best individual transaction. Instead, it considers the entire sequence of decisions and proves that the optimal answer can be constructed from optimal states that have already been solved.
This idea appears in many areas beyond trading. Route planning, resource allocation, artificial intelligence, and biological data analysis all contain problems where many possible choices share common structures.
The most important lesson is that algorithms do not create information or predict the future. They organize knowledge and reveal hidden patterns.
Even when tomorrow’s stock price is already known, the hardest part is still deciding what to do.
And that is exactly where algorithms begin to think.