OPEN-SOURCE SCRIPT

Dickey-Fuller Test for Mean Reversion and Stationarity

**IF YOU NEED EXTRA SPECIAL HELP UNDERSTANDING THIS INDICATOR, GO TO THE BOTTOM OF THE DESCRIPTION FOR AN EVEN SIMPLER DESCRIPTION**

Dickey Fuller Test:
The Dickey-Fuller test is a statistical test used to determine whether a time series is stationary or has a unit root (a characteristic of a time series that makes it non-stationary), indicating that it is non-stationary. Stationarity means that the statistical properties of a time series, such as mean and variance, are constant over time. The test checks to see if the time series is mean-reverting or not. Many traders falsely assume that raw stock prices are mean-reverting when they are not, as evidenced by many different types of statistical models that show how stock prices are almost always positively autocorrelated or statistical tests like this one, which show that stock prices are not stationary.

Note: This indicator uses past results, and the results will always be changing as new data comes in. Just because it's stationary during a rare occurrence doesn't mean it will always be stationary. Especially in price, where this would be a rare occurrence on this test. (The Test Statistic is below the critical value.)

The indicator also shows the option to either choose Raw Price, Simple Returns, or Log Returns for the test.
Raw Prices:
  • Stock prices are usually non-stationary because they follow some type of random walk, exhibiting positive autocorrelation and trends in the long term.
  • The Dickey-Fuller test on raw prices will indicate non-stationary most of the time since prices are expected to have a unit root. (If the test statistic is higher than the critical value, it suggests the presence of a unit root, confirming non-stationarity.)


Simple Returns and Log Returns:
  • Simple and log returns are more stationary than prices, if not completely stationary, because they measure relative changes rather than absolute levels.
  • This test on simple and log returns may indicate stationary behavior, especially over longer periods. (The test statistic being below the critical value suggests the absence of a unit root, indicating stationarity.)


snapshot

  • Null Hypothesis (H0): The time series has a unit root (it is non-stationary).
  • Alternative Hypothesis (H1): The time series does not have a unit root (it is stationary)
  • Interpretation: If the test statistic is less than the critical value, we reject the null hypothesis and conclude that the time series is stationary.


Types of Dickey-Fuller Tests:
1. (What this indicator uses) Standard Dickey-Fuller Test:
  • Tests the null hypothesis that a unit root is present in a simple autoregressive model.
  • This test is used for simple cases where we just want to check if the series has a consistent statistical property over time without considering any trends or additional complexities.
  • It examines the relationship between the current value of the series and its previous value to see if the series tends to drift over time or revert to the mean.


2. Augmented Dickey-Fuller (ADF) Test:
  • Tests for a unit root while accounting for more complex structures like trends and higher-order correlations in the data.
  • This test is more robust and is used when the time series has trends or other patterns that need to be considered.
  • It extends the regular test by including additional terms to account for the complexities, and this test may be more reliable than the regular Dickey-Fuller Test.


For things like stock prices, the ADF would be more appropriate because stock prices are almost always trending and positively autocorrelated, while the Dickey-Fuller Test is more appropriate for more simple time series.


Critical Values
This indicator uses the following critical values that are essential for interpreting the Dickey-Fuller test results. The critical values depend on the chosen significance levels:
  • 1% Significance Level: Critical value of -3.43.
  • 5% Significance Level: Critical value of -2.86.
  • 10% Significance Level: Critical value of -2.57.


These critical values are thresholds that help determine whether to reject the null hypothesis of a unit root (non-stationarity). If the test statistic is less than (or more negative than) the critical value, it indicates that the time series is stationary. Conversely, if the test statistic is greater than the critical value, the series is considered non-stationary.

This indicator uses a dotted blue line by default to show the critical value. If the test-static, which is the gray column, goes below the critical value, then the test-static will become yellow, and the test will indicate that the time series is stationary or mean reverting for the current period of time.

What does this mean?
This is the weekly chart of BTCUSD with the Dickey-Fuller Test, with a length of 100 and a critical value of 1%.
snapshot

So basically, in the long term, mean-reversion strategies that involve raw prices are not a good idea. You don't really need a statistical test either for this; just from seeing the chart itself, you can see that prices in the long term are trending and no mean reversion is present.

snapshot

For the people who can't understand that the gray column being above the blue dotted line means price doesn't mean revert, here is a more simple description (you know you are):

Average (I have to include the meaning because they may not know what average is): The middle number is when you add up all the numbers and then divide by how many numbers there are. EX: If you have the numbers 2, 4, and 6, you add them up to get 12, and then divide by 3 (because there are 3 numbers), so the average is 4. It tells you what a typical number is in a group of numbers.

This indicator checks if a time series (like stock prices) tends to return to its average value or time.

Raw prices, which is just the regular price chart, are usually not mean-reverting (It's "always" positively autocorrelating but this group of people doesn't like that word). Price follows trends.

Simple returns and log returns are more likely to have periods of mean reversion.

How to use it:
  • Gray Column (the gray bars) Above the Blue Dotted Line: The price does not mean revert (non-stationary).
  • Gray Column Below Blue Line: The time series mean reverts (stationary)


So, if the test statistic (gray column) is below the critical value, which is the blue dotted line, then the series is stationary and mean reverting, but if it is above the blue dotted line, then the time series is not stationary or mean reverting, and strategies involving mean reversion will most likely result in a loss given enough occurrences.

Bitcoin (Cryptocurrency)meanmean-reversionmeanreversionmoving_averageS&P 500 (SPX500)statisticstrendTrend AnalysisVolatility

Script open-source

Dans le plus pur esprit TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par nos Règles. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Vous voulez utiliser ce script sur un graphique ?


Aussi sur:

Clause de non-responsabilité