参考:
https://www.cnblogs.com/hbhjkzhangmin/p/10869504.html
权限页面
https://tushare.pro/document/1?doc_id=108
安装tushare
pip install tushare
参考: http://tushare.org/
获取token:
https://tushare.pro/document/1?doc_id=39
调取数据
https://tushare.pro/document/1?doc_id=40
旧版:
http://tushare.org/
python基础篇—监控股票
使用tushare库
获取股票信息
1.安装tushare库
win+R输入cmd进入控制台
输入pip install tushare
2.获取股票信息
import tushare,time
#导入tushare库
data = tushare.get_realtime_quotes('000581')
#获取股票代码为000581的股票信息
print(data)
log 为:
D:\PYTHON_ENV\stock\Scripts\python.exe C:/Users/xxxx/PycharmProjects/stock/test.py
name open pre_close price ... a5_p date time code
0 威孚高科 20.070 20.000 20.320 ... 20.380 2020-01-16 11:07:24 000581
[1 rows x 33 columns]
Process finished with exit code 0
3.根据自定义要求,获取指定数据
因为tushare获取的返回值是一个pandas类型数据。可以用loc方式获取单独数据
data = tushare.get_realtime_quotes(share.code)
share.name = data.loc[0][0]
share.open = float(data.loc[0][1])
share.price = float(data.loc[0][3])
share.high = float(data.loc[0][4])
share.low = float(data.loc[0][5])
4.tushare库详细用法
参考资料:http://tushare.waditu.com/trading.html
完整代码-------------------------------------------------------------------
import tushare,time
def getrealtimedata(share):
data = tushare.get_realtime_quotes(share.code)
share.name = data.loc[0][0]
share.open = float(data.loc[0][1])
share.price = float(data.loc[0][3])
share.high = float(data.loc[0][4])
share.low = float(data.loc[0][5])
share.describe='股票编号:{},股票名称:{},今日开盘价:{},当前价格:{},今日最高价:{},今日最低价:{}'.format(share.code,share.name,share.open,share.price,share.high,share.low)
return share
class Share():
def __init__(self,code,buy,sale):
self.name = ''
self.open = ''
self.price = ''
self.high = ''
self.low = ''
self.describe=''
self.code = code
self.buy = buy
self.sale = sale
def main(sharelist):
# share = Share(code)
for share in sharelist:
sss=getrealtimedata(share)
print(sss.describe)
if sss.price <=sss.buy:
print('价格超低,赶紧买入!')
elif sss.price >= sss.sale:
print('赶紧卖出。大赚了!')
else:
print('静观其变……')
while True:
share1=Share("000581",18.7,19.0)
share2=Share("600106",18.7,19.0)
share3=Share("000591",18.7,19.0)
sharelist = [share1,share2,share3]
main(sharelist)
time.sleep(5)
上证指数:
share0 = Share(“sh”, 2900, 3300)
Python获取股票交易数据
https://www.jianshu.com/p/6b416cfdb7a0
6.获取实时分笔数据get_realtime_quotes()
获取实时分笔数据,可以实时取得股票当前报价和成交信息,其中一种场景是,写一个python定时程序来调用本接口(可两三秒执行一次,性能与行情软件基本一致),然后通过DataFrame的矩阵计算实现交易监控,可实时监测交易量和价格的变化。
参数说明:
symbols:6位数字股票代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板) 可输入的类型:str、list、set或者pandas的Series对象
返回值说明:
0:name,股票名字
1:open,今日开盘价
2:pre_close,昨日收盘价
3:price,当前价格
4:high,今日最高价
5:low,今日最低价
6:bid,竞买价,即“买一”报价
7:ask,竞卖价,即“卖一”报价
8:volume,成交量 maybe you need do volume/100
9:amount,成交金额(元 CNY)
10:b1_v,委买一(笔数 bid volume)
11:b1_p,委买一(价格 bid price)
12:b2_v,“买二”
13:b2_p,“买二”
14:b3_v,“买三”
15:b3_p,“买三”
16:b4_v,“买四”
17:b4_p,“买四”
18:b5_v,“买五”
19:b5_p,“买五”
20:a1_v,委卖一(笔数 ask volume)
21:a1_p,委卖一(价格 ask price)
...
30:date,日期
31:time,时间
使用方法
# 单个股票实时行情 df = ts.get_realtime_quotes(code) df
执行结果
使用方法
# 需要显示的属性df[['code','name','price','bid','ask','volume','amount','time']]
执行结果
使用方法
for i in range(5): df = ts.get_realtime_quotes(code) df = df[['code','name','price','bid',
作者:X人工智能学院
链接:https://www.jianshu.com/p/6b416cfdb7a0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我是通过新浪财经和腾讯的API接口去拿股票行情数据的,目前A股数据是实时的,港股和美股只能拿到延迟15分钟的数据
爬取数据
https://blog.youkuaiyun.com/hanyun9988/article/details/78598060