Python 办公室股票盯盘小工具(源码含价格提示)

技术:python3、tushare、tkinter

tushare安装:pip install tushare
tkinter安装:pip install tkinter

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import tushare as ts
import time
import tkinter
import tkinter.messagebox
import datetime

# 保存代码信息
class tusharemodel():
    '''
        code: 代码
        low: 跌破价格
        height: 涨穿价格
    '''
    def __init__(self, code, low, height):
        self.code = code
        self.low = low
        self.height = height
        # 设置当前时间
        self.time = datetime.datetime.now()

# 函数
def load():
    # 初始化数据
    codelist = []

    # 先进数通
    codelist.append(tusharemodel('300541', 26.9, 27.8))
    # 飞天诚信
    codelist.append(tusharemodel('300386', 15, 0))
    # 天华超净
    codelist.append(tusharemodel('300390', 0, 0))
    # 日上集团
    codelist.append(tusharemodel('002593', 3.7, 0))
    # 卧龙电驱
    codelist.append(tusharemodel('600580', 10.5, 0))
    # 恒信东方
    codelist.append(tusharemodel('300081', 10.8, 0))
    # 苏垦农发
    codelist.append(tusharemodel('601952', 7.05, 0))
    # 易尚展示
    codelist.append(tusharemodel('002751', 0, 0))
    # 国脉科技
    codelist.append(tusharemodel('002093', 7.8, 0))
    # 汉鼎宇佑
    codelist.append(tusharemodel('300300', 10.6, 0))
    # 高伟达
    codelist.append(tusharemodel('300465', 0, 0))

    # 过滤器,得到code(代码)的数组
    codes = [outcode.code for outcode in codelist]

    while True:
        # 获取实时分笔数据  文档:http://tushare.org/trading.html#id6
        df = ts.get_realtime_quotes(codes)

        # 声明索引
        i = 0

        # 打印标头
        line_new = '{:<12}  {:<12}  {:<12}  {:<14}  {:<14}  {:<14}  {:<14}'.format('代码', '名称', '价格', '买一', '卖一', '成交量', '时间')
        while i < len(codes):
            # 获取实时价格
            price = float(df['price'].values[i])

            # 筛选器,得到某个集合(单个对象的集合)
            outdata = [outcode for outcode in codelist if outcode.code == codes[i]]

            # 是否含有对象
            if len(outdata) > 0:
                # 将集合 改变 为对象
                outdata = outdata[0]

                # 价格穿破N时提示,主要用于封板时使用
                if price <= outdata.low and outdata.low > 0 and datetime.datetime.now() >= outdata.time:
                    # 设置N分钟后再提示
                    outdata.time = datetime.datetime.now() + datetime.timedelta(minutes=5)

                    # 提示穿破
                    tkinter.messagebox.showwarning('穿破提示', df['name'].values[i] + ' 快跑!\n穿破' + str(outdata.low))

                    # print('下次提示时间:' + outdata.time.strftime("%Y-%m-%d %H:%M:%S"))

                # 价格破N时提示,主要用于上板时使用
                elif price >= outdata.height and outdata.height > 0 and datetime.datetime.now() >= outdata.time:
                    # 设置N分钟后再提示
                    outdata.time = datetime.datetime.now() + datetime.timedelta(minutes=5)

                    # 提示上穿
                    tkinter.messagebox.showinfo('上穿提示', df['name'].values[i] + ' 入袋为空!\n' + str(outdata.height))

                    # print('下次提示时间:' + outdata.time.strftime("%Y-%m-%d %H:%M:%S"))

            # 打印内容
            line_new += '\n{:<12}  {:<12}  {:<12}  {:<14}  {:<14}  {:<14}  {:<14}'.format(
                df['code'].values[i],
                df['name'].values[i],
                df['price'].values[i],
                df['bid'].values[i] + '(' + df['b1_v'].values[i] + ')',
                df['ask'].values[i] + '(' + df['a1_v'].values[i] + ')',
                df['volume'].values[i],
                df['time'].values[i])

            # 集合索引
            i += 1

        # 添加换行
        line_new += '\n'

        # 打印输出数据
        print(line_new)

        # 线程停止,每隔 0.8 秒执行一次  ## 可自定义
        time.sleep(0.8)

if __name__ == '__main__':
    load()

你可能感兴趣的:(Python 办公室股票盯盘小工具(源码含价格提示))