8

I'm want to calculate the slope for the Simple Moving Average's (SMA). I have already tried the following code which is mathematically correct.

rad2degree = 180/3.14159265359  //pi
sma2sample = sma(close,50) 
slopeD = rad2degree*atan( (sma2sample[0] - nz(sma2sample[1]))/1 )

However, the problem is that the value for each stock differs, so the slopeD value is not in a fixed rang, between [-90 to 90] degrees. Which would be more logical.

I believe that I would need to normalize the date in order to have this variable in a fixed range, but I do not know how.

How can I normalize the range of the slopeD?

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Aerox
  • 353
  • 3
  • 13
  • This question lacks context (what is an SMA?) and doesn't seem to really be a programming question at all so much as a technical analysis methodology question (so perhaps [money.se] might be a better place to post -- they have a `technical-analysis` tag). Also -- you use the word "slope" but seem to be talking about *angles* -- which are not the same thing. – John Coleman Sep 05 '18 at 10:30
  • 6
    I've edited the OP to clarify what I believe is the author's intention. Yes, this is a programming problem in *pine-script*. – not2qubit Sep 20 '18 at 09:06
  • Is your question still relevant? – AnyDozer Jan 23 '21 at 15:44
  • Can you explain us what is the 'date' you want to normalize ? I can't see 'date' in your pinescript extract, only bars – G.Lebret May 27 '22 at 07:55

1 Answers1

5

With the version 5 of pinescript there is a way to get the slope of a function : You should import the slope function form the library with :

import mentalRock19315/Slope_TK/1 as TK

Then you can use the slope function with the serie from which you want to extract the slope.
The variable 'size' represent the number of bar between the two points used to calculate the slope.

TK.slope( ta.sma(close, 200), 10 )

The full code could be :

//@version=5

import mentalRock19315/Slope_TK/1 as TK

indicator("Slope", overlay=false)
size = input.int(1,"Number of bars to calculate the slope", minval=1)

sma200 = ta.sma(close,200)

plot( TK.slope(sma200, size), title = "Slope", color=color.blue)
G.Lebret
  • 2,826
  • 2
  • 16
  • 27