Beispiel #1
0
def test_gta():
    from jqdata import gta, query
    df = gta.run_query(query(gta.STK_STOCKINFO))
    assert len(df.index) == 3000
    df = gta.run_query(gta.query(gta.STK_STOCKINFO).limit(10))
    assert isinstance(df, pd.DataFrame)
    assert len(df.index) == 10
    pass
Beispiel #2
0
def dividend_rate(stock, end_date=None, paymentdate=2015, start_date=None, count=1, date=None,  no_data_return=NaN, skip_paused=False):
    def get_div(df, paymentdate):
        df.PAYMENTDATE = pd.to_datetime(df.PAYMENTDATE)
        try:
            if len(df)>0:
                div = df[[x.year == (int(paymentdate)+1) for x in df.PAYMENTDATE]]['DIVIDENTBT'].iloc[-1]
                return float(div)
            else:
                return no_data_return
        except:
            return no_data_return
    
    symbol = stock[:6]
    df = gta.run_query(query(
            gta.STK_MKT_DIVIDENT.SYMBOL,

            gta.STK_MKT_DIVIDENT.PAYMENTDATE,
            gta.STK_MKT_DIVIDENT.DIVIDENTBT,
            ).filter(gta.STK_MKT_DIVIDENT.SYMBOL.in_([symbol])
            ).order_by(gta.STK_MKT_DIVIDENT.PAYMENTDATE))
    div = get_div(df, paymentdate)
    from math import isnan
    if isnan(div) == True:
        div = get_div(df, paymentdate-1)
    df = get_price(stock, start_date=start_date, count=count, end_date=end_date, fields=['close'], frequency='daily', fq=None)
    df['dividend_rate'] = float(div)/df['close']*100
    df['dividend_rate'] = df['dividend_rate'].round(4)
    del df['close']
    df.index = [stock]
    return df
Beispiel #3
0
    def fun_get_Divid_by_year(self, context, stocks):
        year = context.current_dt.year - 1
        #将当前股票池转换为国泰安的6位股票池
        stocks_symbol = []
        for s in stocks:
            stocks_symbol.append(s[0:6])

        df = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  # 股票代码
                gta.STK_DIVIDEND.DECLAREDATE,  # 分红消息的时间
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year,
                gta.STK_DIVIDEND.TERMCODE == 'P2702',  # 年度分红
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol))).fillna(
                    value=0, method=None, axis=0)
        # 转换时间格式
        df['pubtime'] = map(
            lambda x: int(x.split('-')[0] + x.split('-')[1] + x.split('-')[2]),
            df['DECLAREDATE'])
        # 取得当前时间
        currenttime = int(
            str(context.current_dt)[0:4] + str(context.current_dt)[5:7] +
            str(context.current_dt)[8:10])
        # 选择在当前时间能看到的记录
        df = df[(df.pubtime < currenttime)]
        # 得到目前看起来,有上一年度年度分红的股票
        stocks_symbol_this_year = list(df['SYMBOL'])
        # 得到目前看起来,上一年度没有年度分红的股票
        stocks_symbol_past_year = list(
            set(stocks_symbol) - set(stocks_symbol_this_year))

        # 查有上一年度年度分红的
        df1 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  # 股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  # 股票分红
                gta.STK_DIVIDEND.DECLAREDATE,  # 分红消息的时间
                gta.STK_DIVIDEND.DISTRIBUTIONBASESHARES  # 分红时的股本基数
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year,
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol_this_year))).fillna(
                    value=0, method=None, axis=0)

        df1['pubtime'] = map(
            lambda x: int(x.split('-')[0] + x.split('-')[1] + x.split('-')[2]),
            df1['DECLAREDATE'])
        currenttime = int(
            str(context.current_dt)[0:4] + str(context.current_dt)[5:7] +
            str(context.current_dt)[8:10])
        df1 = df1[(df1.pubtime < currenttime)]

        # 求上上年的年度分红
        df2 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  # 股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  # 股票分红
                gta.STK_DIVIDEND.DECLAREDATE,  # 分红消息的时间
                gta.STK_DIVIDEND.DISTRIBUTIONBASESHARES  # 分红时的股本基数
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == (year - 1),
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol_past_year))).fillna(
                    value=0, method=None, axis=0)

        df2['pubtime'] = map(
            lambda x: int(x.split('-')[0] + x.split('-')[1] + x.split('-')[2]),
            df2['DECLAREDATE'])
        currenttime = int(
            str(context.current_dt)[0:4] + str(context.current_dt)[5:7] +
            str(context.current_dt)[8:10])
        df2 = df2[(df2.pubtime < currenttime)]

        df = pd.concat((df2, df1))

        df['SYMBOL'] = map(normalize_code, list(df['SYMBOL']))
        df.index = list(df['SYMBOL'])

        # 获取最新股本
        q = query(valuation.code, valuation.capitalization).filter(
            valuation.code.in_(list(df.index)))

        df3 = get_fundamentals(q).fillna(value=0)
        df3['SYMBOL'] = df3['code']
        df3 = df3.drop(['code'], axis=1)

        # 合并成一个 dataframe
        df = df.merge(df3, on='SYMBOL')
        df.index = list(df['SYMBOL'])

        # 转换成 float
        df['DISTRIBUTIONBASESHARES'] = map(float, df['DISTRIBUTIONBASESHARES'])
        # 计算股份比值
        df['CAP_RATIO'] = df['DISTRIBUTIONBASESHARES'] / (
            df['capitalization'] * 10000)

        df['DIVIDENTBT'] = map(float, df['DIVIDENTBT'])
        # 计算相对于目前股份而言的分红额度
        df['DIVIDENTBT'] = df['DIVIDENTBT'] * df['CAP_RATIO']
        df = df.drop([
            'SYMBOL', 'DECLAREDATE', 'DISTRIBUTIONBASESHARES',
            'capitalization', 'CAP_RATIO'
        ],
                     axis=1)

        #接下来这一步是考虑多次分红的股票,因此需要累加股票的多次分红
        df = df.groupby(df.index).sum()

        #得到当前股价
        Price = history(1,
                        unit='1d',
                        field='close',
                        security_list=list(df.index),
                        df=True,
                        skip_paused=False,
                        fq='pre')
        Price = Price.T

        df['pre_close'] = Price

        #计算股息率 = 股息/股票价格,* 10 是因为取到的是每 10 股分红
        df['divpercent'] = df['DIVIDENTBT'] / (df['pre_close'] * 10)

        df['code'] = np.array(df.index)

        return df
