首頁>技術>

1、從網易上下載滬深300的資料,先儲存為data.csv資料,然後再用pandas讀取。

import pandas as pdimport numpy as npimport requestsimport timeimport osstart=''end=time.localtime()end=time.strftime('%Y%m%d',end)code='1399300'url= 'http://quotes.money.163.com/service/chddata.html?code=%s&start=%s&end=%s&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER'%(code,start,end)r=requests.get(url)path="data.csv"with open(path,"wb") as f:    f.write(r.content)f.close()pf=pd.read_csv('data.csv',encoding='gbk')if os.path.exists(path):    os.remove(path)print(pf)

讀取的pandas資料如下顯示:

    日期     股票程式碼     名稱        收盤價        最高價        最低價        開盤價  \0     2021-02-09  '399300  滬深300  5686.2502  5686.2502  5555.0537  5584.1650   1     2021-02-08  '399300  滬深300  5564.5618  5576.4908  5479.8529  5506.1423   2     2021-02-05  '399300  滬深300  5483.4140  5550.7714  5477.6517  5494.7011   。。。

2、書寫方法,其中的frame是原始的表格,column是要標記波峰谷的資料,interval是每隔幾天標記一次,返回的表格會多出幾列分別記錄是波峰還是波谷,如果是波峰則是1,如果是波谷則是-1,如果既不是波峰也不是波谷就用0表示,還有順便記錄持續時間等。:

def biao_ji_bo_feng_gu(frame,cloumn,interval):    indexs=np.arange(0,len(frame),interval)    new_frame=frame.loc[indexs]    new_frame['feng_gu']=0  #1為波峰,0為持續狀態,-1為波谷    new_frame['chi_xu_shi_jian']=0#下跌或上漲持續的時間    new_frame['feng_gu_bian_hua_lv']=0    new_frame['bo_feng_bian_hua_lv']=0#兩個波峰之間的變化率,兩個波谷之間的變化率    new_frame['bo_gu_bian_hua_lv']=0    i=1    s_close=new_frame['close']    s_feng_gu=pd.Series(np.zeros(len(indexs)),index=indexs)    while i <len(indexs):        if s_close[indexs[i]]>s_close[indexs[i-1]]:            s_feng_gu[indexs[i]]=1            if s_feng_gu[indexs[i]]==s_feng_gu[indexs[i-1]]:                s_feng_gu[indexs[i-1]]=0        else:            s_feng_gu[indexs[i]]=-1            if s_feng_gu[indexs[i]]==s_feng_gu[indexs[i-1]]:                s_feng_gu[indexs[i-1]]=0        i+=1    i=1    new_frame['feng_gu']=s_feng_gu        i=0    j=1    s_chi_xu_shi_jian=pd.Series(np.zeros(len(indexs)),index=indexs)    while i<len(s_feng_gu):        if s_feng_gu[indexs[i]]==0:            s_chi_xu_shi_jian[indexs[i]]=j*interval            j+=1        else:            s_chi_xu_shi_jian[indexs[i]]=j*interval            j=1        i+=1    new_frame['chi_xu_shi_jian']=s_chi_xu_shi_jian            s_feng_gu_bian_hua_lv=pd.Series(np.zeros(len(indexs)),index=indexs)    s_tmp=new_frame[new_frame['feng_gu']!=0]['close']    index_tmp=s_tmp.index    i=1    while i<len(index_tmp):        s_feng_gu_bian_hua_lv[index_tmp[i]]=(s_tmp[index_tmp[i]]-s_tmp[index_tmp[i-1]])/s_tmp[index_tmp[i-1]]        i+=1    new_frame['feng_gu_bian_hua_lv']=s_feng_gu_bian_hua_lv            s_bo_feng_bian_hua_lv=pd.Series(np.zeros(len(indexs)),index=indexs)    s_tmp=new_frame[new_frame['feng_gu']==1]['close']    index_tmp=s_tmp.index    i=1    while i<len(index_tmp):        s_bo_feng_bian_hua_lv[index_tmp[i]]=(s_tmp[index_tmp[i]]-s_tmp[index_tmp[i-1]])/s_tmp[index_tmp[i-1]]        i+=1    new_frame['bo_feng_bian_hua_lv']=s_bo_feng_bian_hua_lv            s_bo_gu_bian_hua_lv=pd.Series(np.zeros(len(indexs)),index=indexs)    s_tmp=new_frame[new_frame['feng_gu']==-1]['close']    index_tmp=s_tmp.index    i=1    while i<len(index_tmp):        s_bo_gu_bian_hua_lv[index_tmp[i]]=(s_tmp[index_tmp[i]]-s_tmp[index_tmp[i-1]])/s_tmp[index_tmp[i-1]]        i+=1    new_frame['bo_gu_bian_hua_lv']=s_bo_gu_bian_hua_lv          return new_frame
columns=['data','code','name','close','high','low','open','qian_shou_pan','zhang_die_e','zhang_die_fu','cheng_jiao_liang','cheng_jiao_e']pf.columns=columns#將原來的表格column改成英文名new_frame=biao_ji_bo_feng_gu(pf,'close',1)

下面把標記好的峰谷篩選出來:

new_frame=new_frame[new_frame['feng_gu']!=0]plt.plot(new_frame.loc[len(pf)-50:len(pf)]['close'],'r-')plt.plot(pf.loc[len(pf)-50:len(pf)]['close'],'b--')

每隔五天標記一次會更加明顯一點:

new_frame=biao_ji_bo_feng_gu(pf,'close',5)new_frame=new_frame[new_frame['feng_gu']!=0]plt.plot(new_frame.loc[len(pf)-100:len(pf)]['close'],'r-')plt.plot(pf.loc[len(pf)-100:len(pf)]['close'],'b--')

13
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 在應用中導航時使用 SafeArgs |MAD Skills