From 5eded16f48b3149565f808f4ce0312ebb6633f5e Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 3 Jul 2019 13:19:17 +0800 Subject: [PATCH] [Add] send_email function to ScriptEngine --- examples/vn_trader/demo_script.py | 15 ++++++++++++++- vnpy/app/script_trader/engine.py | 27 +++++++++++++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/examples/vn_trader/demo_script.py b/examples/vn_trader/demo_script.py index 1078ee0b..db4ffe40 100644 --- a/examples/vn_trader/demo_script.py +++ b/examples/vn_trader/demo_script.py @@ -3,7 +3,20 @@ from vnpy.app.script_trader import ScriptEngine def run(engine: ScriptEngine): - """""" + """ + 脚本策略的主函数说明: + 1. 唯一入参是脚本引擎ScriptEngine对象,通用它来完成查询和请求操作 + 2. 该函数会通过一个独立的线程来启动运行,区别于其他策略模块的事件驱动 + 3. while循环的维护,请通过engine.strategy_active状态来判断,实现可控退出 + + 脚本策略的应用举例: + 1. 自定义篮子委托执行执行算法 + 2. 股指期货和一篮子股票之间的对冲策略 + 3. 国内外商品、数字货币跨交易所的套利 + 4. 自定义组合指数行情监控以及消息通知 + 5. 股票市场扫描选股类交易策略(龙一、龙二) + 6. 等等~~~ + """ vt_symbols = ["IF1912.CFFEX", "rb2001.SHFE"] # 订阅行情 diff --git a/vnpy/app/script_trader/engine.py b/vnpy/app/script_trader/engine.py index 33ec2aec..8eb754d5 100644 --- a/vnpy/app/script_trader/engine.py +++ b/vnpy/app/script_trader/engine.py @@ -3,7 +3,7 @@ import sys import importlib import traceback -from typing import Sequence, Callable, Any +from typing import Sequence, Any from pathlib import Path from datetime import datetime from threading import Thread @@ -54,7 +54,9 @@ class ScriptEngine(BaseEngine): self.write_log("RQData数据接口初始化成功") def start_strategy(self, script_path: str): - """""" + """ + Start running strategy function in strategy_thread. + """ if self.strategy_active: return self.strategy_active = True @@ -66,7 +68,9 @@ class ScriptEngine(BaseEngine): self.write_log("策略交易脚本启动") def run_strategy(self, script_path: str): - """""" + """ + Load strategy script and call the run function. + """ path = Path(script_path) sys.path.append(str(path.parent)) @@ -81,12 +85,10 @@ class ScriptEngine(BaseEngine): msg = f"触发异常已停止\n{traceback.format_exc()}" self.write_log(msg) - def connect_gateway(self, setting: dict, gateway_name: str): - """""" - self.main_engine.connect(setting, gateway_name) - def stop_strategy(self): - """""" + """ + Stop the running strategy. + """ if not self.strategy_active: return self.strategy_active = False @@ -97,6 +99,10 @@ class ScriptEngine(BaseEngine): self.write_log("策略交易脚本停止") + def connect_gateway(self, setting: dict, gateway_name: str): + """""" + self.main_engine.connect(setting, gateway_name) + def send_order( self, vt_symbol: str, @@ -259,6 +265,11 @@ class ScriptEngine(BaseEngine): event = Event(EVENT_SCRIPT_LOG, log) self.event_engine.put(event) + def send_email(self, msg: str) -> None: + """""" + subject = "脚本策略引擎通知" + self.main_engine.send_email(subject, msg) + def to_df(data_list: Sequence): """"""