Category Oscilator  at 16/05/2023

Slope Divergence


Slope divergence is an intriguing concept in technical analysis, where the slope of a price and the slope of a momentum indicator (e.g., RSI) are compared to identify potential divergences. 

This indicator calculates the slope of the price and RSI over a specific lookback period and normalizes these values to bring them onto a similar scale. It plots two lines representing the normalized slopes of the price and RSI.

To analyze the indicator values, follow these steps:

  1. Observe the two lines on the chart: one line represents the normalized slope of the price, and the other line represents the normalized slope of the RSI. When the lines are close to each other or overlap, it indicates that the price and RSI are moving in the same direction, and there is no divergence.

  2. Look for instances where the two lines move in opposite directions. This may indicate a divergence between the price and the RSI. Divergence can suggest potential trend reversals or weakening trends.

  3. Specifically, monitor for the following scenarios: a. If the price slope line is moving upward while the RSI slope line is moving downward, this indicates a bearish divergence. It suggests that the price increase might be losing momentum, and a downward trend reversal could follow. b. If the price slope line is moving downward while the RSI slope line is moving upward, this indicates a bullish divergence. It suggests that the price decrease might be losing momentum, and an upward trend reversal could follow.

  4. Keep in mind that divergences are not always reliable signals for trend reversals, and they should be used in conjunction with other technical analysis tools and market context. Look for additional confirmation signals such as trendlines, support, and resistance levels, or other technical indicators to strengthen the divergence analysis.


I also talk in detail about the Concept of Slope Divergence in my Blog Here. Also, check my Divergence Indicator Here.


If you have any suggestions or Feedback pls contact me:

Check my new website:



using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Linq;

namespace cAlgo.Indicators
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SlopeDivergenceOscillator : Indicator
        [Parameter("RSI Period", DefaultValue = 14, MinValue = 1)]
        public int RsiPeriod { get; set; }

        [Parameter("Lookback Period", DefaultValue = 14, MinValue = 1)]
        public int LookbackPeriod { get; set; }

        [Output("Normalized Price Slope", LineColor = "Green")]
        public IndicatorDataSeries PriceSlopeLine { get; set; }

        [Output("Normalized RSI Slope", LineColor = "Red")]
        public IndicatorDataSeries RsiSlopeLine { get; set; }

        private RelativeStrengthIndex _rsi;

        protected override void Initialize()
            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriod);

        public override void Calculate(int index)
            if (index < LookbackPeriod)

            double[] priceData = new double[LookbackPeriod + 1];
            double[] rsiData = new double[LookbackPeriod + 1];

            for (int i = 0; i <= LookbackPeriod; i++)
                priceData[i] = Bars.ClosePrices[index - LookbackPeriod + i];
                rsiData[i] = _rsi.Result[index - LookbackPeriod + i];

            double priceSlope = CalculateSlope(priceData);
            double rsiSlope = CalculateSlope(rsiData);

            double[] slopes = new double[LookbackPeriod * 2 + 2];

            for (int i = 0; i <= LookbackPeriod; i++)
                slopes[i] = CalculateSlope(priceData.Skip(i).Take(LookbackPeriod).ToArray());
                slopes[LookbackPeriod + 1 + i] = CalculateSlope(rsiData.Skip(i).Take(LookbackPeriod).ToArray()) * 100;

            double minSlope = slopes.Min();
            double maxSlope = slopes.Max();

            PriceSlopeLine[index] = Normalize(priceSlope, minSlope, maxSlope);
            RsiSlopeLine[index] = Normalize(rsiSlope * 100, minSlope, maxSlope);

        private double CalculateSlope(double[] data)
            int n = data.Length;
            double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;

            for (int i = 0; i < n; i++)
                sumX += i;
                sumY += data[i];
                sumXY += i * data[i];
                sumX2 += i * i;

            double numerator = n * sumXY - sumX * sumY;
            double denominator = n * sumX2 - sumX * sumX;

            if (denominator == 0)
                return 0;

            return numerator / denominator;

        private double Normalize(double value, double min, double max)
            return (value - min) / ((max - min));


Joined 07.05.2023

  • Type: Free
  • Language: C#
  • Trading Platform: cTrader Automate
  • Filename: Slope Divergence.algo
  • Rating: 5
  • Downloads: 212
Only logged in users can post a comment
Comments not found