One article on commonly used technical indicators: Understanding the KDJ indicator

What is KDJ indicator?

The Chinese name of KDJ is also called stochastic indicator, and the English name is Stochastic oscillator. It was first created by George Lane in the 1950s and was first used in the futures market. The KDJ indicator can quickly and intuitively judge the market. It is mainly used to analyze short- and medium-term trends. It is a commonly used technical analysis tool in the futures and stock markets. It mainly uses the true amplitude of price fluctuations to reflect the strength of price trends and overbought and oversold phenomena, and sends buy and sell signals before prices rise or fall.

The picture below is Tesla’s recent K-line and KDJ indicator charts.

As can be seen from the picture, KDJ has three lines, namely K line, D line and J line. The value range of K line and D line is 0~100, and the value range of J line can exceed 100 and be lower than 0. Compared to speed, J > K > D. Compared to safety, J < K < D.

KDJ formula description

Based on statistical principles, KDJ calculates the immature random value RSV through the highest price, lowest price and latest closing price that have appeared in a specific period N and the proportional relationship between the three, and then calculates the immature random value RSV based on the moving average (SMA/ EMA) method to calculate K value, D value and J value, and draw a curve chart to judge the stock trend. In the design process, it mainly studies the relationship between the highest price, lowest price and closing price. It also integrates some advantages of momentum concept, strength indicator and moving average. Therefore, it can make judgments quickly, quickly and intuitively. Quotes.

The specific calculation method of KDJ indicator is as follows:

  • RSV = (closing price - lowest price in N periods) / (highest price in N periods - lowest price in N periods) * 100

  • K value = SMA(RSV, M), the M period moving average of RSV

  • D value = SMA(K, L), L period moving average of K value

  • J value = 3 K-2 D

illustrate:

  • RSV is the abbreviation of English Raw StochasticValue, which means immature random value in Chinese. It determines the relationship between the price closing level and the K-line fluctuation range within a period of time. For example, if the RSV is above 80, it indicates that the current close is near the top of the range. On the other hand, if the RSV is below 20, it means the index closed near the low of the range

  • SMA is a simple moving average, and some also use EMA exponential moving average.

  • The K curve is obtained by smoothing the RSV value. Its technical characteristics are similar to the RSV value. They both reveal the strength of the price within N days.

  • The D curve is obtained by smoothing the K value, which can also be understood as smoothing the RSV value again. Compared with the K curve, the D curve has a better filtering effect on small price fluctuations, and the curve itself fluctuates more gently.

  • The J curve represents the difference between 2 times the K value and the D value, plus the K value of the day. It makes it easier to see the difference between the K curve and the D curve. Since the J-curve has nothing to do with the RSV value, in order to pursue more concise technical indicators, sometimes it is not necessary to use the J-curve.

  • The D curve and the K curve form a "double moving average" combination. All analysis rules related to moving averages can be applied to the KDJ indicator. Investors can make a clear judgment on whether the stock price is overbought or oversold through the intersection of the two curves at low and high levels; the D curve provides preliminary support and resistance to the K curve, providing a basis for observing changes in stock prices. Provides a reference basis; however, the shortcomings of the K and D curves are that they are limited to the space between 0 and 100.

  • The movement direction of the J curve represents the trend state of the K curve and D curve. The running speed (slope) of the J-curve is faster than that of the K-curve and the D-curve, which can help investors predict in advance the upcoming overbought and oversold stock price phenomena; the J-curve can break through the constraints of the 0-axis and the 100-axis, and then reach the extreme value, it can be revealed that the K curve and D curve have begun to passivate

KDJ indicator time period value

Generally speaking, the domestic values ​​of N, M, and L are usually 9, 3, and 3. The most commonly used periods of N abroad are 5 and 14. After investors have a thorough understanding of the KDJ indicator, these parameters can be adjusted according to their own understanding according to the corresponding market and target.

KDJ formula calculation

The function corresponding to calculating the KDJ value in talib is the talib.STOCH() function, which has two return values, one is the K value and the other is the D value. The J value needs to be calculated by yourself using a formula. If N is set to 9, the KDJ indicator requires at least 9 days of data to be used normally.

  1. Using KDJ indicator in talib

