行情统计模块分享

策略分享
标签: #<Tag:0x00007f20c9ba3e98>

(XiaoyuDu) #1

做了一个可视化的行情统计模块分享给大家,既可以用于看盘,也可以用导出的数据进行进一步分析:

1. 统计每日涨停跌停股票,和突破K日内高/低点的股票
2. 申万宏源行业涨幅跌幅排序
3. 输入申万宏源行业号,获取该行业内股票涨跌幅排序

如果想要获取模块的原始代码可以输入下面的代码:


点击查看
print(M.Show_industry_rank.v3.m_sourcecode[0][1])

代码解释:
M(总模块).Show_industry_rank(函数部分).v3(版本,更新几次).m_sourcecode(取原始代码)[0][1]


下面是使用指南:

1. 第一步:获取当日的股票行情数据和计算k日突破所需的数据:

image
我们需要严格按格式(YYYY-MM-DD)输入需要查询的日期和所需要考虑的k日(k个真实的交易日,不交易的时间被排除了),当日休息无交易时系统会返回报错,并提示如下:当天没有交易数据
image

第一步于data_1输出股票行情数据,data_2输出查询日期,data_3输出回溯天数

2. 第二步:统计查询日期涨停跌停和突破K日最高最低的股票:

查询结果如图所示,没有参数

本部分输出的是一个字典,可以通过调用输出接口获得:

3. 第三步:统计查询日期特定行业股票涨跌幅排名:

image

输入参数分别为:
port:我们统计的股票占该行业内股票的百分比(你需要统计前port%的股票)
Code: 行业代码(整数格式)
type1: 如果需要涨幅最高,则用参数’Desc’,如果需要跌幅最高,则用参数’Asc’
n: 这里的n是n日收益率,用户可以查询不仅仅是当日收益率。

image
如图,我们获得了,410000行业的10日最高回报排行榜。

输出端:
data_1: 数据表 data_2:行业代码(整数) data_3: 排序方式(Desc/Asc)

4. 第四步:统计查询日期行业总涨幅:

运行结果如图所示:
image

输入参数:image
type1: 排序方式(Desc:取最高 / Asc:取最低)
n1: 我们想知道的行业数量

输出接口分别为: data_1:排序后的列表,data_2:排序方式,data_3:展示的行业数量


策略分享如下:

