# Scanner function question about SLOPE

## Recommended Posts

I would like to use the slope in a scanner rule but cannot find that function in the available functions. Stock charts describes it as following.

the slope measures the rise over run for the linear regression. This is the ending value of the linear regression less the beginning value divided by the timeframe. If the ending value were 35, the beginning value 29 and the run 12, then the slope would be .5 (35 - 29 = 6, 6/12 = .50). The slope indicator is zero when the linear regression is flat, positive when the linear regression slants up and negative when the linear regression slopes down. The steepness of the slope is also reflected in the value.

Do you have something like this in the scanner?

Thanks

##### Share on other sites

Since the advanced scanner code allows you to look at the value for the indicator N candles back, you can do it that way for any indicator. Including the Single Regression Channel indicator.

Let's say you added the Single Regression Channel to the list of variables under the name of SRC. Then the slope would be

(SRC-SRC)/12

Thanks

##### Share on other sites

• 4 weeks later...

Mike, can you elaborate on slope some more and give some examples such as....

How can I calculate the slope of the 20 EMA?

This is new to me and if I see some examples I can probably figure it out...  Thanks

##### Share on other sites

Slope, in mathematics, is dy/dx - where dx is the difference in x between two points and dy is the difference in y. So - if x stays constant, the slope is 0. If the line is vertical (dx=0) then the slope is infinite. (On a coordinate system where X and Y are equivalent, the slope would be the tangent of the angle of the slope).

The problem, of course, is - what are the units of dx (dy, I presume, is price). Because obviously the slope itself, unless it is exactly 0, is pretty meaningless if the units of x are arbitrary and unrelated to the units of y (as is the case in stock charts).

Thus, the only way you can use slope in stock charts is to compare two of them.

To find an approximation of a slope of 20 EMA, or any other function, at point X (let's say X is candle number) is to take its value at X+1, its value at X-1, and divide it by 2 (X+1)-(X-1). The reason it is an approximation and not a real slope is because the chart is not a curve but a set of segments, and the slope at the points where the segments connect is, strictly mathematically, indeterminate.

Another way to find a slope would be to fit a Bezier curve or a spline through a set of points surrounding point X, do symbolic differentiation of the curve function, and find its value at point X. Not really feasible. Too complicated.

A third way would be to do a linear regression slope calculation on let's say last 5 points and presume that the slope is the slope of the regression line. or, in advanced code in paintbars or scanner (presuming that EMA is the variable representing the value of the indicator)

```.fctbNone{ color:#000000; }
.fctbStyle4{ color:#0000ff; }
.fctbStyle3{ color:#000000; }     double sumxy    = 0;
double sumxx    = 0;
double averagex = 2;
double averagey = Average(5,EMA);
for (int x=0;x<5;x++)
{
sumxx += (x-averagex)*(x-averagex);
sumxy += (x-averagex)*(EMA[x]-averagey);
}
var Slope = sumxy/sumxx;```

Which reminds me - I really should find a way to have the MT looping functions to incorporate the n variable. Right now there is no way to do that, so I had to do the actual loop.

• 1
##### Share on other sites

speaking of slopes, I like the way you implemented  the 'inverse' of a line.

seems obvious, but I haven't seen it elsewhere. Might be out there, but not in anything I use

##### Share on other sites

3 hours ago, stock777 said:

speaking of slopes, I like the way you implemented  the 'inverse' of a line.

seems obvious, but I haven't seen it elsewhere. Might be out there, but not in anything I use

You mean in annotations, right?

Always happy to please.

##### Share on other sites

Actually , its the drawing tool , mirror copy.

##### Share on other sites

Drawing = annotations And hm I just put it in for completeness didn't know no one else is doing it.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.