sclistella13
New member
I am new to this forum and not a coder. I am trying to paint line color based on the trend. I have it semi working now, however the issue I'm having is when the line ticks up it turns green, but if the next move is sideways, then it paints red; and vice versa, when price ticks down it is red, but if the next move is sideways it is green. See attached screenshot and code. Notice how when the price stair steps down it prints red down and green sideways.
Code:
declare lower;
script macd {
input agg = AggregationPeriod.TWO_MIN;
input fastLength = 3;
input slowLength = 10;
input MACDLength = 1000;
input averageType = AverageType.EXPONENTIAL;
plot Value = MovingAverage(averageType, close(period = agg), fastLength) - MovingAverage(averageType, close(period = agg), slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
}
input agg1 = AggregationPeriod.FIVE_MIN;
input agg2 = AggregationPeriod.TEN_MIN;
input agg3 = AggregationPeriod.FIFTEEN_MIN;
input agg4 = AggregationPeriod.TWENTY_MIN;
input agg5 = AggregationPeriod.THIRTY_MIN;
input agg6 = AggregationPeriod.HOUR;
plot diff1 = MACD(agg1).Diff;
plot diff2 = MACD(agg2).Diff;
plot diff3 = MACD(agg3).Diff;
plot diff4 = MACD(agg4).Diff;
plot diff5 = MACD(agg5).Diff;
plot diff6 = MACD(agg6).Diff;
plot ZeroLine = 0;
def xdiff1 = diff1 >= 0;
def xdiff2 = diff2 >= 0;
def xdiff3 = diff3 >= 0;
def xdiff4 = diff4 >= 0;
def xdiff5 = diff5 >= 0;
def xdiff6 = diff6 >= 0;
Diff1.SetPaintingStrategy(PaintingStrategy.LINE);
Diff1.SetLineWeight(1);
Diff1.DefineColor("Positive and Up", Color.GREEN);
Diff1.DefineColor("Positive and Down", Color.RED);
Diff1.DefineColor("Negative and Down", Color.RED);
Diff1.DefineColor("Negative and Up", Color.GREEN);
Diff1.AssignValueColor(if Diff1 >= 0 then if Diff1 > Diff1[1] then Diff1.Color("Positive and Up") else Diff1.Color("Positive and Down") else if Diff1 < Diff1[1] then Diff1.Color("Negative and Down") else Diff1.Color("Negative and Up"));
Diff2.SetPaintingStrategy(PaintingStrategy.LINE);
Diff2.SetLineWeight(1);
Diff2.DefineColor("Positive and Up", Color.GREEN);
Diff2.DefineColor("Positive and Down", Color.RED);
Diff2.DefineColor("Negative and Down", Color.RED);
Diff2.DefineColor("Negative and Up", Color.GREEN);
Diff2.AssignValueColor(if Diff2 >= 0 then if Diff2 > Diff2[1] then Diff2.Color("Positive and Up") else Diff2.Color("Positive and Down") else if Diff2 < Diff2[1] then Diff2.Color("Negative and Down") else Diff2.Color("Negative and Up"));
Diff3.SetPaintingStrategy(PaintingStrategy.LINE);
Diff3.SetLineWeight(1);
Diff3.DefineColor("Positive and Up", Color.GREEN);
Diff3.DefineColor("Positive and Down", Color.RED);
Diff3.DefineColor("Negative and Down", Color.RED);
Diff3.DefineColor("Negative and Up", Color.GREEN);
Diff3.AssignValueColor(if Diff3 >= 0 then if Diff3 > Diff3[1] then Diff3.Color("Positive and Up") else Diff3.Color("Positive and Down") else if Diff3 < Diff3[1] then Diff3.Color("Negative and Down") else Diff3.Color("Negative and Up"));
Diff4.SetPaintingStrategy(PaintingStrategy.LINE);
Diff4.SetLineWeight(1);
Diff4.DefineColor("Positive and Up", Color.GREEN);
Diff4.DefineColor("Positive and Down", Color.RED);
Diff4.DefineColor("Negative and Down", Color.RED);
Diff4.DefineColor("Negative and Up", Color.GREEN);
Diff4.AssignValueColor(if Diff4 >= 0 then if Diff4 > Diff4[1] then Diff4.Color("Positive and Up") else Diff4.Color("Positive and Down") else if Diff4 < Diff4[1] then Diff4.Color("Negative and Down") else Diff4.Color("Negative and Up"));
Diff5.SetPaintingStrategy(PaintingStrategy.LINE);
Diff5.SetLineWeight(1);
Diff5.DefineColor("Positive and Up", Color.GREEN);
Diff5.DefineColor("Positive and Down", Color.RED);
Diff5.DefineColor("Negative and Down", Color.RED);
Diff5.DefineColor("Negative and Up", Color.GREEN);
Diff5.AssignValueColor(if Diff5 >= 0 then if Diff5 > Diff5[1] then Diff5.Color("Positive and Up") else Diff5.Color("Positive and Down") else if Diff5 < Diff5[1] then Diff5.Color("Negative and Down") else Diff5.Color("Negative and Up"));
Diff6.SetPaintingStrategy(PaintingStrategy.LINE);
Diff6.SetLineWeight(1);
Diff6.DefineColor("Positive and Up", Color.GREEN);
Diff6.DefineColor("Positive and Down", Color.RED);
Diff6.DefineColor("Negative and Down", Color.RED);
Diff6.DefineColor("Negative and Up", Color.GREEN);
Diff6.AssignValueColor(if Diff6 >= 0 then if Diff6 > Diff6[1] then Diff6.Color("Positive and Up") else Diff6.Color("Positive and Down") else if Diff6 < Diff6[1] then Diff6.Color("Negative and Down") else Diff6.Color("Negative and Up"));