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

对SLOPE函数的修改 #20

Open
qzhjiang opened this issue Nov 20, 2021 · 3 comments
Open

对SLOPE函数的修改 #20

qzhjiang opened this issue Nov 20, 2021 · 3 comments

Comments

@qzhjiang
Copy link

qzhjiang commented Nov 20, 2021

myTT中现在的SLOPE函数:

def SLOPE(S, N, RS=False):  # 返S序列N周期回线性回归斜率 (默认只返回斜率,不返回整个直线序列)
    M = pd.Series(S[-N:])
    poly = np.polyfit(M.index, M.values, deg=1)
    Y = np.polyval(poly, M.index)
    if RS:
        return Y[1] - Y[0], Y
    return Y[1] - Y[0]

存在如下问题:
1) 有三个参数,与通达信不一致;
2)违背了array in, array out的原则;
3)写法繁琐

特修改如下:

def SLOPE(S, N):
    # type: (np.ndarray, int) -> np.ndarray
    """
    通达信SLOPE。返S序列N周期回线性回归斜率,N暂时不支持变量。
    """
    M = pd.Series(S)
    return M.rolling(window=N).apply(lambda y: np.polyfit(y.index, y.values, deg=1)[0], raw=False)
@mpquant
Copy link
Owner

mpquant commented Nov 20, 2021

def SLOPE(S, N):           #返S序列N周期回线性回归斜率        
    return pd.Series(S).rolling(N).apply(lambda x: np.polyfit(x.index,x.values,deg=1)[0],raw=False).values  

感谢! FORCAST() 还需要研究

@mpquant
Copy link
Owner

mpquant commented Nov 21, 2021

def FORCAST(S,N):                      #返S序列N周期回线性回归后的预测值    
    M=pd.Series(S[-N:]);      poly = np.polyfit(M.index, M.values,deg=1);     
    return np.polyval(poly, M.index)[-1]

还是只能返回最后单值

@qzhjiang
Copy link
Author

2021年11月12日修改

def slope(S, N):
    # type: (np.ndarray, int) -> np.ndarray
    """
    通达信SLOPE。返S序列N周期回线性回归斜率,N暂时不支持变量。
    """
    return pd.Series(S).rolling(window=N).apply(lambda y: np.polyfit(range(N), y, deg=1)[0], raw=False).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