MACD Averages

Rojo Grande

Member
VIP
Last February, @mashume was kind enough to write a program that let me do averaging of RSI's. I was trying to use that as a template and do the same thing with MACD's. I don't code , so it's cut and paste and I have not been able to get it to work. Any help is appreciated.

rC6PXtC.png


Here is the original from Mashume.

Code:
# AVERAGE of RSIs FOR GROUP OF STOCKS
#
# useThinkScript
# 2020-02-26
# [USER=153]@mashume[/USER]

declare lower;

input RSI_TYPE = {default WILDERS, SIMPLE, EXPONENTIAL};
input RSI_LENGTH = 14;

input OS = 40;
input OB = 60;

input ticker_1 = "SBUX";
input ticker_2 = "MSFT";
input ticker_3 = "AMZN";
input ticker_4 = "COST";
input ticker_5 = "FFIV";
input ticker_6 = "DAIO";
input ticker_7 = "MRNA";


def RSI_1 = RSI(price = close(ticker_1), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_2 = RSI(price = close(ticker_2), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_3 = RSI(price = close(ticker_3), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_4 = RSI(price = close(ticker_4), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_5 = RSI(price = close(ticker_5), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_6 = RSI(price = close(ticker_6), "average type" = RSI_TYPE, length = RSI_LENGTH);
def RSI_7 = RSI(price = close(ticker_7), "average type" = RSI_TYPE, length = RSI_LENGTH);


plot data = (RSI_1 + RSI_2 + RSI_3 + RSI_4 + RSI_5 + RSI_6 + RSI_7) / 7;

plot midline = 50;
midline.SetDefaultColor(Color.GRAY);

# plot rsi = rsi(price = data);
data.SetDefaultColor(Color.CYAN);
data.SetLineWeight(2);

plot oversold = OS;
oversold.SetDefaultColor(Color.RED);

plot overbought = OB;
overbought.SetDefaultColor(Color.GREEN);

Here's the code I'm having trouble with:

Code:
declare lower;

input AverageType = {default WILDERS, SIMPLE, EXPONENTIAL, WEIGHTED};

input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input ticker_1 = "COMP";

input ticker_2 = "$DJI";

input ticker_3 = "AAPL";

input ticker_4 = "FB";

input ticker_5 = "MSFT";

input ticker_6 = "NVDA";

input ticker_7 = "NFLX";

input ticker_8 = "ZM";

input ticker_9 = "TSLA";

input ticker_10 = "SQ";



def MACD_1 = MACD(Value = close(ticker_1), fastLength, slowLength, MACDLength, averageType);

def MACD_2 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_3 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_4 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_5 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_6 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_7 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_8 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_9 = MACD(fastLength, slowLength, MACDLength, averageType);

def MACD_10 = MACD(fastLength, slowLength, MACDLength, averageType);

plot value = (MACD_1 +  MACD_2 + MACD_3 + MACD_4 + MACD_5 + MACD_6 + MACD_7 + MACD_8 + MACD_9 + MACD_10) / 10;
 

XeoNoX

Well-known member
VIP
heres an example, you just have to keep copying and pasting and change accordingly macd1, macd2 macd3, macd4, etc

Code:
#

declare lower;
input ticker_1 = "SBUX";
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot MACD1_Value = MovingAverage(averageType, close(ticker_1), fastLength) - MovingAverage(averageType, close(ticker_1), slowLength);
plot Avg = MovingAverage(averageType, MACD1_Value, MACDLength);

try it out and post your final code
 

Rojo Grande

Member
VIP
Here is what I got when I tried it.

d0yZhV7.png


@XeoNoX I gave this a try, no errors reported, but does not graph properly.

Code:
# AVERAGE of RSIs FOR GROUP OF STOCKS
#
# useThinkScript
# 2020-02-26
# [USER=153]@mashume[/USER]

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;


input ticker_1 = "SBUX";
input ticker_2 = "MSFT";
input ticker_3 = "AMZN";
input ticker_4 = "COST";
input ticker_5 = "FFIV";
input ticker_6 = "DAIO";
input ticker_7 = "MRNA";


def MACD1_Value = MovingAverage(averageType, close(ticker_1), fastLength) - MovingAverage(averageType, close(ticker_1), slowLength);
def MACD2_Value = MovingAverage(averageType, close(ticker_2), fastLength) - MovingAverage(averageType, close(ticker_2), slowLength);
def MACD3_Value = MovingAverage(averageType, close(ticker_3), fastLength) - MovingAverage(averageType, close(ticker_3), slowLength);
def MACD4_Value = MovingAverage(averageType, close(ticker_4), fastLength) - MovingAverage(averageType, close(ticker_4), slowLength);
def MACD5_Value = MovingAverage(averageType, close(ticker_5), fastLength) - MovingAverage(averageType, close(ticker_5), slowLength);
def MACD6_Value = MovingAverage(averageType, close(ticker_6), fastLength) - MovingAverage(averageType, close(ticker_6), slowLength);
def MACD7_Value = MovingAverage(averageType, close(ticker_7), fastLength) - MovingAverage(averageType, close(ticker_7), slowLength);


plot MovingAverage = (MACD1_Value + MACD2_Value + MACD3_Value + MACD4_Value + MACD5_Value + MACD6_Value + MACD7_Value) / 7;

plot midline = 50;
midline.SetDefaultColor(Color.MAGENTA);

MovingAverage.AssignValueColor(if MovingAverage > midline then color.GREEN else if MovingAverage < midline then color.RED else color.gray);
 

rad14733

Well-known member
VIP
@Rojo Grande I think it would be easier of you were to reference the MACD Study rather than keep copying the code multiple times... By doing so you wouldn't be required to change variable names for every iteration, you would only need different plot names and then use the plot names for formatting assignments... You would only need one set of inputs and defs and then pass them to each iteration of MACD... If you can't figure that out we'll post an example...
 

Rojo Grande

Member
VIP
In post #1, there is an indicator by @mashume that takes the RSI of numerous tickers, and averages them into one. You can add or subtract as many as wanted. I am trying to convert it to do the same thing for the MACD two line study.
 

rad14733

Well-known member
VIP
In post #1, there is an indicator by @mashume that takes the RSI of numerous tickers, and averages them into one. You can add or subtract as many as wanted. I am trying to convert it to do the same thing for the MACD two line study.

Edited: Ok... I see that the problem with MACD() is that we can't send either price or symbol as parameters...

Further: Your calculation is wrong... The proper calculation is subtractive the fast EMA from slow EMA... You are subtracting slow from fast...

Check out the proper formula HERE...
 
Last edited:

rad14733

Well-known member
VIP
@Rojo Grande I tried playing with your code and found other issues, like the lack of a 9 period MA of each tickers MACD... You also should be using 0.5 for your midline rather than 50... Unfortunately, even after being able to successfully plot any given tickers MACD for some reason the averages don't play well together... I've put enough time into this at this point...

Here is my test code... I may have changed variable names in the code...

Ruby:
declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;


input ticker_1 = "SBUX";
input ticker_2 = "MSFT";
input ticker_3 = "AMZN";
input ticker_4 = "COST";
input ticker_5 = "FFIV";
input ticker_6 = "DAIO";
input ticker_7 = "MRNA";

def MACD1_Value = MovingAverage(averageType, close(ticker_1), fastLength) - MovingAverage(averageType, close(ticker_1), slowLength);
def MACD1_Avg = MovingAverage(averageType, MACD1_Value, MACDLength);

def MACD2_Value = MovingAverage(averageType, close(ticker_2), fastLength) - MovingAverage(averageType, close(ticker_2), slowLength);
def MACD2_Avg = MovingAverage(averageType, MACD2_Value, MACDLength);

def MACD3_Value = MovingAverage(averageType, close(ticker_3), fastLength) - MovingAverage(averageType, close(ticker_3), slowLength);
def MACD3_Avg = MovingAverage(averageType, MACD3_Value, MACDLength);

def MACD4_Value = MovingAverage(averageType, close(ticker_4), fastLength) - MovingAverage(averageType, close(ticker_4), slowLength);
def MACD4_Avg = MovingAverage(averageType, MACD4_Value, MACDLength);

def MACD5_Value = MovingAverage(averageType, close(ticker_5), fastLength) - MovingAverage(averageType, close(ticker_5),slowLength);
def MACD5_Avg = MovingAverage(averageType, MACD5_Value, MACDLength);

def MACD6_Value = MovingAverage(averageType, close(ticker_6), fastLength) - MovingAverage(averageType, close(ticker_6), slowLength);
def MACD6_Avg = MovingAverage(averageType, MACD6_Value, MACDLength);

def MACD7_Value = MovingAverage(averageType, close(ticker_7), fastLength) - MovingAverage(averageType, close(ticker_7), slowLength);
def MACD7_Avg = MovingAverage(averageType, MACD7_Value, MACDLength);


plot midline = 0.5;
midline.SetDefaultColor(Color.MAGENTA);

plot macdAverage = MACD1_Avg;

#plot macdAverage = (MACD1_Avg + MACD2_Avg + MACD3_Avg + MACD4_Avg + MACD5_Avg + MACD6_Avg + MACD7_Avg) / 7;

macdAverage.AssignValueColor(if macdAverage > midline then color.GREEN else if macdAverage < midline then color.RED else color.gray);
 

Similar threads

Top