Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with MACD Percentage Custom Factor

Hi,

I am attempting to create a custom factor to deliver two outputs the MACD as a % (as opposed to a value) and the signal line of that.

I used the Zipline MovingAverageConvergenceDivergenceSignal Class as a basis, but I get the following error

ValueError: operands could not be broadcast together with shapes:

Can anyone suggest what I am missing please.

Paul

3 responses

Hmm. I think you want to pass the entire 'close' data to the '_ewma' function and then simply slice off the last n rows of data where n is your window length.

    def _ewma(self, data, length):  
        decay_rate = 1.0 - (2.0 / (1.0 + length))  
        return average(  
            data[-length:],  
            axis=0,  
            weights=_exponential_weights(length, decay_rate)  
        )

        #  then call it with al the data in close - not sure you need 'rolling window'  
        slow_EWMA = self._ewma(  
            close,   #rolling_window(close, slow_period, axis=1),  
            slow_period  
        )

Also, not sure what you are trying to do with the last line. One problem at a time. I commented it out in the attached notebook.

        #out.MACD_Signal[:] = self._ewma(macd_percent.T, signal_period)   

Hi Dan, thanks for looking at this.

If I understand the results this is only delivering a single value for the slow and fast ema now, which is calculating the MACD as a % which is great.

But what I am trying to do is get a window so I can then get the signal line of this data which is the EMWA that you commented out

and return a factor looking at both for use in a possible momentum strategy.

If you have any other suggestions that would be great

Factors by definition only return a single scaler (ie not an array or other structure) value per output. Maybe put your test inside the custom factor and return a 1 or 0?