hahahahey
hahahahey

Reputation: 99

Simple Python Pandas EMA (ewma)?

I wrote some code to build my own EMA/MACD, but have decided to give Pandas a try instead.

I am using this website below as a basic understanding of EMA and trying to get pandas to give me the same answers to be sure I am using pandas correctly:

http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages

And here is the chart with the data that Im working with.

enter image description here

Here is the code I'm trying to get to work, but it gives me different output than the 10-day EMA column

import pandas as pd

data=[22.27,22.19,22.08,22.17,22.18,22.13,22.23,22.43,22.24,22.29,22.15,22.39,22.38,22.61,23.36,24.05,23.75,23.83]

df=pd.Series(data)

pd.ewma(df, span=10)

I've also tried this with no luck.

pd.ewma(df, span=10, min_periods=10)

Any help is appreciated.

Upvotes: 3

Views: 35826

Answers (2)

A. STEFANI
A. STEFANI

Reputation: 6736

According to calculate-exponential-moving-average-with-pandas self-answer, and assuming that close serie is corresponding to the close price, you may use this to get the EMA 10: (change the span to what you want if you want another span)

df['ema10'] = pd.Series.ewm(df['close'], span=10).mean()

Upvotes: 4

Peter Leimbigler
Peter Leimbigler

Reputation: 11105

Coincidentally, this question was asked and answered here: Does Pandas calculate ewm wrong?

Check out @chrisb's answer there. To compute the EWM as described in the article you're studying:

  1. manually compute the first valid simple MA to serve as a starting point for EWA
  2. run pandas' EWM with adjust=False

Upvotes: 9

Related Questions