Research

  • [X] I have searched the [pandas] tag on StackOverflow for similar questions.

  • [X] I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/74705156/pandas-vs-pinescript-weighted-moving-avarages-ema

Question about pandas

I'm rebuilding a tradingview pinescript i created in python. The strategy I'm trying to convert to python contains exponentially weighted moving average (EMA)

Pinscript EMA description

The ema function returns the exponentially weighted moving average. 
In ema weighting factors decrease exponentially. 
It calculates by using a formula: EMA = alpha * x + (1 - alpha) * EMA[1], where alpha = 2 / (y + 1)


//the same on pine
pine_ema(src, length) =>
alpha = 2 / (length + 1)
sum = 0.0
sum := na(sum[1]) ? sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])

This would be my python version

def calculateEMA(df, length):
    alpha = 2 / (length + 1)
    for i in range(len(df)):
        if i == 0:
            #Execute 'sma(src, length)' here
            pass
        else:
           # Execute 'alpha * src + (1 - alpha) * nz(sum[1])' here
           df.iat[i,1] = alpha * df.iat[i,1] + (1 - alpha) * df.iat[i -1, 1]

But there is also the pandas df.column.evm() command. I would like to know if the pandas .ewm() command uses the same formula for calculating the EMA as Tradingview Pinescript?

Comment From: MarcoGorelli

Hi @aghering23

you can check https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html for the formulas pandas uses

Comment From: aghering23

Hi Marco,

I checked the docs but i'm not sure if i'm correct, but it seems that pandas .evm() is calculated different.

Looking at the example of trading view it says: "EMA = alpha * x + (1 - alpha) * EMA[1]" where alpha = 2 / (y + 1) Now looking at the pandas docs i see the following

  • alpha parameter: uses the same math as tradingview ema()
  • adjust-false parameter:
  • .- Y0 = would represent the x value in trading ema()?
  • .- Yt = would represent the (1 - alpha) * EMA[1] but also + ALPHAxt?

And where can i find how pandas places the parameter in the formula? I just want to be sure that pandas/tradingview uses the same formula or else my results would not match.

Comment From: aghering23

@MarcoGorelli why did you close my question?

Comment From: MarcoGorelli

There's not really anything actionable on the pandas side - if you think there's some issue with the pandas formula / implementation, then we can reopen

There's a lot of open issues here, and really limited resources to deal with them

You can check here for the pandas code https://github.com/pandas-dev/pandas/blob/v1.5.2/pandas/core/generic.py#L12035-L12063

Comment From: ZRT-1998

嗨马可,

我检查了文档,但我不确定我是否正确,但似乎熊猫 .evm() 的计算方式不同。

查看交易视图的示例,它说:“EMA = alpha * x + (1 - alpha) *** EMA[1]**”,其中alpha = 2 / (y + 1) 现在查看熊猫文档,我看到以下内容

  • alpha 参数:使用与交易视图相同的数学 ema()
  • 调整错误参数:
  • .- Y0 = 将代表交易 ema() 中的 x 值?
  • .- Yt = 将代表 (1 - alpha) * EMA[1] 但也表示 + ALPHAxt?

我在哪里可以找到熊猫如何在公式中放置参数?我只想确保熊猫/交易视图使用相同的公式,否则我的结果将不匹配。

did you solve it?