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

关注股市,发家致富

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

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

一、配置centos的python版本

  1. centos7自带了python2,但是安装模块的时候各种报错,基本上都是版本的原因,pip install 默认都下载了最新版本的模块包,但是最新版本的模块包都不支持python2,需要python3,不闲累的话,可以指定版本号进行模块的安装。
  2. 两个等号用于指定版本 pip install pandas==0.19.0 -i http://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脚本

   

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

# encoding: utf-8

import requests

import tushare, time

import datetime

# 消息内容,url地址

from pandas._libs import json

# 机器人回调地址

webhook = 'https://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.415)

# 600100同方股份

share2 = Share("600100"5.926.03)

# 000810创维数字

share3 = Share("000810"7.667.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")<br data-filtered="filtered">  # 间隔5s执行一次  

    time.sleep(5)

  

四、逐步更python教程新优化

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值