Trend Following Algo Building Series - Part 2
Developing an intuition around BTC trends, moving averages, and more.
We’re now in Part 2 of the Trend Following Algo Building Series.
If you want a refresher, here’s Part 1.
The goal of this post is to synthesize what we learned from research and develop an intuition around a few unanswered questions. As well as come up with a shortlist of cryptos to test with.
Let’s begin!
Questions to answer
At this early stage, the questions we want answers to are:
What’s a reasonable simple and accurate way to determine up and down trends?
Of the trends we detect, can we find a simple and accurate signal for entry?
Again, can we find a simple and accurate signal for exit?
For building this intuition, let’s use our naive understanding and begin backtesting to see how far off we are.
Keep in mind, even though crypto is largely dominated by BTC, it’s not always the case that everything rise and falls together in unison, all of the time. For the benefit of our initial backtest, BTC is sufficient to develop our intuition.
Count the trends
First order of business, we need to get an idea of how trends work for BTC.
Let’s first download all the available data from our exchange.
For the purpose of the post, I’ll skip over the code used. If there’s any interest, please share in the comments or DM me on Twitter (0xSmartCrypto).
OK. We now have the candle (kline) data.
Let’s test by using the 20 period simple moving average, with these initial rules:
When the price is above the available 20 period SMA, we’ll call it an uptrend
When the price is below an available 20 period SMA, we’ll call it a downtrend
Let’s see what we find, shall we?
Initial findings
There isn’t much on the surface. I’ve tested these timeframes — and some general findings.
I wanted to know the historical distribution of times that BTC price is above a chosen moving average period, note the length of its streaks, checking all the available timeframes that the exchange provides. And, get the same data for times BTC price is below the same chosen moving average period.
I started off with an SMA of 3.
Here’s what I found:
X-axis is the Streak, Y-axis is the Times Above Moving Average. (E.g. there are almost 2000 times where BTC price had a streak of 2 candles above the moving average on the 1h timeframe).
The data:
And here’s the chart for the times BTC prices is below the moving average.
X-axis is the Streak, Y-axis is the Times BELOW Moving Average.
The data:
Quick observations:
Streaks happen and taper off quickly past 2-streak and 4-streak
The distribution, aside from the 2-streak for above the moving average, seems unremarkable
This makes me want to test out another variable - the SMA period. We’re likely missing out on some pattern here that is more representative of how BTC moves.
Let’s try a moving average period of 5, 7, 10, 14, 20.
Varying the MA period
The results from varying the moving average period gave us an interesting insight. Each row below is the result of one moving period (3, 5, 7, …) on the 1 week timeframe.
------------------------------------
SMA(3) max above streak 15; max below streak 8; avg above streak 3.52; avg below streak 2.77; median above streak 2; median below streak 2
------------------------------------
SMA(5) max above streak 16; max below streak 14; avg above streak 4.36; avg below streak 3.91; median above streak 3; median below streak 3
------------------------------------
SMA(7) max above streak 27; max below streak 14; avg above streak 6.29; avg below streak 6.36; median above streak 5; median below streak 3
------------------------------------
SMA(10) max above streak 28; max below streak 16; avg above streak 6.20; avg below streak 5.40; median above streak 4; median below streak 16
------------------------------------
SMA(14) max above streak 43; max below streak 21; avg above streak 12.00; avg below streak 9.63; median above streak 20; median below streak 3
------------------------------------
SMA(20) max above streak 40; max below streak 30; avg above streak 13.00; avg below streak 10.86; median above streak 2; median below streak 2
Notice how the average and median streaks skew when testing between SMA(10) and SMA (14).
Not sure what to make of this just yet.
It’d be interesting to see if other patterns emerge on other timeframes.
Next steps
Because there’s still so much to unpack about BTC, I’m going to put off shortlisting the cryptos for another time.
As for next steps, my guess is that in order to beat BTC buy and hold, we need to:
go long during enough uptrends (and go short during enough downtrends)
avoid false uptrends and false downtrends (where false is just unprofitable or getting stopped out)
Next up in Part 3, I’ll explore and go deeper to analyze the BTC data, and come up with practical insights that I can use.
For those keeping track, we’re on steps 5-7 of our general algo development framework.
Side note: I’m tempted to introduce dynamic stop loss because it “looks” like a good idea to not get stopped out prematurely. If I had to do it, it’d revolve around using Average True Range (ATR) as a variable.
And while I may eventually revisit this, now is not the right time.
Happy Holidays
After all, it’s time for the Christmas holidays in December.
I’m off for a few days to spend time with the fam.
Follow me on Twitter (0xSmartCrypto)
Have a good one. I’ll see you (with Part 3) in the new year! 🎊🥳