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

py2版SMA函数定义错误 #13

Open
qzhjiang opened this issue Oct 18, 2021 · 1 comment
Open

py2版SMA函数定义错误 #13

qzhjiang opened this issue Oct 18, 2021 · 1 comment

Comments

@qzhjiang
Copy link

qzhjiang commented Oct 18, 2021

mytt.py的py2版本中,SMA定义为:

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

已知:
alpha =1/(1+com)

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

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

所以,py2中SMA中正确的定义应该是:

def SMA(S, N, M=1):        #中国式的SMA,至少需要120周期才精确 (雪球180周期)    alpha=1/(1+com)
    return pd.ewma(S,com=((N-M)*1.0)/M,adjust=False)  

或者:

def SMA(S, N, M=1):        #中国式的SMA,至少需要120周期才精确 (雪球180周期)    alpha=1/(1+com)
    return pd.ewma(S,com=(N*1.0)/M - 1,adjust=False)

注意到画蛇添足地乘以了1.0,是因为直接除,py2中会变成整除。先乘以1.0,int转化为float后,再除,就是正确的了。

已经在一创聚宽验证过了,上述两个修改版,计算结果都是正确的。

@mpquant
Copy link
Owner

mpquant commented Oct 20, 2021

感谢,已经修正

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