繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

邮件发送(支持多人接收,支持回测、模拟、研究)

作者/asjnajdja 2019-05-10 05:05 0 来源: FX168财经网人物频道

写了一个邮件发送的功能,没想到很多人都想用,这次重新整理封装了一下代码,希望大家在使用的时候更加方便。感谢各位的支持[]~( ̄▽ ̄)~*

下面是邮件的使用效果截图:

效果图

导入邮件使用的模块¶

import smtplib
from email.mime.text import MIMEText
from email.header import Header

封装邮件发送函数¶

def send_email(subject,message,sender,username,password,receiver_list):
    """
    邮件发送
    subject:标题
    message:内容
    sender:发送邮箱
    username:邮箱名
    password:邮箱密码
    receiver_list:收件箱列表
    记得请先开启邮箱的SMTP服务
    """

    sender = subject #发送的邮箱
    receiver = receiver_list  #要接受的邮箱(注:测试中发送其他邮箱会提示错误)
    smtpserver = 'smtp.163.com' 
    username = username #你的邮箱账号
    password = password #你的邮箱密码

    msg = MIMEText(message,'html','utf-8') #中文需参数‘utf-8',单字节字符不需要
    msg['Subject'] = Header(subject, 'utf-8') #邮件主题
    msg['from'] = sender    #自己的邮件地址 

    smtp = smtplib.SMTP()
    try :
        smtp.connect('smtp.163.com') # 链接
        smtp.login(username, password) # 登陆
        smtp.sendmail(sender, receiver, msg.as_string()) #发送
        log.info('邮件发送成功')
    except smtplib.SMTPException:
        log.info('邮件发送失败') 
    smtp.quit() # 结束

封装打包信息的函数¶

def build_context(context, hold_count, buy_list, sell_list, url='', total_returns=0, last_returns=0, strategy_name='策略', title_color='LightBLue'):
    """
    打包主体信息,html格式
    context:不解释
    strategy_name:策略名称
    hold_count:最大持仓数量
    buy_list:当日要买进的股票列表
    sell_list:当日要卖出的股票列表
    url:如果策略发布,可以填写上它的地址
    total_returns:总收益
    last_returns:上一个交易日的收益
    title_color:表头的颜色
    """
    
    count = 0
    current_data = get_current_data()
    
    '''计算当天买卖仓位'''
    hold_count = len(context.portfolio.positions) - len(sell_list)
    buy_how_cout = hold_count - hold_count
    if len(buy_list) > buy_how_cout:
        buy_how_cout = len(buy_list) - hold_count
    
    '''打包hmlt表格'''
    color_list = ['White','WhiteSmoke', 'Crimson', 'Green']
    mail_msg = ''
    mail_msg += '<p>策略名称:"'+strategy_name+'"</p>'
    mail_msg += '<p>交易时间:'+context.current_dt.strftime('%Y-%m-%d')+'</p>'
    mail_msg += '<table cellpadding="10">'
    mail_msg += '<tr bgcolor="'+title_color+'" ><th>交易时间</th><th>股票</th><th>操作</th><th>仓位</th><th>参考价格</th></tr>'
    for stock in sell_list:
        mail_msg += '<tr bgcolor="'+str(color_list[count%2])+'"><td>9:30</td><td>'+current_data[stock].name+stock+'</td><td><font color="Green">卖</font></td><td>全</td><td>¥'+str(current_data[stock].last_price)+'</td></tr>'
        count += 1
    for stock in buy_list:
        mail_msg += '<tr bgcolor="'+str(color_list[count%2])+'"><td>9:30</td><td>'+current_data[stock].name+stock+'</td><td><font color="Crimson">买</font></td><td>1/'+str(buy_how_cout)+'</td><td>¥'+str(current_data[stock].last_price)+'</td</tr>'
        count += 1
    mail_msg += '<tr><th colspan="5">累积收益:<font color="'+[color_list[2] if total_returns>=0 else color_list[3]][0] +'">'+str(round(total_returns,2))+'%</font> 昨日收益:<font color="'+[color_list[2] if last_returns>=0 else color_list[3]][0] +'">'+str(round(day_returns, 2))+'%</font><br/><a href="'+url+'">点击访问策略</a></th></tr></table>'
    
    return mail_msg

调用封装的函数¶

这里的参数是按我的方式计算出来的,各位在使用的时候,可以按自已的实际需求进行调整!!︿( ̄︶ ̄)︿

'''每天计算总收益和上一日收益'''
total_returns = context.portfolio.returns * 100  # 计算收益
day_returns = total_returns - g.last_returns
g.last_returns = total_returns
'''接收者邮箱列表'''
mail_list = ['mail1@163.com', 'mail2@163.com', 'mail3@126.com']
'''策略地址'''
url = ''

'''打包信息'''
message = build_context(context, 
                        hold_count=5, 
                        buy_list=g.buy, 
                        sell_list=g.sell, 
                        url=url, 
                        total_returns=total_returns, 
                        last_returns=day_returns, 
                        strategy_name='遛狗策略', 
                        title_color='red')    
'''发送邮件'''
send_email(subject='遛狗策略交易提醒',
           message=message, 
           sender='发送者邮箱',
           username='发送者邮件名',
           password='邮箱密码',
           receiver_list=mail_list)

效果如下:¶

 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2144文章4145粉丝45

    暂无个人简介信息

  • 梁孟梵

    人气2152文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 指导老师

    人气1856文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2296文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 刘钥钥1

    人气2016文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2320文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4728文章8329粉丝118

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...