基于python的股票检测邮件通知代码
时间: 2023-07-29 09:13:35 浏览: 43
以下是一个基于Python的股票检测邮件通知的代码示例,仅供参考:
```python
import tushare as ts
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 邮箱配置信息
sender = 'your_email@example.com'
password = 'your_password'
smtp_server = 'smtp.example.com'
smtp_port = 25
receiver = 'receiver@example.com'
# 股票代码
stock_code = '600000'
# 获取股票数据
df = ts.get_k_data(stock_code)
# 计算均线和RSI指标
df['ma5'] = df['close'].rolling(window=5).mean()
df['ma10'] = df['close'].rolling(window=10).mean()
df['rsi'] = ta.RSI(df['close'].values)
# 定义股票检测函数
def detect_stock(df):
if df['close'] > df['ma5'] and df['close'] > df['ma10'] and df['rsi'] > 70:
return True
else:
return False
# 检测股票是否符合条件
if detect_stock(df):
# 发送邮件通知
content = f"股票代码:{stock_code},当前价格:{df.iloc[-1]['close']}"
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receiver, 'utf-8')
message['Subject'] = Header('股票检测通知', 'utf-8')
try:
smtpObj = smtplib.SMTP(smtp_server, smtp_port)
smtpObj.login(sender, password)
smtpObj.sendmail(sender, [receiver], message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败")
```
这段代码使用了tushare库获取股票数据,并使用pandas和ta-lib库计算了均线和RSI指标。在detect_stock函数中定义了符合条件的判断逻辑。当检测到符合条件的股票时,会发送一封邮件通知。需要注意的是,你需要替换代码中的邮箱配置信息和股票代码。