How to calculate the gradient (slope) of a line

 

Hi,

I want to write an EA which works with the gradient (slope) of the EMA-Indicator.

How can I do that? Do you have any examples?

Thank you, sunshineh

 
the different of values of EMA is gradient, since dx is only count of periods.
 

To expand on what DxdCn is saying...if you take the difference (subtract) between any two sequential EMA values the result is the slope.

slope = rise/run

The "rise" is the change in the EMA value from one candle to the next. The "run" is the difference in candles which would be 1 in this simplistic example.

This results in the units of the gradient as being "change in EMA price per candle"...so if you are using a M5 timeframe the calculation would result in a value that was "the change in EMA price per 5-minute candle".

If you wanted to convert this to change in price per unit time (say "change in EMA price per minute") irrespective of the timeframe then you would need to also divide the gradient by the timeframe.

Some people prefer gradients in candles, others prefer them to be in time. There is no wrong answer unless you meant to compute one but inadvertantly computed the other. So make sure your gradient's units end up being what you actually want the gradient value itself to speak to.

 
I think what was asked is the indicator line gradient in degrees. i.e. horizontal: 0 degrees and vertical 90 degrees.
 
debeertinus: I think what was asked is the indicator line gradient in degrees. i.e. horizontal: 0 degrees and vertical 90 degrees.
Can't be done.
  1. There is no angle from 2 anchor points. An angle requires distance divided by distance; a unit-less number. A chart has price and time. What is the angle of going 30 miles in 45 minutes? Meaningless!

  2. You can get the slope from a trendline: m=ΔPrice÷ΔTime. Changing the price scale, bar width, or window size, changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create an angled line using one point and setting the angle (Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference.) The line is drawn that angle in pixels. Changing the axes scales does NOT change the angle (and thus is meaningless.)

  4. If you insist, take the two price/time coordinates, convert them to pixel coordinates and then to apparent angle with arctan.
              How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 programming forum - Page 2

 

Unsure if this is correct, but I use something like the below to create an idea of slope to filter out heavily trending markets.

double MA[];
ArraySetAsSeries(MA, true);
MovingAverage = iMA(Symbol(), PERIOD_D1, 200, 0, MODE_EMA, PRICE_CLOSE);
CopyBuffer(MovingAverage, 0, 0, 11, MA);

double MovingAverageValue = NormalizeDouble(MA[0], 4);
double MovingAverageValueOld = NormalizeDouble(MA[10], 4);

Slope = ((MovingAverageValue-MovingAverageValueOld) / 10);
        
  if( (Slope<0)
 
okwh #:
the different of values of EMA is gradient, since dx is only count of periods.
#property strict

// Input parameters
input int emaPeriod = 20; // Period for EMA calculation
input double threshold = 0.001; // Threshold for slope detection
input int signalBarCount = 2; // Number of bars to consider for signal confirmation

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Calculate EMA values
    double emaCurrent = iMA(NULL, 0, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
    double emaPrevious = iMA(NULL, 0, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

    // Calculate slope
    double slope = emaCurrent - emaPrevious;

    // Check for bullish slope
    if (slope > threshold)
    {
        // Add your buy signal logic here
        // Example: Place a buy trade
        // OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "Buy Order", 0, 0, clrGreen);
        Print("Bullish slope detected!");
    }
    // Check for bearish slope
    else if (slope < -threshold)
    {
        // Add your sell signal logic here
        // Example: Place a sell trade
        // OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, 0, 0, "Sell Order", 0, 0, clrRed);
        Print("Bearish slope detected!");
    }
}

//+------------------------------------------------------------------+
you can use the iMA() function to calculate the EMA values and then analyze the slope based on the change in EMA values over time.
Reason: