Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMA函数定义错误 #12

Open
qzhjiang opened this issue Oct 17, 2021 · 5 comments
Open

SMA函数定义错误 #12

qzhjiang opened this issue Oct 17, 2021 · 5 comments

Comments

@qzhjiang
Copy link

SMA函数正确的写法应该是:

def SMA(S, N, M=1):  # 中国式的SMA,至少需要120周期才精确,直接定义alpha=m/n
    return pd.Series(S).ewm(alpha=M / N, adjust=True).mean().values
@mpquant
Copy link
Owner

mpquant commented Oct 17, 2021

SMA(S, N, M=1): #中国式的SMA,至少需要120周期才精确 (雪球180周期) alpha=1/(1+com)
return pd.Series(S).ewm(alpha=1/(1+N-M), adjust=True).mean().values

@qzhjiang
Copy link
Author

你改过之后的与原来写的无差别,仍然是错误的。
我上面提出的正确写法,你可以自己验证,也可以看看我的这篇文章:
通达信转Python神器——myTT库

@mpquant
Copy link
Owner

mpquant commented Oct 18, 2021

 #RSI    10-12  10-13  10-14  10-15  (上证指数 000001  RSI 24日)
#通达信:47.81  49.07  48.78  50.02  
#雪球:  47.81  49.07  48.78  50.02  

纯粹SMA指标不好对比,用主力使用SMA的RSI对比

    return pd.Series(S).ewm(alpha=M/N, adjust=True).mean().values
    return pd.Series(S).ewm(com=N-M, adjust=True).mean().values

上面这2个写法得出的结果是一样的

@qzhjiang
Copy link
Author

return pd.Series(S).ewm(alpha=M/N, adjust=True).mean().values
return pd.Series(S).ewm(com=(N-M)/M, adjust=True).mean().values
这两个写法才是等价的。

推导过程如下:
已知:
alpha =1/(1+com)

又知道,SMA正确的alpha是M/N
alpha = M/N

容易求出:
com = N/M-1=(N-M)/M

详细验证过程请见:
通达信转Python神器——myTT库,https://www.joinquant.com/view/community/detail/a6cc7d1fb73a57dbac4b77044a33b15d?type=1

@mpquant
Copy link
Owner

mpquant commented Oct 20, 2021

感谢,已经修正
pd.Series(S).ewm(alpha=M/N, adjust=True).mean().values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants