[Add] demo jupyter notebook for cli trading

This commit is contained in:
vn.py 2019-07-02 16:55:00 +08:00
parent 2906ac724a
commit 1ed35377e6
6 changed files with 1277 additions and 19 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,14 +5,14 @@ from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp
# from vnpy.gateway.binance import BinanceGateway
from vnpy.gateway.bitmex import BitmexGateway
# from vnpy.gateway.bitmex import BitmexGateway
# from vnpy.gateway.futu import FutuGateway
# from vnpy.gateway.ib import IbGateway
# from vnpy.gateway.ctp import CtpGateway
# from vnpy.gateway.ctptest import CtptestGateway
# from vnpy.gateway.femas import FemasGateway
# from vnpy.gateway.tiger import TigerGateway
# from vnpy.gateway.oes import OesGateway
from vnpy.gateway.oes import OesGateway
# from vnpy.gateway.okex import OkexGateway
# from vnpy.gateway.huobi import HuobiGateway
# from vnpy.gateway.bitfinex import BitfinexGateway
@ -48,9 +48,9 @@ def main():
# main_engine.add_gateway(FemasGateway)
# main_engine.add_gateway(IbGateway)
# main_engine.add_gateway(FutuGateway)
main_engine.add_gateway(BitmexGateway)
# main_engine.add_gateway(BitmexGateway)
# main_engine.add_gateway(TigerGateway)
# main_engine.add_gateway(OesGateway)
main_engine.add_gateway(OesGateway)
# main_engine.add_gateway(OkexGateway)
# main_engine.add_gateway(HuobiGateway)
# main_engine.add_gateway(BitfinexGateway)

View File

@ -1,6 +1,7 @@
from pathlib import Path
from vnpy.trader.app import BaseApp
from .engine import ScriptEngine, APP_NAME
from .cli import init_cli_trading
class ScriptTraderApp(BaseApp):

View File

@ -0,0 +1,28 @@
from typing import Sequence
from vnpy.event import EventEngine, Event
from vnpy.trader.engine import MainEngine
from vnpy.trader.gateway import BaseGateway
from vnpy.trader.event import EVENT_LOG
from .engine import ScriptEngine
def process_log_event(event: Event):
""""""
log = event.data
print(f"{log.time}\t{log.msg}")
def init_cli_trading(gateways: Sequence[BaseGateway]):
""""""
event_engine = EventEngine()
event_engine.register(EVENT_LOG, process_log_event)
main_engine = MainEngine(event_engine)
for gateway in gateways:
main_engine.add_gateway(gateway)
script_engine = main_engine.add_engine(ScriptEngine)
return script_engine

View File

@ -40,20 +40,6 @@ class ScriptEngine(BaseEngine):
""""""
super().__init__(main_engine, event_engine, APP_NAME)
self.get_tick = main_engine.get_tick
self.get_order = main_engine.get_order
self.get_trade = main_engine.get_trade
self.get_position = main_engine.get_position
self.get_account = main_engine.get_account
self.get_contract = main_engine.get_contract
self.get_all_ticks = main_engine.get_all_ticks
self.get_all_orders = main_engine.get_all_orders
self.get_all_trades = main_engine.get_all_trades
self.get_all_positions = main_engine.get_all_positions
self.get_all_accounts = main_engine.get_all_accounts
self.get_all_contracts = main_engine.get_all_contracts
self.get_all_active_orders = main_engine.get_all_active_orders
self.strategy_active = False
self.strategy_thread = None
@ -93,6 +79,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):
""""""
if not self.strategy_active:
@ -122,6 +112,7 @@ class ScriptEngine(BaseEngine):
req = OrderRequest(
symbol=contract.symbol,
exchange=contract.exchange,
direction=direction,
type=order_type,
volume=volume,
price=price,

View File

@ -296,7 +296,7 @@ class OesMdApi:
mds_req.subSecurityCnt = 1
entry.exchId = EXCHANGE_VT2MDS[req.exchange]
entry.securityType = eMdsMdProductTypeT.MDS_SECURITY_TYPE_STOCK # todo: option and others
entry.mdProductType = eMdsMdProductTypeT.MDS_SECURITY_TYPE_STOCK # todo: option and others
entry.instrId = int(req.symbol)
self._message_loop.register_symbol(req.symbol, req.exchange)