admin
2025-01-17 0f9b7eeb8891e62510add7dd91bd21c04a34828c
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
import datetime
import time
import strategy.data_cache
 
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})
 
 
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 = JueJinApi.get_exchanges_codes(["SHSE", "SZSE"])
        fdatas = []
        for d in datas:
            if d["sec_level"] != 1:
                continue
            fdatas.append(d)
        return fdatas
 
 
 
if __name__ == '__main__':
    # 获取目标代码(获取目标票)
    print(JueJinApi.get_exchanges_codes(["SHSE", "SZSE"]))
    #
    strategy.data_cache.all_stocks = JueJinApi.get_exchanges_codes(["SHSE", "SZSE"])
    print(len(JueJinApi.get_exchanges_codes(["SHSE", "SZSE"])))