Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
talib MACD errors driving me crazy.

To start out on quantopian I decided to make a few technical indicators and test them, they all work except for one, the MACD. I'm just a physics major and have only had one course in Python so forgive me if I'm overlooking something very obvious.

Normally I do something like this which works fine:

aroondown, aroonup = talib.AROON(high, low, 2)  

But with the MACD, in any shape or form resembling this one:

macd, macdsignal, macdhist = talib.MACD(close, 12, 26, 9)  

It keeps returning the "AssertionError: real has wrong dimensions" Error...

I've tried the example MACD but as soon as I try to make the function return more than just a single integer value so that I can compute something a little more complex like so:

def handle_data(context, data):  
    # Load historical data for the stocks  
    close = history(26, '1d', 'close_price')  
    macd, macdsignal, macdhist = MACD(close)  
   # then do some stuff here

def MACD(close, fastperiod=12, slowperiod=26, signalperiod=9):  
    macd, macdsignal, macdhist = talib.MACD(close, fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod)  
    return (macd, macdsignal, macdhist)

The bloody thing returns the "AssertionError: real has wrong dimensions" error again...

I'm at a loss here, google didn't help me at all, hoping someone might want to tell me what I'm doing wrong.

Thanks.

5 responses

I'm just starting out as well so I'm not exactly sure what the issue is either but there are a few things I noticed.

First is that you are feeding MACD the data in "close" which has dimensions N by 2 (the first column is the index of N stocks and the second column is the close prices.) MACD is supposed to just get a list N. The second possible problem is that when you called the function MACD, you didn't feed it the 3 other parameters.

From the documentation, it looks like the way to go about using the values that are in "close" is to use " =close.apply(MACD, fastperiod=12, slowperiod=26, signalperiod=9)" instead of "=MACD(close)"
The ."apply" needs the "pandas" library by the way.

Unfortunately I'm not sure if that will work exactly the way that is expected. The documentation has "return macd[-1]" instead of just "return macd." I don't know exactly what that's doing but would give a try if it still doesn't work.

Let me know how it goes, I just started learning this a few days ago as well and I've had a lot of confusion about how the data is handled so I'm curious to know if it works out.

I tried a few things,

The documentation and all other indicators that do work suggests the dimension should not matter, even the example algorithm uses

prices = history(26, '1d', 'price')  

When calling the function without with or without the 3 other parameters it gave the same error. Leaving a few out when having them set in the function definition I think should cause the function to use the defined as default when not receiving all values.
Looking further at the documentation it's worth noting that the example works exactly in the same way as I'm using it, the prices.apply is indeed used but in the function MACD the talib.MACD still receives the same prices/close information of the same dimension.

[-1] returns the last element of the list. Now I tried printing the return of that function and it gives "**NaN dtype: float64**" for every day which I think is odd because I expected a number for some reason.

Now I'm really wondering, what does the Talib MACD actually return?

macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)  

I expect macd to be the slow line, macdsignal to be the fast line, and macdhist to be the histogram.
This simple algorithm returns the "AssertionError: real has wrong dimensions" error:

import talib  
def initialize(context):  
    context.stock = symbols('SPY')

def handle_data(context, data):  
    prices = history(26, '1d', 'price')  
    macd, signal, hist = talib.MACD(prices, fastperiod=1, slowperiod=2, signalperiod=3)  

I'm at a loss.

Hi Marco, znr is right, you need to provide a one dimensional array to the talib function:

macd, macdsignal, macdhist = talib.MACD(close[stock], 12, 26, 9)

Ofcourse...

Thanks Charles, znr, works as it should now.

For anyone that wants it:

import talib  
def initialize(context):  
    context.stock = symbol('SPY')

def handle_data(context, data):  
    close = history(36, '1d', 'close_price')[context.stock]  
    macd, macdsignal, macdhist = talib.MACD(close, 12, 26, 9)  
    macd = macd[-1]  
    macdsignal = macdsignal[-1]  
    record(macd = macd, macdsignal = macdsignal)  

Ahh I see, thanks for the clarification Charles and thanks for sharing the working code Marco. Glad that it worked out.