def run(): """ Running in the child process. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(BinancesGateway) 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("注册日志事件监听") main_engine.connect(binances_setting, "BINANCES") main_engine.write_log("连接BINANCES接口") 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(10)
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 main(): """""" qapp = create_qapp() event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(BinanceGateway) main_engine.add_gateway(BinancesGateway) main_engine.add_app(CtaStrategyApp) main_engine.add_app(CtaBacktesterApp) main_engine.add_app(DataManagerApp) main_engine.add_app(AlgoTradingApp) main_engine.add_app(DataRecorderApp) main_engine.add_app(RiskManagerApp) main_engine.add_app(SpreadTradingApp) main_window = MainWindow(main_engine, event_engine) main_window.showMaximized() qapp.exec()
SETTINGS["log.level"] = INFO SETTINGS["log.console"] = True # 打印信息到终端. binance_settings = { "key": "xx", "secret": "xxx", "session_number": 3, "proxy_host": "127.0.0.1", "proxy_port": 1087 } if __name__ == "__main__": SETTINGS["log.file"] = True event_engine = EventEngine() # 初始化事件引擎 main_engine = MainEngine(event_engine) # 初始化主引擎 main_engine.add_gateway(BinanceGateway) # 添加cta策略的app cta_engine: CtaEngine = main_engine.add_app(CtaStrategyApp) # 添加cta引擎, 实际上就是初始化引擎。 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(binance_settings, "BINANCE") main_engine.write_log("连接BINANCE接口")
class MainEngine: """ Acts as the core of VN Trader. """ 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 add_engine(self, engine_class: Any) -> "BaseEngine": """ Add function engine. """ engine = engine_class(self, self.event_engine) self.engines[engine.engine_name] = engine return engine def add_gateway(self, gateway_class: Type[BaseGateway]) -> BaseGateway: """ Add gateway. """ gateway = gateway_class(self.event_engine) self.gateways[gateway.gateway_name] = gateway # Add gateway supported exchanges into engine for exchange in gateway.exchanges: if exchange not in self.exchanges: self.exchanges.append(exchange) return gateway def add_app(self, app_class: Type[BaseApp]) -> "BaseEngine": """ Add app. """ app = app_class() self.apps[app.app_name] = app engine = self.add_engine(app.engine_class) return engine def init_engines(self) -> None: """ Init all engines. """ self.add_engine(LogEngine) self.add_engine(OmsEngine) self.add_engine(EmailEngine) def write_log(self, msg: str, source: str = "") -> None: """ 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) -> BaseGateway: """ 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) -> "BaseEngine": """ 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) -> Optional[Dict[str, Any]]: """ 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) -> List[str]: """ Get all names of gateway added in main engine. """ return list(self.gateways.keys()) def get_all_apps(self) -> List[BaseApp]: """ Get all app objects. """ return list(self.apps.values()) def get_all_exchanges(self) -> List[Exchange]: """ Get all exchanges. """ return self.exchanges def connect(self, setting: dict, gateway_name: str) -> None: """ 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) -> None: """ 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) -> 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) -> None: """ Send cancel order request to a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: gateway.cancel_order(req) def query_order(self, req: QueryRequest, gateway_name: str) -> None: """ Send query order request to a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway and hasattr(gateway, 'query_order'): gateway.query_order(req) def send_orders(self, reqs: Sequence[OrderRequest], gateway_name: str) -> List[str]: """ """ gateway = self.get_gateway(gateway_name) if gateway: return gateway.send_orders(reqs) else: return ["" for req in reqs] def cancel_orders(self, reqs: Sequence[CancelRequest], gateway_name: str) -> None: """ """ gateway = self.get_gateway(gateway_name) if gateway: gateway.cancel_orders(reqs) def query_history(self, req: HistoryRequest, gateway_name: str) -> Optional[List[BarData]]: """ Send cancel order request to a specific gateway. """ gateway = self.get_gateway(gateway_name) if gateway: return gateway.query_history(req) else: return None # # def query_account_position(self): # """ # query the account and position # """ # for gateway in self.gateways.values(): # gateway.query_account() # gateway.query_position() def close(self) -> None: """ 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()
exchange=exchange, interval=Interval(interval), start=start, end=end) data = gate_way.query_history(req) print("start12_end13", data) if __name__ == "__main__": """ for crawling data from Binance exchange. """ SETTINGS["log.file"] = True event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(BinanceGateway) # spot # main_engine.add_gateway(BinancesGateway) # future main_engine.connect(binance_setting, "BINANCE") # spot # main_engine.connect(binances_setting, "BINANCES") # future sleep(3) main_engine.write_log("连接BINANCE接口") # spot # main_engine.write_log("连接BINANCES接口") # future gate_way = main_engine.get_gateway("BINANCE") # spot # gate_way = main_engine.get_gateway("BINANCES") # future print(gate_way) symbol = "btcusdt" # spot for lower case while the future will be upper case.