Skip to content

Instantly share code, notes, and snippets.

@TsaiKoga
Created April 20, 2021 08:05
Show Gist options
  • Save TsaiKoga/8b7c3a550742537111935198e3de9696 to your computer and use it in GitHub Desktop.
Save TsaiKoga/8b7c3a550742537111935198e3de9696 to your computer and use it in GitHub Desktop.
协整性股票对查询
def find_cointegrated_pairs(dataframe):
"""
判断协整关系的函数
返回协整性p值矩阵,协整性强的股票对
:param :dataframe
:return :pvalue_matrix, :pairs
"""
# 得到DataFrame长度
n = dataframe.shape[1]
# 初始化p值矩阵
pvalue_matrix = np.ones((n, n))
# 抽取列的名称
keys = dataframe.keys()
# 初始化强协整组
pairs = []
# 对于每一个i
for i in range(n):
# 对于大于i的j
for j in range(i+1, n):
# 获取相应的两只股票的价格Series
stock1 = dataframe[keys[i]]
stock2 = dataframe[keys[j]]
# 分析它们的协整关系
result = sm.tsa.stattools.coint(stock1, stock2)
# 取出并记录p值
pvalue = result[1]
pvalue_matrix[i, j] = pvalue
# 如果p值小于0.05
if pvalue < 0.05:
# 记录股票对和相应的p值
pairs.append((keys[i], keys[j], pvalue))
# 返回结果
return pvalue_matrix, pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment