import setting import gm.api as gmapi class JueJinApi: __previous_trading_date_cache = {} # 获取掘金参数 @classmethod def getJueJinAccountInfo(cls): # strategy_id, token = setting.get_juejin_params() return "", "95a982ce-fc2d-11ec-8ff3-0a0027000010", "77fa387b37e9bc1586ae1135827e2ccb3e7cd4e4" @classmethod def get_juejin_code_list_with_prefix(cls, codes): list = [] for d in codes: if d.find(".") > 0: list.append(d) elif d[0:2] == '00': list.append("SZSE.{}".format(d)) elif d[0:2] == '60': list.append("SHSE.{}".format(d)) return list @classmethod def get_gp_latest_info(cls, codes, fields=None): if not codes: return [] symbols = cls.get_juejin_code_list_with_prefix(codes) account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) data = gmapi.get_instruments(symbols=",".join(symbols), fields=fields) return data @classmethod def get_history_tick_n(cls, code, count, fields=None, frequency="1d"): symbols = cls.get_juejin_code_list_with_prefix([code]) account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) # 前除权 results = gmapi.history_n(symbol=symbols[0], frequency=frequency, count=count, adjust=1, fields=fields) return results @classmethod def get_history_tick(cls, code, start_time, end_time, fields=None, frequency="1d"): symbols = cls.get_juejin_code_list_with_prefix([code]) account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) # 前除权 results = gmapi.history(symbol=symbols[0], frequency=frequency, start_time=start_time, end_time=end_time, adjust=1, fields=fields) return results @classmethod def get_gp_current_info(cls, codes, fields=''): if not codes: return [] symbols = cls.get_juejin_code_list_with_prefix(codes) account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) data = gmapi.current(symbols=",".join(symbols), fields=fields) return data # 获取交易所的代码 @classmethod def get_exchanges_codes(cls, exchanges): account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) return gmapi.get_instruments(exchanges=exchanges, sec_types=[1], skip_suspended=True, skip_st=True, fields="symbol,sec_type,sec_id,sec_name,listed_date,sec_level,is_suspended,pre_close") @classmethod def get_previous_trading_date(cls, date): account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) return gmapi.get_previous_trading_date("SHSE", date) @classmethod def get_previous_trading_date_cache(cls, date): if date not in cls.__previous_trading_date_cache: pre_date = cls.get_previous_trading_date(date) if pre_date: cls.__previous_trading_date_cache[date] = pre_date return cls.__previous_trading_date_cache.get(date) # 返回指定日期的下个交易日 @classmethod def get_next_trading_date(cls, date): account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) return gmapi.get_next_trading_date("SHSE", date) @classmethod def get_trading_dates(cls, start_date, end_date): account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) return gmapi.get_trading_dates("SHSE", start_date, end_date) @classmethod def get_codes(cls, sec_type): """ 代码获取 :param sec_type: 8:可转债 1:股票 :return: """ account_id, s_id, token = cls.getJueJinAccountInfo() gmapi.set_token(token) return gmapi.get_instrumentinfos(exchanges="SHSE,SZSE", sec_types=[sec_type]) @classmethod def get_underlying_code(cls, symbols): """ 获取可转债正股代码 :param symbols: 如:[SZSE.128144] :return: 正股代码,如:[SZSE.002734] """ account_id, s_id, token = JueJinApi.getJueJinAccountInfo() gmapi.set_token(token) datas = gmapi.get_instruments(symbols=",".join(symbols), sec_types="8", fields="symbol, sec_type, sec_id,sec_name, underlying_symbol") fdatas = {} for d in datas: fdatas[d['symbol']] = d['underlying_symbol'] return fdatas if __name__ == "__main__": print(JueJinApi.get_previous_trading_date("2024-12-31"))