python tushare 股票数据基本统计量

pip install tushare

get_stock.py

# coding: utf-8
import os, sys
import urllib.request 
import pandas as pd
import tushare as ts

if len(sys.argv) ==2:
    stock = sys.argv[1]
else:
    print('usage: python get_stock.py stockcode ')
    sys.exit(1)

if len(stock) !=8:
    print('stock code length: 8')
    sys.exit(2)

url = 'http://hq.sinajs.cn/?list=%s' % stock
with urllib.request.urlopen(url) as f:
	content = f.read().decode('cp936')
#print(content)
data = content.split('"')[1].split(',')
name = data[0]
price = float(data[3])  # price_current
# change_percent
change = (float(data[3])-float(data[2]))*100 / float(data[2])
print("code:{0} name:{1} price: {2:.2f} 涨跌幅:{3:.2f}%".format(stock,name,price,change))

code = stock[2:]

# help(ts.get_k_data) 了解参数
df = ts.get_k_data(code)
print(df['close'].describe())

vmax = df['high'].max()
vmin = df['low'].min()
print("最高价: {0} , 最低价: {1} , 价差: {2:.3f}".format(vmax, vmin,vmax-vmin))
print("最大升幅: +{0:.3f} , 最大回跌: -{1:.3f}".format(vmax/vmin-1,1-vmin/vmax))
print("  Up space: +{0:.3f}".format(vmax/price -1))
print("Down space: -{0:.3f}".format(1-vmin/price))

运行 python get_stock.py sz000001

你可能感兴趣的:(python)