VeighNa量化社区
你的开源社区量化交易平台
Member
加入于:
帖子: 158
声望: 71

一个简单的引用,分成两步,第一步增强邮件引擎,使得可以发送附件邮件,第二部在回测widget的里面加入一个新方法,在优化完成适合被调用。
下面代码是加入EmailEngine,新增一个add_attch, 增强send_email,不是很优雅,不过不会对已有用到send_email的不会又太大影响。

    def add_attch(self,msg,attch_dir,filename):
        """
        给邮件信息对象添加附件
        """
        file_path = os.path.join(attch_dir, filename)
        ctype, encoding = mimetypes.guess_type(file_path)
        if ctype is None:
            ctype = "application/octet-stream"
        maintype, subtype = ctype.split("/")
        with open(file_path, "rb") as r:
            msg.add_attachment(
                r.read(), maintype=maintype, subtype=subtype, filename=filename)
        return msg

    def send_email(self, subject: str, content: str, receiver: str = "",attch_dir = "",filename = "") -> None:
        """"""
        # Start email engine when sending first email.
        if not self.active:
            self.start()

        # Use default receiver if not specified.
        if not receiver:
            receiver = SETTINGS["email.receiver"]

        msg = EmailMessage()
        msg["From"] = SETTINGS["email.sender"]
        msg["To"] = receiver
        msg["Subject"] = subject
        msg.set_content(content)
        if attch_dir:
            msg = self.add_attch(msg,attch_dir,filename)

        self.queue.put(msg)

然后在BacktesterManager这个类里面新增一个方法,优化结果csv保存到一个预定folder,并发送

    def save_and_send_csv(self) -> None:
        """
        Save table data into a csv file
        """

        filepath  = r"C:\Desktop"
        filename = self.symbol_line.currentText() + self.start_date_edit.dateTime().toPyDateTime() + ".csv"

        path = os.path.join(filepath, filename)


        with open(path, "w", encoding='utf-8-sig') as f:
            writer = csv.writer(f, lineterminator="\n")

            writer.writerow(["参数", self.target_display])

            for tp in self.backtester_engine.get_result_values():
                setting, target_value, _ = tp
                row_data = [str(setting), str(target_value)]
                writer.writerow(row_data)
       self.main_engine.send_email_attchment(subject = filename , content = filename , attch_dir = filepath, filename = filename )
Member
avatar
加入于:
帖子: 1468
声望: 105

这个思路方便啊,啥时候跑完了通知一下

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】