Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Momentum investing from exponential moving average (EMA) signals. Help!

Im unsure if I am setting up my code correctly. Im new to this:

Strategy:
Momentum investing from exponential moving average (EMA) signals.

Every day, we long SPY if EMA 7 is greater than EMA 16, which signals upward price momentum.

Hold 100% cash if SPY crosses below EMA 7.

And short SPY if EMA 16 is greater than EMA 7, which signals downward price momentum.

Also, if a gap of 3.2% opens up between the SPY and EMA 7 (meaning (SPY-EMA7)/EMA7 = 0.032) we short with 50% of capital.
And vice versa, if (EMA7-SPY)/SPY = 0.032 --> we go long with 50% of capital.

I'm totally lost when it comes to coding and would really appreciate some help. Thanks!

1 response

@Zach,

There is some inconsistency between title, post and code.
In the title and post, you talk about the Exponential Moving Average (EMA).
In your code and algorithm investment thesis, you are using a simple moving average.

ema_7 = hist.mean()  

you actually calculate 365 day simple moving average.

ema_16 = hist[-200:].mean()  

you actually calculate 200 day simple moving average.

Algorithm investment thesis:
Momentum investing from simple moving average (SMA) signals will outperform market-at-large and/or simply holding long.
Every day, we long Apple if SMA 15 is greater than SMA 50, which signals upward price momentum.
We short Apple if SMA 50 is greater than SMA 15, which signals downward price momentum.

Can you clarify algorithm investment thesis and its parameters?