Moving Avg with Color Change BUT I want to select colors for up/down

rh19

New member
I searched for something like this but couldn't find it. I'm trying to create a moving average where the color changes based on whether it's in an uptrend or downtrend. BUT I want to be able to change the uptrend/downtrend colors from the study customizing menu, so I can use the same study with different lengths and they don't all look the same (i.e 10MA uptrend = green, downtrend = red; 20MA uptrend = blue, downtrend = pink, etc.). My thinking is I need to create two separate MAs in one study so I can setdefaultcolor. I think I have it close, but issues I'm having are:

1. it's leaving gaps on the chart (I'm assuming because of the double.nan, but I don't know what else to use).
2. It has a couple up days colored red.
3. The uptrend and downtrend colors blend on some days, so if I change avgup to yellow and leave avgdown as red, a lot of the line is an orange color.

input movingAverageType = {default Simple, Exponential};
input length = 10;
input price = close;
input displace = 0;

rec MA;

switch (movingAverageType) {
case Simple:
MA = Average(price[-displace], length);
case Exponential:
MA = ExpAverage(price[-displace], length);
}

def aveup = MA > MA[1];
def avedown = MA < MA[1];

plot avgup = if aveup then ma else double.nan;
avgup.SetDefaultColor(Color.GREEN);
avgup.SetLineWeight(1);
avgup.HideBubble();

plot avgdown = if avedown then ma else avgup;
avgdown.SetDefaultColor(Color.RED);
avgdown.SetLineWeight(1);
avgdown.HideBubble();
 
Solution
You can set custom colours that are used in assigned values like this from another thread...
Code:
DefineGlobalColor ("long", Color.Green);
DefineGlobalColor ("short", Color.Red);
MA.AssignValueColor(if (MA - MA[1]) < 0 then GlobalColor("short") else GlobalColor("long"));

This only works on ToS Desktop. If you want the same effect on mobile, you do have to go through the separate up and down plots as in your code.

You can then change the global colours for each of the plots you've placed on your chart independently in the settings for that indicator.

-mashume
You can set custom colours that are used in assigned values like this from another thread...
Code:
DefineGlobalColor ("long", Color.Green);
DefineGlobalColor ("short", Color.Red);
MA.AssignValueColor(if (MA - MA[1]) < 0 then GlobalColor("short") else GlobalColor("long"));

This only works on ToS Desktop. If you want the same effect on mobile, you do have to go through the separate up and down plots as in your code.

You can then change the global colours for each of the plots you've placed on your chart independently in the settings for that indicator.

-mashume
 
Solution
I searched for something like this but couldn't find it. I'm trying to create a moving average where the color changes based on whether it's in an uptrend or downtrend. BUT I want to be able to change the uptrend/downtrend colors from the study customizing menu, so I can use the same study with different lengths and they don't all look the same (i.e 10MA uptrend = green, downtrend = red; 20MA uptrend = blue, downtrend = pink, etc.). My thinking is I need to create two separate MAs in one study so I can setdefaultcolor. I think I have it close, but issues I'm having are:

1. it's leaving gaps on the chart (I'm assuming because of the double.nan, but I don't know what else to use).
2. It has a couple up days colored red.
3. The uptrend and downtrend colors blend on some days, so if I change avgup to yellow and leave avgdown as red, a lot of the line is an orange color.

input movingAverageType = {default Simple, Exponential};
input length = 10;
input price = close;
input displace = 0;

rec MA;

switch (movingAverageType) {
case Simple:
MA = Average(price[-displace], length);
case Exponential:
MA = ExpAverage(price[-displace], length);
}

def aveup = MA > MA[1];
def avedown = MA < MA[1];

plot avgup = if aveup then ma else double.nan;
avgup.SetDefaultColor(Color.GREEN);
avgup.SetLineWeight(1);
avgup.HideBubble();

plot avgdown = if avedown then ma else avgup;
avgdown.SetDefaultColor(Color.RED);
avgdown.SetLineWeight(1);
avgdown.HideBubble();


Code:
# 1. it's leaving gaps...
# when using 2+ plots to draw a line, there can be sections between bars that are not drawn. this is because 1 signal stops being true and on the next bar, a different variable is true.

# the best fix to use
# AssignValueColor() , like mashume mentioned.

#a workaround would be to have the 2 signals both true on the first bar of a new signal. then the space between bars will be plotted.
# ex.  plot avgup = if ( !avedown[1] and avedown ) or aveup then ma else double.nan;

# i changed the average formula and removed the switch-case

input movingAverageType =  AverageType.simple;
input length = 10;
input price = close;
def ma = MovingAverage(movingAverageType, close, length);

def aveup = MA > MA[1];
def avedown = MA < MA[1];

#plot avgup = if aveup then ma else double.nan;
plot avgup = if ( !avedown[1] and avedown ) or aveup then ma else double.nan;
avgup.SetDefaultColor(Color.GREEN);
avgup.SetLineWeight(1);
avgup.HideBubble();

#plot avgdown = if avedown then ma else avgup;
plot avgdown = if ( !aveup[1] and aveup ) or avedown then ma else double.nan;
avgdown.SetDefaultColor(Color.RED);
avgdown.SetLineWeight(1);
avgdown.HideBubble();

#------------------------------------------------

#if you want to pick colors from the edit studies window, there are a couple of ways.

#pick 3 RGB numbers and define a global color
# here is another way to choose a color.
# define a color by picking 3 RGB numbers

# add global color  0-255
input red1 = 111;
input green1 = 71;
input blue1 = 155;

DefineGlobalColor("shade1", CreateColor(red1,green1,blue1));
# use this  ,  GlobalColor("shade1")
addcloud( (ma*0.996), (ma*0.98), GlobalColor( "shade1"), GlobalColor( "shade1"));

#------------------------------------------------

#choose a color number , 0-9
#and use getcolor( )
input color1 = 0;
addlabel(1, "a number with getcolor() " + color1, getcolor(color1));
#plot z = ma + 1;
#z.setdefaultcolor(getcolor(color1));

#------------------------------------------------

#choose a color name
#this is sort of sloppy and misleading.

#the sequence of color words can't be changed. it coresponds to the colors used with getcolor. when the input variable is used as a color number in getcolor, it is changed to a number, that is the nth number in the series.
#ex. pink is the 3rd number.
#3 is put into addlabel.

input Color2 = {default "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
AddLabel(1, "color2 word  " + color2 + " " + (1 * color2), GetColor(Color2));

#you could change the color words to this and it would work the same.
input Color3 = {default "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9" };
AddLabel(yes, "color3 word " + Color3 + " " + (1 * color3), GetColor(Color3));


pick 3 RGB numbers and define a global color
https://usethinkscript.com/threads/change-color-favorites.7872/#post-75439

choose a color name
https://usethinkscript.com/threads/label-color-in-thinkscript.4045/page-3#post-67014
 
This "MA_color" was a contribution by a member over at a futures site I've been a member of. Thanks to meber Martin Begley.

##############################################
#
# Calculate and Plot Moving Average With Colors
# by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};

def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
215 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