Python

[CP_02] Python案例:A股提醒系统Tushare

2019-03-15  本文已影响0人  Fighting_001

案例描述:
利用第三方接口Tushare,获取实时股票数据,如查询股票的历史行情、实时行情(开盘价、成交笔数、价格...);然后设置预期的买点or卖点,当达到设置的预期值时,系统进行自动提醒

传送门:http://tushare.org/

1. 安装实时获取股票数据的支持接口

1)安装tushare及其他依赖的程序包

pip install tushare

本次tushare依赖的第三方包:
pandas、numpy作数据计算分析
lxml:网页解析
requests:爬虫使用
simplejson:处理json数据
......

2)安装依赖的第三方包:bs4 (BeautifulSoup)

pip install bs4

2. 调试接口请求效果

import tushare

# 在get_realtime_quotes()方法中传入股票代码作为参数
dataNow=tushare.get_realtime_quotes("000591")
# 输出指定股票代码在最近一个工作日的股票数据
print(dataNow)

3. 编码实现Tushare提醒系统

import tushare
import time

# 获取最近一个工作日的股票数据
def getrealtimedata(share): # 传入Share类的对象share作为参数
    dataNow=tushare.get_realtime_quotes(share.code)
    share.name=dataNow.loc[0][0]    # 股票名
    share.price=float(dataNow.loc[0][3])    # 当前价格
    share.high=dataNow.loc[0][4]    # 最高价
    share.low=dataNow.loc[0][5] # 最低价
    share.volumn=dataNow.loc[0][8]  # 成交额
    share.amount=dataNow.loc[0][9]  # 成交量
    share.openToday=dataNow.loc[0][1]   # 开盘价
    share.pre_close=dataNow.loc[0][2]   # 收盘价
    share.date=dataNow.loc[0][30]   # 日期
    share.describe="股票名:"+share.name,"股票代码"+share.code,"当前价格:"+str(share.price)
    return share

# 定义股票类
class Share():
    def __init__(self,code,buy,sale):
        self.name=""
        self.price=""
        self.high=""
        self.low=""
        self.amount=""
        self.openToday=""
        self.pre_close=""
        self.volumn=""
        self.date=""
        self.describe=""
        self.code=code
        self.buy=buy
        self.sale=sale

# 判断股票买点&卖点
def main(shareList):
    for share in shareList:
        ss=getrealtimedata(share)
        print(ss.describe)

        if ss.price<=ss.buy:
            print("已达到买点,可考虑购入")
        elif ss.price >=ss.sale:
            print("已达到卖点,可考虑卖出")
        else:
            print("耐心观察ing")

# 监控指定股票的数据
while True:
    share1=Share("600106",3.0,3.1)
    share2=Share("601988",3.5,3.8)
    share3=Share("000591",3.2,3.5)
    list1=[share1,share2,share3]

    main(list1)
    time.sleep(30)

执行效果:

上一篇下一篇

猜你喜欢

热点阅读