Moving Average Slope

JamesB

Member
I would like to know what the actual angle of the 50 SMA is in degrees. Below is what I have currently but I know that it is not correct as I believe that it is measuring the change in the SMA to 3 bars ago. I greatly appreciate your help in this matter!

input length = 50;
plot SMA = Average(close, length);
SMA.SetDefaultColor(Color.WHITE);
SMA.DefineColor("Up", Color.YELLOW);
SMA.DefineColor("Down", Color.DARK_GRAY);
SMA.AssignValueColor(if SMA > SMA[1] then SMA.Color("Up") else SMA.Color("Down"));
SMA.SetLineWeight(5);
SMA.HideBubble();

# ----- apply linear regression slope to SMA line -----
def LRlength = 3;
def angle = ATan(LinearRegressionSlope(SMA, LRlength)) * 180 / Double.Pi;
AddLabel(yes, "SMA: " + round(angle,1), Color.Yellow);
 
@JamesB this is how I've always calculated slopes.
Ruby:
def length = 50;
def SMA = Average(close,length);
def height = AbsValue(SMA - SMA[length]);
def SMAAngle = (ATan(height / length) * 180) / Double.Pi;
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

My ultimate goal behind gaining this information is to know when the 50 SMA is fairly flat in comparison to trending. Sometimes the 50 can be angling ever so slightly in one direction or another, but in reality it is flat. Thus, I want to be able to quantify this.
 
this code does not work
this code does not work, does not provide enough information to say where you went astray.

This code snippet is a textbook computation of slope.

This is not a study, it is just a code snippet. What study did you incorporate this slope equation into?
Please provide:
  1. an image of what you were attempting to calculate with this slope computation
  2. the complete study that you added this slope equation to
  3. a description of what you wanted your study to accomplish
then members will be better able to assist you.
 
Last edited:
@JamesB

OK. I'll bite.

I've written about angles around here somewhere before, but I can't find it at the moment. The issue with trying to describe an angle for the movement of an average is that what you think of as angles generally have fixed units for x and y -- centimeters, kilometers, etc... -- and what you have for a moving average for a financial instrument is price vs time. You can talk about rates in finance (as in +5¢ per hour or -$0.0025 per minute) the same way you can talk about highway grades (100 feet per mile up, etc...) but it is very hard to talk about the angles of a moving average as you can adjust both the vertical resolution and the timeframe in so many ways.

Add to this that, as you noted above, the way we calculate a slope in ThinkScript is to look at the change from some point in the past (in the case above I believe the angle was calculated from the 3 bar linear regression slope). The minimum distance for finding an angle in ThinkScript is going to be 2 bars. This bar and the last. IF the curve of the moving average had a nice clean mathematical definition (as a function of t) then you could find the derivative of it at the final point in time (at time t) and determine the slope of the line at a single point. But we don't generally have nice mathematical functions for price movement. If you have found one, please do post it here though. :)

That said, I believe what you're looking for is a way to determine whether the 50SMA is moving up or down. or is largely flat. If you use this bar and the last bar as the two points for the slope of your line, the you would have something like this:

Code:
declare lower;
def sma_length = 50;
def slope_lookback = 1;
def SMA = Average(close, sma_length);
def height = AbsValue(SMA - SMA[slope_lookback]);
plot SMAAngle = (ATan(height / slope_lookback) * 180) / Double.Pi;

You can then do a comparison of the value of the SMAAngle this bar with the value from some point in the past if you want.

There are more advanced statistical things you can do with a slope, including z-scores to determine whether the current slope is in line with the slope from the last n bars or whether it is different in a statistically significant way. You can plot the slope vs 0, or deviation from 0. You can remove the absValue function and integrate (sum) over the last n bars to decide how flat it has been (the closer to 0 the flatter).

What you do with this information is up to you. There are a great many talented coders around here who can help, if you can define what it is you want (mathematically or simply as specifically as you can in non-math terms). However, without better specs and an example or two (images are great, with annotations are better -- your investment of time and sweat are generally appreciated when asking for help) it will be difficult for people to help out much beyond what is here already.

Good luck and happy trading,
-mashume
 
My ultimate goal behind gaining this information is to know when the 50 SMA is fairly flat in comparison to trending. Sometimes the 50 can be angling ever so slightly in one direction or another, but in reality it is flat. Thus, I want to be able to quantify this.

this doesn't make any sense.
Comparing one line to some trend line does not determine flatness.
on a chart a horizontal price line is flat. any other line is not.
you can compare lines to see if they are parallel.

If you perceive a line to be angled slightly but then changing the view/zoom, makes it appear different , then that shows how trying to use angles on a stock chart isn't going to work, because you have different units for X and Y.

If you look at svanoy formula, at the heart of it is a change in height divided by a horizontal distance.
that is what you should be looking for,
a rate of change in price divided by X quantity of bars.
to make it universal, change that to be a percentage change in price over x bars.
 
@JamesB

OK. I'll bite.

