shibor与沪深300指数的相关性图示

我通过baostock数据平台获取了shibor隔夜拆借利率和shibor 3个月的拆借利率的历史数据,并且获取了相同时间内沪深300指数的数据,来分析两者是不是存在关联。因为相比于上证指数,沪深300指数中的小盘股,更加容易受到资金面的影响。一来公募基金偏爱于大盘股,都是长期资金,不需要短期拆借,而私募或者中小投资者,可能需要进行资金拆借,受制于短中期利率。

 

   下面是代码。

import baostock as bs

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.font_manager as matfont

import matplotlib

import datetime

 

def get_shibor_his_data(startDate,endDate):

    """获取历史shibor数据"""

    # 登陆系统

    lg = bs.login()

    # 显示登陆返回信息

    print('login respond error_code:'+lg.error_code)

    print('login respond  error_msg:'+lg.error_msg)

   

    # 获取银行间同业拆放利率

    rs = bs.query_shibor_data(start_date=startDate, end_date=endDate)

    print('query_shibor_data respond error_code:'+rs.error_code)

    print('query_shibor_data respond  error_msg:'+rs.error_msg)

   

    # 打印结果集

    data_list = []

    while (rs.error_code == '0') & rs.next():

        # 获取一条记录,将记录合并在一起

        data_list.append(rs.get_row_data())

    result = pd.DataFrame(data_list, columns=rs.fields)

 

   

    # 登出系统

    bs.logout()

    return result

 

def get_index_his_data(index,startDate,endDate):

    #### 登陆系统 ####

    lg = bs.login()

    # 显示登陆返回信息

    print('login respond error_code:'+lg.error_code)

    print('login respond  error_msg:'+lg.error_msg)

   

    #### 获取沪深A股历史K线数据 ####

    # 详细指标参数,参见历史行情指标参数章节

    rs = bs.query_history_k_data(index,

        "date,close",

        start_date=startDate, end_date= endDate,

        frequency="d", adjustflag="3")

    print('query_history_k_data respond error_code:'+rs.error_code)

    print('query_history_k_data respond  error_msg:'+rs.error_msg)

   

    #### 打印结果集 ####

    data_list = []

    while (rs.error_code == '0') & rs.next():

        # 获取一条记录,将记录合并在一起

        data_list.append(rs.get_row_data())

    result = pd.DataFrame(data_list, columns=rs.fields)

 

   

    #### 登出系统 ####

    bs.logout()

    return result

 

def plot_two_curve_line(tradingDateList,y1,y2,y1name='y1',y2name ='y2'):

    """根据日期,画出相同时间序列的两个值的曲线"""

   

    #将不同量纲的两个值转化为相似的数量级

    multi_num = y1[-1]/y2[-1]

    y2 = [y*multi_num for y in y2]

   

    x1 = range(len(tradingDateList))

          

    datelable = []

    for i_days in range(len(tradingDateList)):

        tradingdate = tradingDateList[i_days]

        date = int(tradingdate[8:])

#         print date

#         if date %10 == 0:

        if date % 30 == 0:

            datelable.append(tradingdate)

        else:

            datelable.append("")

                  

    x1 = np.array(x1)

    y1 = np.array(y1)

    y2 = np.array(y2)

 

    fig, ax = plt.subplots()   

    plt.xticks(x1,datelable,rotation=30)

    ax.plot(x1,y1, label=r"%s"%y1name)

    ax.plot(x1,y2, label=r"%s"%y2name)

   

    #显示在右上角

    ax.legend(loc=1)

 

plt.show()

 

 

def plot_shibor_hs300_pic():

    """  画出shibor指数和上证指数之间的相关指数 """

    startdate = "2012-01-05"

    endate = "2018-08-16"

    index = "sh.000300"

   

    shiborData = get_shibor_his_data(startdate, endate)

    datelist = shiborData.loc[:,'date']

    shiborON = shiborData.loc[:,'shiborON']

   

    hs300_data = get_index_his_data(index,startdate,endate)

    hs300_datelist = hs300_data.loc[:,'date']

    hs300_price = hs300_data.loc[:,'close']

    print(len(datelist),len(hs300_datelist),len(hs300_price))

   

    modified_datelist = []

    modified_shiborlist = []

    modified_index_hisdata = []

   

    i = 0

    j = 0

    while True:

        if i == len(datelist) and j == len(hs300_datelist):

            break

        if datelist[i] == hs300_datelist[j]:

            modified_datelist.append(datelist[i])

            modified_shiborlist.append(float(shiborON[i]))

            modified_index_hisdata.append(float(hs300_price[j]))

            i += 1

            j += 1

        elif datelist[i] < hs300_datelist[j]:

            i += 1

#             print(datelist[i] , hs300_datelist[j])

        elif datelist[i] > hs300_datelist[j]:

#             print(datelist[i] , hs300_datelist[j])

            j += 1

        else:

            print("error")

   

    print(len(modified_datelist),len(modified_index_hisdata),len(modified_shiborlist))

    plot_two_curve_line(modified_datelist,modified_index_hisdata,modified_shiborlist,y1name='hs300',y2name ='shiborON')

 

if __name__ == '__main__':

    plot_shibor_hs300_pic()

画出图像如下:

shibor与沪深300指数的相关性图示