Comments (5)
Hi Tom,
You are not incorrect. There are a few different ways that EMA is being calculated out in the wild. One of them includes using the method where the SMA is calculated for the first period and the rest of the EMA is created off that initial value and the rest of closing prices. I thought it was odd also, but I found it being used by the popular TALib.
In fact, it was documented in ta_EMA.c:
/* The first EMA is calculated differently. It
* then become the seed for subsequent EMA.
*
* The algorithm for this seed vary widely.
* Only 3 are implemented here:
*
* TA_MA_CLASSIC:
* Use a simple MA of the first 'period'.
* This is the approach most widely documented.
*
* TA_MA_METASTOCK:
* Use first price bar value as a seed
* from the begining of all the available
* data.
*
* TA_MA_TRADESTATION:
* Use 4th price bar as a seed, except when
* period is 1 who use 2th price bar or something
* like that... (not an obvious one...).
*/
In my version so far, I have incorporated both methods. Your straight up Pandas ewm version that you have coded as well as @christian-janiake-movile submission. Furthermore, I have added a bunch of other indicators on top of the current code base while extending the Pandas DataFrame.
KJ
from ta.
Good question Tom,
What do you think about this? @cjaniake @christian-janiake-movile @paduel
from ta.
hi @bukosabino
@TomLouisKeller 's proposition looks perfect, and is much more accurate when series are incomplete.
agreed!
from ta.
Hy Guys,
I did some more research on EMAs.
I initially assumed that EMAs, like SMAs, only take the N last periods in a series,
and then weights them somehow. This assumption is incorrect.
From wikipedia:
... EMA is referred to as an N-day EMA. Despite the name suggesting there are N periods, the terminology only specifies the α factor. N is not a stopping point for the calculation in the way it is in an SMA or WMA.
The EMA takes all prior available data points for it's calculation.
The variable N/periods/span is for the calculation of alpha, which is then used to calculate the weight of each prior data point.
In the notebook you see, that index 9 and 10 are different if we cut off the first 5 data points:
https://colab.research.google.com/drive/10DMtAQ7quQ-oVkuq0Tk8SYnOuCntnvGQ
If we use the existing ta.ema code, the values of index 9 and 10 in cell 5 and 7 are even further apart:
https://colab.research.google.com/drive/1pbBfZTo8VE4K6XsWv8k7OYlawaHF6kE9
If we turn adjust=True, the values of index 9 and 10 in cell 6 are closer to their true values:
https://colab.research.google.com/drive/1HbcbaD4nocxAZgtzGtlAkTZG9PWUnnCg
My updated proposition: (adjust=True is the default)
def ema(series, periods, fillna=False):
if fillna:
return series.ewm(span=periods, min_periods=0).mean()
return series.ewm(span=periods, min_periods=periods).mean()
What do you guys think?
from ta.
The last code suggested by Tom is implemented in the last ta library version (0.3.4).
Regards.
from ta.
Related Issues (20)
- Default window
- MACD errors HOT 1
- volume_price_trend is completly wrong HOT 2
- requesting two indicators HOT 1
- Confusion about Force Index HOT 1
- Add parameter "exclude_ta_features" from add_all_ta_features.
- Why does Bollinger Bands use ddof=0 instead of the default ddof=1? HOT 1
- FutureWarning (with pandas 1.5.1) HOT 1
- ADXIndicator.adx_pos / adx_neg head assertion failed
- AroonIndicator from trend.py based on inadequate input HOT 2
- Bollinger Band data series - something is off HOT 1
- Feature Request: Support incremental computation
- calculate daily return will use future infomation HOT 3
- ADX indicator on stockcharts is wrong (and therefore, it's implementation here too, I guess) HOT 1
- RSIIndicator: Value Error HOT 1
- add_all_ta_features not installing due to bug on OnBalanceVolumeIndicator
- Issue with ADXIndicator interacting with numpy HOT 1
- FutureWarning HOT 1
- not compatible with python 3.12? HOT 1
- Can't install ta with Anaconda on windows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ta.