Due date: 2024/11/23 23:59:59
Optimum Trading Strategy with Complete Price Info
Suppose we have only a single stock to trade, and all its prices are known in advance. In this case, we can use dynamic programming (DP) to achieve the best return rate, as explained in the class
(
).
In this homework, we will extend this task to include multiple stocks. In other words, you need to determine the best timing for 'what to buy' and 'what to sell' across different stocks to maximize the overall return. The TA will use a dataset to rank your return rate based on your submissions. Your function, submitted to the judging system, should follow the input/output format below:
actionMat = myAction01(priceMat, transFeeRate);
where
- priceMat: An $m \times n$ matrix which holds $n$ stocks' price over $m$ days. That is, each of column is the price vector of $m$ days for a specific stock.
- transFeeRate: Rate for transaction fee, which is usually 1/100.
- actionMat: An $k \times 4$ action matrix which holds $k$ transaction records. In particular, each row of $[d, a, b, z]$ represents a transaction record, as explained next:
- $d$: The index of day, starting from 0 with monotonically non-decreasing values.
- $a$: The index of "from" stock, with "-1" being "cash" and all the other integers being stock index (within $[0, n-1]$).
- $b$: The index of "to" stock, with "-1 being "cash" and all the other integers being stock index (within $[0, n-1]$).
- $z$: The equivalent cash for this transaction. This value must be positive.
For instance:
- [5, -1, 7, 100]: At day 5, use cash of $100(1-\rho)$ dollars to buy stock 7, where $\rho$ is the rate for transaction fee.
- [3, 8, -1, 50]: At day 3, sell stock 8 to have cash of $50(1-\rho)$.
- [12, 6, 9, 80]: At day 12, sell stock 6 and buy stock 9. Such a transaction between stocks incurs two-time transaction fees. That is, the cash equivalent of stock 6 you sell is $80(1-\rho)$, and that of stock 9 you buy is $80(1-\rho)^2$.
For example, we can demonstrate how everything works by using a simple greedy strategy, as shown below:
- Buy if the price increases the next day. If multiple stocks are rising, choose the one with the largest increase.
- Sell if the price decreases the next day. If multiple stocks are falling, choose the one with the largest drop."
To estimate the return rate, we will need two files:
- myAction.py: A collection of different trading strategies, including the simple greedy one (as explained earlier) in myActionSimple(). Other functions, such as myAction01(), myAction02(), and myAction03(), are to be implemented by you.
- rrEstimateOpen.py: The main program that uses myActionSimple() to estimate the return rate.
To run the main program using the simple greedy approach on 4 stocks over 992 days (stored at priceMat0992.txt), you can enter the following command:
python rrEstimateOpen.py priceMat0992.txt 0.01
The result will look like this:
578094.413707%
Now you need to implement three functions in myAction.py to meet the following requirements:
- myAction01(): This function should implement any strategy to optimize the return. While you can use any approach (such as the previously mentioned greedy strategy), a dynamic programming (DP) method is expected to yield the best result.
- myAction02(): In this function, you need to optimize the overall return while adhering to the constraint that you must hold cash for at least K days throughout the investment process. For example, if K = 30, you must spend at least 30 days holding cash (without holding any stocks) by the end of each of these days. (The last day counts as one of them.)
- myAction03(): This function should optimize the overall return under the constraint that you must hold cash for at least K consecutive days during the investment process. For example, if K = 30, you must have at least 30 consecutive days holding cash (without holding any stocks) at the end of each of those days.
Once you have completed these functions, you can use rrEstimateOpen02.py to test them:
python rrEstimateOpen02.py priceMat0992.txt 0.01
The result will appear like this:
Reading priceMat0992.txt...
------------Problem 1-------------
Time: 0.0
rr=0.000000%
Non continueous cash holding=992
Continueous cash holding=992
------------Problem 2-------------
Time: 0.0
rr=0.000000%
Non continueous cash holding=992
Continueous cash holding=992
------------Problem 3-------------
Time: 0.0
rr=0.000000%
Non continueous cash holding=992
Continueous cash holding=992
Keep in mind that rrEstimateOpen02.py is also the main program used by the TA to evaluate your submission. Make sure to run this program to test your three functions in myAction.py before submitting the file.
Note that:
- All example programs can be found in the file list of example code to help you get started.
- We assume that the prices are available at the beginning of each day, and you can use them to 'buy' and 'sell,' with every transaction guaranteed.
- Transactions can also be made on both the first and last days.
For submission:
- You only need to submit "myAction.py".
- Your score for his homework is based on the sum of your rankings of myAction01~03, as follows.
- myAction01(): Ranking is based on the return rate.
- myAction02(): Ranking is based on the average return rate when K is around 200, 300, and 400, respectively.
- myAction03(): Ranking is based on the average return rate when K is around 200, 300, and 400, respectively.
- If the returned action matrix does not meet the 'cash-holding constraints' required by myAction02 and myAction03, the return rate will automatically be set to zero.
- The TA will use a hidden dataset of 4 stocks with a similar time span to evaluate your trading strategies.
Hints by Roger:
- For myAction01(), DP is applicable.
- For myAction02(), DP can still be applied, but you'll need to carefully design the optimal value function and its recurrence formula.
- For myAction03(), DP might still be applicable, but I¡¦m not entirely sure, as I haven't spent enough time working out the details.
Hints by TA:
- You are allowed 10 submissions per day. The limit will reset at midnight, and between 12:00 AM and 12:15 AM, the TA will check the judge system for any issues and reset the submission limit. During this time, students may not be able to access the system.
- The time limit for myAction01() is 2 seconds, with a memory limit of 256MB.
- The time limit for myAction02() is 100 seconds, with a memory limit of 1GB.
- The time limit for myAction03() is 100 seconds, with a memory limit of 256MB.
- You may need to wait for other students to finish their submissions before yours is processed, so please be patient.
- FAQ by TAs