1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
| import datetime
| import time
| import strategy.data_cache
| import gm.api as gmapi
|
| from trade import middle_api_protocol
|
|
| class JueJinHttpApi:
| @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
|
| fdata = middle_api_protocol.load_juejin(path_str, data_json)
| __start_time = time.time()
| try:
| fdata = middle_api_protocol.request(fdata)
| finally:
| __use_time = time.time() - __start_time
| if fdata:
| if type(fdata) == list:
| for d in fdata:
| if type(d) != dict:
| continue
| for k in d:
| d[k] = deformat_date(d[k])
| elif type(fdata) == dict:
| for k in fdata:
| fdata[k] = deformat_date(fdata[k])
| return fdata
| else:
| return None
|
| @classmethod
| def get_exchanges_codes(cls, exchanges, sec_types, skip_suspended, skip_st, fields):
| return cls.__request("get_instruments",
| {"exchanges": exchanges, "sec_types": sec_types, "skip_suspended": skip_suspended,
| "skip_st": skip_st, "fields": fields})
|
| @classmethod
| def current(cls, symbols, fields):
| return cls.__request("current", {"symbols": symbols, "fields": fields})
|
| @classmethod
| def history_n(cls, symbol, frequency, count, adjust, fields, end_time=None):
| params = {"symbol": symbol, "frequency": frequency, "count": count, "adjust": adjust, "fields": fields}
| if end_time:
| params["end_time"] = end_time
| params["adjust_end_time"] = end_time
| return cls.__request("history_n", params)
|
| @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:
| # 获取交易所的代码
| @classmethod
| def get_exchanges_codes(cls, exchanges, skip_suspended=True, skip_st=True):
| return JueJinHttpApi.get_exchanges_codes(exchanges=exchanges, sec_types=[1], skip_suspended=skip_suspended,
| skip_st=skip_st,
| fields="symbol,sec_type,sec_id,sec_name,listed_date,sec_level,"
| "is_suspended,pre_close")
|
| @classmethod
| def get_target_codes(cls):
| """
| 获取目标代码
| :return:
| """
| datas = cls.get_exchanges_codes(["SHSE", "SZSE"])
| fdatas = []
| for d in datas:
| if d["sec_level"] != 1:
| continue
| fdatas.append(d)
| return fdatas
|
| # 获取目标股票范围内的开盘价
| @classmethod
| def get_codes_open(cls, symbols, fields):
| current = JueJinHttpApi.current(symbols, fields)
| # print(f"current=={current}")
| # current_datas==[{'symbol': 'SZSE.001288', 'open': 30.27, 'high': 31.77, 'low': 30.27, 'price': 30.77, 'quotes': [{'bid_p': 30.77, 'bid_v': 500, 'ask_p': 30.78, 'ask_v': 3900}, {'bid_p': 30.76, 'bid_v': 800, 'ask_p': 30.79, 'ask_v': 3100}, {'bid_p': 30.75, 'bid_v': 21900, 'ask_p': 30.8, 'ask_v': 22100}, {'bid_p': 30.72, 'bid_v': 1300, 'ask_p': 30.82, 'ask_v': 300}, {'bid_p': 30.7, 'bid_v': 600, 'ask_p': 30.83, 'ask_v': 2700}], 'cum_volume': 2586914, 'cum_amount': 80020708.18, 'trade_type': 8, 'created_at': datetime.datetime(2025, 2, 12, 14, 50, 18, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))}]
| return current
|
| # 获取目标股票范围内的日内实时最高价和最低价
| @classmethod
| def get_codes_high_and_low(cls, symbols, fields):
| current = JueJinHttpApi.current(symbols, fields)
| # print(f"current=={current}")
| # current_datas==[{'symbol': 'SZSE.001288', 'open': 30.27, 'high': 31.77, 'low': 30.27, 'price': 30.77, 'quotes': [{'bid_p': 30.77, 'bid_v': 500, 'ask_p': 30.78, 'ask_v': 3900}, {'bid_p': 30.76, 'bid_v': 800, 'ask_p': 30.79, 'ask_v': 3100}, {'bid_p': 30.75, 'bid_v': 21900, 'ask_p': 30.8, 'ask_v': 22100}, {'bid_p': 30.72, 'bid_v': 1300, 'ask_p': 30.82, 'ask_v': 300}, {'bid_p': 30.7, 'bid_v': 600, 'ask_p': 30.83, 'ask_v': 2700}], 'cum_volume': 2586914, 'cum_amount': 80020708.18, 'trade_type': 8, 'created_at': datetime.datetime(2025, 2, 12, 14, 50, 18, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))}]
| return current
|
| @classmethod
| def history_n(cls, symbol, frequency, count, adjust, fields):
| return JueJinHttpApi.history_n(symbol, frequency, count, adjust, fields)
|
| @classmethod
| def history_tick_n(cls, symbol, count, end_time, adjust, fields=None, local_dwonload=True):
| if local_dwonload:
| gmapi.set_token("6c1dbe95191fb77cced9d805cb9c853805551ddb")
| return gmapi.history_n(symbol, 'tick', count, end_time=end_time, adjust_end_time=end_time, adjust=adjust, fields=fields)
| else:
| return JueJinHttpApi.history_n(symbol, 'tick', count, adjust, fields, end_time=end_time)
|
| @classmethod
| def get_previous_trading_date(cls, date):
| return JueJinHttpApi.get_previous_trading_date("SHSE", date)
|
| # 返回指定日期的下个交易日
| @classmethod
| def get_next_trading_date(cls, date):
| return JueJinHttpApi.get_next_trading_date("SHSE", date)
|
| @classmethod
| def get_trading_dates(cls, start_date, end_date):
| return JueJinHttpApi.get_trading_dates("SHSE", start_date, end_date)
|
| # if __name__ == '__main__':
| # datas = JueJinApi.history_n("SHSE.000300", "1d", 10, 1, "high")
| # print(datas)
|
| # 获取目标代码(获取目标票)
| # print(f"JueJinApi.get_exchanges_codes==={JueJinApi.get_exchanges_codes(['SHSE', 'SZSE'])}")
| # symbols = ['SZSE.001288', 'SZSE.000042']
| # fields = 'symbol,open'
| # JueJinApi.get_codes_open(symbols, fields)
| #
| # strategy.data_cache.all_stocks = JueJinApi.get_exchanges_codes(["SHSE", "SZSE"])
| # current = JueJinHttpApi.current(symbols, fields)
| # print(current)
| # print(f"JueJinApi.get_exchanges_codes(['SHSE', 'SZSE'])=={len(JueJinApi.get_exchanges_codes(['SHSE', 'SZSE']))}")
|
|