【上班摸鱼】企业微信、钉钉、飞书自动提醒基金预估加减仓

企业微信、钉钉、飞书自动提醒股票加减仓

序言

大家是否有这样的苦恼,上班期间,下午三点时候前因工作原因影响,不能及时查看基金导致损失几百万,近一步增加深圳买房距离。
图1
有没有一种工具能在下午三点前定时每隔多久(时间自己设置)通过机器人我们,且能显示当前时间、最近三天、五天、一周、两周的涨跌,以及连续涨跌多少,重要的是工具免费,方便使用。作为一名屌丝,经过损失几辆“宝马”的痛苦经历,根据自己掌握的知识,终于通过python爬虫以及CI的方式实现,加班不容易,想要原码微信联系。

效果展示
图2

功能解析

实现如上图的功能拆分为如下几个步骤:

  • python接口爬虫基金
    1、根据当前基金获取基金名称
    2、获取基金当前时间点的收益率
    3、获取基金最近每天的收益率
    4、计算基金最近三天、一周、半月收益率
    5、计算最近一周的涨跌情况

  • ci定时触发
    1、通过使用免费的ci工具,当前使用CODING平台(该平台每月提供1000分钟构建,白嫖就是香
    链接(https://coding.net/)
    图1
    2、设置构建时间(工作日11点-12点,每半个小时触发一次。下午2点-4点,每10分钟触发一次。金鱼都知道加/减仓)
    图1

  • 企业微信markdown格式
    1、展示基金格式,具体如下:

# 实时基金
## 当前时间:<font color="blue">**2021-09-20 15:50:20**</font>
>**1、** 基金号:<font color="black"> **011329** </font> 基金名称:<font color="black"> **景顺长城新能源产业股票C**</font> 当日长跌:<font color="red"> **0.60%**</font> 三天长跌:<font color="green"> **-3.05%**</font> 一周长跌:<font color="green"> **-1.06%**</font> 半月长跌:<font color="green"> **-0.87%**</font> 近一周长跌天数:<font color="red"> **3**</font>  <font color="green"> **2**</font>

功能实现

基金接口分析

当前是通过天天基金免费查询,链接地址如下:
https://fund.eastmoney.com/

python实现

  • 获取近3天,近5天,近7天,近15天,近1个月涨跌幅
    def get_fund_range_form_code(self, fundCode,dayRange=50):
        #预计获取近3天,近5天,近7天,近15天,近1个月涨跌幅
        fundinformation = dict()
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36",
            'Referer': f'http://fundf10.eastmoney.com/jjjz_{0}.html'.format(fundCode)
        }
        pageIndex = 1
        pageSize = 40
        startDate = (datetime.datetime.now()-datetime.timedelta(days=dayRange)).strftime("%Y-%m-%d")
        endDate = time.strftime("%Y-%m-%d", time.localtime())
        url = f'http://api.fund.eastmoney.com/f10/lsjz?fundCode={fundCode}&pageIndex={pageIndex}&pageSize={pageSize}&startDate={startDate}&endDate={endDate}'
        rs = self.get(url, headers=headers)
        results = json.loads(rs.text)['Data']['LSJZList']

        if len(results)>10:
            print(len(results))
            pass
        fundinformation['risedays'] = 0
        fundinformation['dropdays'] = 0
        for i in range(0,5,1):
            if float(results[i]['JZZZL'])>0:
                fundinformation['risedays']+=1
            if float(results[i]['JZZZL'])<0:
                fundinformation['dropdays']+=1
        fundinformation['threeday'] = results[2]['DWJZ']
        fundinformation['weekday'] = results[4]['DWJZ']
        fundinformation['twoweekday'] = results[9]['DWJZ']

        return fundinformation
  • 获取最新的基金涨幅、涨跌天数以及实时信息
    def get_fund_from_code(self,fundcode):
        '''
        从'http://fundgz.1234567.com.cn/js/%s.js'%funcode
        :return: 返回字典类型,{title:标题,girlurl:图片地址
        '''
        fundinformation = {} #返回的数据
        url = "http://fundgz.1234567.com.cn/js/%s.js?rt=1627999965000"%fundcode
        rs = self.get(url,headers=self.headers)
        # 返回信息
        content = rs.text
        print(content)
        # content = """jsonpgz({"fundcode":"501019","name":"国泰国证航天军工指数","jzrq":"2020-08-13","dwjz":"1.2327","gsz":"1.2690","gszzl":"2.95","gztime":"2020-08-14 15:00"});"""

        #正则匹配
        pattern = r'^jsonpgz((.*))'
        # 查找结果
        search = re.findall(pattern, content)
        # 遍历结果
        for i in search:
            data = json.loads(i)
            # print(data,type(data))
            fundinformation["gztime"] = data['gztime']
            fundinformation["fundcode"] = data['fundcode']
            fundinformation["name"] = data['name']
            fundinformation["cur_price"] = data['gsz']
            fundinformation["gszzl"] = data['gszzl']
            print("当前时间:{},基金号:{},基金:{},实时长幅: {}".format(data['gztime'],data['fundcode'],data['name'], data['gszzl']))
        fundrangeprice = self.get_fund_range_form_code(fundcode)
        fundinformation["twoday_gszzl"] = '%.2f' % ((float(fundinformation["cur_price"]) - float(fundrangeprice['threeday']))/float(fundrangeprice['threeday']))
        fundinformation["weekday_gszzl"] = '%.2f' % (
            (float(fundinformation["cur_price"]) - float(fundrangeprice['weekday'])) / float(
                fundrangeprice['weekday']))
        fundinformation["twoweekday_gszzl"] = '%.2f' % (
            (float(fundinformation["cur_price"]) - float(fundrangeprice['twoweekday'])) / float(
                fundrangeprice['twoweekday']))
        fundinformation['risedays'] = fundrangeprice['risedays']
        fundinformation['dropdays'] = fundrangeprice['dropdays']
        print(fundinformation)
        return fundinformation
  • 批量获取基金
    def get_funds_from_code(self,fundcodes):
        funcodeslist = fundcodes.split(',')
        fundinfors = []
        for fundcode in funcodeslist:
            fundinfors.append(self.get_fund_from_code(fundcode))
        return fundinfors
  • 基金转换为markdown格式方式企业微信以及叮叮、飞书发送
    def deal_message_to_markdown(self,fundinfors=None):
        '''
        处理各种途径消息
        :param messages:
        :return:
        '''
        message = '# 实时基金

## 当前时间:<font color="blue">**%s**</font>
' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        i = 0
        for fundinfor in fundinfors:
            i+=1
            message += f'>**{i}、** 基金号:<font color="black"> **%s** </font> 基金名称:<font color="black"> **%s**</font>' %(fundinfor['fundcode'], fundinfor['name'])
            if float(fundinfor['gszzl']) > 0:
                message += ' 当日长跌:<font color="red"> **%s**</font>' % (fundinfor['gszzl'])
            else:
                message += ' 当日长跌:<font color="green"> **%s**</font>' % (fundinfor['gszzl'])
            #最近3天、一周、半月长跌
            if float(fundinfor['twoday_gszzl']) > 0:
                message += ' 三天长跌:<font color="red"> **%s**</font>' % (fundinfor['twoday_gszzl'])
            else:
                message += ' 三天长跌:<font color="green"> **%s**</font>' % (fundinfor['twoday_gszzl'])

            if float(fundinfor['weekday_gszzl']) > 0:
                message += ' 一周长跌:<font color="red"> **%s**</font>' % (fundinfor['weekday_gszzl'])
            else:
                message += ' 一周长跌:<font color="green"> **%s**</font>' % (fundinfor['weekday_gszzl'])

            if float(fundinfor['twoweekday_gszzl']) > 0:
                message += ' 半月长跌:<font color="red"> **%s**</font>' % (fundinfor['twoweekday_gszzl'])
            else:
                message += ' 半月长跌:<font color="green"> **%s**</font>' % (fundinfor['twoweekday_gszzl'])

             #近一周长跌天数
            message += ' 近一周长跌天数:<font color="red"> **%s**</font>  <font color="green"> **%s**</font>
' % (fundinfor['risedays'],fundinfor['dropdays'])
        return message
  • 企业微信机器人
def send_wechart(self,wechart_url,msg=None):
        '''
        企业微信发送图片资源消息
        :param wechart_url: 企业微信机器人地址
        :param msg: 发送资源消息
        :return:
        '''
        header = {
            'Content-Type': 'application/json'
        }
        data = {
            "msgtype": "markdown",
            "markdown": {
                "content": msg
            }
        }
        respone = requests.post(url=wechart_url, headers=header, json=data)
        return respone

coding配置

图2 图2

效果

图2

总结

当前基金功能不太完善,基本够用,后续可以自动计算涨跌来投入以及止损的方式来进行建议

测试
原文地址:https://www.cnblogs.com/package/p/15315932.html