Problem 45776. Algorithmic Trading - 1 (optimize simple trading strategy)
This is the first of a series of problems on quant trading. Quant traders seek to optimize investment performance devising trading strategies (i.e. rules) that exploit patterns found in historical market data.
Creative solutions, comments and suggestions for improvement of the problem are appreciated! Please respect basic courtesy guidelines: e.g. refrain from using Cody hacks to get a leading score.
Goal
- Trade one single security, based on one single trading signal (observed variable)
- Use an all or nothing, fractional trading strategy, with no leverage nor short selling - each day invest either 100% of the funds at market open, or leave them all in cash)
- Maximize the compound annual growth return (CAGR) of your investment, over the test period
- Check <https://en.wikipedia.org/wiki/Compound_annual_growth_rate>
You are given
- P, a [n x 1] vector of daily prices of the traded security, at market open (always positive)
- S, a [n x 1] vector of trading signals, calculated just ahead of market open (can take any real value)
- These will serve both as training and test set
Your function should return
- t, a scalar threshold that determines this simple trading strategy: invest all the available funds in the security when S>t, stay in cash otherwise
Assumptions
- You transact once per day at market open
- You buy and sell the security instantaneously at market open
- You can buy, sell, and hold fractional volumes of the security
- You don't incur in transaction costs, nor do you receive any return on the funds left in cash
Hints
- The array of daily returns can be calculated by R = D.*(S(1:n-1)>t), where D is the day-ahead price change, i.e. D=P(2:n)./P(1:n-1)-1;
- You can assume there are 252 trading days/year, so your goal is to maximize CAGR = prod(1+D.*(S(1:n-1)>t))^(252/n)-1;
- You can find the solution by maximizing the sum of log returns or, more simply, maximizes sum(log(1+D.*(S(1:n-1)>t)));
Solution Stats
Problem Comments
-
5 Comments
Show
2 older comments
Alfonso Nieto-Castanon
on 23 Aug 2020
please fix testsuite
Tokdemir Ibrahim Kerem
on 6 Oct 2020
my solution works well with test cases presented here when I run it on my computer, but it fails when I submit same solution
Dyuman Joshi
on 2 Jan 2023
Please note that test suites of this problem series (Algorithmic Trading) are broken or rather incomplete.
They will be fixed on a temporary basis, soon, as only the author of the problem has an idea what they want to implement.
minnolina
on 12 Aug 2023
When I run the script, I'm getting the error message on Test4.m :
Error: File: Test4.m Line: 2 Column: 7132 This statement is incomplete.
EducatedIdiot
on 27 Sep 2024 at 23:44
Test Issue, Error: File: Test4.m Line: 2 Column: 7132 This statement is incomplete. Just Like minnolina on 12 Aug 2023
Solution Comments
Show commentsProblem Recent Solvers1
Suggested Problems
More from this Author10
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!