def current(symbols, fields=''): # type: (GmSymbols, Text) -> List[Any] """ 查询当前行情快照,返回tick数据 """ symbols = load_to_list(symbols) fields = load_to_list(fields) symbols_str = ','.join(symbols) fields_str = ','.join(fields) ticks = Ticks() status, data = py_gmi_current(symbols_str, fields_str) if c_status_fail(status, 'py_gmi_current'): return [] ticks.ParseFromString(data) ticks = [ protobuf_to_dict(tick, including_default_value_fields=False) for tick in ticks.data ] if not fields: return ticks return ticks
def get_history_instruments(self, symbols, fields=None, start_date=None, end_date=None): """ 返回指定的symbols的标的日指标数据 """ self._init_addr() symbols = load_to_list(symbols) start_date = utils.to_datestr(start_date) end_date = utils.to_datestr(end_date) if not start_date: start_date = '' if not end_date: end_date = '' req = GetHistoryInstrumentsReq(symbols=','.join(symbols), fields=fields, start_date=start_date, end_date=end_date) resp = self.stub.GetHistoryInstruments(req, metadata=[ (str('authorization'), context.token), (str('sdk-lang'), context.sdk_lang), (str('sdk-version'), context.sdk_version), (str('sdk-arch'), context.sdk_arch), (str('sdk-os'), context.sdk_os), ]) result = [protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in resp.data] for info in result: created_at_val = info.get('created_at', None) if isinstance(created_at_val, Datetime): info['trade_date'] = utils.utc_datetime2beijing_datetime(created_at_val) info.pop('created_at') return result
def subscribe(symbols, frequency='1d', count=1, wait_group=False, wait_group_timeout='10s', unsubscribe_previous=False): # type:(GmSymbols, Text, int, bool, Text, bool) ->NoReturn """ 订阅行情,可以指定symbol, 数据滑窗大小,以及是否需要等待全部代码的数据到齐再触发事件。 wait_group: 是否等待全部相同频度订阅的symbol到齐再触发on_bar事件。 一个 frequency, 只能有一个 wait_group_timeout 也就是说如果后面调用该函数时, 相同的frequency, 但是 wait_group_timeout 不同, 则 wait_group_timeout 被忽略. 同时要注意, 一个symbol与frequency组合, 只能有一种wait_group, 即wait_group要么为true, 要么为false """ frequency = adjust_frequency(frequency) symbols = load_to_list(symbols) symbols_str = ','.join(symbols) status = py_gmi_subscribe(symbols_str, frequency, unsubscribe_previous) if c_status_fail(status, 'py_gmi_subscribe'): return if unsubscribe_previous: _unsubscribe_all() if frequency == DATA_TYPE_TICK: if context.max_tick_data_count < count: context.max_tick_data_count = count for sy in symbols: context.tick_data_cache[sy] = collections.deque(maxlen=count) context.tick_sub_symbols.add(sy) return # 处理订阅bar的情况 context._set_onbar_waitgroup_timeout_check() wait_group_timeoutint = load_to_second(wait_group_timeout) if context.max_bar_data_count < count: context.max_bar_data_count = count for sy in symbols: context.bar_data_cache["{}_{}".format( sy, frequency)] = collections.deque(maxlen=count) barsubinfo = BarSubInfo(sy, frequency) if barsubinfo not in context.bar_sub_infos: context.bar_sub_infos.add(barsubinfo) if wait_group: if frequency not in context.bar_waitgroup_frequency2Info: context.bar_waitgroup_frequency2Info[ frequency] = BarWaitgroupInfo(frequency, wait_group_timeoutint) context.bar_waitgroup_frequency2Info[frequency].add_one_symbol( sy) else: gmsdklogger.debug("symbol=%s frequency=%s 已订阅过", sy, frequency) continue
def order_cancel(wait_cancel_orders): # type: (Union[Dict[Text,Any], List[Dict[Text, Any]]]) ->NoReturn """ 撤销委托. 传入单个字典. 或者list字典. 每个字典包含key: cl_ord_id, account_id """ wait_cancel_orders = load_to_list(wait_cancel_orders) orders = Orders() for wait_cancel_order in wait_cancel_orders: order = orders.data.add() order.cl_ord_id = wait_cancel_order.get('cl_ord_id') order.account_id = wait_cancel_order.get('account_id') req = orders.SerializeToString() py_gmi_cancel_order(req)
def unsubscribe(symbols, frequency='1d'): # type: (GmSymbols, Text) ->NoReturn """ unsubscribe - 取消行情订阅 取消行情订阅,默认取消所有已订阅行情 """ symbols = load_to_list(symbols) symbols_str = ','.join(symbols) frequency = adjust_frequency(frequency) status = py_gmi_unsubscribe(symbols_str, frequency) if c_status_fail(status, 'py_gmi_unsubscribe'): return if symbols_str == '*': _unsubscribe_all() return if frequency == DATA_TYPE_TICK: for sy in symbols: if sy in list(six.iterkeys(context.tick_data_cache)): del context.tick_data_cache[sy] context.tick_sub_symbols.remove(sy) return # 处理bar的退订 for sy in symbols: k = sy + "_" + frequency if k in list(six.iterkeys(context.bar_data_cache)): del context.bar_data_cache[k] context.bar_sub_infos.remove(BarSubInfo(sy, frequency)) barwaitgroupinfo = context.bar_waitgroup_frequency2Info.get( frequency, None) if barwaitgroupinfo: barwaitgroupinfo.remove_one_symbol(sy) # 处理已全部退订的 frequency for frequency in list(six.iterkeys(context.bar_waitgroup_frequency2Info)): if len(context.bar_waitgroup_frequency2Info[frequency]) == 0: gmsdklogger.debug('frequency=%s 已全部取消订阅', frequency) del context.bar_waitgroup_frequency2Info[frequency]