def __init__(self, gateway): """Constructor""" super(CtpTdApi, self).__init__() self.gateway = gateway self.gateway_name = gateway.gateway_name self.reqid = 0 self.order_ref = 0 self.connect_status = False self.login_status = False self.auth_status = False self.login_failed = False self.contract_inited = False self.userid = "" self.password = "" self.brokerid = "" self.auth_code = "" self.appid = "" self.product_info = "" self.frontid = 0 self.sessionid = 0 self.order_data = [] self.trade_data = [] self.positions = {} self.sysid_orderid_map = {} self.account_date = None #账户日期 self.commission_file_name = 'vt_commission_data' self.commission_file_path = get_folder_path(self.commission_file_name) self.commission_req = {} # 手续费查询字典 self.commission_data = {} # 手续费字典 self.margin_ratio_file_name = 'vt_margin_ratio_data' self.margin_ratio_file_path = get_folder_path( self.margin_ratio_file_name) self.margin_ratio_req = {} # 保证金率查询字典 self.margin_ratio_data = {} # 保证金率字典 self.commission_symbol = [ symbol for symbol in load_json("vt_symbol.json").keys() ] self.margin_ratio_symbol = [ symbol for symbol in load_json("vt_symbol.json").keys() ] #读取硬盘存储手续费数据,保证金率数据 self.load_commission() # 赋值给 self.commission_data字典 self.load_margin_ratio() # 赋值给 self.margin_ratio_data字典
def change_strategy_name(self, old_name, new_name): """ 在线更换策略实例名称,需要把Json文件也转移 :param old_name: :param new_name: :return: """ if old_name == new_name: self.write_log(u'更换策略实例名称失败,old:{} =>new:{}'.format( old_name, new_name)) return data_folder = get_folder_path('data') self.json_name = new_name # 旧文件 old_json_file = str( data_folder.joinpath(u'{0}_Grids.json'.format(old_name))) if os.path.isfile(old_json_file): # 新文件若存在,移除 try: os.remove(old_json_file) except Exception as ex: self.write_error( u'GridTrade.change_strategy_name 删除文件:{}异常:{}'.format( old_json_file, str(ex))) self.save()
def connect(self, userid: str, password: str, client_id: int, server_ip: str, server_port: int, software_key: str): """""" if self.api: return self.userid = userid self.password = password self.client_id = client_id self.server_ip = server_ip self.server_port = server_port self.software_key = software_key # Create API object path = str(get_folder_path(self.gateway_name.lower())) self.api = API.TraderApi.CreateTraderApi( self.client_id, path, XTP_LOG_LEVEL.XTP_LOG_LEVEL_TRACE) self.api.RegisterSpi(self) self.api.SetSoftwareKey(self.software_key) self.api.SubscribePublicTopic(XTP_TE_RESUME_TYPE.XTP_TERT_RESTART) self.gateway.write_log("交易接口初始化成功") # Login to server Thread(target=self.login).start()
def connect(self, userid: str, password: str, client_id: int, server_ip: str, server_port: int, quote_protocol: str): """""" if self.api: return self.userid = userid self.password = password self.client_id = client_id self.server_ip = server_ip self.server_port = server_port if quote_protocol == "CTP": self.quote_protocol = XTP_PROTOCOL_TYPE.XTP_PROTOCOL_TCP else: self.quote_protocol = XTP_PROTOCOL_TYPE.XTP_PROTOCOL_UDP # Create API object path = str(get_folder_path(self.gateway_name.lower())) self.api = API.QuoteApi.CreateQuoteApi( self.client_id, path, XTP_LOG_LEVEL.XTP_LOG_LEVEL_TRACE) self.api.RegisterSpi(self) self.gateway.write_log("行情接口初始化成功") # Login to server Thread(target=self.login).start()
def get_logger(logger_name="default_logger", file_path=None, level=INFO, format_s="%(asctime)s %(levelname)s: %(message)s", is_console=True, is_file=True): current_logger = logging.getLogger(logger_name) current_logger.setLevel(level) formatter = logging.Formatter(format_s) if is_console: console_handler = logging.StreamHandler() console_handler.setLevel(level) console_handler.setFormatter(formatter) current_logger.addHandler(console_handler) if is_file: if not file_path: today_date = datetime.now().strftime("%Y%m%d") filename = f"vt_{today_date}.log" log_path = get_folder_path("log") file_path = log_path.joinpath(filename) file_handler = file_handlers.get(file_path, None) if file_handler is None: file_handler = logging.FileHandler(file_path, mode="a", encoding="utf8") file_handler.setLevel(level) file_handler.setFormatter(formatter) current_logger.addHandler(file_handler) return current_logger
def connect( self, address: str, server_license: str, userid: str, password: str, auth_code: str, appid: str ) -> None: """ Start connection to server. """ self.userid = userid self.password = password self.auth_code = auth_code self.appid = appid if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.newTradeApi(str(path) + "\\Td") self.rgisterSubModel("1") # ?? self.registerFront(address) self.init( server_license, "", "", "", "" ) self.connect_status = True else: self.authenticate()
def connect( self, address: str, userid: str, password: str, brokerid: int, auth_code: str, appid: str ) -> None: """连接服务器""" self.userid = userid self.password = password self.brokerid = brokerid self.auth_code = auth_code self.appid = appid if not self.connect_status: path: Path = get_folder_path(self.gateway_name.lower()) self.createFtdcTraderApi((str(path) + "\\Td").encode("GBK")) self.subscribePrivateTopic(0) self.subscribePublicTopic(0) self.registerFront(address) self.init() self.connect_status = True else: self.authenticate()
def connect( self, userid: str, password: str, client_id: int, server_ip: str, server_port: int, software_key: str ) -> None: """""" self.userid = userid self.password = password self.client_id = client_id self.server_ip = server_ip self.server_port = server_port self.software_key = software_key self.protocol = PROTOCOL_VT2XTP["TCP"] # Create API object if not self.connect_status: path = str(get_folder_path(self.gateway_name.lower())) self.createTraderApi(self.client_id, path) self.setSoftwareKey(self.software_key) self.subscribePublicTopic(0) self.login_server()
def connect( self, userid: str, password: str, orgid: str, branchid: str, system_id: str, host: str, port: str, ) -> None: """ Start connection to server. """ self.userid = userid self.password = password self.prefix = datetime.now().strftime("%Y%m%d%H%M%S") if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.setLogConfig(str(path)) self.createTraderApi() account_info = { "account_id": userid, "account_type": AccountType_Stock, "account_pwd": password, "cust_orgid": orgid, "cust_branchid": branchid, "cl_system_id": system_id, } self.login(host, int(port), account_info, get_terminal_info()) self.connect_status = True
def write_log(self, msg: str, strategy: CtaTemplate = None, level: int = logging.INFO): """ Create cta engine log event. """ if strategy: msg = f"{strategy.strategy_name}: {msg}" if strategy: strategy_logger = self.strategy_loggers.get(strategy.strategy_name, None) if not strategy_logger: log_path = get_folder_path('log') log_filename = str(log_path.joinpath(str(strategy.strategy_name))) print(u'create logger:{}'.format(log_filename)) self.strategy_loggers[strategy.strategy_name] = setup_logger(file_name=log_filename, name=str(strategy.strategy_name)) strategy_logger = self.strategy_loggers.get(strategy.strategy_name) if strategy_logger: strategy_logger.log(level, msg) # 如果日志数据异常,错误和告警,输出至sys.stderr if level in [logging.CRITICAL, logging.ERROR, logging.WARNING]: if strategy: print(f"{strategy.strategy_name}: {msg}" if strategy.strategy_name else msg, file=sys.stderr) else: print(f"{msg}", file=sys.stderr) msg = msg.replace("\033[1;32;31m", "") msg = msg.replace("\033[0m", "") log = LogData(msg=msg, gateway_name=APP_NAME) event = Event(type=EVENT_CTA_LOG, data=log) self.event_engine.put(event)
def __init__(self, editor_manager): """""" super().__init__() self.editor_manager = editor_manager self.editor_engine = editor_manager.editor_engine # 将自身的引用赋值到引擎中 self.editor_engine.editor_box = self self.event_engine = editor_manager.event_engine self.editor_textarea = None self.current_strategy_name = "" self.highlighter = None self.script_folder = Path(get_folder_path("script")) self.script_template = script_template self.script_text = script_template self.insert_keyword = "# 麦语言脚本插入位置" self.insert_position = 0 self.current_python_file = "" self.current_txt_file = "" self.new_txt_file = "" self.mc_code = "" self.param_dialog = None # 脚本线程 self.script_thread = None self.init_ui()
def connect(self, username: str, password: str, host: str, port: int, auth_code: str): """ Starting connection to TAP server. """ # General API setting path = get_folder_path(self.gateway_name.lower()) SetITapTradeAPIDataPath(str(path)) SetITapTradeAPILogLevel(APILOGLEVEL_NONE) # Create API object info = TapTradeAPIApplicationInfo() info.AuthCode = auth_code info.KeyOperationLogPath = str(path) self.api, iResult = CreateITapTradeAPI(info) if not self.api: self.gateway.write_log("交易API初始化失败") return # Set server address and port self.api.SetAPINotify(self) self.api.SetHostAddress(host, port, False) # Start connection login_auth = TapAPITradeLoginAuth() login_auth.UserNo = username login_auth.Password = password login_auth.ISModifyPassword = APIYNFLAG_NO self.api.Login(login_auth)
def connect(self, address: str, userid: str, password: str, brokerid: int): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid # If not connected, then start connection first. if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcMdApi(str(path) + "\\Md") self.registerFront(address) self.init() self.connect_status = True # Sleep 1 second and check trigger callback manually # (temp fix of the bug of Huaxi futures SOPT system) sleep(1) if not self.login_status: self.onFrontConnected() # If already connected, then login immediately. elif not self.login_status: self.login()
def connect( self, address: str, userid: str, password: str, login_type: int, ) -> None: """ Start connection to server. """ self.userid = userid self.password = password self.login_type = login_type if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createGoldTraderApi(str(path) + "\\Td") self.subscribePrivateTopic(0) self.subscribePublicTopic(0) self.registerFront(address) self.init() self.connect_status = True
def __init__(self, strategy, **kwargs): """初始化 maxlots,最大网格数 height,网格高度(绝对值,包含minDiff) win,盈利数(包含minDiff) vol,网格开仓数 minDiff, 最小价格跳动 """ super(CtaGridTrade, self).__init__(strategy=strategy) self.price_tick = kwargs.get('price_tick', 1) self.json_name = self.strategy.strategy_name # 策略名称 self.max_lots = kwargs.get('max_lots', 10) # 缺省网格数量 self.grid_height = kwargs.get('grid_height', 10 * self.price_tick) # 最小网格高度 self.grid_win = kwargs.get('grid_win', 10 * self.price_tick) # 最小止盈高度 self.volume = kwargs.get('volume', 1) # 每次网格开仓数量 self.rate_list = kwargs.get('rate_list', [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 梯级开仓数量比例 self.up_grids = [] # 上网格列表,专门做空 self.dn_grids = [] # 下网格列表,专门做多 self.avg_up_open_price = 0.0 # 上网格开仓均价 self.avg_dn_open_price = 0.0 # 下网格开仓均价 self.max_up_open_price = 0.0 # 上网格最高开仓价 self.min_dn_open_price = 0.0 # 下网格最小开仓价 # 网格json文件的路径 self.json_file_path = str( get_folder_path('data').joinpath(f'{self.json_name}_Grids.json'))
def connect(self, username: str, password: str, host: str, port: int, auth_code: str) -> None: """ Starting connection to TAP server. """ self.init() # General API setting path = get_folder_path(self.gateway_name.lower()) self.setITapTradeAPIDataPath(str(path)) self.setITapTradeAPILogLevel(LOGLEVEL_VT2TAP["APILOGLEVEL_NONE"]) # Create API object req = {"AuthCode": auth_code, "KeyOperationLogPath": str(path)} self.createITapTradeAPI(req, 0) # Set server address and port self.setHostAddress(host, port) # Start connection data = { "UserNo": username, "Password": password, "ISModifyPassword": FLAG_VT2TAP["APIYNFLAG_NO"], "NoticeIgnoreFlag": "TAPI_NOTICE_IGNORE_POSITIONPROFIT" } self.login(data)
def connect( self, address: str, userid: str, password: str, brokerid: int, auth_code: str, appid: str, product_info ): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid self.auth_code = auth_code self.appid = appid self.product_info = product_info if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcTraderApi(str(path) + "\\Td") self.subscribePrivateTopic(0) self.subscribePublicTopic(0) self.registerFront(address) self.init() self.connect_status = True else: self.authenticate()
def load(self, direction, open_status_filter=[]): """ 加载本地Json至网格 :param direction: Direction.SHORT,做空网格;Direction.LONG,做多网格 :param open_status_filter: 缺省,不做过滤;True,只提取已开仓的数据,False,只提取未开仓的数据 :return: """ data = {} grids_save_path = get_folder_path('data') if self.json_name != self.strategy.strategy_name: self.write_log(u'JsonName {} 与 上层策略名{} 不一致.'.format(self.json_name, self.strategy.strategy_name)) self.json_name = self.strategy.strategy_name # 若json文件不存在,就保存一个;若存在,就优先使用数据文件 grid_json_file = str(grids_save_path.joinpath(u'{}_Grids.json'.format(self.json_name))) if not os.path.exists(grid_json_file): data['up_grids'] = [] data['dn_grids'] = [] self.write_log(u'{}不存在,新建保存保存'.format(grid_json_file)) try: with open(grid_json_file, 'w') as f: json_data = json.dumps(data, indent=4, ensure_ascii=False) f.write(json_data) except Exception as ex: self.write_log(u'写入网格文件{}异常:{}'.format(grid_json_file, str(ex))) else: # 读取json文件 try: with open(grid_json_file, 'r', encoding='utf8') as f: data = json.load(f) except Exception as ex: self.write_error(u'读取网格文件{}异常:{}'.format(grid_json_file, str(ex))) # 从文件获取数据 json_grids = [] if direction == Direction.SHORT: json_grids = data['up_grids'] if 'up_grids' in data else [] elif direction == Direction.LONG: json_grids = data['dn_grids'] if 'dn_grids' in data else [] grids = [] for grid_obj in json_grids: grid = CtaGrid(**grid_obj) self.write_log(grid.to_str()) # 增加对开仓状态的过滤,满足某些策略只提取已开仓的网格数据 if len(open_status_filter) > 0: if grid.open_status not in open_status_filter: continue grids.append(grid) # 更新开仓均价 self.recount_avg_open_price() return grids
def load_by_handle( self, data, symbol: str, exchange: Exchange, interval: Interval, datetime_head: str, open_head: str, high_head: str, low_head: str, close_head: str, volume_head: str, open_interest_head: str, datetime_format: str, progress_bar_dict:dict, opt_str: str ): start_time = time.time() if isinstance(data[datetime_head][0], str): data[datetime_head] = data[datetime_head].apply( lambda x: datetime.strptime(x, datetime_format) if datetime_format else datetime.fromisoformat(x)) elif isinstance(data[datetime_head][0], pd.Timestamp): self.main_engine.write_log("datetime 格式为 pd.Timestamp, 不用处理.") else: self.main_engine.write_log("未知datetime类型, 请检查") self.main_engine.write_log(f'df apply 处理日期时间 cost {time.time() - start_time:.2f}s') if opt_str == "to_db": start_time = time.time() bars = data.apply( self.to_bar_data, args=( symbol, exchange, interval, datetime_head, open_head, high_head, low_head, close_head, volume_head, open_interest_head ), axis=1).tolist() self.main_engine.write_log(f'df apply 处理bars时间 cost {time.time() - start_time:.2f}s') # insert into database database_manager.save_bar_data(bars, progress_bar_dict) elif opt_str == "to_csv": csv_file_dir = get_folder_path("csv_files") data.to_csv(f'{csv_file_dir}/{exchange.value}_{symbol}.csv', index=False) start = data[datetime_head].iloc[0] end = data[datetime_head].iloc[-1] count = len(data) return start, end, count
def init(driver: Driver, settings: dict): database = settings["database"] store_path = str(get_folder_path('db_store')) path = str(get_file_path(database)) db = SqliteDatabase(path) bcolz_meta, bar, tick = init_models(db, driver) return SqlManager(store_path, bcolz_meta, bar, tick)
def connect(self, address: str, server_license: str) -> None: """ Start connection to server. """ if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.newMdApi(str(path) + "\\Md") self.registerFront(address) self.init(server_license, "", "", "", "") self.connect_status = True
def __init__(self, editor_manager): super().__init__() self.editor_manager = editor_manager self.event_engine = editor_manager.event_engine self.script_folder = Path(get_folder_path("script")) self.strategy_list_view = None self.strategy_list_model = None self.script_textarea = None self.saved_strategy_list = [] # 当前策略文件名 self.current_strategy_name = None self.register_event() self.init_ui()
def connect(self, userid: str, password: str, client_id: int, server_ip: str, server_port: int, quote_protocol: int) -> None: """""" self.userid = userid self.password = password self.client_id = client_id self.server_ip = server_ip self.server_port = server_port self.protocol = PROTOCOL_VT2XTP[quote_protocol] # Create API object if not self.connect_status: path = str(get_folder_path(self.gateway_name.lower())) self.createQuoteApi(self.client_id, path) self.login_server()
def connect(self, address: str, userid: str, password: str, brokerid: int) -> None: """连接服务器""" self.userid = userid self.password = password self.brokerid = brokerid # 禁止重复发起连接,会导致异常崩溃 if not self.connect_status: path: Path = get_folder_path(self.gateway_name.lower()) self.createFtdcMdApi((str(path) + "\\Md").encode("GBK")) self.registerFront(address) self.init() self.connect_status = True
def write_log(self, msg: str) -> None: """ Write a log event from gateway. """ log = LogData(msg=msg, gateway_name=self.gateway_name) self.on_log(log) gateway_logger = self.gateway_loggers.get(self.gateway_name, None) if not gateway_logger: log_path = get_folder_path('log') log_filename = str(log_path.joinpath(str(self.gateway_name))) print(u'create logger:{}'.format(log_filename)) self.gateway_loggers[self.gateway_name] = setup_logger( file_name=log_filename, name=str(self.gateway_name)) gateway_logger = self.gateway_loggers.get(self.gateway_name) if gateway_logger: gateway_logger.log(logging.INFO, msg)
def connect(self, address: str, userid: str, password: str, brokerid: int): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid # If not connected, then start connection first. if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcMdApi(str(path) + "\\Md") self.registerFront(address) self.init() # If already connected, then login immediately. elif not self.login_status: self.login()
def connect(self, address: str, userid: str, password: str, brokerid: int): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid # If not connected, then start connection first. if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcMdApi(str(path) + "\\Md") self.registerFront(address) self.init() self.connect_status = True # If already connected, then login immediately. elif not self.login_status: self.login()
def connect(self): """ connect to self.td_address using self.username, self.password :return: """ flow_path = str(get_folder_path(self.gateway.gateway_name.lower())) self._native_api = CTORATstpTraderApi.CreateTstpTraderApi( flow_path, True) self._spi = ToraTdSpi(self.session_info, self, self.gateway, self.orders) self._native_api.RegisterSpi(self._spi) self._native_api.RegisterFront(self.td_address) self._native_api.SubscribePublicTopic( TORA_TE_RESUME_TYPE.TORA_TERT_RESTART) self._native_api.SubscribePrivateTopic( TORA_TE_RESUME_TYPE.TORA_TERT_RESTART) self._native_api.Init() return True
def connect(self, address: str, userid: str, password: str, brokerid: int, auth_code: str, product_info: str): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid self.auth_code = auth_code self.product_info = product_info if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcTraderApi(str(path) + "\\Td") self.subscribePrivateTopic(0) self.subscribePublicTopic(0) self.registerFront(address) self.init() else: self.authenticate()
def save(self): """ 保存网格至本地Json文件 2017/11/23 update: 保存时,空的列表也保存 :return: """ "" # 回测模式不保存 if self.strategy and getattr(self.strategy, 'backtesting', False): return # 更新开仓均价 self.recount_avg_open_price() grids_save_path = get_folder_path('data') # 确保json名字与策略一致 if self.json_name != self.strategy.strategy_name: self.write_log(u'JsonName {} 与 上层策略名{} 不一致.'.format( self.json_name, self.strategy.strategy_name)) self.json_name = self.strategy.strategy_name # 新版网格持久化文件 grid_json_file = str( grids_save_path.joinpath(u'{}_Grids.json'.format(self.json_name))) self.json_file_path = grid_json_file data = {} up_grids = [] for grid in self.up_grids: up_grids.append(grid.to_json()) dn_grids = [] for grid in self.dn_grids: dn_grids.append(grid.to_json()) data[u'up_grids'] = up_grids data[u'dn_grids'] = dn_grids with open(grid_json_file, 'w', encoding='utf8') as f: json_data = json.dumps(data, indent=4, ensure_ascii=False) f.write(json_data) self.write_log(u'GrideTrade保存文件{}完成'.format(grid_json_file))
def connect(self, address: str, userid: str, password: str, brokerid: int): """ Start connection to server. """ self.userid = userid self.password = password self.brokerid = brokerid self.address = address if not self.connect_status: path = get_folder_path(self.gateway_name.lower()) self.createFtdcTraderApi(str(path) + "\\Td") self.subscribePrivateTopic(0) self.subscribePublicTopic(0) self.subscribeUserTopic(0) self.registerFront(address) self.init() else: if not self.login_status: self.login()
def load(self): """ 从持久化文件中获取 :return: """ json_file = str( get_folder_path('data').joinpath(u'{}_Policy.json'.format( self.strategy.strategy_name))) json_data = {} if os.path.exists(json_file): try: with open(json_file, 'r', encoding='utf8') as f: # 解析json文件 json_data = json.load(f) except Exception as ex: self.write_error(u'读取Policy文件{}出错,ex:{}'.format( json_file, str(ex))) json_data = {} # 从持久化文件恢复数据 self.from_json(json_data)