| | |
| | | |
| | | import setting |
| | | import trade_gui |
| | | from convertible_bonds.main import JueJinUtil |
| | | from juejin_core import JueJinApi |
| | | from kpl import kpl_util, kpl_api |
| | | from kpl.kpl_data_manager import KPLLimitUpDataManager |
| | | from utils import tool, xgb_api |
| | |
| | | elif url.startswith("/kpl/get_plate_codes"): |
| | | return cls.__get_plate_codes(url) |
| | | |
| | | elif url.startswith("/get_last_trade_day_reasons"): |
| | | return cls.__get_last_trade_day_reasons(url) |
| | | |
| | | elif url.startswith("/get_xgb_limit_up_reasons"): |
| | | return cls.__get_xgb_limit_up_reasons(url) |
| | | |
| | | elif url.startswith("/get_kpl_market_feelings"): |
| | | return cls.__get_kpl_market_feelings() |
| | | elif url.startswith("/get_cb_list"): |
| | | # 获取可转债的列表 |
| | | return cls.get_cb_list() |
| | | elif url.startswith("/buy_by_ths"): |
| | | # 获取可转债的列表 |
| | | return cls.buy_by_ths(url) |
| | |
| | | response_data = json.dumps({"code": 0, "data": results}) |
| | | return response_data, True |
| | | |
| | | @classmethod |
| | | def get_codes_limit_rate(cls, codes): |
| | | return cls.__get_codes_limit_rate(codes) |
| | | |
| | | # 获取昨日收盘价 |
| | | __pre_close_cache = {} |
| | | |
| | | @classmethod |
| | | def __get_codes_limit_rate(cls, codes): |
| | | latest_info_codes = set() |
| | | for code in codes: |
| | | if code not in cls.__pre_close_cache: |
| | | latest_info_codes.add(code) |
| | | if latest_info_codes: |
| | | datas = JueJinApi.get_gp_latest_info(latest_info_codes, fields="sec_id,pre_close") |
| | | for data in datas: |
| | | code = data["sec_id"] |
| | | pre_close = data['pre_close'] |
| | | cls.__pre_close_cache[code] = pre_close |
| | | |
| | | current_info = JueJinApi.get_gp_current_info(codes, fields="symbol,price") |
| | | now_prices = [(c["symbol"].split(".")[1], c["price"]) for c in current_info] |
| | | f_results = [] |
| | | for data in now_prices: |
| | | code = data[0] |
| | | price = data[1] |
| | | pre_price = float(cls.__pre_close_cache.get(code)) |
| | | rate = round((price - pre_price) * 100 / pre_price, 2) |
| | | f_results.append((code, rate)) |
| | | f_results.sort(key=lambda tup: tup[1]) |
| | | f_results.reverse() |
| | | return f_results |
| | | |
| | | @classmethod |
| | | def __get_limit_rate_list(cls, codes): |
| | | if not codes: |
| | | return [] |
| | | need_request_codes = set() |
| | | if tool.trade_time_sub(tool.get_now_time_str(), "09:30:00") < 0: |
| | | need_request_codes |= set(codes) |
| | | else: |
| | | now_time = time.time() |
| | | for c in codes: |
| | | if c not in cls.__code_limit_rate_dict: |
| | | need_request_codes.add(c) |
| | | elif now_time - cls.__code_limit_rate_dict[c][1] > 60: |
| | | need_request_codes.add(c) |
| | | if need_request_codes: |
| | | _limit_rate_list = cls.__get_codes_limit_rate(list(need_request_codes)) |
| | | for d in _limit_rate_list: |
| | | cls.__code_limit_rate_dict[d[0]] = (d[1], time.time()) |
| | | return [(c_, cls.__code_limit_rate_dict[c_][0]) for c_ in codes] |
| | | |
| | | @classmethod |
| | | def __get_last_trade_day_reasons(cls, url): |
| | | # 获取 昨日涨停数据 |
| | | ps_dict = dict([(k, v[0]) for k, v in parse_qs(urlparse(url).query).items()]) |
| | | code = ps_dict["code"] |
| | | # 获取昨日涨停数据 |
| | | day = JueJinApi.get_previous_trading_date_cache(tool.get_now_date_str()) |
| | | # (代码, 名称, 首次涨停时间, 最近涨停时间, 几板, 涨停原因, 板块, 实际流通, 主力净额, 涨停原因代码, 涨停原因代码数量) |
| | | limit_up_records = KPLLimitUpDataManager().get_limit_up_history_datas_cache(day) |
| | | if not limit_up_records: |
| | | limit_up_records = [] |
| | | reasons = [] |
| | | for d in limit_up_records: |
| | | if d[0] == code: |
| | | reasons.append(d) |
| | | # 获取代码的原因 |
| | | if reasons: |
| | | reasons = list(reasons) |
| | | reasons.sort(key=lambda x: x[9]) |
| | | reason = reasons[-1][5] |
| | | # 获取涨停数据 |
| | | datas = KPLLimitUpDataManager().get_limit_up_current_datas_cache(day) |
| | | # (代码,名称,首次涨停时间,最近涨停时间,几板,涨停原因,板块,实际流通,主力净额,涨停原因代码,涨停原因代码数量) |
| | | yesterday_result_list = [] |
| | | percent_rate = 0 |
| | | if datas: |
| | | yesterday_codes = set() |
| | | for d in datas: |
| | | if d[5] == reason: |
| | | yesterday_codes.add(d[0]) |
| | | # 获取涨幅 |
| | | limit_rate_list = cls.__get_limit_rate_list(yesterday_codes) |
| | | limit_rate_dict = {} |
| | | if limit_rate_list: |
| | | total_rate = 0 |
| | | for d in limit_rate_list: |
| | | limit_rate_dict[d[0]] = d[1] |
| | | total_rate += d[1] |
| | | percent_rate = round(total_rate / len(limit_rate_list), 2) |
| | | |
| | | for d in datas: |
| | | if d[5] == reason: |
| | | yesterday_codes.add(d[0]) |
| | | if d[0] != code: |
| | | # (代码,名称, 涨幅) |
| | | yesterday_result_list.append((d[0], d[1], limit_rate_dict.get(d[0]))) |
| | | |
| | | current_limit_up_list = KPLLimitUpDataManager().get_limit_up_current_datas() |
| | | current_result_list = [] |
| | | if current_limit_up_list: |
| | | for c in current_limit_up_list: |
| | | if c[5] == reason and c[0] != code: |
| | | current_result_list.append((c[0], c[1])) |
| | | response_data = json.dumps({"code": 0, "data": {"reason": reason, "reason_rate": percent_rate, |
| | | "data": {"yesterday": yesterday_result_list, |
| | | "current": current_result_list}}}) |
| | | else: |
| | | response_data = json.dumps({"code": 1, "msg": "昨日未涨停"}) |
| | | |
| | | return response_data, True |
| | | |
| | | @classmethod |
| | | def __get_xgb_limit_up_reasons(cls, url): |
| | |
| | | __cb_codes_list = None # 所有可转债数据 |
| | | __pre_price_dict = {} # 昨日收盘价 |
| | | |
| | | # 获取可转债的列表 |
| | | @classmethod |
| | | def get_cb_list(cls): |
| | | # 获取所有可转债列表 |
| | | if not cls.__cb_codes_list: |
| | | cls.__cb_codes_list = JueJinUtil.get_all_convertible_bonds_codes() |
| | | # 获取昨日收盘价 |
| | | symbols = [x[0][0] for x in cls.__cb_codes_list] |
| | | symbols.extend([x[1] for x in cls.__cb_codes_list]) |
| | | # 获取现价 |
| | | results = JueJinApi.get_gp_latest_info(symbols, fields='symbol,pre_close') |
| | | cls.__pre_price_dict = {x['symbol']: x['pre_close'] for x in results} |
| | | |
| | | # 获取涨停列表 |
| | | records = KPLLimitUpDataManager().get_limit_up_history_datas() |
| | | currents = KPLLimitUpDataManager().get_limit_up_current_datas() |
| | | current_codes = [d[0] for d in currents] |
| | | record_codes = [d[0] for d in records] |
| | | record_map = {} |
| | | for r in records: |
| | | record_map[r[0]] = r |
| | | # 格式[([转债代码,转债名称, (现价,涨幅)],[正股代码,正股名称,(现价,涨幅)],正股涨停时间,是否炸板)] |
| | | datas = [] |
| | | for b in cls.__cb_codes_list: |
| | | code = b[1].split(".")[1] |
| | | if code in record_codes: |
| | | if record_map[code][4] != '首板': |
| | | continue |
| | | datas.append( |
| | | [[b[0][0], b[0][1], None], [b[1], record_map[code][1], None], tool.time_format(record_map[code][2]), |
| | | code not in current_codes]) |
| | | # 获取所有的涨幅 |
| | | symbols = [] |
| | | for d in datas: |
| | | symbols.append(d[0][0]) |
| | | symbols.append(d[1][0]) |
| | | # 获取当前 |
| | | current_infos = JueJinApi.get_gp_current_info(symbols, fields='symbol,price') |
| | | rate_dict = {x['symbol']: (x['price'], round( |
| | | (x['price'] - cls.__pre_price_dict[x['symbol']]) * 100 / cls.__pre_price_dict[x['symbol']], 2)) for x in |
| | | current_infos} |
| | | for d in datas: |
| | | d[0][2] = rate_dict[d[0][0]] |
| | | d[1][2] = rate_dict[d[1][0]] |
| | | d[0][0] = d[0][0].split('.')[1] |
| | | d[1][0] = d[1][0].split('.')[1] |
| | | datas.sort(key=lambda x: int(x[2].replace(":", "")), reverse=True) |
| | | |
| | | # 获取今日正股涨停的可转债列表 |
| | | return json.dumps( |
| | | {"code": 0, "data": datas}), True |
| | | |
| | | @classmethod |
| | | def buy_by_ths(cls, url): |
| | | can_buy = setting.is_can_ths_buy() |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | codes = ["002523", "603095"] |
| | | datas = LocalKanPanNetworkDelegate.get_cb_list() |
| | | print(datas) |
| | | pass |