钉钉机器人自动推送股票信息

项目时间和要求

马上五一了,估计很多小伙伴会休假!我们时间稍晚长一点 ,4/29-5/11.给大家两周的时间完成。

这虽然是一个小系统,但是玩好了,可以做各种好玩的东西。

为了照顾新手同学,我把这个项目进行分解:

-------新手----------

Step0:安装钉钉的环境,并且获取Token

Step1:用钉钉的api接口给自己的手机上发一条消息 这里用到的代码改10行左右,新手应该能完成

-------老手----------

Step2:利用Tushare接口,获取某一只股票的数据,比如招商银行的当日的实时的股票数据。

然后你设定一个止盈价格 和止损价格,

若高于则给钉钉发出卖出信号。如低于则发出买入信号!

这个信号可以是推送的一段消息,比如:

股票 xxx ,现价大于xxx,可以卖出

股票 xxx ,现价低于xxx,可以买入

Step3:把上面的止盈和止损 和股票的编码,写入一个配置文件中,比如config.ini ,

然后把上面的代码进行类的封装

class Stock

class Dingding

class Warning

然后在Warning中读取config,ini文件,这样整个提醒系统在跑的时候,我们只需要改变config.ini就可以动态的修改我们的提醒系统。

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

只完成了前两个要求

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan

from time import sleep
from dingtalkchatbot.chatbot import DingtalkChatbot
import tushare as ts

ts_code = "600036"  # 招商银行
webhook_prefix = "https://oapi.dingtalk.com/robot/send?access_token="
access_token = "这里输入自己钉钉机器人的access_token"
webhook = webhook_prefix + access_token


def send_msg(msg):
    ding_robot = DingtalkChatbot(webhook)
    ding_robot.send_text(msg, is_at_all=0)


def get_real_price(code):
    df = ts.get_realtime_quotes(code)
    name = df.name[0]
    price = df.price[0]
    price = round(float(price), 2)
    return name, price


def price_comparison(profit_ceiling_price, stop_price, realtime_price, name):
    """
    若高于则给钉钉发出卖出信号。如低于则发出买入信号!
    :param profit_ceiling_price: 止盈价格
    :param stop_price:           止损价格
    :param realtime_price:       实时价格
    :param name:                 股票名称
    :return: None
    """
    if realtime_price > profit_ceiling_price:
        msg = "股票:%s ,现价:%s,看好就收,快抛!!" % (name, realtime_price)
        # print(msg)
        send_msg(msg)
    elif realtime_price < stop_price:
        msg = "股票:%s ,现价:%s,买买买!" % (name, realtime_price)
        send_msg(msg)
        # print(msg)


def run2():
    msg = "因股市下班,价格不更新,为了达到价格有变,预先设置了几个价格"
    print(msg)
    # send_msg(msg)
    profit_ceiling_price = 34.0  # 止盈价格
    stop_price = 32.0            # 止损价格
    for i in [
            32.32,
            31.55,
            32.66,
            33.68,
            34.12,
            34.99,
            35.36,
            34.20,
            33.50,
            31.57,
            30.86]:
        name, price = get_real_price(ts_code)
        price = i
        price_comparison(profit_ceiling_price, stop_price, price, name)
        # msg = "股票:%s ,现价:%s" % (name, price)
        # send_msg(msg)
        # print(msg)
        for j in range(10):
            sleep(1)


def run():
    profit_ceiling_price = 33.0  # 止盈价格
    stop_price = 32.0            # 止损价格
    while True:
        name, price = get_real_price(ts_code)
        # msg = "股票:%s ,现价:%s" % (name, price)
        # send_msg(msg)
        # print(msg)
        price_comparison(profit_ceiling_price, stop_price, price, name)
        for j in range(10):
            sleep(1)


if __name__ == '__main__':
    run()  # 正常就用这个
    # run2()  # 股市下班就用这个

  

猜你喜欢

转载自www.cnblogs.com/hiuhungwan/p/10846717.html