Python股票盯盘助手

学习股票交易有一段时间了,还未逃脱盯盘的 初级阶段,索性写了一段微信盯盘脚本,将账户信息实时发送到微信助手中

  • 先导入需要的包
import tushare as ts
import itchat, time
from itchat.content import TEXT
import datetime
  • 登陆微信
itchat.logout()
itchat.auto_login(hotReload=True)
  • 编写主要计算函数,通过给定的列表获取实时行情信息,并计算股票收益情况
#定义股票列表,格式为【‘股票名称_股票代码_建仓价格_数量’】,可建多个仓位
#stock_list=['中科曙光_603019_44.836_200','大华股份_002236_12.830_100','掌阅科技_603533_19.370_100','紫光股份_000938_35.850_100']
#计算函数,通过给定的股票列表,获取实时行情信息,并计算收益情况
def earnings(stock_list):
    message=nowTime+"收益播报:\r\n"+"\n"
    #获取上证指数
    sh=ts.get_realtime_quotes('sh')
    sh_pro=float(sh.to_dict()['price'][0])-float(sh.to_dict()['pre_close'][0])
    message+="{}\t{}\t{}\t{}".format("上证指数",str(round(float(sh.to_dict()['price'][0]),2)),str(round(sh_pro,3)),sh.to_dict()['time'][0])+"\r\n"
    #message+=sh.to_dict()['name'][0]+"\t"+sh.to_dict()['price'][0]+"\t"+str(round(sh_pro,4))+"\t"+sh.to_dict()['time'][0]+"\r\n"
    pro_sum=0.000
    #遍历股票列表,分割出股票的名称,代码,价格,数量
    for stock in stock_list:
        #名称
        stock_name=stock.split("_")[0]
        #代码
        stock_code=stock.split("_")[1]
        #价格
        stock_price=stock.split("_")[2]
        #数量
        stock_num=stock.split("_")[3]
        
        #通过ts获取股票实时信息
        df = ts.get_realtime_quotes(stock_code)
        
        #计算收益
        profit=(float(df['price'])-float(stock_price))*int(stock_num)
        pro_sum+=profit
        
        #写成txt
        str_pro="{}\t{}\t{}\t{}".format(stock_name,str(round(float(df.to_dict()['price'][0]),2)),str(round(profit,4)),df.to_dict()['time'][0])
        #str_pro=stock_name+"\t"+str(round(float(df.to_dict()['price'][0]),2))+"\t"+str(round(profit,4))+"\t"+df.to_dict()['time'][0]
        message+=str_pro+"\r\n"
    sh=ts.get_realtime_quotes('sh')
    
    message+='总盈亏为: '+str(round(pro_sum,4))
    return message
  
  • 通过while死循环不停的执行上文的函数,并将结果发送到微信上
stock_list1=['中科曙光_603019_44.836_200','大华股份_002236_12.830_100','掌阅科技_603533_19.370_100','紫光股份_000938_35.850_100']
while True:
    nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#现在
    string = earnings(stock_list1)
    print(string)
    #通过微信助手发送给自己
    itchat.send(msg=string, toUserName="filehelper")
    #设置间隔时间(秒)
    time.sleep(60)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值