OPEN-SOURCE SCRIPT

Barndorff-Nielsen and Shephard Jump Statistic [Loxx]

Atualizado
The following comments and descriptions are from from "Problems in the Application of Jump Detection Tests to Stock Price Data" by Michael William Schwert; Professor George Tauchen, Faculty Advisor.

This indicator applies several jump detection tests to intraday stock price data sampled at various frequencies. It finds that the choice of sampling frequency has an effect on both the amount of jumps detected by these tests, as well as the timing of those jumps. Furthermore, although these tests are designed to identify the same phenomenon, they find different amounts and timing of jumps when performed on the same data. These results suggest that these jump detection tests are probably identifying different types of jump behavior in stock price data, so they are not really substitutes for one another.

In recent years there has been a great deal of interest in studying jumps in asset price movements. Reasons why it is important to know when and how frequently jumps occur include risk management and the pricing and hedging of derivative contracts. Investors would benefit greatly from knowing the properties of jumps, since large instantaneous drops in asset prices result in large instantaneous losses. The effect of jumps on derivative pricing is equally significant, especially considering the important role derivatives play in modern financial markets. When asset price movements are continuous, investors can perfectly hedge derivative contracts such as options, but when jumps occur, they cause a change in the derivative price that is non-linear to the change in the price of the underlying asset. Thus, jumps introduce an unhedgeable risk to the holders of derivative contracts.

The ability to identify realized jumps in the financial markets could provide helpful information such as how frequently jumps occur, how large the jumps are, and whether they tend to occur in clusters. With this goal in mind, several authors have developed tests to determine whether or not an asset price movement is a statistically significant jump. These tests take advantage of the high-frequency intraday price data available today through electronic sources. Barndorff-Nielsen and Shephard (2004, 2006) use the difference between an estimate of variance and a jump-robust measure of variance to detect jumps over the course of a day. Approaching the problem differently, Jiang and Oomen (2007) exploit high order sample moments of returns to identify days that include jumps. Aїt-Sahalia and Jacod (2008) also exploit high order sample moments of returns to detect jumps by comparing price data sampled at two different frequencies. Lee and Mykland (2007) test for jumps at individual price observations by scaling returns by a local volatility measure. While these tests employ different strategies for detecting jumps, they are all designed to identify the same phenomenon.

For this indicator we are focused on the Barndorff-Nielsen and Shephard jump statistic.

Barndorff-Nielsen and Shephard (2004, 2006) developed a test that uses high-frequency price data to determine whether there is a jump over the course of a day. Their test compares two measures of variance: Realized Variance, which converges to the integrated variance plus a jump component as the time between observations approaches zero; and Bipower Variation, which converges to the integrated variance as the time between observations approaches zero, and is robust to jumps in the price path, an important fact for this application. The integrated variance of a price process is the integral of the square of the σ(t) term in (2.2.2), taken over the course of a day. Since prices cannot be observed continuously, one cannot calculate integrated variance exactly, and must estimate it instead.

For our purposes here, this is calculated as:

r = log(p[ti]/p[ti-1])

This the geometric return from time ti-1 to time ti.

Then, Realized Variance and Bipower Variation are described by the following functions (see code for details)

realizedVariance(float src, int per)

and

bipowerVariance(float src, int per)

Huang and Tauchen (2005) also consider Relative Jump, a measure that approximates the percentage of total variance attributable to jumps:

RJ = (RV - BV) / RV

This statistic approximates the ratio of the sum of squared jumps to the total variance and is useful because it scales out long-term trends in volatility so one can compare the relative contribution of jumps to the variance of two price series with different volatilities.

To develop a statistical test to determine whether there is a significant difference between RV and BV, one needs an estimate of integrated quarticity. Andersen, Bollerslev, and Diebold (2004) recommend using a jump-robust realized Tri-Power Quarticity, I've included commentary in code to better explain how this indicator is collocated. See code for details.

How to use this indicator
When the bars turn gray, it's an indication that a jump has occurred in the market. It serves a warning that price jumped. I've included a percent point function (or inverse cumulative distribution function) to cutoff Z-score values depicted by histogram values. The top line at 3 is the empirical maximum Z-score value a serves merely as a point of reference. The Red line is the cutoff line calculated using PPF. When the histogram is green, no jumps have been detected. This indicator also includes alerts, signals, and bar coloring. I've also expanded the possible source types using my own Expanded Source Types library so you can test different log return methods as inputs. It is recommended to use window sizes of 7, 16, 78, 110, 156, and 270 returns for sampling intervals of 1 week, 1 day, 1 hour, 30 minutes, 15 minutes, and 5 minutes, respectively.

If you'ed like to better understand PPF, see here: Distributions in python

Included:
  • Bar coloring
  • Signals
  • Alerts
  • Loxx's Expanded Source Types
Notas de Lançamento
-Swapped out the PPF function for the ndtri function. you can read about this here: Inverse of Normal distribution
-Changed coloring. Yellow means jump detected, green means no jump detected. If a jump is detected, bars are colored yellow. I removed green bar coloring to make this easier to understand and use.
Notas de Lançamento
Reduced lag by one bar by adjusting for loops to start at current bar.
barndorffbarndorffnielsonbarndorffnielsonshephardCentered OscillatorseducationaljumpjumpstatisticndtrivarianceVolatility

Script de código aberto

No verdadeiro espírito do TradingView, o autor desse script o publicou como código aberto, para que os traders possam compreendê-lo e analisá-lo. Parabéns ao autor! Você pode usá-lo gratuitamente, mas a reutilização desse código em publicações é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Quer usar esse script no gráfico?


Public Telegram Group, t.me/algxtrading_public

VIP Membership Info: patreon.com/algxtrading/membership
Também em:

Aviso legal