This project demonstrates how to detect divergence between price movements and an indicator (e.g., RSI). It is a part of a private event-based trading system strategy aimed at showcasing and studying the concept of divergence in trading.
In trading, divergence occurs when the price of an asset moves in the opposite direction of an indicator, such as the Relative Strength Index (RSI) or the Moving Average Convergence Divergence (MACD). Divergence can signal potential trend reversals or continuations, making it a valuable tool for traders.
There are two primary types of divergence:
-
Bullish Divergence:
- Occurs when the price creates lower lows, but the indicator creates higher lows.
- Often indicates a potential upward reversal in price.
-
Bearish Divergence:
- Occurs when the price creates higher highs, but the indicator creates lower highs.
- Often signals a potential downward reversal in price.
By detecting divergence, traders aim to identify potential entry and exit points more effectively.
The demonstration begins by loading historical candlestick data from a file. For this example, we are using the last 500 four-hour (4H) candles fetched from the Bybit API.
The loaded candle data is visualized as a line chart to provide a clear overview of the market movements during the observed period. Below is an example plot of the loaded candles:
To make the example clearer, we use a smaller subset of the data — the first 80 candles. For these 80 data points, we use only the close values of the candles.
Using the close values of the first 80 candles, we calculate the Relative Strength Index (RSI), which will be used to compare with the price data to identify divergences.
For both the RSI values and the candle closes, we identify local highs and lows using a custom function. These local extrema are determined based on a given order value (in this case, 4
). The order value determines how many data points on either side are compared to classify a point as a local high or low.
After identifying the local maxima and minima in the previous step, we now draw trend lines between these points to detect potential trends and divergences.
For each set of maxima (or minima), we draw lines connecting the points, creating a visual representation of trends:
- Highs: Connecting successive higher or lower highs.
- Lows: Connecting successive higher or lower lows.
These lines help highlight possible divergences between price and RSI.
We overlay these trend lines on two charts:
- RSI Chart: Displays the RSI values with the trend lines drawn between the maxima (or minima).
- Price Chart: Displays the candle close prices with similar trend lines.
The trend lines use the following color scheme:
- Red: Higher highs.
- Green: Higher lows.
- Blue: Lower lows.
- Pink: Lower highs.
After plotting the trend lines, we now analyze the peaks (highs and lows) for both the price and RSI to identify possible divergences.
We create separate lists for:
- High Peaks: Contains the high points for both price and RSI.
- Low Peaks: Contains the low points for both price and RSI.
By comparing the lists of highs and lows for price and RSI:
- We detect whether the price and RSI are diverging.
- For example, if the price shows a higher high while the RSI shows a lower high(or vice versa), this could signal a divergence.
The detected divergences are saved to a list and printed for reference.
In our example, the results are as follows:
Divergences Detected:
- Hidden Bullish Divergence:
Date: 2024-10-18 10:00:00 +0200 CEST - Regular Bearish Divergence:
Date: 2024-10-19 06:00:00 +0200 CEST - Regular Bearish Divergence:
Date: 2024-10-21 14:00:00 +0200 CEST
We confirm these divergences visually using the previously generated plots.
Note: The images above are generated programmatically from the loaded data.