python脚本监控股票价格钉钉推送

  • 2021 年 8 月 19 日
  • 笔记

关注股市,发家致富

  问题:一天天盯着股市多累,尤其上班,还不能暴露,股票软件,红红绿绿,这么明显的列表页面,一看就知道在摸鱼。被领导发现饭碗就没了

  解决:搞个脚本监听一下自己关注的股票,一到价格就发个钉钉消息推送,上班摸鱼两不误。

一、配置centos的python版本

  1. centos7自带了python2,但是安装模块的时候各种报错,基本上都是版本的原因,pip install 默认都下载了最新版本的模块包,但是最新版本的模块包都不支持python2,需要python3,不闲累的话,可以指定版本号进行模块的安装。
  2. 两个等号用于指定版本 pip install pandas==0.19.0 -i //pypi.douban.com/simple/ –trusted-host pypi.douban.com (带上代理站点,安装起来嗖嗖的)
  3. 还是安装个python3比较靠谱,yum install python3
  4. 不同版本的模块安装:
  5. python2 -m pip install 模块名称
    python3 -m pip install 模块名称 

二、添加钉钉机器人

  1.                            
  2.                
  3.  添加完机器人后,获取webhook地址  

三、编写python脚本

   

# encoding: utf-8

import requests
import tushare, time
import datetime

# 消息内容,url地址
from pandas._libs import json

# 机器人回调地址
webhook = '//oapi.dingtalk.com/robot/send?access_token=23e2b46e8b55a0573a0e92a26b427281f9aa85f387593c5e9f1b3c889c141148'

# 开市时间、闭市时间
# 09:20 11:30 13:00 15:00
amStart = 920
amEnd = 1130
pmStart = 1300
pmEnd = 1500

# 默认当前状态为闭市
nowStatus = 'off'


def dingtalk(msg):
    print('【钉钉】:', msg)
    headers = {'Content-Type': 'application/json; charset=utf-8'}
    data = {'msgtype': 'text', 'text': {'content': msg}, 'at': {'atMobiles': [], 'isAtAll': False}}
    post_data = json.dumps(data)
    response = requests.post(webhook, headers=headers, data=post_data)
    return response.text


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.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
        self.num = 0


def main(sharelist):
    for share in sharelist:
        stock = getrealtimedata(share)
        print(stock.describe)
        if stock.price == 0:
            continue
        if stock.price <= stock.buy:
            # 如果连续提示10次,就不再提示
            if share.num > 5:
                continue
            dingtalk('【价格低于[{}]赶紧买入】{}'.format(share.buy, stock.describe))
            print(share.num)
            share.num += 1

            print(share.num)
        elif stock.price >= stock.sale:
            dingtalk('【价格高于[{}]赶紧卖出】{}'.format(share.sale, stock.describe))
        else:
        print('静观其变……')


# 重置计数器num
def reset(sharelist):
    for share in sharelist:
        share.num = 0


# 股票编号  买价提示,,卖价提示
# 002273水晶光电
share1 = Share("002273", 13.4, 15)
# 600100同方股份
share2 = Share("600100", 5.92, 6.03)
# 000810创维数字
share3 = Share("000810", 7.66, 7.77)
sharelist = [share1, share2, share3]
while True:
    now = datetime.datetime.now()
    dayOfWeek = now.isoweekday()
    print(dayOfWeek)
    # 工作日
    if dayOfWeek < 6:
        print("workday")
        nowTimeInt = int(now.strftime("%H%M"))
        print("当前时间:", nowTimeInt)
        #     判断时间
        if amStart < nowTimeInt < amEnd or pmStart < nowTimeInt < pmEnd:
            if nowStatus == 'off':
                reset(sharelist)
                nowStatus = 'on'
                # dingtalk('股票开市啦!!!!!')
            main(sharelist)
        else:
            if nowStatus == 'on':
                nowStatus = 'off'
                # dingtalk('股票闭市啦!!!!!')
    else:
        print("weekend")
  # 间隔5s执行一次   time.sleep(5)

  

四、逐步更新优化