运用Python语言开发一个简单的“A股提醒系统”

1.首先在cmd命令中安装pip模块,tushare模块,bs4模块,pyemali

python -m pip install --upgrade pip
pip install tushare
pip install bs4
pip install pyemail

2.然后打印出某只股票的详细信息

import tushare                                  #调用tushare模块
dataNow=tushare.get_realtime_quotes("000591")   #括号内为股票代码
print(dataNow)

运行结果如下

img

  • name:股票名
  • open:开盘价格
  • pre_close:闭盘价格
  • price:当前价格
  • a5_p:五日的金价
  • date:上一次开盘日期
  • time:收盘时间
  • code:股票代码

3.接下来整理数据

import tushare                                  #调用tushare模块
dataNow=tushare.get_realtime_quotes("000591")   #括号内为股票代码

name=dataNow.loc[0][0]                #获取股票名
price=dataNow.loc[0][3]               #获取当前价格
high=dataNow.loc[0][4]                #获取最高价
low=dataNow.loc[0][5]                 #获取最低价
volumn=dataNow.loc[0][8]              #获取成交量
amount=dataNow.loc[0][9]              #获取成交金额
openToday=dataNow.loc[0][1]           #获取当天开盘量
pre_close=dataNow.loc[0][2]           #获取昨日收盘价
timee=dataNow.loc[0][30]              #获取时间

print("股票名:",name,"当前价格:",price,"最高价:",high,"最低价:",low,"成交量:",volumn,"成交额:",amount,"开盘价:",openToday,"收盘价:",pre_close,"时间:",timee)

运行结果如下

img

4.设置一个买点与卖点并比较价格

import tushare                                  #调用tushare模块
dataNow=tushare.get_realtime_quotes("000591")   #括号内为股票代码

name=dataNow.loc[0][0]                #获取股票名
price=dataNow.loc[0][3]               #获取当前价格
high=dataNow.loc[0][4]                #获取最高价
low=dataNow.loc[0][5]                 #获取最低价
volumn=dataNow.loc[0][8]              #获取成交量
amount=dataNow.loc[0][9]              #获取成交金额
openToday=dataNow.loc[0][1]           #获取当天开盘量
pre_close=dataNow.loc[0][2]           #获取昨日收盘价
timee=dataNow.loc[0][30]              #获取时间

print("股票名:",name,"当前价格:",price,"最高价:",high,"最低价:",low,"成交量:",volumn,"成交额:",amount,"开盘价:",openToday,"收盘价:",pre_close,"时间:",timee)

buy="3.85"           #买点                        
sale="3.9"           #卖点
#将当前价格与买点、卖点进行比较
if price<=buy:
    print("达到买点")
elif price>=sale:
    print("达到卖点")
else:
    print("既没有达到买点,也没有达到卖点")

5.添加循环,每隔30秒更新一次数据

import tushare                                  #调用tushare模块
import time

while 1==1:
    dataNow=tushare.get_realtime_quotes("000591")   #括号内为股票代码

    name=dataNow.loc[0][0]                #获取股票名
    price=dataNow.loc[0][3]               #获取当前价格
    high=dataNow.loc[0][4]                #获取最高价
    low=dataNow.loc[0][5]                 #获取最低价
    volumn=dataNow.loc[0][8]              #获取成交量
    amount=dataNow.loc[0][9]              #获取成交金额
    openToday=dataNow.loc[0][1]           #获取当天开盘量
    pre_close=dataNow.loc[0][2]           #获取昨日收盘价
    timee=dataNow.loc[0][30]              #获取时间

    print("股票名:",name,"当前价格:",price,"最高价:",high,"最低价:",low,"成交量:",volumn,"成交额:",amount,"开盘价:",openToday,"收盘价:",pre_close,"时间:",timee)

    buy="3.85"           #买点                        
    sale="3.9"           #卖点
    #将当前价格与买点、卖点进行比较
    if price<=buy:
        print("达到买点")
    elif price>=sale:
        print("达到卖点")
    else:
        print("既没有达到买点,也没有达到卖点")

    time.sleep(30)      #每隔30秒自动获取一次最新数据

6.优化代码,封装类和方法

