# HW02: Trading strategies using technical indicators
## Instructional Video
https://www.youtube.com/watch?v=i1DisY9ARzA
## Goal
In this assignment, you are required to design a stock trading strategy using the technical indicators (TIs) covered in class. You will then upload your strategy to Gradescope to compete with your classmates.
We will provide a public dataset for your testing. The final score will be calculated as the average of your performance on both the public dataset and a private dataset (i.e., (public_score + private_score) / 2).
## File List
- [bestParamByExhaustiveSearch.py](https://drive.google.com/file/d/1cZtPDp2PqikOufNOiSdkt27waiQOXgaQ/view?usp=sharing)
- [myStrategy.py](https://drive.google.com/file/d/1IgAzWFLXEkAlFhsO96vW8rVn9W2w3ptk/view?usp=sharing)
- [readme.txt](https://drive.google.com/file/d/1ZcOidMT7GPntVo5fxv9kGC7SK72Kj-sz/view?usp=sharing)
- [rrEstimate.py](https://drive.google.com/file/d/1gXsH3Dkxwq6yxVE5Iy0cT-BiIJt71WG4/view?usp=sharing)
- [public.csv](https://drive.google.com/file/d/1McOubLdeKfRIQoDoUEDzOa5qAFGC5C6a/view?usp=sharing)
## Design Flow Example
1. Choose the technical indicator(s) you want to use (or design your own).
2. Define the trading strategy based on these indicators, including modifiable parameters for optimization.
3. Optimize the parameters using historical price data.
4. Apply the optimized parameters to evaluate performance over a longer period.
**Example:**
In the sample code `bestParamByExhaustiveSearch.py`, we maximize the return rate on *public.csv* via exhaustive search:
```bash
python bestParamByExhaustiveSearch.py public.csv
```
After a while, the best settings with maximum return rate will be found:
```
Best settings: fast_period=8, slow_period=24, signal_period=9 ==> returnRate=0.007356
```
Then you can put these best parameters into the program myStrategy.py , and try the trading strategy on the same dataset by typing
```
python rrEstimate.py public.csv
```
You should see:
```
rr=0.735616%
```
## Notes
- Your trading strategy can only use historical prices available up to the current day to make buy/sell decisions.
- You only need to submit the function myStrategy.py
- Only technical indicators are allowed. Machine learning, DP, or other methods are not permitted. (Gradescope does not provide GPU resources.)
- Each call to myStrategy() must finish within 0.01 sec, otherwise it will be terminated and the default output will be 0.
- Scoring is based on the average of your return rates on both public and private datasets (each approximately 2,400 days).
- The results for the public dataset will be displayed immediately, while the results for the private dataset will be announced after the assignment deadline.
- The last submission on Gradescope before the deadline will be used for grading.
- On Gradescope, the only available packages are:
- pandas==2.2.2
- numpy==2.1.1
- scipy==1.14.1
- statsmodels==0.14.2
- Do NOT use random numbers in your strategy
- Strategies must be deterministic (same input โ same output)
- Using random will result in disqualification
## FAQ
Questions from students will be added to this FAQ if necessary for other students' reference.
### Q1: How is the autograder score calculated?
A: The score is calculated based on your Return Rate using a predefined scoring table (not publicly disclosed).
Current Score Display:
+ Before deadline: Only shows your score on the public dataset
+ After deadline: Final score = (public_score + private_score) รท 2
### Q2: What does "Public Time Used" mean? Is my strategy being penalized for timeouts?
A: "Public Time Used" represents the total time for the **entire** backtesting process, not individual myStrategy() calls.
Time Limits:
+ Individual call limit: **Each** myStrategy() call must complete within 0.01 seconds
+ If timeout occurs: That specific call returns action=0 (hold)
### Q3: Can I use Machine Learning or Neural Networks in my strategy?
A: It depends on where you use it:
+ In submitted myStrategy.py: โ NOT ALLOWED
As stated in the assignment requirements, Machine Learning/Neural Network models are NOT permitted, even if implemented using numpy.
+ For parameter optimization: โ ALLOWED
You can use any method (including ML/NN) to find optimal parameters for your technical indicators.
### Q4: Why did I get exactly 60 points?
A: A score of 60 points typically indicates one of the following issues:
+ Your strategy's return rate is below our simple baseline
+ Incorrect filename (must be exactly myStrategy.py - case sensitive)
+ File not uploaded successfully
+ Syntax errors preventing execution
+ Import errors or missing dependencies