99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python數(shù)據(jù)分析:股價(jià)相關(guān)性
Python數(shù)據(jù)分析:股價(jià)相關(guān)性
2018-01-26
收藏

Python數(shù)據(jù)分析:股價(jià)相關(guān)性

為什么要分析股價(jià)相關(guān)度呢,我們來(lái)引入一個(gè)概念——配對(duì)交易
所謂的配對(duì)交易,是基于統(tǒng)計(jì)套利的配對(duì)交易策略是一種市場(chǎng)中性策略,具體的說(shuō),是指從市場(chǎng)上找出歷史股價(jià)走勢(shì)相近的股票進(jìn)行配對(duì),當(dāng)配對(duì)的股票價(jià)格差偏離歷史均值時(shí),則做空股價(jià)較高的股票同時(shí)買(mǎi)進(jìn)股價(jià)較低的股票,等待他們回歸到長(zhǎng)期均衡關(guān)系,由此賺取兩股票價(jià)格收斂的報(bào)酬。
接下來(lái)開(kāi)始我們的股價(jià)相關(guān)度分析,首先我們選兩個(gè)股票~


感覺(jué)全聚德和光明乳業(yè)都很好吃的樣子,我們就選它們了吧!= ̄ω ̄=
1、導(dǎo)入數(shù)據(jù)包
簡(jiǎn)單介紹一下要用到的數(shù)據(jù)包
matplotlib.pyplot:繪圖庫(kù),其中pyplot子包提供一個(gè)類MATLAB的繪圖框架
numpy:科學(xué)計(jì)算庫(kù),支持高級(jí)大量的維度數(shù)組與矩陣運(yùn)算
pandas:納入了大量庫(kù)和一些標(biāo)準(zhǔn)的數(shù)據(jù)模型,提供高效地操作大型數(shù)據(jù)集所需的工具
tushare:財(cái)經(jīng)數(shù)據(jù)接口包
[python] view plain copy

    <span style="font-size:18px;">import matplotlib.pyplot as plt </span>  
    <span style="font-size:18px;">import numpy as np</span>  
    <span style="font-size:18px;">import pandas as pd</span>  
    <span style="font-size:18px;">import tushare as ts  
    </span> 
2、根據(jù)全聚德和光明乳業(yè)的股票代碼獲取數(shù)據(jù),這里獲取的是2016年一整年的收盤(pán)價(jià),獲取完后合并,因?yàn)橥E频拇嬖?,用前一天的價(jià)格去填寫(xiě)缺失數(shù)據(jù),最終以CSV格式保存數(shù)據(jù)
[python] view plain copy
    <span style="font-size:18px;">s_qjd = '002186' #全聚德</span>  
    <span style="font-size:18px;">s_gm = '600597' #光明乳業(yè)</span>  
    <span style="font-size:18px;">sdate = '2016-01-01'#起止日期</span>  
    <span style="font-size:18px;">edate = '2016-12-31'</span>  
    <span style="font-size:18px;">df_qjd = ts.get_h_data(s_qjd, start = sdate, end = edate).sort_index(axis = 0,ascending=True)#獲取歷史數(shù)據(jù)</span>  
    <span style="font-size:18px;">df_gm = ts.get_h_data(s_gm, start = sdate, end = edate).sort_index(axis = 0,ascending=True)</span>  
    <span style="font-size:18px;">df = pd.concat([df_qjd.close,df_gm.close], axis = 1, keys=['qjd_close', 'gm_close'])#合并</span>  
    <span style="font-size:18px;">df.ffill(axis=0, inplace=True)#填充缺失數(shù)據(jù)</span>  
    <span style="font-size:18px;">df.to_csv('qjd_gm.csv')  
    </span>  

