python 从mysql数据库读取tushare下载的1个股票的每日k线并比较收盘价格是否高于设置的卖出价格,并且通过ljyhis@163.com邮箱通知ljyhis@126.com
时间: 2023-07-15 19:13:05 浏览: 142
以下是一个简单的Python脚本,可以从MySQL数据库中读取股票的每日K线数据,并比较收盘价格是否高于卖出价格。如果符合条件,则通过SMTP协议发送电子邮件通知。
```python
import pymysql
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# MySQL数据库连接配置
host = 'localhost'
port = 3306
user = 'username'
passwd = 'password'
db = 'database'
# 股票代码和卖出价格
symbol = '600000.SH'
sell_price = 12.0
# 连接MySQL数据库
conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db)
cursor = conn.cursor()
# 查询股票的每日K线数据
query = "SELECT * FROM kline WHERE symbol = %s ORDER BY date DESC LIMIT 1"
cursor.execute(query, (symbol,))
result = cursor.fetchone()
# 关闭数据库连接
cursor.close()
conn.close()
# 比较收盘价格和卖出价格
close_price = result[4]
if close_price >= sell_price:
# 发送电子邮件通知
mail_host = 'smtp.163.com'
mail_port = 465
mail_user = 'ljyhis@163.com'
mail_passwd = 'password'
sender = 'ljyhis@163.com'
receivers = ['ljyhis@126.com']
message = MIMEText('股票%s的收盘价格为%.2f,已高于卖出价格%.2f,请注意及时处理。' % (symbol, close_price, sell_price), 'plain', 'utf-8')
message['From'] = Header('股票提醒', 'utf-8')
message['To'] = Header('ljyhis@126.com', 'utf-8')
message['Subject'] = Header('股票%s提醒' % symbol, 'utf-8')
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)
smtpObj.login(mail_user, mail_passwd)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
```
需要注意的是,需要先安装并导入pymysql和smtplib模块。另外,需要将代码中的MySQL连接配置、股票代码和卖出价格、发件人和收件人邮箱等信息替换为自己的信息。
阅读全文