Trend Following Algo Building Series - Part 3
Determine trends; Define entry and exit criteria; First attempt at backtesting
I started Part 1 of the Trend Following Algo Building series on Dec 15, 2023.
Where I “promised” to develop a profitable trend following algo from scratch.
In 90 days or less.
90 days would put my target deadline on Mar 15, 2024, which doesn’t leave me a lot of time.
After playing around with the historical data, I feel a key part is to have a few options for how I would determine what the trend is.
In Part 2, I previously mentioned that uptrends are indicated by price being above the SMA, and downtrends are indicated by price being below the SMA.
However, I neglected the “sideways” trend where price consolidates in a range and it’s not clear whether price is in an up or down trend.
(More on this in the next section).
Our path is still the same — we need to solve these problems, in order of priority:
Accurately and simply determine the start and end of a trend
Use the simplest possible entry and exit criteria that is profitable
Let’s go.
Determining Trends
The simple moving average (SMA) is a great proxy for identifying trends.
If prices stay above the SMA, we can identify an uptrend. If prices stay below the SMA, we can identify a downtrend.
We’ll keep this method (let’s call this “SMA flip”) as Method 1 for comparison’s sake in backtesting.
A difficult part is detecting the transition between trends, particular if price consolidates around a range.
We can turn to simple statistics to help us — the standard deviation, to be specific.
Standard deviation (square root of variance), measures the amount of variation in a set of values. In our case, we want the close price of each candle in a set of the most recent N candles.
And if price is within a threshold factor (percentage of the standard deviation) of the SMA, we count price as being in a “sideways” trend.
Let’s call this the “SMA flip with threshold” as Method 2.
Lastly, I want to test out whether looking at the price data differently would provide a simpler and more accurate way to determine trends.
I’m talking about the Heiken Ashi candles.
Heikin ashi is a charting style where the heikin ashi candle is created by combining the midpoint of the previous bar with the open, high, low, and close of the prevailing bar. A red bar means the average closing price of the prior six bars is in the lower 50% of its range, indicating a bearish bias. The opposite is true of the green bars.
Let’s summarize the 3 methods for determining price trends.
Method 1 (SMA Flip):
If price closes above the SMA, we’re in an up trend
If price closes below the SMA, we’re in a down trend
Method 2 (SMA Flip with Threshold):
If price closes above the SMA + the recent standard deviation * threshold, we’re in an up trend
If price closes below the SMA - the recent standard deviation * threshold, we’re in a down trend
Otherwise, we are in a "sideways” trend
Method 3 (Heikin Ashi):
Convert regular K-line (OHLC) candles into Heikin Ashi candles
If we see a green candle and price closes above the SMA, we’re in an up trend
If we see a red candle and price closes below the SMA, we’re in a down trend
Before starting to backtest, let’s get more specific now and decide on our entry and exit criteria.
Entry and Exit Criteria
For Method 1 (SMA Flip):
Entry — if we’re in an uptrend (and we’re not in a position), go long; if we’re in a downtrend (and we’re not in a position), go short;
Exit — close our position if we’re no longer in the trend we entered
For Method 2 (SMA Flip with Threshold):
Entry — if we’re in an uptrend (and we’re not in a position), go long; if we’re in a downtrend (and we’re not in a position), go short;
Exit — close our position if we’re in the opposite trend we entered (i.e. if you entered long on uptrend, close when you encounter a downtrend, and vice versa)
For Method 3 (Heikin Ashi):
Entry — if we’re in an uptrend (and we’re not in a position), go long; if we’re in a downtrend (and we’re not in a position), go short;
Exit — close our position if we’re no longer in the trend we entered
Minimum Viable Parameters
So, we have 3-5 parameters to test and find favorable combinations of:
timeframe
stop loss percentage
simple moving average period
lookback period (for standard deviation calculations)
threshold (see whether price is close enough to SMA to count as sideways trend)
Backtesting Attempt #1
We’ll be backtesting all 3 methods for determining trends, comparing them using a daily timeframe and an arbitrary stop loss percentage of 1% (we’ll do stop loss testing later) so it’s as close to a fair comparison as possible.
And use these different subsets:
“All” available data when I downloaded (2020-03-25 to 2023-12-24)
“Bull” (2020-09-04 to 2021-04-15)
“Bear” (2021-11-09 to 2022-11-09)
“2023” (2023-01-01 to 2023-12-24)
Method 1 (SMA Flip) Findings
SMA (32) stood out
“All” - ROI of +616.14%
“Bull” - ROI of +242.19%
“Bear” - ROI of +8.45%
“2023” - ROI of -4.52%
Method 2 (SMA Flip with Threshold) Findings
SMA (32) stood out again
“All” - ROI of +739.89%
“Bull” - ROI of +232.59%
“Bear” - ROI of +13.25%
“2023” - ROI of -7.00%
Method 3 (Heikin Ashi) Findings
SMA (32) stood out again
“All” - ROI of +521.78%
“Bull” - ROI of +253.46%
“Bear” - ROI of +6.32%
“2023” - ROI of +0.02%
Yes, you read correctly, there’s something off with the way BTC price has moved in 2023 (Jan - Dec) compared to other years.
I haven’t been able to put my finger on this peculiarity yet.
However, we’ve gotten fairly optimistic results with almost no optimization.
I can still be wrong by being too optimistic here — but I’d be a lot more worried if I had to optimize parameters a ton just to get the results above.
Right now (Jan 3, 2024), we’re 18 days (20%) into the 90 day challenge.
I say we move ahead with building out the signal generator for all 3 methods using live, 2024, data, instead of waiting around for the perfect pattern from backtesting.
If our live signals are performing poorly (it’ll probably take a few trades to show this), then we’ll try backtesting again for other tweaks.
See you in the next one!