| | |
| | | """ |
| | | 历史K线服务 |
| | | """ |
| | | import datetime |
| | | import decimal |
| | | import json |
| | | |
| | | import tool |
| | | import requests |
| | | |
| | | import constant |
| | | from utils import tool |
| | | from db import redis_manager |
| | | import gm.api as gmapi |
| | | |
| | | |
| | | class JueJinHttpApi: |
| | | __BASE_URL = "http://193.112.35.168:10009/" |
| | | |
| | | @classmethod |
| | | def __request(cls, path_str, data_json): |
| | | def deformat_date(val): |
| | | if type(val) == str and val.find('T') > -1 and val.find(':') > -1 and val.find( |
| | | '+') > -1: |
| | | return datetime.datetime.fromisoformat(val) |
| | | return val |
| | | |
| | | url = f'{cls.__BASE_URL}{path_str}' |
| | | # 发送POST请求 |
| | | response = requests.post(url, json=data_json) |
| | | result = response.text |
| | | resultJson = json.loads(result) |
| | | if resultJson['code'] == 0: |
| | | data = resultJson['data'] |
| | | if type(data) == list: |
| | | for d in data: |
| | | if type(d) != dict: |
| | | continue |
| | | for k in d: |
| | | d[k] = deformat_date(d[k]) |
| | | elif type(data) == dict: |
| | | for k in data: |
| | | data[k] = deformat_date(data[k]) |
| | | return data |
| | | |
| | | return None |
| | | |
| | | @classmethod |
| | | def get_instruments(cls, symbols, fields): |
| | | return cls.__request("get_instruments", {"symbols": symbols, "fields": fields}) |
| | | |
| | | @classmethod |
| | | def history_n(cls, symbol, frequency, count, adjust, fields): |
| | | return cls.__request("history_n", {"symbol": symbol, "frequency": frequency, "count": count, "adjust": adjust, |
| | | "fields": fields}) |
| | | |
| | | @classmethod |
| | | def current(cls, symbols, fields): |
| | | return cls.__request("current", {"symbols": symbols, "fields": fields}) |
| | | |
| | | @classmethod |
| | | def get_previous_trading_date(cls, exchange, date): |
| | | return cls.__request("get_previous_trading_date", {"exchange": exchange, "date": date}) |
| | | |
| | | @classmethod |
| | | def get_next_trading_date(cls, exchange, date): |
| | | return cls.__request("get_next_trading_date", {"exchange": exchange, "date": date}) |
| | | |
| | | @classmethod |
| | | def get_trading_dates(cls, exchange, start_date, end_date): |
| | | return cls.__request("get_trading_dates", |
| | | {"exchange": exchange, "start_date": start_date, "end_date": end_date}) |
| | | |
| | | |
| | | class JueJinApi: |
| | |
| | | def get_gp_latest_info(cls, codes, fields=None): |
| | | if not codes: |
| | | return [] |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | symbols = cls.get_juejin_code_list_with_prefix(codes) |
| | | gmapi.set_token(token) |
| | | data = gmapi.get_instruments(symbols=",".join(symbols), fields=fields) |
| | | print(data) |
| | | return data |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | data = gmapi.get_instruments(symbols=",".join(symbols), fields=fields) |
| | | return data |
| | | else: |
| | | return JueJinHttpApi.get_instruments(symbols=",".join(symbols), fields=fields) |
| | | |
| | | @classmethod |
| | | def get_history_tick_n(cls, code, count, fields=None): |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | symbols = cls.get_juejin_code_list_with_prefix([code]) |
| | | gmapi.set_token(token) |
| | | # 前除权 |
| | | results = gmapi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) |
| | | return results |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | # 前除权 |
| | | results = gmapi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) |
| | | return results |
| | | else: |
| | | results = JueJinHttpApi.history_n(symbol=symbols[0], frequency="1d", count=count, adjust=1, fields=fields) |
| | | return results |
| | | |
| | | @classmethod |
| | | def get_gp_current_info(cls, codes): |
| | | if not codes: |
| | | return [] |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | symbols = cls.get_juejin_code_list_with_prefix(codes) |
| | | gmapi.set_token(token) |
| | | data = gmapi.current(symbols=",".join(symbols)) |
| | | print(data) |
| | | return data |
| | | |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | data = gmapi.current(symbols=",".join(symbols)) |
| | | return data |
| | | else: |
| | | data = JueJinHttpApi.current(symbols=",".join(symbols), fields='') |
| | | return data |
| | | # 返回指定日期的上个交易日 |
| | | |
| | | @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) |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | return gmapi.get_previous_trading_date("SHSE", date) |
| | | else: |
| | | return JueJinHttpApi.get_previous_trading_date("SHSE", date) |
| | | |
| | | # 返回指定日期的下个交易日 |
| | | @classmethod |
| | | def get_next_trading_date(cls, date): |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | return gmapi.get_previous_trading_date("SHSE", date) |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | return gmapi.get_next_trading_date("SHSE", date) |
| | | else: |
| | | return JueJinHttpApi.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) |
| | | if constant.JUEJIN_LOCAL_API: |
| | | account_id, s_id, token = cls.getJueJinAccountInfo() |
| | | gmapi.set_token(token) |
| | | return gmapi.get_trading_dates("SHSE", start_date, end_date) |
| | | else: |
| | | return JueJinHttpApi.get_trading_dates("SHSE", start_date, end_date) |
| | | |
| | | |
| | | class HistoryKDatasUtils(object): |
| | |
| | | code_name = data['sec_name'] |
| | | results[code] = code_name |
| | | return results |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | print(JueJinApi.get_gp_current_info(["000333", "600686"])) |