利用tushare进行对兴业银行股价的爬取,并使用numpy进行分析
生活随笔
收集整理的這篇文章主要介紹了
利用tushare进行对兴业银行股价的爬取,并使用numpy进行分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import sys
import tushare as ts
import numpy as np
data=ts.get_h_data('601066')
print(data)
#讀出興業(yè)銀行7列數(shù)據(jù)
date? ?open? ? high close low? ? ?volume? ? ? amount? ?? data.to_csv('E:/csv statistic/day/601066.csv') 把讀取的股票數(shù)據(jù)存入某個內(nèi)存空間中 #開始讀某列數(shù)據(jù)
c,v=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,5),unpack=True)#收盤價,交易量
vwap=np.average(c,weights=v)#成交量加權平均值,
print('vwap=',vwap)
vwap= 9.63994147552952
print(np.mean(c))
9.0965625
#計算時間加權平均值
t=np.arange(len(c))
print(np.average(c,weights=t))
9.534940476190476
#尋找最大值和最小值
h,l=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(2,4),unpack=True)
print(np.max(h))
11.98
print(np.min(l))
6.5
#計算中程數(shù)和極差
print(np.max(h)+np.min(l)/2)#計算中程數(shù)
print(np.ptp(h))#計算最大值和最小值的差值
print(np.ptp(l))#計算交易量化的差值
15.254999999999999
4.19
4.92
#統(tǒng)計分析c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=Trueprint(np.median(c))#中間數(shù)
8.850000000000001
print('sorted:',np.msort(c))#從小到大排列 sorted: [ 7.77 7.83 7.87 7.92 7.95 8. 8.06 8.15 8.17 8.19 8.2 8.2 8.23 8.27 8.28 8.32 8.32 8.35 8.36 8.36 8.36 8.36
8.48 8.5 8.53 8.53 8.54 8.55 8.61 8.61 8.64 8.82 8.88 9.09 9.15 9.37 9.38 9.38 9.39 9.45 9.45 9.48 9.52 9.67 9.7 9.73 9.8 9.83
9.86 9.91 9.93 10.06 10.23 10.27 10.31 10.33 10.33 10.41 10.42 10.56 10.8 11.09 11.46 11.85] print('方差:',np.var(c))
方差: 0.9610756835937502
print('手動計算方差:',np.mean((c-c.mean())**2))
手動計算方差: 0.9610756835937502
#股票收益率的計算
c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
returns=np.diff(c)/c[:-1] #diff計算差分,總體是求股票收益率
print(np.std(returns)) # 求標準差
log_returns=np.diff(np.log(c)) #股票對數(shù)收益率
print(log_returns)
posretindices=np.where(returns>0)#股票收益率大于0的那幾天
print(posretindices)
ann_vol=np.std(log_returns)/np.mean(log_returns)#日收益率
print(ann_vol)
ann_vol=ann_vol/np.sqrt(1./252.)#年收益率
print(ann_vol)
print(ann_vol*np.sqrt(1./12.))#月收益率
#定義一個 把日期轉(zhuǎn)換為數(shù)字的函數(shù)
from datetime import datetime
def date2num(s):
return datetime.strptime(s,'%Y-%m-%d').date().weekday()
#讀取收盤價
close=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
#讀取日期
dates=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(0,),unpack=True,dtype=bytes).astype(str)
#讀取的str數(shù)據(jù)轉(zhuǎn)換為 數(shù)字
for i in range (dates.size):
dates[i]=date2num(dates[i])
dates=dates.astype(np.int8)
print(dates.dtype)
print(dates) averages = np.zeros(5) # 定義一個空數(shù)組
# 依次獲取不同天的索引,然后根據(jù)索引求得close(收盤價)數(shù)組中所有的對應值,計算平均值,把平均值加入到averages數(shù)組中
for i in range(5):
indices = np.where(dates == i)
prices = np.take(close, indices)
avg = np.mean(prices)
print("Day", i, "prices", prices, "Average", avg)
averages[i] = avg
top = np.max(averages) # 計算周每日的收盤價平均值的最大值
np.argmax(averages) # 獲取周每日的收盤價平均值的最大值是哪一天
bottom = np.min(averages) # 計算周每日的收盤價平均值的最小值
np.argmin(averages) # 獲取周每日的收盤價平均值的最小值是哪一天 ?
? ?
import tushare as ts
import numpy as np
data=ts.get_h_data('601066')
print(data)
#讀出興業(yè)銀行7列數(shù)據(jù)
date? ?open? ? high close low? ? ?volume? ? ? amount? ?? data.to_csv('E:/csv statistic/day/601066.csv') 把讀取的股票數(shù)據(jù)存入某個內(nèi)存空間中 #開始讀某列數(shù)據(jù)
c,v=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,5),unpack=True)#收盤價,交易量
vwap=np.average(c,weights=v)#成交量加權平均值,
print('vwap=',vwap)
vwap= 9.63994147552952
print(np.mean(c))
9.0965625
#計算時間加權平均值
t=np.arange(len(c))
print(np.average(c,weights=t))
9.534940476190476
#尋找最大值和最小值
h,l=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(2,4),unpack=True)
print(np.max(h))
11.98
print(np.min(l))
6.5
#計算中程數(shù)和極差
print(np.max(h)+np.min(l)/2)#計算中程數(shù)
print(np.ptp(h))#計算最大值和最小值的差值
print(np.ptp(l))#計算交易量化的差值
15.254999999999999
4.19
4.92
#統(tǒng)計分析c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=Trueprint(np.median(c))#中間數(shù)
8.850000000000001
print('sorted:',np.msort(c))#從小到大排列 sorted: [ 7.77 7.83 7.87 7.92 7.95 8. 8.06 8.15 8.17 8.19 8.2 8.2 8.23 8.27 8.28 8.32 8.32 8.35 8.36 8.36 8.36 8.36
8.48 8.5 8.53 8.53 8.54 8.55 8.61 8.61 8.64 8.82 8.88 9.09 9.15 9.37 9.38 9.38 9.39 9.45 9.45 9.48 9.52 9.67 9.7 9.73 9.8 9.83
9.86 9.91 9.93 10.06 10.23 10.27 10.31 10.33 10.33 10.41 10.42 10.56 10.8 11.09 11.46 11.85] print('方差:',np.var(c))
方差: 0.9610756835937502
print('手動計算方差:',np.mean((c-c.mean())**2))
手動計算方差: 0.9610756835937502
#股票收益率的計算
c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
returns=np.diff(c)/c[:-1] #diff計算差分,總體是求股票收益率
print(np.std(returns)) # 求標準差
log_returns=np.diff(np.log(c)) #股票對數(shù)收益率
print(log_returns)
posretindices=np.where(returns>0)#股票收益率大于0的那幾天
print(posretindices)
ann_vol=np.std(log_returns)/np.mean(log_returns)#日收益率
print(ann_vol)
ann_vol=ann_vol/np.sqrt(1./252.)#年收益率
print(ann_vol)
print(ann_vol*np.sqrt(1./12.))#月收益率
#定義一個 把日期轉(zhuǎn)換為數(shù)字的函數(shù)
from datetime import datetime
def date2num(s):
return datetime.strptime(s,'%Y-%m-%d').date().weekday()
#讀取收盤價
close=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
#讀取日期
dates=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(0,),unpack=True,dtype=bytes).astype(str)
#讀取的str數(shù)據(jù)轉(zhuǎn)換為 數(shù)字
for i in range (dates.size):
dates[i]=date2num(dates[i])
dates=dates.astype(np.int8)
print(dates.dtype)
print(dates) averages = np.zeros(5) # 定義一個空數(shù)組
# 依次獲取不同天的索引,然后根據(jù)索引求得close(收盤價)數(shù)組中所有的對應值,計算平均值,把平均值加入到averages數(shù)組中
for i in range(5):
indices = np.where(dates == i)
prices = np.take(close, indices)
avg = np.mean(prices)
print("Day", i, "prices", prices, "Average", avg)
averages[i] = avg
top = np.max(averages) # 計算周每日的收盤價平均值的最大值
np.argmax(averages) # 獲取周每日的收盤價平均值的最大值是哪一天
bottom = np.min(averages) # 計算周每日的收盤價平均值的最小值
np.argmin(averages) # 獲取周每日的收盤價平均值的最小值是哪一天 ?
? ?
轉(zhuǎn)載于:https://www.cnblogs.com/ConnorShip/p/9667473.html
總結
以上是生活随笔為你收集整理的利用tushare进行对兴业银行股价的爬取,并使用numpy进行分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue 事件修饰符
- 下一篇: [学习总结] python语言学习总结