Giter Site home page Giter Site logo

Comments (5)

twopirllc avatar twopirllc commented on August 22, 2024

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.

bukosabino avatar bukosabino commented on August 22, 2024

Good question Tom,

What do you think about this? @cjaniake @christian-janiake-movile @paduel

from ta.

christian-janiake-movile avatar christian-janiake-movile commented on August 22, 2024

hi @bukosabino
@TomLouisKeller 's proposition looks perfect, and is much more accurate when series are incomplete.
agreed!

from ta.

TomLouisKeller avatar TomLouisKeller commented on August 22, 2024

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.

bukosabino avatar bukosabino commented on August 22, 2024

The last code suggested by Tom is implemented in the last ta library version (0.3.4).

Regards.

from ta.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.