SPL Quantitative Trading Practice Series: Trend Trading Strategy
Usually, the rise of stock market will create an upward trend. If one is able to capture the trend, they will get handsome benefits. This essay will introduce the commonly used Trend Trading Strategy.
First, let’s look at some related concepts before introducing the trading strategy:
1. The latest trough: the stock price lower than that over the N trading days before and after the date when it occurs.
2. The crest before the trough: The stock price that is higher than that over the N trading days before and after the day and that appears before the latest trough.
Signs to buy:
1. The latest trough is lower than the 5th percentile of the stock price over the past M days;
2. The stock price of the current day is higher than the crest before the trough.
When both conditions are met, mark the buy signal as signal=1.
Signs to sell:
1. The stock price is lower than the latest trough;
2. The RoR is above R.
When any of the two conditions are met, mark the sell signal as signal=1.
SPL code:
A | B | C | |
---|---|---|---|
1 | =file(“daily/300750.csv”).import@tc() | ||
2 | =A1.select(trade_date>=20200101&&trade_date<=20240506) | ||
3 | =A2.derive(if(#>1, close/ pre_close *factor[-1], close/pre_close):factor) | ||
4 | =hfq_fst=A3(1),A3.derive(round(factor/hfq_fst.factor*hfq_fst.close,2): hfq_close) | ||
5 | =A4.(hfq_close) | ||
6 | =windown=252 | ||
7 | =extrem_itv=3 | ||
8 | =inrate=0.5 | ||
9 | =to(windown+1,A4.len()) | ||
10 | =[] | ||
11 | for A9 | =A5.to(A11-windown+1,A11) | |
12 | =B11.median(:5).~ | ||
13 | =B11.pselect@z1(#<=windown-extrem_itv&&~<~[-extrem_itv:-1].min()&&~<=~[1,extrem_itv].min()) | ||
14 | =B11(B13) | ||
15 | =B11.pselect@z1(#<=B13&&~>~[-extrem_itv:-1].max()&&~>=~[1:extrem_itv].max()) | ||
16 | =B11(B15) | ||
17 | =A5(A11) | ||
18 | if B14<B12&&B17>B16 | =A10.insert(0,1) | |
19 | else if B17<B14 | =A10.insert(0,-1) | |
20 | else | =A10.insert(0,0) | |
21 | =ps=0,bprice=0,A4(A9).derive(A10(#):signal,if(ps==0&&signal==1,(ps=1,bprice=hfq_close,1),if(ps!=0&&(signal==-1||hfq_close/bprice>1+inrate),(ps=0,-1),0)):flag,if(flag!=0,100,0):shares) |
A4: Backward adjust the closing price.
A6-A8: Set up parameters. Here we set N=3, M=252, and R=0.5
A11 code block: Perform loop from the 253th day – when buy conditions are met, record signal=1 and when sell conditions are met, record signal=-1. As the specific buy point cannot be determined, we cannot compute the RoR. The signal value does not include the sell point where the RoR is above R.
A21: Use buy/sell signal (signal) to compute order signal (flag) and specify the order quantity (100 shares). When no position is held and signal=1, mark it as flag=1 and place an order to buy; when signal=-1 or the RoR is above 50% and positions are held, mark it as flag=-1 and perform selling.
Not only does SPL offer the convenient to use loop functions, it can also use the for loop to accomplish complicated loop computations – as Python, Java and other high-level languages do. The A11 code block is such an example. It shows every computing step in the loop statement for achieving the task.
Plot the buy/sell run chart according to A21’s data:
In the above chart, red dots represent buy points, and green squares are sell points.
Get records where flag values are not 0 from A21’s result set and call the backtesting interface to get the chart displaying backtested RoRs and backtested indicators.
Below is the backtested RORs chart:
Backtested indicators:
Indicators | Value |
---|---|
Cumulative rate of return | 6.78% |
Annualized rate of return | 3.5% |
Annual volatility | 23.08% |
Sharpe ratio | 0.02 |
Maximum drawdown | 37.34% |
Cash invested | 43355.99 |
Total assets | 46294.99 |
Stock holding ratio | 83.11% |
Profits count | 2 |
Losses count | 8 |
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/2bkGwqTj
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version