3、用pearson相關(guān)系數(shù)計(jì)算相關(guān)度(Pearson相關(guān)系數(shù)是用來(lái)衡量?jī)蓚€(gè)數(shù)據(jù)集合是否在一條線上面,它用來(lái)衡量定距變量間的線性關(guān)系。),再打印出來(lái)看一眼
[python] view plain copy

    <span style="font-size:18px;">corr = df.corr(method = 'pearson', min_periods = 1)#pearson方法計(jì)算相關(guān)性</span>  
    <span style="font-size:18px;">print(corr)</span> 


算出來(lái)有0.81,超過(guò)0.8,按值域等級(jí)來(lái)說(shuō)屬于極強(qiáng)相關(guān),不過(guò)話說(shuō)一個(gè)賣烤鴨的為什么會(huì)和賣牛奶的相關(guān)度那么高。。。。難道大家吃烤鴨的時(shí)候都喜歡喝牛奶嗎。。。
4、繪制圖像出來(lái)喵一眼,看看趨勢(shì)上來(lái)說(shuō)什么時(shí)候可以有機(jī)會(huì)做配對(duì)交易
[python] view plain copy
    <span style="font-size:18px;">df.plot(figsize = (20,12))</span>  
    <span style="font-size:18px;">plt.savefig('qjd_gm.jpg')</span>  
    <span style="font-size:18px;">plt.close()</span>  

5、按分析日期的第一天的股價(jià)為基準(zhǔn)做歸一化處理,打印圖像
[python] view plain copy
    <span style="font-size:18px;">df['qjd_one'] = df.qjd_close / float(df.qjd_close[0])*100</span>  
    <span style="font-size:18px;">df['gm_one'] = df.gm_close / float(df.gm_close[0])*100</span>  
    <span style="font-size:18px;">df.qjd_one.plot(figsize = (20,12))</span>  
    <span style="font-size:18px;">df.gm_one.plot(figsize = (20,12))</span>  
    <span style="font-size:18px;">plt.savefig('qjd_gm_one.jpg')</span>  
    <span style="font-size:18px;">  
    </span>  

好啦,做完啦,雖然我也不知道為什么全聚德會(huì)和光明乳業(yè)這么高相關(guān)性
不過(guò)理論上是可以做配對(duì)交易的,不過(guò)股票有風(fēng)險(xiǎn),投資需謹(jǐn)慎~
以下是完整代碼:
[python] view plain copy

    # coding=gbk  
      
    import matplotlib.pyplot as plt #提供類matlab里繪圖框架  
    import numpy as np  
    import pandas as pd  
    import tushare as ts  
      
    #獲取數(shù)據(jù)  
    s_qjd = '002186' #全聚德  
    s_gm = '600597' #光明乳業(yè)  
    sdate = '2016-01-01'#起止日期  
    edate = '2016-12-31'  
    df_qjd = ts.get_h_data(s_qjd, start = sdate, end = edate).sort_index(axis = 0,ascending=True)#獲取歷史數(shù)據(jù)  
    df_gm = ts.get_h_data(s_gm, start = sdate, end = edate).sort_index(axis = 0,ascending=True)  
    df = pd.concat([df_qjd.close,df_gm.close], axis = 1, keys=['qjd_close', 'gm_close'])#合并  
    df.ffill(axis=0, inplace=True)#填充缺失數(shù)據(jù)  
    df.to_csv('qjd_gm.csv')  
      
    #pearson方法計(jì)算相關(guān)性  
    corr = df.corr(method = 'pearson', min_periods = 1)  
    print(corr)  
      
    #打印圖像  
    df.plot(figsize = (20,12))  
    plt.savefig('qjd_gm.jpg')  
    plt.close()  
      
    #歸一化處理打印圖像  
    df['qjd_one'] = df.qjd_close / float(df.qjd_close[0])*100  
    df['gm_one'] = df.gm_close / float(df.gm_close[0])*100  
    df.qjd_one.plot(figsize = (20,12))  
    df.gm_one.plot(figsize = (20,12))  
    plt.savefig('qjd_gm_one.jpg')

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }