Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to add Exponential Weighted Moving Average into a pipeline?

I'm building an algorithm with a pipeline, in the pipeline I have the sma but I want to have a EWMA. I want to use those indicators to take decisions. And I want to be able to create an 'if statement' to compare a sma_10 and a EWMA. I hope you can help me.

2 responses

Use the ExponentialWeightedMovingAverage built in factor (see the docs https://www.quantopian.com/help#built-in-factors and scroll down a bit)

You can use an 'if' statement in the algorithm but perhaps a cleaner approach is simply define a filter within the pipeline definition.

from quantopian.pipeline.factors import SimpleMovingAverage, ExponentialWeightedMovingAverage

sma_10 = SimpleMovingAverage(  
                            inputs=[USEquityPricing.close],  
                            window_length = 10)


ema_10 = ExponentialWeightedMovingAverage(  
                            inputs=[USEquityPricing.close],  
                            window_length=30,  
                            decay_rate=(1 - (2.0 / (1 + 15.0))),  
                              )

sma_greater_than_ema = sma_10 > ema_10


Thank you this is exactly what i was looking for. I made a different thread asking about the (if) statement. I wonder if is there a way to adjust it. Because when the sma and the ewma are too close to each other I lose money. I want the algorithm to make a decision when the two indicators start to be more separated.