掘金社区

如何在产生新K线时执行邮件报警Pinned highlighted

peter 4 发表在策略研究 2021-05-14 15:16:42

策略研究
123
2
0

刚刚接触python程序化交易,真正的小小白。
准备写一个简单的邮件报警程序,辅助日常交易。
策略思路:
比较上一根K线收盘价和开盘价大小,如果收盘价大于开盘价,则发出做多邮件报警,反之则发出做空报警。

现在写好了,可以运行发出正确的邮件报警,但是遇到一个问题:
只能执行一次邮件报警,当出现新的K线时,不能持续触发条件执行动作。

我的想法:
能不能用K线的index值做标记,当index新增时,再次执行策略。

下面是代码情况,各位大神帮帮忙:
import pandas
from email.header import Header
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
import email.message

mark = 0

def init(ContextInfo):
#获取当前主图标的
ContextInfo.tradefuture = ContextInfo.stockcode+"."+ContextInfo.market
#设定标的池,即要操作的标的
ContextInfo.set_universe([ContextInfo.tradefuture])
print(ContextInfo.get_universe())

def handlebar(ContextInfo):
close = ContextInfo.get_history_data(1, '1d', 'close')
open = ContextInfo.get_history_data(1, '1d', 'open')
index = ContextInfo.barpos
print(close,open,index)
global mark
if 'close' > 'open' and mark != 1:
SendEmail1()
mark = 1

if 'close' <'open' and mark != 1:
   SendEmail2()
   mark = 1

#判断卖出条件满足则发出卖出邮件报警

def SendEmail1():
sender = 'email address'
receiver = 'email address'
subject = Header()
mail_host = 'smtp.qq.com'
mail_user = 'name'
mail_pass = 'password'
message = MIMEText('请注意期货交易提醒:日K线级别,阳线做多', 'plain', 'utf-8')
# 邮件主题
message['subject'] = '国内期货交易提醒'+'ContextInfo.stockcode'
# 发送方信息
message['From'] = sender
# 接受方信息
message['To'] = receiver[0]
smtpObj = smtplib.SMTP()
# 连接到服务器
smtpObj.connect(mail_host,25)
# 登录到服务器
smtpObj.login(sender, mail_pass)
# 发送
smtpObj.sendmail(sender, receiver, message.as_string())
# 退出
smtpObj.quit()
print('success')

def SendEmail2():
sender = 'email address'
receiver = 'email address'
subject = Header()
mail_host = 'smtp.qq.com'
mail_user = 'name'
mail_pass = 'password'message = MIMEText('请注意期货交易提醒:日K线级别,阴线做空', 'plain', 'utf-8')
# 邮件主题
message['subject'] = '国内期货交易提醒'+'ContextInfo.stockcode'
# 发送方信息
message['From'] = sender
# 接受方信息
message['To'] = receiver[0]
smtpObj = smtplib.SMTP()
# 连接到服务器
smtpObj.connect(mail_host,25)
# 登录到服务器
smtpObj.login(sender, mail_pass)
# 发送
smtpObj.sendmail(sender, receiver, message.as_string())
# 退出
smtpObj.quit()
print('success')

评论: 2

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.