From afa7f1a227ede89d08d83694b810ddae9a78e5a7 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 3 Jul 2019 11:28:08 +0800 Subject: [PATCH] [Add] demo_script.py for ScriptTrader --- examples/vn_trader/demo_script.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/vn_trader/demo_script.py diff --git a/examples/vn_trader/demo_script.py b/examples/vn_trader/demo_script.py new file mode 100644 index 00000000..1078ee0b --- /dev/null +++ b/examples/vn_trader/demo_script.py @@ -0,0 +1,27 @@ +from time import sleep +from vnpy.app.script_trader import ScriptEngine + + +def run(engine: ScriptEngine): + """""" + vt_symbols = ["IF1912.CFFEX", "rb2001.SHFE"] + + # 订阅行情 + engine.subscribe(vt_symbols) + + # 获取合约信息 + for vt_symbol in vt_symbols: + contract = engine.get_contract(vt_symbol) + msg = f"合约信息,{contract}" + engine.write_log(msg) + + # 持续运行,使用strategy_active来判断是否要退出程序 + while engine.strategy_active: + # 轮询获取行情 + for vt_symbol in vt_symbols: + tick = engine.get_tick(vt_symbol) + msg = f"最新行情, {tick}" + engine.write_log(msg) + + # 等待3秒进入下一轮 + sleep(3)