python实现1分钟内股价波动邮件提示

  • 一、通过tushare接口实时监控股票价格
    • 1. 通过get_realtime_quotes获得股票的开盘价与实时价格
    • 2. 计算1min时间内的股价波动
    • 3. 编写自动定时函数实现固定1min间隔循环
  • 二、邮件提示
    • 1. 设置邮箱信息
    • 2. 通过SMTP工具发送邮件
  • 三、设置脚本运行时间范围
    • 1. 根据交易时间设置脚本开始和结束时间
    • 2. 以上证指数为例实现自动化交易时间内股票盯盘与提醒
  • 四、效果如下图

一、通过tushare接口实时监控股票价格

1. 通过get_realtime_quotes获得股票的开盘价与实时价格

def get_open_price(code): #获得开盘价df = ts.get_realtime_quotes(code)open_price=df['open'][0]return float(open_price)
def get_price(code): #获得股票实时价格df = ts.get_realtime_quotes(code)price=df['price'][0]return float(price)

2. 计算1min时间内的股价波动

def cal_fluctuation(price_1min_ago,cur_price): #计算股票每分钟浮动比例fluctuation=float((cur_price-price_1min_ago)/price_1min_ago)return fluctuation

3. 编写自动定时函数实现固定1min间隔循环

def runTask(code,name,day=0, hour=0, min=0, second=0):# Init timenow = datetime.now()#获取当前时间strnow = now.strftime('%Y-%m-%d %H:%M:%S')#print("now:", strnow)# First next run timeperiod = timedelta(days=day, hours=hour, minutes=min, seconds=second)#获取时间间隔next_time = now + periodstrnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S')#print("next run:", strnext_time)price_1min_ago=get_price(code)price_2min_ago = get_price(code)open_price=get_open_price(code)if open_price==0:open_price=price_2min_agoindex_up = 0index_down = 0while True:# Get system current timeiter_now = datetime.now()iter_now_time = iter_now.strftime('%Y-%m-%d %H:%M:%S')if str(iter_now_time) == str(strnext_time):# Get every start work time#print("start work: %s" % iter_now_time)# Call task funccur_price=get_price(code)print(cur_price)cur_fluctuation=cal_fluctuation(price_1min_ago,cur_price)cur_fluctuation_2min = cal_fluctuation(price_2min_ago, cur_price)#计算前一分钟内的股价变化值accumulated_fluctuation=cal_fluctuation(open_price,cur_price)#计算当前股价与开盘价的累计波动值if cur_fluctuation>=0.002:print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')print("Now: %s" % iter_now_time)content = '%s 当前1min内股价浮动为:+%.2f%% ,推荐减仓,%s' % (name, cur_fluctuation*100,iter_now_time)#自定义提醒内容sendEmail(content)#发送提醒邮件print(content)elif cur_fluctuation<=-0.002:print('????????????????????????????????????????')print("Now: %s" % iter_now_time)content='%s 当前1min内股价浮动为:%.2f%% ,推荐加仓,%s'%(name,cur_fluctuation*100,iter_now_time)sendEmail(content)print(content)if cur_fluctuation_2min>=0.002:print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')print("Now: %s" % iter_now_time)content = '%s 当前2min内股价浮动为:%.2f%% ,当前股价:%.2f,%s' % (name, cur_fluctuation_2min*100,cur_price,iter_now_time)sendEmail(content)print(content)elif cur_fluctuation_2min<=-0.002:print('????????????????????????????????????????')print("Now: %s" % iter_now_time)content='%s 当前2min内股价浮动为:%.2f%% ,当前股价:%.2f,%s'%(name,cur_fluctuation_2min*100,cur_price,iter_now_time)sendEmail(content)print(content)if (accumulated_fluctuation>=0.008 and index_up==0) or (accumulated_fluctuation>=0.012 and index_up==1):print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')print("Now: %s" % iter_now_time)content = '%s 已经上涨:+%.2f%% ,请关注实时行情' % (name, accumulated_fluctuation*100)print(content)sendEmail(content)index_up+=1elif (accumulated_fluctuation<=-0.008 and index_down==0) or (accumulated_fluctuation<=-0.012 and index_up==1):print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')print("Now: %s" % iter_now_time)content='%s 已经下跌:%.2f%% ,请关注实时行情'%(name,accumulated_fluctuation*100)print(content)sendEmail(content)index_down+=1price_2min_ago = price_1min_agoprice_1min_ago = cur_price#print("task done.")# Get next iteration timeiter_time = iter_now + periodstrnext_time = iter_time.strftime('%Y-%m-%d %H:%M:%S')#print("next_iter: %s" % strnext_time)# Continue next iterationcontinue

二、邮件提示

1. 设置邮箱信息

