Jump to content
Medved Trader Forums

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

Link to comment
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[11])/12  

 

 

Link to comment
Share on other sites

  • 4 weeks later...

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.image.png

or, in advanced code in paintbars or scanner (presuming that EMA is the variable representing the value of the indicator)

     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.

 

 

 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...