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();
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();