Compute the Slope of a Moving Average

Chuck

Moderator
Staff member
Used to compute the slope of a 34-period Hull moving average of Heikin-Ashi Range 5-tick candles.

This script is different from TOS's LinearRegressionSlope study which seems to use the slopes of the designated-period linear regression lines to display the changing slope of linear regression lines (The TOS Linear Regression Slope). This one uses trigonometry. It computes ArcTangent of Rise/Run in radians then converts to degrees.

The angle is apparently in degrees, however the values on the right of the chart are different from values directed by ThinkScript. A ThingScript value designated as 0.01 degrees is plotted as 10 degrees on the chart.
This looks like it could be useful in picturing swing-tops and swing-bottoms, and of course, a positive (negative) slope indicates a rising( falling) trend.

Code:
SCRIPT:
#   Slope of longer-term Moving Average------------------------
declare lower;

def c = close;
input length = 34;
input averageType = AverageType.Hull;

def avg = MovingAverage(averageType, c, length);

def height = avg - avg[1];

plot “Angle, deg” = Atan(height/length) * 180 / Double.Pi;

“Angle, deg”.SetDefaultColor(Color.BLUE);
“Angle, deg”.SetLineWeight(5);
“Angle, deg”.SetStyle(Curve.FIRM);
“Angle, deg”.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
“Angle, deg”.HideBubble();
“Angle, deg”.HideTitle();


“Angle, deg”.AssignValueColor
(if
“Angle, deg” > “Angle, deg” [1]
  then
Color.ORANGE else (if “Angle, deg” == “Angle, deg” [1] then Color.BLUE else Color.BLUE));

plot zero = 0.00;
zero.SetStyle(Curve.SHORT_DASH);
zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);
zero.HideBubble();
zero.HideTitle();

input SIGpos = 0.01;
input SIGneg = -0.01;

plot Sp = SIGpos;
plot Sn = SIGneg;

Sp.SetStyle(Curve.FIRM);
Sp.SetDefaultColor(Color.LIGHT_GREEN);
Sp.SetLineWeight(1);
Sp.HideBubble();
Sp.HideTitle();

Sn.SetStyle(Curve.FIRM);
Sn.SetDefaultColor(Color.MAGENTA);
Sn.SetLineWeight(1);
Sn.HideBubble();
Sn.HideTitle();

def hiLevel = if “Angle, deg” >= 0 then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY;

AddCloud (0, hiLevel, Color.DARK_GRAY, Color.gray);

#     END===================================================
 

Mashume

New member
Angles are funny things. To say that an angle is 45° is also to say that for every unit to the right, the line moves 1 unit up. Since our x axis units are minutes and our y axis are dollars, would a 45° angle be $1 per minute? 1¢ per 5 minutes...? You see where this is going. Expressing slopes in dollars per hour might be useful, but then so might ticks per 500 trades. As long as you are comparing apples to apples you should be ok. Figure out what ToS is giving you for units and what you're calculating and then decide what you want to see.

Happy trading,
Mashume
 
Top