# 第三方 SMTP 服务
mail_host = "smtp.126.com"  # SMTP服务器
mail_user = "modas_***@126.com"  # 用户名
mail_pass = "******"  # 授权密码,非登录密码sender = 'modas_***@126.com'# 发件人邮箱(最好写全, 不然会失败)
receivers = ['131****1130@163.com','492498180@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

2. 通过SMTP工具发送邮件

建议使用网易邮箱大师,设置起来简单

def sendEmail(title):content='Stock market realtime notice:\nWish you making fucking much money!\n\n\nProduced by Modas Li'message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码message['From'] = "{}".format(sender)message['To'] = ",".join(receivers)message['Subject'] = titletry:smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465smtpObj.login(mail_user, mail_pass)  # 登录验证smtpObj.sendmail(sender, receivers, message.as_string())  # 发送print("notice mail has been send successfully.")except smtplib.SMTPException as e:print(e)

三、设置脚本运行时间范围

1. 根据交易时间设置脚本开始和结束时间

start_dt=datetime(datetime.today().year,datetime.today().month,datetime.today().day,hour=9,minute=28,second=0)
stop_dt=datetime(datetime.today().year,datetime.today().month,datetime.today().day,hour=15,minute=30,second=0)now=datetime.now()#.strftime( '%Y-%m-%d %H:%M:%S')

2. 以上证指数为例实现自动化交易时间内股票盯盘与提醒

import tushare as ts
from datetime import *
import smtplib
from email.mime.text import MIMETextif __name__ == '__main__':stock=['sh','上证指数']# open_price=get_open_price(code)while True:if now>start_dt and now<stop_dt:print('Start')print(datetime.now())runTask(stock[0],stock[1],min=1)else:print('当前时间未在股市交易时间内,请耐心等待~')

四、效果如下图

python实现1分钟内股价波动邮件提示相关推荐

  1. python记录日志_5分钟内解释日志记录—使用Python演练

    python记录日志 Making your code production-ready is not an easy task. There are so many things to consid ...

  2. python hook_五分钟内用Python实现GitHook

    githooks.png Githook 也称 Git 钩子,是在 Git 仓库中特定事件发生时自动运行的脚本.它可以让你自定义 Git 内部的行为,在开发周期中的关键点出发自定义行为. Git Ho ...

  3. 用python做预测模型的好处_如何用Python在10分钟内建立一个预测模型

    匿名用户 1级 2017-01-01 回答 预测模型的分解过程 我总是集中于投入有质量的时间在建模的初始阶段,比如,假设生成.头脑风暴.讨论或理解可能的结果范围.所有这些活动都有助于我解决问题,并最终 ...

  4. 在五分钟内学习使用Python进行类型转换

    by PALAKOLLU SRI MANIKANTA 通过PALAKOLLU SRI MANIKANTA 在五分钟内学习使用Python进行类型转换 (Learn typecasting in Pyt ...

  5. 10分钟内用Ezo和Python构建以太坊Oracle

    上一篇,我写了用Web3.js构建以太坊Oracle.这个练习给了我一些新的Web3.js 1.0版本知识.许多新的好东西可供选择而且使用它实现一个简单的oracle非常容易.但是,显然必须有更好的方 ...

  6. python判断邮件发送成功_【基本解决】python中用SMTP发送QQ邮件提示成功但是收件人收不到邮件...

    折腾: 期间, 已经用了smtp的ssl去发送邮件了,但是结果: 第二收件人也没有收到邮件... 那去把端口号从465改为587: smtpPort=587, 结果直接出错: smtpObj = sm ...

  7. Python smtp发邮件提示错误554, b'DT:SPM 163 smtp1

    使用163邮箱的SMTP服务,发送到QQ邮箱时出现错误: 注意,你是用的password应该是授权码不是你的邮箱登陆密码. 授权码可以在你开通SMTP服务的时候得到. 554错误汇总: •554 DT ...

  8. python程序-30分钟学会用Python编写简单程序

    原标题:30分钟学会用Python编写简单程序 参与文末每日话题讨论,赠送异步新书 异步图书君 学习目标 知道有序的软件开发过程的步骤. 了解遵循输入.处理.输出(IPO)模式的程序,并能够以简单的方 ...

  9. 【手把手】如何在10分钟内搭建一个以太坊私有链?

    在开发以太坊时,很多时候需要搭建一条以太坊私有链,这篇来自作者熊丽兵的文章,手把手教你10分钟内如何在Mac上进行搭建. 作者 | 熊丽兵 整理 | 科科 阅读本文前,你应该对以太坊语言有所了解,如果 ...

  10. 10分钟内基于gpu的目标检测

    10分钟内基于gpu的目标检测 Object Detection on GPUs in 10 Minutes 目标检测仍然是自动驾驶和智能视频分析等应用的主要驱动力.目标检测应用程序需要使用大量数据集 ...

最新文章

  1. Kafka JAVA客户端代码示例--高级应用
  2. 性能优化实战|使用eBPF代替iptables优化服务网格数据面性能
  3. 这 5 条 IntelliJ IDEA 调试技巧太强了!
  4. caffe的调试技巧 和 使用split层
  5. netlink 0005 -- Generic Netlink详解
  6. C++程序设计必知:多文件结构和编译预处理命令
  7. sql根据经纬度计算距离
  8. (Python学习) 10位老师随机分配到4个教室,保证每个教室至少有2个老师
  9. 九度1035 -树 - 找出直系亲属
  10. 在线文档可以直接打印吗?哪里可以打印在线文档
  11. 计算机取消uefi启动项,如何使用老毛桃winpe删除或添加UEFI BIOS启动项?
  12. linux fat32 乱码,FAT32文件系统乱码的研究和分析
  13. Editplus下载安装
  14. C++中使用代码修改字体颜色
  15. 【概率】COGS1487 麻球繁衍
  16. mysql char30_Mysql中varchar与char的区别以及varchar(30)中的30代表的涵义
  17. Swagger注释@API详细说明
  18. 目标检测------损失函数=类别损失+位置损失
  19. 鸿蒙宴文言知识积累,文言文鸿门宴知识点汇总
  20. java flowlayout 大小_java – 调整FlowLayout面板的大小

热门文章

  1. storyBoard配置错误导致崩溃 superview]: unrecognized selector...
  2. 如何让百度收录你的网站
  3. ubuntu 安装咖啡壶-chemex命令详解
  4. 中国股票市场化整为零,然后聚沙成塔
  5. 机器学习FP、TP、FN、TN、sensitivity、specificity及代码实现
  6. 学医后才知道的小知识...
  7. 在linux上临时挂载NTFS格式的优盘
  8. freemarker模板动态生成word文档之配置模板路径
  9. GPS从入门到放弃(十七) --- 对流层延时
  10. tf.flags用法