2

I am trying to copy the functionality of classical EMA from trading. In trading view DOCS they say for EMA:

It calculates by sing a formula: EMA = alpha * x + (1 - alpha) * EMA[1], where alpha = 2 / (y + 1)

But what do I provide to EWM of Pandas as alpha (or span?) param to have the same computation?

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ewm.html

If not possible to compute it via Pandas, how to do it via Numpy?

=================================

Edit:

So it should be simple as:

serie.ewm(span=window, adjust=False, ignore_na=True).mean()

But for some strange reason it computes different values than the Tradingview indicator.. I even checked the input values. Really strange, i can't understand it.

The numbers are same like for window smaller than 3 but then it starts get different.

Like TV.EMA(3) = PD.EMA(3) but TV.EMA(6) != PD.EMA(6). crazy..

Edit: I added some sample data.

**`Note: i am displaying EMA for last 3 values only`**

input:

1.9646
1.9441
1.9296
1.8883
1.8916
1.9277

tradingview EMA(3): (given unlimited inputs)

1.91471466
1.90315733
1.91542866

pandas EMA(3): (given 49 inputs)

1.914715
1.903157
1.915429

pandas EMA(3): (given 3 inputs)

1.888300
1.889950
1.908825

==========

tradingview EMA(6): (given unlimited inputs)

1.93477990
1.92244279
1.92394485

pandas EMA(6): (given 96 inputs)

1.934780
1.922443
1.923945

pandas EMA(6): (given 6 inputs)

1.932669
1.920935
1.922868

==========

tradingview EMA(20): (given unlimited inputs)

1.95649590
1.95031533
1.94816149

pandas EMA(20): (given 20 inputs)

1.953533
1.944004
1.941496

pandas EMA(20): (given 40 inputs)

1.953454
1.943938
1.941440

pandas EMA(20): (given 320 inputs)

1.953483
1.943962
1.941461

So 3 new questions:

  1. How to know, how much input is required for EMA(N) - I was thinking it could be N * 2 to have also data for first input (maybe more N*2-1) - but doesn't work for EMA(20) obviously.
  2. Why there is only 6 precision computation out of Pandas. What if i need better precision eg to 8 numbers.
  3. EMA(20) obviously doesn't work well, while lower ema works (if rounded to 6)

How i compute it with pandas:

period = 6, 20 etc

t = c.ewm(span=period, adjust=False, ignore_na=True).mean()
luky
  • 2,263
  • 3
  • 22
  • 40
  • Do you have any example of expected output? – mathfux Dec 19 '21 at 06:28
  • I will try put something together. There is one problem with Tradiview pine script that i cannot call the EMA on my sample array (at least i dont know how) but on live prices. But i can at least get out the list of prices that means input and list of output from TV and my code. – luky Dec 19 '21 at 09:55
  • @mathfux hi i added some sample input output, and 3 questions below the data. thanks – luky Dec 19 '21 at 11:33
  • @luky Import matplotlib and plot both Tradingview and pandas' exponential moving averages using like 1000 points, otherwise it's hard to notice significant differences. – Guimoute Dec 19 '21 at 11:41
  • @Guimoute there are the differences as i said. the problem is maybe the precission? pandas has only 6 digits precision? maybe it is the problem? – luky Dec 19 '21 at 13:36
  • 2
    I have noticed the same, and am unable to deduce where the issue is. I am looking at price data for MANA-USD from Coinbase, and have verified that the OHLC data I have pulled from Coinbase's API exactly matches 100% what shows in TradingView. But I cannot figure out why the Pandas ewm method produces different results than what TradingView does. – K-83Rick Jan 06 '22 at 17:15
  • @K-83Rick yes let me know if you find something. i wanted computed trix, but i dont want anymore. i then used EMA in one strategy and it kind of worked even with error, but i dont use EMA anymore, for now. – luky Jan 08 '22 at 13:01

0 Answers0