import tushare                                  #调用tushare模块
import time
#封装:获取股票数据
def getrealtimedata(share):
    dataNow=tushare.get_realtime_quotes(share.code)   #括号内为股票代码
    share.name=dataNow.loc[0][0]                #获取股票名
    share.price=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.timee=dataNow.loc[0][30]              #获取时间
    share.describe="股票名:"+share.name,"当前价格:"+str(share.price)
    return share

#封装类
class Share():
    def __init__(self,code):
        self.name=""
        self.price=""
        self.high=""
        self.low=""
        self.volumn=""
        self.amount=""
        self.openToday=""
        self.pre_close=""
        self.timee=""
        self.describe=""
        self.code=code

#设置主函数
def main(code,buy,sale):
    share=Share("000591")
    sss=getrealtimedata(share)
    print(sss.describe)
    #将当前价格与买点、卖点进行比较
    if sss.price<=buy:
        print("达到买点")
    elif sss.price>=sale:
        print("达到卖点")
    else:
        print("既没有达到买点,也没有达到卖点")

#主程序
while 1==1:
    main("000591","3.3","3.36")
    time.sleep(30)      #每隔30秒自动获取一次最新数据

7.继续优化代码,同时显示多只股票信息

import tushare                                  #调用tushare模块
import time
#封装:获取股票数据
def getrealtimedata(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.timee=dataNow.loc[0][30]
    share.describe="股票名:"+share.name+"当前价格:"+str(share.price)

    return share

#封装类
class Share():
    def __init__(self,code,buy,sale):
        self.name=""
        self.price=""
        self.high=""
        self.low=""
        self.volumn=""
        self.amount=""
        self.openToday=""
        self.pre_close=""
        self.timee=""
        self.describe=""
        self.code=code
        self.buy=buy
        self.sale=sale

#设置主函数
def main(shareList):

    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 1==1:
    share1=Share("600106",3.1,3.18)
    share2=Share("601988",3.4,3.5)
    share3=Share("000591",3.0,3.7)
    list1=[share1,share2,share3]
    print("-----------")
    main(list1)

    time.sleep(30) #每30秒更新数据

8.添加邮箱提醒

import tushare                                  #调用tushare模块
import time
import smtplib
from email.mime.text import MIMEText
#封装:获取股票数据
def getrealtimedata(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.timee=dataNow.loc[0][30]
    share.describe="股票名:"+share.name+"当前价格:"+str(share.price)

    return share
#发送邮件
def sendemail(subject,content):
    masg_from="xxxxx@qq.com"  #发送方地址
    pwd="xxxxxxxxx"           #客户端授权码
    to="xxxxxxxx@qq.com"      #收件方地址
    #构造邮件
    msg=MIMEText(content)
    msg["Subject"]=subject
    msg["From"]=masg_from
    msg["To"]=to
    #发送邮件
    try:
        ss=smtplib.SMTP_SSL("smtp.qq.com",465)   #smtp服务端地址和端口号
        ss.login(masg_from,pwd)                   #登陆邮箱
        ss.sendmail(masg_from,to,msg.as_string()) #发送
        print("发送成功!")
    except Exception as e:
        print("发送失败!详情:",e)


#封装类
class Share():
    def __init__(self,code,buy,sale):
        self.name=""
        self.price=""
        self.high=""
        self.low=""
        self.volumn=""
        self.amount=""
        self.openToday=""
        self.pre_close=""
        self.timee=""
        self.describe=""
        self.code=code
        self.buy=buy
        self.sale=sale

#设置主函数
def main(shareList):

    for share in shareList:
        sss=getrealtimedata(share)
        print(sss.describe)
        if sss.price<=sss.buy:
            print("达到买点")
            sendemail("达到买点",sss.describe)
        elif sss.price>=sss.sale:
            print("达到卖点")
            sendemail("达到卖点",sss.describe)
        else:
            print("既没有达到卖点,也没有达到买点")


#主程序
while 1==1:
    share1=Share("600106",3.1,3.18)
    share2=Share("601988",3.4,3.5)
    share3=Share("000591",3.0,3.7)
    list1=[share1,share2,share3]
    print("-----------")
    main(list1)

    time.sleep(30) #每30秒更新数据

Comments | NOTHING