python 从mysql数据库读取1个股票的每日k线并比较收盘价格是否高于设置的卖出价格,并且通过ljyhis@163.com邮箱通知ljyhis@126.com
时间: 2023-11-22 08:52:33 浏览: 183
以下是Python代码示例,用于从MySQL数据库中读取每日K线数据并比较收盘价格:
```python
import mysql.connector
import smtplib
from email.mime.text import MIMEText
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
mycursor = mydb.cursor()
# 选择要比较的股票代码和卖出价格
stock_code = "AAPL"
sell_price = 150.0
# 查询股票的每日K线数据
mycursor.execute("SELECT * FROM daily_kline WHERE stock_code = %s", (stock_code,))
# 比较每日K线的收盘价格
for kline in mycursor:
close_price = kline[4]
if close_price > sell_price:
# 发送邮件通知
sender_email = "ljyhis@163.com"
receiver_email = "ljyhis@126.com"
message = MIMEText(f"The stock {stock_code} has reached the sell price of {sell_price}")
message['Subject'] = f"{stock_code} sell price alert"
message['From'] = sender_email
message['To'] = receiver_email
with smtplib.SMTP('smtp.163.com', 25) as smtp:
smtp.login(sender_email, 'yourpassword')
smtp.sendmail(sender_email, receiver_email, message.as_string())
```
上述代码假设您已经设置了MySQL数据库的连接参数,并且有一个名为"daily_kline"的数据表,其中包含每日K线数据,其列包括"stock_code"、"open_price"、"high_price"、"low_price"和"close_price"。您需要将这些参数替换为您自己的值。同时,您还需要设置用于发送邮件的SMTP服务器、发件人电子邮件地址和SMTP登录密码。
阅读全文