# Code from Hahn Tech had no header.
# Added labels for three time frames.
# The chart time frame and two user chosen higher time frames. Labels turn red or green depending on MACD.
# Additions by Horserider 9/30/2019
input midTermPeriod = {"1 min", "2 min", "3 min", "4 min", "5 min", "10 min", "15 min", "20 min", "30 min", "60 min", "120 min", "4 hours", "Daily", "2 days", "3 days", "4 days", default "Weekly", "Monthly"};
input longTermPeriod = { "2 min", "3 min", "4 min", "5 min", "10 min", "15 min", "20 min", "30 min", "60 min", "120 min", "4 hours", "Daily", "2 days", "3 days", "4 days", "Weekly", default "Monthly"};
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.WEIGHTED;
input midTermFastLength = 12;
input midTermSlowLength = 26;
input midTermMACDLength = 9;
input midTermaverageType = AverageType.WEIGHTED;
input longTermFastLength = 12;
input longTermSlowLength = 26;
input longTermMACDLength = 9;
input longTermaverageType = AverageType.WEIGHTED;
def middleAggregation;
switch (midTermPeriod) {
case "1 min":
middleAggregation = AggregationPeriod.MIN;
case "2 min":
middleAggregation = AggregationPeriod.TWO_MIN;
case "3 min":
middleAggregation = AggregationPeriod.THREE_MIN;
case "4 min":
middleAggregation = AggregationPeriod.FOUR_MIN;
case "5 min":
middleAggregation = AggregationPeriod.FIVE_MIN;
case "10 min":
middleAggregation = AggregationPeriod.TEN_MIN;
case "15 min":
middleAggregation = AggregationPeriod.FIFTEEN_MIN;
case "20 min":
middleAggregation = AggregationPeriod.TWENTY_MIN;
case "30 min":
middleAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
middleAggregation = AggregationPeriod.HOUR;
case "120 min":
middleAggregation = AggregationPeriod.TWO_HOURS;
case "4 hours":
middleAggregation = AggregationPeriod.FOUR_HOURS;
case "Daily":
middleAggregation = AggregationPeriod.DAY;
case "2 days":
middleAggregation = AggregationPeriod.TWO_DAYS;
case "3 days":
middleAggregation = AggregationPeriod.THREE_DAYS;
case "4 days":
middleAggregation = AggregationPeriod.FOUR_DAYS;
case "Weekly":
middleAggregation = AggregationPeriod.WEEK;
case "Monthly":
middleAggregation = AggregationPeriod.MONTH;
}
def highestAggregation;
switch (longTermPeriod) {
case "2 min":
highestAggregation = AggregationPeriod.TWO_MIN;
case "3 min":
highestAggregation = AggregationPeriod.THREE_MIN;
case "4 min":
highestAggregation = AggregationPeriod.FOUR_MIN;
case "5 min":
highestAggregation = AggregationPeriod.FIVE_MIN;
case "10 min":
highestAggregation = AggregationPeriod.TEN_MIN;
case "15 min":
highestAggregation = AggregationPeriod.FIFTEEN_MIN;
case "20 min":
highestAggregation = AggregationPeriod.TWENTY_MIN;
case "30 min":
highestAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
highestAggregation = AggregationPeriod.HOUR;
case "120 min":
highestAggregation = AggregationPeriod.TWO_HOURS;
case "4 hours":
highestAggregation = AggregationPeriod.FOUR_HOURS;
case "Daily":
highestAggregation = AggregationPeriod.DAY;
case "2 days":
highestAggregation = AggregationPeriod.TWO_DAYS;
case "3 days":
highestAggregation = AggregationPeriod.THREE_DAYS;
case "4 days":
highestAggregation = AggregationPeriod.FOUR_DAYS;
case "Weekly":
highestAggregation = AggregationPeriod.WEEK;
case "Monthly":
highestAggregation = AggregationPeriod.MONTH;
}
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.RED);
DefineGlobalColor("NoTrend", Color.LIGHT_GRAY);
def timeFrame = GetAggregationPeriod();
def testTimeFrames = if timeFrame < middleAggregation and middleAggregation < highestAggregation then yes else no;
AddLabel(yes, if testTimeFrames then "TF Correct" else "TF Wrong", if testTimeFrames then Color.GREEN else Color.RED);
# This section is for the chart level MACD
def fastAvg = WMA(close, fastLength);
def slowAvg = WMA(close, slowLength);
plot Value = fastAvg - slowAvg;
Value.SetDefaultColor(Color.CYAN);
plot Avg = WMA(Value, MACDLength);
Avg.SetDefaultColor(Color.YELLOW);
plot Diff = (Value - Avg) * 3;
# This section is for the medium term MACD
def midTermFastAvg = WMA(close(period = middleAggregation) , midTermFastLength);
def midTermSlowAvg = WMA(close(period = middleAggregation) , midTermSlowLength);
def midTermValue = midTermFastAvg - midTermSlowAvg;
def midTermAvg = WMA(midTermValue, midTermMACDLength);
plot midTermDiff = (midTermValue - midTermAvg) * 3;
midTermDiff.Hide();
midTermDiff.HideBubble();
# This section is for the long term MACD
def longTermFastAvg = WMA(close(period = highestAggregation) , longTermFastLength);
def longTermSlowAvg = WMA(close(period = highestAggregation) , longTermSlowLength);
def longTermValue = longTermFastAvg - longTermSlowAvg;
def longTermAvg = WMA(longTermValue, longTermMACDLength);
plot longTermDiff = (longTermValue - longTermAvg) * 3;
longTermDiff.Hide();
longTermDiff.HideBubble();
def midTermLower = midTermDiff < midTermDiff[1];
def midTermHigher = midTermDiff > midTermDiff[1];
rec midTermSignal = if midTermLower then yes else if midTermSignal[1] == yes and midTermHigher == no then yes else no;
#plot test = midTermSignal;
def longTermLower = longTermDiff < longTermDiff[1];
def longTermHigher = longTermDiff > longTermDiff[1];
rec longTermSignal = if longTermLower then yes else if longTermSignal[1] == yes and longTermHigher == no then yes else no;
midTermDiff.AssignValueColor(if midTermSignal then Color.RED else Color.BLUE);
longTermDiff.AssignValueColor(if longTermSignal then Color.RED else Color.BLUE);
Diff.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
plot zeroLine = if close[-1] > 0 then 0 else Double.NaN;
zeroLine.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
zeroLine.SetPaintingStrategy(PaintingStrategy.POINTS);
zeroLine.SetLineWeight(3);
AddLabel(yes, if Value > Avg == yes then "SHORT" else "Short", if Value < Avg == no then Color.GREEN else Color.RED);
AddLabel(yes, if midTermSignal == yes then "MID" else "Mid", if midTermSignal == no then Color.GREEN else Color.RED);
AddLabel(yes, if longTermSignal == yes then "LONG" else "Long", if longTermSignal == no then Color.GREEN else Color.RED);