Moving Average with Slope Based Color

Posted by on Mar 15, 2009 in Indicator, Support and Resistance | 0 comments

Here’s a real simple script that will plot the moving average of your choice with the color of the line based on the slope of the line. If you want different colors, just change the two color constants in the line of code:
ave.AssignValueColor(if ave > ave[1] then color.cyan else color.dark_red);
to your preference.

Moving Average with Slope Based Color

Moving Average with Slope Based Color

# TS_MOVINGAVERAGE
# http://www.thinkscripter.com
# thinkscripter@gmail.com
# Last Update 15 MAR 2009

input displace = 0;
input length = 9;
input price = close;
input movingAverageType = {default Simple, Exponential, Weighted, Hull, Variable};

rec data;

switch (movingAverageType) {
case Simple:
    data = compoundValue(1, Average(price[-displace], length), price);
case Exponential:
    data = compoundValue(1, ExpAverage(price[-displace], length), price);
case Weighted:
    data = compoundValue(1, wma(price[-displace], length), price);
Case Hull:
    data = compoundValue(1, hullMovingAvg(price[-displace], length), price);
case variable:
    data = VariableMA(price=price, length=length);
}

plot ave = data;
ave.SetLineWeight(2);
ave.AssignValueColor(if ave > ave[1] then color.cyan else color.dark_red);
ave.HideBubble();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>