I've written about angles around here somewhere before, but I can't find it at the moment. The issue with trying to describe an angle for the movement of an average is that what you think of as angles generally have fixed units for x and y -- centimeters, kilometers, etc... -- and what you have for a moving average for a financial instrument is price vs time. You can talk about rates in finance (as in +5¢ per hour or -$0.0025 per minute) the same way you can talk about highway grades (100 feet per mile up, etc...) but it is very hard to talk about the angles of a moving average as you can adjust both the vertical resolution and the timeframe in so many ways.

Add to this that, as you noted above, the way we calculate a slope in ThinkScript is to look at the change from some point in the past (in the case above I believe the angle was calculated from the 3 bar linear regression slope). The minimum distance for finding an angle in ThinkScript is going to be 2 bars. This bar and the last. IF the curve of the moving average had a nice clean mathematical definition (as a function of t) then you could find the derivative of it at the final point in time (at time t) and determine the slope of the line at a single point. But we don't generally have nice mathematical functions for price movement. If you have found one, please do post it here though. :)

That said, I believe what you're looking for is a way to determine whether the 50SMA is moving up or down. or is largely flat. If you use this bar and the last bar as the two points for the slope of your line, the you would have something like this:

Code:
declare lower;
def sma_length = 50;
def slope_lookback = 1;
def SMA = Average(close, sma_length);
def height = AbsValue(SMA - SMA[slope_lookback]);
plot SMAAngle = (ATan(height / slope_lookback) * 180) / Double.Pi;

You can then do a comparison of the value of the SMAAngle this bar with the value from some point in the past if you want.

There are more advanced statistical things you can do with a slope, including z-scores to determine whether the current slope is in line with the slope from the last n bars or whether it is different in a statistically significant way. You can plot the slope vs 0, or deviation from 0. You can remove the absValue function and integrate (sum) over the last n bars to decide how flat it has been (the closer to 0 the flatter).

What you do with this information is up to you. There are a great many talented coders around here who can help, if you can define what it is you want (mathematically or simply as specifically as you can in non-math terms). However, without better specs and an example or two (images are great, with annotations are better -- your investment of time and sweat are generally appreciated when asking for help) it will be difficult for people to help out much beyond what is here already.

Good luck and happy trading,
-mashume

I am new to programming (mostly...took a class in BASIC 40ish years ago), so please excuse my ignorance. I am trying to learn thinkscript and wrote/copied a program similar to the one above. I think I understand most of the logic behind the process but i believe the syntax and possibly misunderstandings of the way thinkscript counts and processes data are the main source of my confusion.

The program below is the start of a program that I want to use to identify channels. I think I have narrowed the problem down to my conditional statements because they are not giving the desired return. plot FlatUp returns a horizontal line at slopeVal0 for all negative values, while Flat down does not give a return.
I appreciate any and all help.
Thank you


declare lower;
input length = 10;
input slopeVal0 = .5;
def avg = Average(close, length);
def height = avg - avg[length];
def Slope = ATan(height / length) * 180 / Double.Pi;
#def degree0 =- 0.5 < Angle < .5;
#def degree0 = -.5 < Slope < .5;
def SlopePos0 = 0<=Slope<slopeVal0;
def SlopeNeg0 = -slopeVal0<Slope<0;

#plot "Angle, deg" = ATan(height/length) * 180 / Double.Pi;
plot FlatUpp = if slopePos0 then slopeVal0 else Double.NaN;
plot FlatDn=if slopeNeg0 then -slopeVal0 else double.nan;
plot SlopeCurve=Slope/10;
plot SlopeRef=0;
SlopeRef.setDefaultColor(color.magenta);
 
I am new to programming (mostly...took a class in BASIC 40ish years ago), so please excuse my ignorance. I am trying to learn thinkscript and wrote/copied a program similar to the one above. I think I understand most of the logic behind the process but i believe the syntax and possibly misunderstandings of the way thinkscript counts and processes data are the main source of my confusion.

The program below is the start of a program that I want to use to identify channels. I think I have narrowed the problem down to my conditional statements because they are not giving the desired return. plot FlatUp returns a horizontal line at slopeVal0 for all negative values, while Flat down does not give a return.
I appreciate any and all help.
Thank you


declare lower;
input length = 10;
input slopeVal0 = .5;
def avg = Average(close, length);
def height = avg - avg[length];
def Slope = ATan(height / length) * 180 / Double.Pi;
#def degree0 =- 0.5 < Angle < .5;
#def degree0 = -.5 < Slope < .5;
def SlopePos0 = 0<=Slope<slopeVal0;
def SlopeNeg0 = -slopeVal0<Slope<0;

#plot "Angle, deg" = ATan(height/length) * 180 / Double.Pi;
plot FlatUpp = if slopePos0 then slopeVal0 else Double.NaN;
plot FlatDn=if slopeNeg0 then -slopeVal0 else double.nan;
plot SlopeCurve=Slope/10;
plot SlopeRef=0;
SlopeRef.setDefaultColor(color.magenta);
Nevermind....found my mistake. Still not 100% comfortable but it's coming.
:)
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
372 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top