场内的代码表, 感觉水很深

场内的代码表, 感觉水很深

写了一个爬取代码表的小爬虫, 很久以前的事了.
现在想好好分析一下, 代码的分类, 又写了一个统计函数. 分别统计
7个不同字头的代码里, 分别有多少只品种.

先上菜:

代码运行结果(cmd窗口里启动Ipython的场景):

spyder的Ipython窗口里的场景: 想看看他们之间在博客里的展现有何不同:
备注: 这次spyder的字体改成了: DFKai_SB
因为新宋体确实没法用: 该字体在代码窗口里的花括号太难看了, 以至于无法与方括号无异
所以不得不放弃新宋体.

然后再现上厨艺


def get_codelist():
    cn_dict = p_load('d:/db/amipy/data/codename_dict.pkl')
    lst = [c for c in cn_dict.keys()]
    lst.sort()
    return lst

def get_xzt_security_num(codelist,xzt='1'):
    u'''
    检索代码列表, 得到'1'字头的品种的数量
    >>> codelist = get_codelist()
    >>> get_xzt_security_num(codelist, '0')
    '''
    count = 0
    for c in codelist:
        if c.startswith(xzt):
            count +=1
    return count

def stats_security_num(codelist, dt='2018-03-01'):
    u'''
    分别统计某个字头的代码的品种个数
    Examples:
    >>> clist = get_codelist()
    >>> stats_security_num(clist)
    
    '''
    print('沪深两市证券代码表采集日期: ', dt)
    print('Number of securities in both market: {}'.format(len(codelist)))
    # 4237
    prefix = ['0', '1', '2', '3', '5', '6', '9',]
    nums=[]
    total=0
    for pre in prefix:
        num=get_xzt_security_num(codelist, pre)
        nums.append(num)
        total += num
    print("沪深市场里, 证券品种的总数量是: ", total)
    
    for i in range(len(nums)):
        print('沪深两个市场里, "{}"字头的证券数量是: {} '.format(prefix[i], nums[i]))
        

然后再顺便说一下: cmd窗口里如何如何启动Ipython环境:

代码表的更新:

利用rainx的pytdx可以方便地获取当天的代码表.
特别感谢rainx的奉献!

代码:

def test_get_s_list(mkt_id=0, show=True):
    '''
    >>> codelist0 = test_get_s_list(0)        
    >>> codelist1 = test_get_s_list(1)        
    '''
    api = TdxHq_API(**param)
    with api.connect(ip='123.125.108.90', port=7709):
        total = api.get_security_count(mkt_id)
        tmp='深' if mkt_id==0 else '沪'
        print('{}市里的总个数: '.format(tmp), total, '整除1000的值: ',total//1000)
        clist=[]
        print('  Num 本次里的品种个数')
        print('start counts')
        print('-'*40)
        for i in range(0, (total//1000+1)*1000,1000):
            li = api.get_security_list(mkt_id, i)
            print('{:5d} {:4d}'.format(i, len(li)))
            for j in range(len(li)):
                clist.append(li[j])
        
        if show:
            print('{} {} {}'.format('  sn.', 'Code  ', 'Name'))
            print('-'*50)
            #for i in [0,1,2]:
            for i in range(len(clist)):
                if i%100==0:
                    print('{0:5d} {1[code]:} {1[name]:} '.format( i, clist[i]))
        
        
        # =============================================================================
        # d0[2]
        # Out[94]: 
        # OrderedDict([('code', '999997'),
        #              ('volunit', 100),
        #              ('decimal_point', 2),
        #              ('name', 'B股指数'),
        #              ('pre_close', 286.32940673828125)])
        #         
        # =============================================================================
        return clist

结果图:

原文地址:https://www.cnblogs.com/duan-qs/p/10409971.html