克隆策略

    {"Description":"实验创建于2019/1/9","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-47:input_2","SourceOutputPortId":"-34:data_1"},{"DestinationInputPortId":"-24:input_1","SourceOutputPortId":"-67:data_1"},{"DestinationInputPortId":"-24:input_2","SourceOutputPortId":"-67:data_2"},{"DestinationInputPortId":"-34:today","SourceOutputPortId":"-67:data_2"},{"DestinationInputPortId":"-47:input_1","SourceOutputPortId":"-67:data_2"},{"DestinationInputPortId":"-88:input_1","SourceOutputPortId":"-67:data_2"},{"DestinationInputPortId":"-24:input_3","SourceOutputPortId":"-67:data_3"}],"ModuleNodes":[{"Id":"-24","ModuleId":"BigQuantSpace.Find_the_top_stocks.Find_the_top_stocks-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-24"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-24"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-24"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-34","ModuleId":"BigQuantSpace.Get_Industry_return.Get_Industry_return-v5","ModuleParameters":[{"Name":"n_day","Value":"1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"today","NodeId":"-34"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-34","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-67","ModuleId":"BigQuantSpace.Get_Data.Get_Data-v7","ModuleParameters":[{"Name":"today","Value":"2019-01-07","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"k","Value":"250","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-67","OutputType":null},{"Name":"data_2","NodeId":"-67","OutputType":null},{"Name":"data_3","NodeId":"-67","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-47","ModuleId":"BigQuantSpace.Show_industry_rank.Show_industry_rank-v3","ModuleParameters":[{"Name":"type1","Value":"Desc","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n1","Value":"15","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-47"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-47"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-47","OutputType":null},{"Name":"data_2","NodeId":"-47","OutputType":null},{"Name":"data_3","NodeId":"-47","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-88","ModuleId":"BigQuantSpace.Industry_top_stocks.Industry_top_stocks-v8","ModuleParameters":[{"Name":"port","Value":"0.1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"Code","Value":"410000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"type1","Value":"Desc","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"n","Value":"20","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-88"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-88","OutputType":null},{"Name":"data_2","NodeId":"-88","OutputType":null},{"Name":"data_3","NodeId":"-88","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-24' Position='-258,79,200,200'/><NodePosition Node='-34' Position='1152,-493,200,200'/><NodePosition Node='-67' Position='-121,-575,200,200'/><NodePosition Node='-47' Position='1295,250,200,200'/><NodePosition Node='-88' Position='211,17,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [16]:
    # 本代码由可视化策略环境自动生成 2019年1月10日 15:38
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m7 = M.Get_Data.v7(
        today='2019-01-07',
        k=250
    )
    
    m5 = M.Get_Industry_return.v5(
        today=m7.data_2,
        n_day=1
    )
    
    m3 = M.Show_industry_rank.v3(
        input_1=m7.data_2,
        input_2=m5.data_1,
        type1='Desc',
        n1=15
    )
    
    m2 = M.Industry_top_stocks.v8(
        input_1=m7.data_2,
        port=0.1,
        Code=410000,
        type1='Desc',
        n=20
    )
    
    m1 = M.Find_the_top_stocks.v2(
        input_1=m7.data_1,
        input_2=m7.data_2,
        input_3=m7.data_3
    )
    
    [2019-01-10 15:35:51.751684] INFO: bigquant: Get_Data.v7 开始运行..
    [2019-01-10 15:35:57.219494] WARNING: bigquant: 返回的Outputs实例(size=280133300) 过大,将不会被缓存。
    观测日期
    2019-01-07
    [2019-01-10 15:35:57.264100] INFO: bigquant: Get_Data.v7 运行完成[5.512391s].
    [2019-01-10 15:35:57.298224] INFO: bigquant: Get_Industry_return.v5 开始运行..
    [2019-01-10 15:35:57.313808] INFO: bigquant: 命中缓存
    [2019-01-10 15:35:57.314807] INFO: bigquant: Get_Industry_return.v5 运行完成[0.016692s].
    [2019-01-10 15:35:57.318600] INFO: bigquant: Show_industry_rank.v3 开始运行..
    [2019-01-10 15:35:57.324261] INFO: bigquant: 命中缓存
    展示前15个 收益率最高的行业指数 
              date    return          symbol    instrument
    137 2019-01-07  0.043420       国防军工(申万1)  SW650000.HIX
    27  2019-01-07  0.032569       电气设备(申万1)  SW630000.HIX
    97  2019-01-07  0.031917         通信(申万1)  SW730000.HIX
    52  2019-01-07  0.028651         电子(申万1)  SW270000.HIX
    107 2019-01-07  0.027628        计算机(申万1)  SW710000.HIX
    112 2019-01-07  0.023405       机械设备(申万1)  SW640000.HIX
    42  2019-01-07  0.023403       农林牧渔(申万1)  SW110000.HIX
    87  2019-01-07  0.019424         综合(申万1)  SW510000.HIX
    67  2019-01-07  0.018493       有色金属(申万1)  SW240000.HIX
    62  2019-01-07  0.018166  建筑材料,建筑建材(申万1)  SW610000.HIX
    57  2019-01-07  0.017067    传媒,信息服务(申万1)  SW720000.HIX
    7   2019-01-07  0.015086         化工(申万1)  SW220000.HIX
    82  2019-01-07  0.014915         采掘(申万1)  SW210000.HIX
    22  2019-01-07  0.014481    汽车,交运设备(申万1)  SW280000.HIX
    37  2019-01-07  0.014170       休闲服务(申万1)  SW460000.HIX
    [2019-01-10 15:35:57.346456] INFO: bigquant: Show_industry_rank.v3 运行完成[0.027833s].
    [2019-01-10 15:35:57.350038] INFO: bigquant: Industry_top_stocks.v8 开始运行..
    [2019-01-10 15:35:57.355803] INFO: bigquant: 命中缓存
    410000行业 最高回报
            instrument    return
    867528  000966.SZA  0.179012
    867843  000035.SZA  0.179105
    868043  000539.SZA  0.095588
    868127  300140.SZA  0.087425
    868540  600863.SHA  0.081818
    868655  600900.SHA  0.071730
    868675  300125.SZA  0.071698
    868691  600025.SHA  0.104730
    868822  600310.SHA  0.095588
    869095  000899.SZA  0.084668
    869496  603603.SHA  0.148536
    869702  000068.SZA  0.208571
    870332  600236.SHA  0.071429
    870536  600021.SHA  0.098573
    870543  603200.SHA  0.150993
    [2019-01-10 15:35:57.359450] INFO: bigquant: Industry_top_stocks.v8 运行完成[0.009407s].
    [2019-01-10 15:36:01.809481] INFO: bigquant: Find_the_top_stocks.v2 开始运行..
    [2019-01-10 15:36:01.845753] INFO: bigquant: 命中缓存
    当日跌停
    Series([], Name: instrument, dtype: float64)
    当日涨停
    1666815    002017.SZA
    1667187    000595.SZA
    1667335    600776.SHA
    1667441    300035.SZA
    1668074    600831.SHA
    1668277    300615.SZA
    1668543    300008.SZA
    1668708    600595.SHA
    1669884    600862.SHA
    1669963    000633.SZA
    Name: instrument, dtype: object
    k日最高
    1666815    002017.SZA
    1667049    300628.SZA
    1667104    600548.SHA
    1667329    002115.SZA
    1667335    600776.SHA
    1667516    300498.SZA
    1667533    300265.SZA
    1667589    002124.SZA
    1667648    000429.SZA
    1667671    002811.SZA
    1667672    300087.SZA
    1667674    600242.SHA
    1667697    600677.SHA
    1668277    300615.SZA
    1668320    600406.SHA
    1668914    000750.SZA
    1668973    002776.SZA
    1669212    002414.SZA
    Name: instrument, dtype: object
    k日最低
    1667759    000693.SZA
    1668356    000995.SZA
    1668758    000792.SZA
    1668818    600771.SHA
    1668916    000028.SZA
    1668952    600401.SHA
    1669166    002343.SZA
    Name: instrument, dtype: object
    [2019-01-10 15:36:01.852969] INFO: bigquant: Find_the_top_stocks.v2 运行完成[0.043539s].
    
    In [31]:
    print(M.Industry_top_stocks.v3.m_sourcecode[0][1])
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def bigquant_run(input_1,port,Code,type1,n):
        
        today = input_1
        today_datetime = datetime.datetime.strptime(today,"%Y-%m-%d")
        past_datetime = today_datetime - datetime.timedelta(days=n+3)
        past = past_datetime.strftime("%Y-%m-%d")
        
        # we get the timeseries for stocks in specified industry, n is the n days return we are calculating
        # Code here is the number for the SW code
        # We first read the index_securities mapping tables
        
        X=DataSource('industry_CN_STOCK_A').read()
        
        # Then we find the latest mapping rule
        T=X.groupby('instrument').apply(lambda x:x.iloc[-1,]['industry_sw_level1'])
        
        # Then we find the securities for the specified SW code
        t=list(T.index[T.values==Code])
        
        # With these securities codes we can now get the timeseries from the datasource
        Y=DataSource('bar1d_CN_STOCK_A').read(start_date=past,end_date=today,instruments=t)
    
        
        def calcu_label1(A,n):
            A['return'] =A['close']/A['close'].shift(n) -1
            return A    
        # Using the apply function to calculate the historical return for all the securities respectly
        
        Y = Y.groupby('instrument').apply(lambda x:calcu_label1(x,n))
        
        Y = Y[Y['date']==today]
        
        if(type1=='Asc'):
            
            Y=Y.sort_values(by='return',ascending=True)
            
        if(type1=='Desc'):
            
            Y=Y.sort_values(by='return',ascending=False)
            
        N=int(port*len(Y))
        Y = Y.iloc[0:N,]
        print(type(Y))
        Y = Y[['instrument','return']].sort_values(by='return',ascending=False)
        return Outputs(data_1=Y, data_2=Code, data_3=type1)
    
    
    
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def bigquant_postrun(outputs):
        
        if outputs.data_3 == 'Desc':
            
            S = '最高回报'
        if outputs.data_3 == 'Asc':
            
            S = '最低回报'
        
        print('%s行业 %s' %(str(outputs.data_2),S))
        
        print(outputs.data_1)
        return outputs
        return outputs
    
    
    In [20]:
    m1.data_1['Max_k'].head()
    
    Out[20]:
    High_price Low_price adjust_factor amount close date deal_number high instrument low open turn volume
    1666815 59.396606 25.845125 5.242419 604017817 59.396606 2019-01-07 49457 59.396606 002017.SZA 52.424191 52.948433 15.953250 55056864.0
    1667049 322.820801 199.830246 4.067290 118474111 322.820801 2019-01-07 5993 322.820801 300628.SZA 308.463287 309.032684 2.028471 1514862.0
    1667104 20.045507 16.590126 2.146200 33793392 20.045507 2019-01-07 6989 20.066971 600548.SHA 19.444572 19.594807 0.255325 3659502.0
    1667329 54.063572 33.332546 4.734113 580336469 54.063572 2019-01-07 39507 54.868370 002115.SZA 49.944893 50.181599 14.192703 51877768.0
    1667335 68.846848 17.896402 4.722006 1381987935 68.846848 2019-01-07 82351 68.846848 600776.SHA 62.566578 62.566578 10.257711 98063726.0
    In [21]:
    m1.data_1['Min_k'].head()
    
    Out[21]:
    High_price Low_price adjust_factor amount close date deal_number high instrument low open turn volume
    1667759 26.869375 7.115011 2.149550 0 7.115011 2019-01-07 0 7.115011 000693.SZA 7.115011 7.115011 0.000000 0.0
    1668356 24.070496 7.669521 1.685609 34394348 7.669521 2019-01-07 2378 7.720089 000995.SZA 7.467248 7.652665 4.296768 7622701.0
    1668758 120.106827 49.221695 7.368517 283028260 49.221695 2019-01-07 17316 50.179600 000792.SZA 48.632214 50.179600 1.541708 42395417.0
    1668818 333.693665 142.943115 5.323766 75720730 142.943115 2019-01-07 3015 145.338806 600771.SHA 141.771881 144.806442 0.929364 2809515.0
    1668916 249.848358 156.856415 3.903843 65831768 156.856415 2019-01-07 3163 158.027557 000028.SZA 155.958527 158.027557 0.532794 1639643.0
    In [22]:
    m1.data_1['Up_limit'].head()
    
    Out[22]:
    adjust_factor amount close date deal_number high instrument low open return turn volume
    1666815 5.242419 604017817 59.396606 2019-01-07 49457 59.396606 002017.SZA 52.424191 52.948433 0.121782 15.953250 55056864.0
    1667187 4.958490 32737833 14.825885 2019-01-07 3308 14.825885 000595.SZA 13.387923 13.487093 0.099265 1.500009 11314393.0
    1667335 4.722006 1381987935 68.846848 2019-01-07 82351 68.846848 600776.SHA 62.566578 62.566578 0.100377 10.257711 98063726.0
    1667441 8.304471 211052471 44.428921 2019-01-07 12224 44.428921 300035.SZA 39.861462 40.359730 0.100823 10.276053 40089951.0
    1668074 6.391206 452262132 48.637077 2019-01-07 26206 48.637077 600831.SHA 43.396290 43.779762 0.110949 10.891195 61365192.0
    In [24]:
    m1.data_1['Down_limit'].head()
    
    Out[24]:
    adjust_factor amount close date deal_number high instrument low open return turn volume