def main(): global me ee = EventEngine() me = MainEngine(ee) ee.register(EVENT_TICK, on_tick_update) me.addGateway(ctpGateway) me.getGateway('CTP').filePath = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'CTP_connect.simnow.mock.json') me.connect('CTP') # 等待 1 秒是为了避免订阅时机 bug import time time.sleep(1) # vt_symbols = ['SR901C5300.CZCE', 'SR901C5200.CZCE'] vt_symbols = ['ru1908.SHFE', 'rb1908.SHFE'] symbols, exchanges = zip( *[vs.split('.') if '.' in vs else (vs, '') for vs in vt_symbols]) for s, e in zip(symbols, exchanges): req = VtSubscribeReq() req.symbol, req.exchange = s, e me.subscribe(req, 'CTP') print('Futures subscribed.')
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) recorder_engine = main_engine.add_app(DataRecorderApp) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_RECORDER_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") sleep(2) main_engine.write_log("数据下载开始") while True: sleep(1)
def start(self, block=True): self.setupFanoutAndLogHandler() # 创建日志引擎 super(MarketService, self).start() self.active = True self.thread.start() le = self.logger # le = LogEngine() # le.setLogLevel(le.LEVEL_INFO) # le.addConsoleHandler() le.info(u'启动行情记录运行子进程') self.ee = EventEngine() le.info(u'事件引擎创建成功') self.mainEngine = MainEngine(self.ee) # 忽略掉 默认的 DataEngine self.mainEngine.addGateway(xtpGateway) le.info(u'主引擎创建成功') le.info(u'注册日志事件监听') self.registerEvent() cfgs = self.cfgs.get('gateway', {}) self.mainEngine.connect(xtpGateway.gatewayName, cfgs) le.info(u'连接CTP接口')
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True # 控制是否要将日志输出到文件中,建议设置为True,否则无法记录生成的日志。 event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) main_engine.add_app(DataRecorderApp) main_engine.write_log("行情记录引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_RECORDER_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") sleep(15) main_engine.write_log("开始记录行情数据……") while True: sleep(1)
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) st_engine = main_engine.add_app(SpreadTradingApp) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_SPREAD_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") sleep(10) st_engine.start() main_engine.write_log("Arb引擎启动") st_engine.strategy_engine.init_all_strategies() # # sleep(60) # Leave enough time to complete strategy initialization main_engine.write_log("Arb策略全部初始化") # st_engine.strategy_engine.start_all_strategies() main_engine.write_log("Arb策略全部启动") print('ok')
def __init__(self): self.mainCTPList = [] self.agentCTPList = [] self.getAccounts() self.ee = EventEngine() self.ee.start() self.setupCTP(self.ee)
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) main_engine.write_log("主引擎创建成功") # 记录引擎 log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(CTP_SETTING, "CTP") main_engine.write_log("连接CTP接口") whole_market_recorder = WholeMarketRecorder(main_engine, event_engine) main_engine.write_log("开始录制数据") oms_engine = main_engine.get_engine("oms") while True: sleep(1)
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.write_log(" main engine created successfully ") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log(" register an event listener log ") main_engine.connect(ctp_setting, "CTP") main_engine.write_log(" connection CTP interface ") sleep(10) cta_engine.init_engine() main_engine.write_log("CTA strategy initialization is complete ") cta_engine.init_all_strategies() sleep(60) # Leave enough time to complete strategy initialization main_engine.write_log("CTA all initialization strategy ") cta_engine.start_all_strategies() main_engine.write_log("CTA policy start all ") while True: sleep(1)
def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动JAQS服务进程') ee = EventEngine() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(jaqsService) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_JS_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') while True: sleep(1)
def main(): event_engine = EventEngine() event_engine.register(EVENT_LOG,printLog) main_engine = MainEngine(event_engine) # geteway = XtpGateway(event_engine) # # default_setting: Dict[str, Any] = { # "账号": "53191000704", # "密码": "vj6JDKlq", # "客户号": 1, # "行情地址": "120.27.164.138", # "行情端口": 6002, # "交易地址": "120.27.164.69", # "交易端口": 6001, # "行情协议": "TCP", # "授权码": "b8aa7173bba3470e390d787219b2112e" # } # geteway.connect(default_setting) geteway = CtpGateway(event_engine) default_setting: Dict[str, Any] = { "用户名": "161239", "密码": "Asdf4321", "经纪商代码": "9999", "交易服务器": "218.202.237.33:10102", "行情服务器": "218.202.237.33:10112", "产品名称": "", "授权编码": "", "产品信息": "" } geteway.connect(default_setting)
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) huobi = main_engine.add_gateway(HuobiGateway) cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") filename = f"connect_{huobi.gateway_name.lower()}.json" setting = load_json(filename) main_engine.connect(setting, huobi.gateway_name) main_engine.write_log("连接HUOBI接口") sleep(10) cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies() sleep(10) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") cta_engine.start_all_strategies() main_engine.write_log("CTA策略全部启动") while True: sleep(1)
def start(self, block=True): self.setupFanoutAndLogHandler() # 创建日志引擎 super(MarketService, self).start() self.active = True self.thread.start() le = self.logger # le = LogEngine() # le.setLogLevel(le.LEVEL_INFO) # le.addConsoleHandler() le.info(u'启动行情记录运行子进程') self.ee = EventEngine() le.info(u'事件引擎创建成功') self.mainEngine = MainEngine(self.ee) # 忽略掉 默认的 DataEngine self.registerEvent() le.info(u'主引擎创建成功') for gw in self.cfgs.get('gateways'): gateway = None if gw.get('name').upper() == binanceGateway.gatewayName: gateway = binanceGateway if gateway: self.mainEngine.addGateway(gateway) self.mainEngine.connect(gateway.gatewayName, gw) le.info(u'注册日志事件监听') le.info(u'连接CTP接口')
def run_child(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.write_log("主引擎创建成功") main_engine.add_app(DataRecorderApp) log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") #sleep(10) cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies() #sleep(60) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") cta_engine.start_all_strategies() main_engine.write_log("CTA策略全部启动") while True: sleep(1)
class DemoApp(object): # ---------------------------------------------------------------------- def __init__(self): """Constructor""" self.eventEngine = EventEngine() self.eventEngine.start() self.tianqinGateway = TianQinGateway(self.eventEngine) def start(self): self.tianqinGateway.connect() self.tianqinGateway.subscribe_quote( ["cu1803", "SR801", "c1801", "IF1708"], self.on_quote_data) self.tianqinGateway.subscribe_chart("cu1803", 5, 1000, self.on_chart_data) self.tianqinGateway.subscribe_chart("au1712", 0, 1000, self.on_chart_data) def on_quote_data(self, ins_id): quote = self.tianqinGateway.get_quote(ins_id) print("quote_update", ins_id, quote) def on_chart_data(self, ins_id, dur_seconds): if dur_seconds == 0: tick_serial = self.tianqinGateway.get_tick_serial(ins_id) print("tick_serial_update", tick_serial) else: kline_serial = self.tianqinGateway.get_kline_serial( ins_id, dur_seconds) print("kline_serial_update", kline_serial)
def __init__(self, event_engine: EventEngine = None): """""" # 无论如何,保证总会有事件引擎对象 if event_engine: self.event_engine: EventEngine = event_engine else: self.event_engine = EventEngine() # 启动事件引擎,主引擎在创建的时候,事件引擎便启动了 self.event_engine.start() # 交易接口字典,无脑推断通过vntrader的UI界面里添加的交易接口都会存在这里 self.gateways: Dict[str, BaseGateway] = {} # 维护其他应用的引擎 # 注意app也会创建应用引擎,在创建的过程中也会保存到engines里 self.engines: Dict[str, BaseEngine] = {} # 存加载的应用实例, 在创建的过程中,应用类都会被实例化 self.apps: Dict[str, BaseApp] = {} # 保存接口支持的交易所 # 在add_gateway的时候自动添加交易接口支持的交易所 self.exchanges: List[Exchange] = [] os.chdir(TRADER_DIR) # Change working directory # 初始化服务引擎包括日志引擎、订单路由引擎、邮件引擎 self.init_engines() # Initialize function engines
def run_child(): SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) cta_engine = main_engine.add_app(CtaStrategyAppPro) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") # sleep(20) sleep(5) # 开发用 cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies() sleep(5) # 开发用 # sleep(60) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") cta_engine.start_all_strategies() main_engine.write_log("CTA策略全部启动") cta_engine.send_run_report_email("账号1监控报表") # 完成启动后,发送监控报表 close_time1 = time(15, 32) close_time2 = time(15, 35) close_time3 = time(15, 36) lock1 = False lock2 = False lock3 = False while True: current_time = datetime.now().time() # 实例交易数据保存;资金曲线&策略评估指标更新 if current_time == close_time1 and not lock1: cta_engine.save_all_trade_data() main_engine.write_log("实例交易数据保存成功") cta_engine.update_all_daily_results() main_engine.write_log("实例资金曲线&策略评估指标更新成功") lock1 = True # 发送实例评估报表 if current_time == close_time2 and not lock2: cta_engine.send_evaluate_report_email("账号1实例评估报表") lock2 = True # 发送实例评估报表 if current_time == close_time3 and not lock3: cta_engine.update_all_k_line() main_engine.write_log("CTA更新K线图完毕") lock3 = True sleep(1)
def __init__(self): self.mainCTPList = [] self.agentCTPList = [] self.getAccountsM() self.ee = EventEngine() self.ee.start() self.setupCTP(self.ee) #初始化订阅列表 self.subscribeSetup()
class Mform(): def __init__(self): self.mainCTPList = [] self.agentCTPList = [] self.getAccounts() self.ee = EventEngine() self.ee.start() self.setupCTP(self.ee) def getAccounts(self): filename = 'G:/main/accounts.ph' self.mainAccountList, self.agentAccountList = getAccounts(filename) print('账号是', self.mainAccountList) return self.mainAccountList, self.agentAccountList def setupCTP(self, eventEngine): for i in self.agentAccountList: print('i is ', i) self.agentCTPList.append(setupAgentCTP(i, eventEngine)) for i in self.mainAccountList: print('mainaccountlist_i is ', i) self.mainCTPList.append(setupMainCTP(i, eventEngine)) for i in self.mainAccountList: if len(i.childs) > 0: print('有孩子') print(i.childs) print(i.userID) mainCtp = getCTP(i, self.mainCTPList) for j in i.childs: agentCtp = getCTP(j, self.agentCTPList) mainCtp.childList.append(agentCtp) agentCtp.father = mainCtp def getCTPList(self): return self.mainCTPList, self.agentCTPList # 关联 def relevance(self): mainAccount = self.mainAccountList[0] agentAccount = self.agentAccountList[0] mainCtp = getCTP(mainAccount, self.mainCTPList) agentCtp = getCTP(agentAccount, self.agentCTPList) agentCtp.opposite = False for i in mainCtp.childList: print('main have guanlian') if agentCtp is i: print('关联信息已经存在') print(agentCtp.userID) return mainAccount.childs.append(agentAccount) agentAccount.father = mainAccount mainCtp.childList.append(agentCtp) agentCtp.father = mainCtp
def run_child(): """ Running in the child process. """ event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(BinancesGateway) # rpc_engine = main_engine.add_app(RpcServiceApp) cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.add_app(RiskManagerApp) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") binances_setting = load_json("connect_binances.json") gateway_name = "BINANCES" main_engine.connect(binances_setting, gateway_name) # connect_data = load_json("proxy_connect_status.json") # connect_data.update({gateway_name: True}) # save_json("proxy_connect_status.json", connect_data) main_engine.write_log("连接BINANCES接口") sleep(10) # rpc_engine.start() cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies() sleep(10) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") cta_engine.start_all_strategies() main_engine.write_log("CTA策略全部启动") try: while True: # connect_data = load_json("proxy_connect_status.json") # if not connect_data.get(gateway_name): # main_engine.connect(binances_setting, gateway_name) # connect_data.update(gateway_name, True) # save_json("proxy_connect_status.json", connect_data) # print("重连接口") sleep(10) except KeyboardInterrupt: main_engine.write_log("CTA策略正在退出") cta_engine.close() # rpc_engine.close() main_engine.close()
def main(): event_engine = EventEngine() event_engine.register("test", handle) event_engine.start() evt = Event("test") evt.data = "df" print("recei22:" + evt.__str__()) event_engine.put(evt) sleep(1) event_engine.stop()
def __init__(self): self.event_engine = EventEngine() self.main_engine = MainEngine(self.event_engine) self.main_engine.add_gateway(HbdmGateway) self.main_engine.add_gateway(BitmexGateway) log_engine = self.main_engine.get_engine("log") self.event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) self.main_engine.write_log("注册日志事件监听") self.main_engine.add_app(CtaStrategyApp) self.main_engine.add_app(AlgoTradingApp)
def main(): """""" qapp = create_qapp() event_engine = EventEngine() main_engine = MainEngine(event_engine) log_engine = main_engine.get_engine("log") event_engine.register(EVENT_FOLLOW_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") # main_engine.add_gateway(BinanceGateway) main_engine.add_gateway(CtpGateway) # main_engine.add_gateway(CtptestGateway) # main_engine.add_gateway(MiniGateway) # main_engine.add_gateway(SoptGateway) # main_engine.add_gateway(MinitestGateway) # main_engine.add_gateway(FemasGateway) # main_engine.add_gateway(IbGateway) # main_engine.add_gateway(FutuGateway) # main_engine.add_gateway(BitmexGateway) # main_engine.add_gateway(TigerGateway) # main_engine.add_gateway(OesGateway) # main_engine.add_gateway(OkexGateway) # main_engine.add_gateway(HuobiGateway) # main_engine.add_gateway(BitfinexGateway) # main_engine.add_gateway(OnetokenGateway) # main_engine.add_gateway(OkexfGateway) # main_engine.add_gateway(HbdmGateway) # main_engine.add_gateway(XtpGateway) # main_engine.add_gateway(TapGateway) # main_engine.add_gateway(ToraGateway) # main_engine.add_gateway(AlpacaGateway) # main_engine.add_gateway(OkexsGateway) # main_engine.add_gateway(DaGateway) # main_engine.add_gateway(CoinbaseGateway) # main_engine.add_gateway(RpcGateway) main_engine.add_gateway(TqdataGateway) main_engine.add_app(CtaStrategyApp) # main_engine.add_app(CtaBacktesterApp) # main_engine.add_app(CsvLoaderApp) # main_engine.add_app(AlgoTradingApp) # main_engine.add_app(DataRecorderApp) # main_engine.add_app(RiskManagerApp) # main_engine.add_app(ScriptTraderApp) # main_engine.add_app(RpcServiceApp) # main_engine.add_app(SpreadTradingApp) # main_engine.add_app(FollowTradingApp) main_window = MainWindow(main_engine, event_engine) main_window.showMaximized() qapp.exec()
def run_child(): """ Running in the child process. """ event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(BinancesGateway) rpc_engine = main_engine.add_app(RpcServiceApp) cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.add_app(RiskManagerApp) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") binances_setting = load_json("connect_binances.json") gateway_name = "BINANCES" main_engine.connect(binances_setting, gateway_name) main_engine.write_log("连接BINANCES接口") sleep(10) rpc_setting = load_json("rpc_service_setting.json") rpc_engine.start(rpc_setting['rep_address'], rpc_setting['pub_address']) cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies(True) # sleep(10) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") # cta_engine.start_all_strategies() # main_engine.write_log("CTA策略全部启动") def ctrl_handler(signum, frame): cta_engine.stop_all_strategies() cta_engine.close() rpc_engine.close() main_engine.close() sys.exit(0) signal.signal(signal.SIGINT, ctrl_handler) while True: # connect_data = load_json("proxy_connect_status.json") # if not connect_data.get(gateway_name): # main_engine.connect(binances_setting, gateway_name) # connect_data.update(gateway_name, True) # save_json("proxy_connect_status.json", connect_data) # print("重连接口") sleep(10)
def run_child(): """ Running in the child process. """ # 全局配置文件机制 # 如果想开发自己的程序,可以把全局配置文件加载成自己的 SETTINGS["log.file"] = True # 创建事件引擎 event_engine = EventEngine() # 创建主引擎 main_engine = MainEngine(event_engine) # 添加交易接口,就是UI界面进入时的左边选择项 main_engine.add_gateway(CtpGateway) # 添加应用程序,就是UI界面进入时的右边选择项 cta_engine = main_engine.add_app(CtaStrategyApp) main_engine.write_log("主引擎创建成功") # 取主引擎的事件引擎 # 向事件引擎注册日志事件监听函数 # 注意主引擎在创建日志引擎的时候,由日志引擎自动向事件引擎注册了主引擎的事件EVENT_LOG -> LogEngine.__init__ log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") # 连接交易接口,相当于UI界面的连接交易接口 main_engine.connect(ctp_setting, "CTP") main_engine.write_log("连接CTP接口") # 等待交易接口连接成功 sleep(10) # 接下来是CtaEngine的操作了 cta_engine.init_engine() main_engine.write_log("CTA策略初始化完成") cta_engine.init_all_strategies() sleep(60) # Leave enough time to complete strategy initialization main_engine.write_log("CTA策略全部初始化") cta_engine.start_all_strategies() main_engine.write_log("CTA策略全部启动") while True: sleep(10) trading = check_trading_period() if not trading: print("关闭子进程") main_engine.close() sys.exit(0)
def init_cli_trading(gateways: Sequence[Type[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
def main(): global me ee = EventEngine() me = MainEngine(ee) ee.register(EVENT_CONTRACT, on_contract) me.addGateway(ctpGateway) me.getGateway('CTP').filePath = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'CTP_connect.simnow.mock.json') me.connect('CTP')
def __init__(self, event_engine: EventEngine = None): """""" if event_engine: self.event_engine = event_engine else: self.event_engine = EventEngine() self.event_engine.start() self.gateways = {} self.engines = {} self.apps = {} self.init_engines()
def __init__(self): self.event_engine = EventEngine() self.main_engine = MainEngine(self.event_engine) self.main_engine.add_gateway(HbdmGateway) self.main_engine.add_gateway(BitmexGateway) log_engine = self.main_engine.get_engine("log") self.event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) self.main_engine.write_log("注册日志事件监听") self.backtester_engine = self.main_engine.add_app(CtaBacktesterApp) self.backtester_engine.init_engine() self.algo_engine:AlgoEngine = self.main_engine.add_app(AlgoTradingApp) self.algo_engine.init_engine() self.close = None
def main(): """""" # 创建 QApplication 对象 并进行初始化 # 事件引擎 event_engine = EventEngine() # 把事件引擎附加到主引擎里 main_engine = MainEngine(event_engine) main_engine.write_log("主引擎创建成功") log_engine = main_engine.get_engine("log") event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event) main_engine.write_log("注册日志事件监听") # 添加火币的交互通道 # main_engine.add_gateway(HuobiGateway) # sleep(1) # main_engine.add_gateway(BitfinexGateway) # main_engine.add_gateway(OnetokenGateway) main_engine.add_gateway(OkexGateway) sleep(1) main_engine.add_gateway(OkexfGateway) # main_engine.add_gateway(HbdmGateway) # 把 app 保存到 apps 和 engines 里 sleep(1) data_recorder_app = main_engine.add_app(DataRecorderApp) sleep(2) # 获取所有交易通道 gateway_names = main_engine.get_all_gateway_names() for name in gateway_names: # 连接火币平台 connect = ConnectNoDialog(main_engine=main_engine, gateway_name=name) connect.connect() sleep(2) sleep(20) for tick in data_recorder_app.tick_recordings.keys(): data_recorder_app.add_tick_recording(tick) for bar in data_recorder_app.bar_recordings.keys(): data_recorder_app.add_bar_recording(bar) while True: # 一天 sleep(100000000)
def __init__(self, event_engine: EventEngine = None): """""" if event_engine: self.event_engine: EventEngine = event_engine else: self.event_engine = EventEngine() self.event_engine.start() self.gateways: Dict[str, BaseGateway] = {} self.engines: Dict[str, BaseEngine] = {} self.apps: Dict[str, BaseApp] = {} self.exchanges: List[Exchange] = [] os.chdir(TRADER_DIR) # Change working directory self.init_engines() # Initialize function engines
def __init__(self, event_engine: EventEngine = None): """""" if event_engine: self.event_engine = event_engine else: self.event_engine = EventEngine() self.event_engine.start() self.gateways = {} self.engines = {} self.apps = {} self.exchanges = [] os.chdir(TRADER_DIR) # Change working directory self.init_engines() # Initialize function engines
class MainEngine: """ Acts as the core of VN Trader. """ def __init__(self, event_engine: EventEngine = None): """""" if event_engine: self.event_engine = event_engine else: self.event_engine = EventEngine() self.event_engine.start() self.gateways = {} self.engines = {} self.apps = {} self.init_engines() def add_engine(self, engine_class: Any): """ Add function engine. """ engine = engine_class(self, self.event_engine) self.engines[engine.engine_name] = engine def add_gateway(self, gateway_class: BaseGateway): """ Add gateway. """ gateway = gateway_class(self.event_engine) self.gateways[gateway.gateway_name] = gateway def add_app(self, app_class: BaseApp): """ Add app. """ app = app_class() self.apps[app.app_name] = app self.add_engine(app.engine_class) def init_engines(self): """ Init all engines. """ self.add_engine(LogEngine) self.add_engine(OmsEngine) self.add_engine(EmailEngine) def write_log(self, msg: str, source: str = ""): """ Put log event with specific message. """ log = LogData(msg=msg, gateway_name=source) event = Event(EVENT_LOG, log) self.event_engine.put(event) def get_gateway(self, gateway_name: str): """ Return gateway object by name. """ gateway = self.gateways.get(gateway_name, None) if not gateway: self.write_log(f"找不到底层接口:{gateway_name}") return gateway def get_engine(self, engine_name: str): """ Return engine object by name. """ engine = self.engines.get(engine_name, None) if not engine: self.write_log(f"找不到引擎:{engine_name}") return engine def get_default_setting(self, gateway_name: str): """ Get default setting dict of a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: return gateway.get_default_setting() return None def get_all_gateway_names(self): """ Get all names of gatewasy added in main engine. """ return list(self.gateways.keys()) def get_all_apps(self): """ Get all app objects. """ return list(self.apps.values()) def connect(self, setting: dict, gateway_name: str): """ Start connection of a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: gateway.connect(setting) def subscribe(self, req: SubscribeRequest, gateway_name: str): """ Subscribe tick data update of a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: gateway.subscribe(req) def send_order(self, req: OrderRequest, gateway_name: str): """ Send new order request to a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: return gateway.send_order(req) else: return "" def cancel_order(self, req: CancelRequest, gateway_name: str): """ Send cancel order request to a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: gateway.cancel_order(req) def close(self): """ Make sure every gateway and app is closed properly before programme exit. """ # Stop event engine first to prevent new timer event. self.event_engine.stop() for engine in self.engines.values(): engine.close() for gateway in self.gateways.values(): gateway.close()