# talib
import talib as ta
import numpy as np
inputs = {
    'open': np.random.random(100),
    'high': np.random.random(100),
    'low': np.random.random(100),
    'close': np.random.random(100),
    'volume': np.random.random(100)
}
slowk, slowd = ta.STOCH(inputs['high'], inputs['low'], inputs['close'], fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
slowj = 3 * slowk - 2 * slowd
  1. Using KDJ indicator in backtrader

Backtrader provides three categories: StochasticFast, Stochastic and StochasticFull, all of which only provide K-curve and D-curve, but not J-curve. in:

  • The K line in the StochasticFast class corresponds to the RSV of the above formula, and the D line corresponds to the K line of the above formula, also called the fast stochastic indicator.

  • The K-line and D-line in the Stochastic class are consistent with the above formula, also called slow stochastic indicator

  • The K and D lines of the StochasticFull class are consistent with the StochasticFast class, and a percDSlow line is added to be consistent with the D line of the above formula.

The following example code adds a J-curve.

class KDJ(bt.Indicator):
    lines = ('K', 'D', 'J')

    params = (
        ('period', 9),
        ('period_dfast', 3),
        ('period_dslow', 3),
    )

    plotlines = dict(
        J=dict(_name='%J')
    )

    def __init__(self):
        self.kd = bt.indicators.StochasticFull(
            self.data,
            period=self.p.period,
            period_dfast=self.p.period_dfast,
            period_dslow=self.p.period_dslow,
        )

        self.l.K = self.kd.percD
        self.l.D = self.kd.percDSlow
        self.l.J = self.K * 3 - self.D * 2

KDJ indicator application

The 80 and 20 lines of the KDJ indicator serve as the dividing line for whether the indicator has been overbought or oversold, and the central axis 50 line is the normal dividing line for long and short.

The golden cross formed by the two curves K and D at the low level and the death cross formed at the high level actually correspond to the normal moving average crossover, just because the position relationship can better reflect the phenomenon of oversold at the low and overbought at the high.

  • Golden Cross: When the K line crosses the D line from below, and the J line crosses the K and D lines from below, it forms a KDJ golden cross, which is a signal that the stock price is strengthening. If the previous K value is less than 10, the D value is less than 20, the J value is less than 0, and the three lines form a golden cross in the oversold zone, the stock price is more likely to successfully rebound.

  • Dead Cross: When the K line crosses the D line downward from the D line, and the J line crosses the K and D lines from above the K and D lines downward, forming a KDJ dead cross, it is a signal that the stock price is weakening. If the previous K value is greater than 90, the D value is greater than 80, and the J value is greater than 100, and the three lines form a dead cross in the overbought zone, the probability of the stock price falling in the short term is higher.

  • Top divergence: When the stock price continues to rise and continues to hit new highs but KDJ does not follow suit and hit new highs at the same time, KDJ top divergence will form, indicating that the short-term rise in stock prices is weak. The stock price is very likely to stagnate and fall back in the short term, which is a short-term exit signal.

  • Bottom divergence: When the stock price continues to fall and continues to hit new lows, but KDJ does not follow to hit new lows at the same time, KDJ bottom divergence is formed, indicating that the short-term downward momentum of the stock price has weakened. It is very easy for the stock price to stop falling and rebound in the short term, which is a signal to seize the opportunity to buy after the short-term rebound.

  • Passivation means that the stock price rises or plummets in the market, causing the indicator data to become stuck, causing the indicator to lose its guiding role. Because KDJ is a very sensitive indicator, it is particularly prone to passivation. Generally, there are two types of passivation: high passivation and low passivation.

  • The high level is passivated. In the unilateral rising market with extremely strong stock price trends, the K value and D value are both in the overbought zone above 80, and the J value is greater than 100. The stock price is still rising rapidly to a new high, and at this time, KDJ high level passivation is formed. Don't panic when you encounter high passivation. Although the indicator fails, it also means the strength of individual stocks. At this time, you can refer to other indicators for operation.

  • The converse of high-level passivation is low-level passivation. Generally, the K value and D value are both below 20 in the oversold zone, and the J value is below 0. Encountering low passivation generally means that the stock price may fall for a period of time. At this time, it is suitable to refer to other indicators to judge when KDJ returns to normal. The KDJ golden cross after the return is the best time to buy the bottom.

  • In the actual trend of stock prices, KDJ buying and selling signals are frequent and there are many false and false signals. Because, we can see that every time there is a top divergence or a bottom divergence, KDJ will appear golden cross and dead cross. However, every time a golden cross or a dead cross appears on KDJ, it does not mean that the stock price has also experienced a top or bottom divergence. Therefore, what we have to do is to judge the accuracy of its golden cross and dead cross.

Conclusion & Communication

Follow the official account for more content. At the same time, you can also get invitations to join investment exchange groups and quantitative investment seminar groups, where you can communicate and discuss with many investment enthusiasts, quantitative practitioners, and technology experts, and quickly improve your investment level.

WeChat public account: Zhuge Talk

Writing articles is not easy. If you think this article is helpful to you, please click and read it.

reference

Guess you like

Origin blog.csdn.net/richardzhutalk/article/details/125114337