def _get_account_info(self, account_id, account_name): # 资金信息 req = GetCashReq() req.account_id = account_id req = req.SerializeToString() status, result = py_gmi_get_cash(req) if c_status_fail(status, 'py_gmi_get_cash'): return cashes = Cashes() cashes.ParseFromString(result) cashes = [protobuf_to_dict(cash) for cash in cashes.data] cash = cashes[0] # 持仓信息 req = GetPositionsReq() req.account_id = account_id req = req.SerializeToString() status, result = py_gmi_get_positions(req) if c_status_fail(status, 'py_gmi_get_positions'): return positions = Positions() positions.ParseFromString(result) positions = [protobuf_to_dict(position, including_default_value_fields=True) for position in positions.data] positions_infos = {'{}.{}'.format(position['symbol'], position['side']): position for position in positions} account = Account(account_id, account_name, cash, positions_infos) self.inside_accounts[account_id] = account
def order_batch(order_infos, combine=False, account=''): """ 批量委托接口 """ orders = Orders() for order_info in order_infos: order_info['account_id'] = get_account_id(account) order = orders.data.add() [setattr(order, k, order_info[k]) for k in order_info] if context.is_backtest_model(): order.created_at.seconds = datetime2timestamp(context.now) req = orders.SerializeToString() status, result = py_gmi_place_order(req) if c_status_fail(status, 'py_gmi_place_order'): return [] if not result: return [] res = Orders() res.ParseFromString(result) return [ protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in res.data ]
def get_orders(): # type: () ->List[Dict[Text, Any]] """ 查询日内全部委托 """ all_orders = [] for account in six.itervalues(context.accounts): req = GetOrdersReq() req.account_id = account.id req = req.SerializeToString() status, result = py_gmi_get_orders(req) if c_status_fail(status, 'py_gmi_get_orders'): return [] if not result: return [] res = Orders() res.ParseFromString(result) all_orders.extend([ protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in res.data ]) return all_orders
def _inner_place_order(o): # type: (Order) ->List[Dict[Text, Any]] """ 下单并返回order的信息. 同步调用, 在下单返回的order 的 client_order_id 将会有值. 下单出错的话, 返回空的list """ # 在回测模式且wait_group=True时, 设置这个created_at, 也就是通知c底层要根据这个时间设置price if context.is_backtest_model() and context.has_wait_group: o.created_at.seconds = datetime2timestamp(context.now) orders = Orders() orders.data.extend([o]) req = orders.SerializeToString() status, result = py_gmi_place_order(req) if c_status_fail(status, 'py_gmi_place_order'): return [] if not result: return [] res = Orders() res.ParseFromString(result) return [ protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in res.data ]
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 _set_accounts(self): status, data = py_gmi_get_accounts() if c_status_fail(status, 'py_gmi_get_accounts'): return accounts = Accounts() accounts.ParseFromString(data) for account in accounts.data: # type: PbAccount self._get_account_info(account.account_id, account.account_name)
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 get_parameters(): # type: () ->List[Dict[Text, Any]] req = GetParametersReq() req.owner_id = context.strategy_id req = req.SerializeToString() status, result = py_gmi_get_parameters(req) if c_status_fail(status, 'py_gmi_get_parameters'): return [] req = Parameters() req.ParseFromString(result) return [protobuf_to_dict(parameters) for parameters in req.parameters]
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]
def get_execution_reports(): # type: () -> List[Dict[Text, Any]] """ 返回执行回报 """ reports = [] for account in six.itervalues(context.accounts): req = GetExecrptsReq() req.account_id = account.id req = req.SerializeToString() status, result = py_gmi_get_execution_reports(req) if c_status_fail(status, 'py_gmi_get_execution_reports'): return [] res = ExecRpts() res.ParseFromString(result) reports.extend([ protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in res.data ]) return reports
def order_close_all(): # type: () ->List[Dict[Text, Any]] """ 平当前所有可平持仓 """ req = CloseAllPositionsReq() for account in six.itervalues(context.accounts): req.account_ids.extend([account.id]) req = req.SerializeToString() status, result = py_gmi_close_all_positions(req) if c_status_fail(status, 'py_gmi_close_all_positions'): return [] if not result: return [] res = Orders() res.ParseFromString(result) return [ protobuf_to_dict(res_order, including_default_value_fields=True) for res_order in res.data ]