February 16, 2026

The Odyssey — Chapter Two: The Moving Average Crossover

The Odyssey — Chapter Two: The Moving Average Crossover

The Odyssey — Chapter Two: The Moving Average Crossover

If you’re just joining us, this is the second entry in this blog series, see the Prologue here for more context.

The phrase “between Scylla and Charybdis” is often used as an idiom for having to pick between two hard choices. In Greek Mythology, ships had to sail away from the six-headed sea monster Scylla while also not get to close to the nearby whirlpool, Charybdis. Therefore, navigating between these two monsters is a tricky task. Steer too far left, and you may find yourself being sucked down to the abyss. Steer too far to the right, and you may find yourself being flung up into a sea monster’s mouth.

Can a deviation from the expected path be used to forecast future trends?

The Moving Averages Crossover (MAC) strategy is a well-known trading technique in financial time series analysis. A moving average, specifically a Simple Moving Averages, is the average of the last n periods for a given time series (SMA-n). There are more complicated types of moving averages such as the Exponential Moving Average. We will be sticking with SMA’s for now.

The idea behind the MAC strategy is very simple: You start with two moving average lines, one that uses a short period, and one that uses a longer period. When the short period crosses over the long period, this can be used as a signal that there is a trend moving upwards, and vice-versa for when it crosses below. We will be working with minute-long period increments. Now let’s look at an example visually using the hour SMA (SMA-60) and the day SMA (SMA-1440):

ETHUSD Snapshot from April 4th, 2017 19:00 to April 6th, 2017 11:30 — Price in Blue, SMA-60 in Orange, SMA-1440 in Yellow.

The plot has quite a few crossovers here. When the orange line initially crosses below the yellow in Crossover #1, we see that we should sell. Then, we should buy back at Crossover #2. Then at Crossover #7, we should sell again.

Now had we bought at Crossovers #3,4,5, and 6 we wouldn’t have actually been catching a strong trend shift. So in order to account for situations where this might be the case, we need to set a threshold that indicates how far below and above we should start to sell and buy.

The code for this is fairly simple and is a direct translation of the theory. We create a new subclass of the Strategy class and call it MovingAveragesCrossover . We set the sell_qty and buy_qty to sell or buy our entire position. The justification for this is that there really isn’t any justification to do otherwise. Here is what the implementation will look like:

from Strategies.strategy import Strategy

class MovingAveragesCrossover(Strategy):
THRESH_SELL = 0.98 # Percentage of long_ma to sell below
THRESH_BUY = 1.02 # Percentage of long_ma to buy above
MA_SHORT = 60 # short moving average look-back period, "n"
MA_LONG = 1440 # long moving average look-back period, "n"

def should_sell(self, **tick_info):
ma_short = tick_info['ma_short']
ma_long = tick_info['ma_long']

return ma_short < ma_long*MovingAveragesCrossover.THRESH_SELL

def sell_qty(self, **position_info):
# Sell all our crypto
return position_info['crypto_position']
def should_buy(self, **tick_info):
ma_short = tick_info['ma_short']
ma_long = tick_info['ma_long']

return ma_short > ma_long*MovingAveragesCrossover.THRESH_BUY

def buy_qty(self, **position_info):
# Buy with all our cash
return position_info['cash_position']

And it’s as simple as that!*

Now let’s get some in-sample data from Athena** and see how well we do!

*I omitted a few methods here: should_stoploss , should_cancel_sell , and should_cancel_buy . You can assume for the purpose of simplicity all three simply return False . After trying out some implementations for each, I decided the marginal benefit (+1-2%) added was not worth writing about.

** In the previous post, I mention how Athena splits up the data into Training, Validation, and Testing. This split is mainly for when we get the machine learning models. For the purposes of this post, we will be referring to the Training and Validation sets as in-sample data, and the Test sets as out-of-sample data. These are standard statistical forecasting terms and they seem much more appropriate to use here.

In order to evaluate how good our performance is, we need to have a benchmark that we can use to compare our strategy against. As is standard with cryptocurrency, we use HODLing (simply not doing any trades) as our benchmark to compare against. Our position is green when we have bought (long), and grey when we have sold (neutral).

We start off long 1 ETH. That was worth about $9 at the start. Our strategy yields us: $422.44, however simply hodling our 1 ETH would have yielded us $734.97. What can be causing this? Let’s take a closer look at the same time period in April 2017 we had above:

SMA Crossover Performance for ETHUSD Snapshot from April 4th, 2017 19:00 to April 6th, 2017 11:30

We see that during this period, even though ETH dropped in value about 14%, our position only lost around 3% (dropping from 35.7 to 34.7) however, we are not able to take advantage of this massive drop in price, we are only able to minimize the damages from it. How do we fix this? Short Selling! Basically, short selling means that we borrow some ETH from someone who owns ETH and immediately sell it, hoping to buy back when the price dips. This helps us make money during the periods where our strategy says there is a downward trend. Adding shorting to the backtester , we see we fare a lot better(shorting is represented as red):

SMA Crossover snapshot with Shorting for ETHUSD Snapshot from April 4th, 2017 19:00 to April 6th, 2017 11:30

Even though our position starts off less than the benchmark, we quickly catch up and even surpass it for a while. Still, we don’t end up making any more money. In fact, we are still lower than the benchmark!

Actually though, as with almost all forecasting techniques, we don’t need to be right all the time. i.e. we don’t care if some trades end up losing up money, we just need to be right more often than not! Indeed, when we look at the entire in-sample performance we get the following:

Our strategy ends up earning us $1,792.26 whereas hodling left us at $734.97. That is almost 244% return over the benchmark!

Can we do better though?

Recall that we initialized our strategy’s parameters as follows:

THRESH_SELL = 0.98
THRESH_BUY = 1.02
MA_SHORT = 60
MA_LONG = 1440

How do we know that these are the optimal values for all four? We don’t! Let’s find this out, however. You will have to trust me that MA_SHORT and MA_LONG are at their best values, otherwise, it would require me to try and visualize the 4D space of all four parameters, and I still haven’t figured that one out yet. Setting the two values to 60 minutes and 1440 minutes, respectively, we get values that are only at or above 100% returns (1.0x the last HODL position). Backtesting values of THRESH_SELL ranging from [0.95, 0.999], and THRESH_BUY values in the range of [1.001, 1.066] we get the following heatmap:

Published at Mon, 05 Aug 2019 05:13:05 +0000

Bitcoin Pic Of The Moment
Attribution MUST include Link to: Cryptocurrency360.com/
Copy Link to Credit: Cryprocurrency360.com
By Crypto360 on 2018-03-15 14:40:55
tags

Previous Article

illustration

Next Article

Youtube Censorship Drives Uptake of Crypto-Powered Video Platforms

You might be interested in …