Beispiel #4
0
def getDivid(context, stocks, year_watch=3):
    year = context.current_dt.year - 1
    #now = datetime.now()
    #year = now.year-1

    #将当前股票池转换为国泰安的6位股票池
    stocks_symbol = []
    for s in stocks:
        stocks_symbol.append(s[0:6])

    # 累加3年分红信息
    # 如果知道前一年的分红,那么得到前一年的分红数据
    df1 = gta.run_query(
        query(
            gta.STK_DIVIDEND.SYMBOL,  #股票代码
            gta.STK_DIVIDEND.DIVIDENTBT,  #股票分红
            gta.STK_DIVIDEND.TOTALDIVIDENDDISTRI,  #派息数(实)
            gta.STK_DIVIDEND.DECLAREDATE  #分红消息的时间
        ).filter(
            gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
            gta.STK_DIVIDEND.DIVDENDYEAR == year,
            #且分红信息在上一年度
            gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol))).dropna(axis=0)

    stocks_symbol_this_year = list(df1['SYMBOL'])

    if year_watch == 3:
        #知道前两年的分红数据
        df2 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  #股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  #股票分红
                gta.STK_DIVIDEND.TOTALDIVIDENDDISTRI,  #派息数(实)
                gta.STK_DIVIDEND.DECLAREDATE  #分红消息的时间
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year - 1,
                #且分红信息在上一年度
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol))).dropna(axis=0)

        #知道前3年的分红数据
        df3 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  #股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  #股票分红
                gta.STK_DIVIDEND.TOTALDIVIDENDDISTRI,  #派息数(实)
                gta.STK_DIVIDEND.DECLAREDATE  #分红消息的时间
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year - 2,
                #且分红信息在上一年度
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol))).dropna(axis=0)

        # 如果前一年的分红不知道,那么知道前4年的分红数据
        df4 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  #股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  #股票分红
                gta.STK_DIVIDEND.TOTALDIVIDENDDISTRI,  #派息数(实)
                gta.STK_DIVIDEND.DECLAREDATE  #分红消息的时间
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year - 3,
                #且分红信息在上一年度
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol),
                gta.STK_DIVIDEND.SYMBOL.notin_(
                    stocks_symbol_this_year)  #不知道今年信息的
            )).dropna(axis=0)
        df = pd.concat((df4, df3, df2, df1))
    elif year_watch == 1:
        # 如果前一年的分红不知道,那么知道前4年的分红数据
        df2 = gta.run_query(
            query(
                gta.STK_DIVIDEND.SYMBOL,  #股票代码
                gta.STK_DIVIDEND.DIVIDENTBT,  #股票分红
                gta.STK_DIVIDEND.TOTALDIVIDENDDISTRI,  #派息数(实)
                gta.STK_DIVIDEND.DECLAREDATE  #分红消息的时间
            ).filter(
                gta.STK_DIVIDEND.ISDIVIDEND == 'Y',  #有分红的股票
                gta.STK_DIVIDEND.DIVDENDYEAR == year - 1,
                #且分红信息在上一年度
                gta.STK_DIVIDEND.SYMBOL.in_(stocks_symbol),
                gta.STK_DIVIDEND.SYMBOL.notin_(
                    stocks_symbol_this_year)  #不知道今年信息的
            )).dropna(axis=0)
        df = pd.concat((df2, df1))
    else:
        log.info('不支持1年和3年之外的参数!!!')
        return

    #print df[(df.SYMBOL == '601006')]

    # 下面四行代码用于选择在当前时间内能已知去年股息信息的股票
    df['pubtime'] = map(
        lambda x: int(x.split('-')[0] + x.split('-')[1] + x.split('-')[2]),
        df['DECLAREDATE'])
    #print df['pubtime']
    currenttime = int(
        str(context.current_dt)[0:4] + str(context.current_dt)[5:7] +
        str(context.current_dt)[8:10])
    #currenttime  = int(str(now.year)+'{:0>2}'.format(str(now.month))+'{:0>2}'.format(str(now.day)))
    #print currenttime
    # 筛选出pubtime小于当前时期的股票,然后剔除'DECLAREDATE','pubtime','SYMBOL'三列
    # 并且将DIVIDENTBT 列转换为float
    df = df[(df.pubtime < currenttime)]
    df['SYMBOL'] = map(normalize_code, list(df['SYMBOL']))
    df.index = list(df['SYMBOL'])

    df = df.drop(['SYMBOL', 'pubtime', 'DECLAREDATE'], axis=1)

    df['DIVIDENTBT'] = map(float, df['DIVIDENTBT'])
    df['TOTALDIVIDENDDISTRI'] = map(float, df['TOTALDIVIDENDDISTRI'])

    q_now = query(valuation.code, valuation.market_cap)
    df_now = get_fundamentals(q_now)
    df_now.index = list(df_now['code'])
    #print df_now

    #接下来这一步是考虑多次分红的股票,因此需要累加股票的多次分红
    #按照股票代码分堆
    df = df.groupby(df.index).sum()
    df['market_cap'] = df_now['market_cap']
    #得到当前股价
    #Price=history(1, unit='1d', field='close', security_list=list(df.index), df=True, skip_paused=False, fq='pre')

    #Price=get_price(list(df.index), count = 1, end_date=now , frequency='daily', fields='close')
    #print Price['close']
    #Price=Price['close'].T
    #print Price
    #Price=Price.T
    #df['pre_close']=Price

    #计算股息率 = 股息/股票价格
    #df['divpercent']=df['DIVIDENTBT']/df['pre_close']
    df['divpercent'] = df['TOTALDIVIDENDDISTRI'] / df[
        'market_cap'] / 1000000 / year_watch
    #print df
    df['code'] = np.array(df.index)
    #print df[(df.code == '601006.XSHG')]
    df = df.sort(['divpercent'], ascending=[False])
    df_name = get_all_securities(['stock'])
    df['name'] = df_name['display_name']

    return df
Beispiel #5
0
def test_gta2():
    from jqdata import gta, query
    print(gta.run_query(query(gta.STK_AF_FORECAST).limit(10)))
    print(gta.run_query(query(gta.STK_AF_FORECAST.SYMBOL).limit(10)))
    print(gta.run_query(gta.query(gta.STK_AF_FORECAST.SYMBOL).limit(10)))
    pass