Esempio n. 1
0
def tagbondfund(start_date, end_date, codes):

    funddf = data.bond_value(start_date, end_date)
    funddf = funddf[codes]

    csibondindexdf              = data.bond_index_value(start_date, end_date, const.csibondindex_code)
    ratebondindexdf             = data.bond_index_value(start_date, end_date, const.ratebondindex_code)    

    creditbondindexdf           = data.bond_index_value(start_date, end_date, const.credictbondindex_code)
    convertiblebondindexdf      = data.bond_index_value(start_date, end_date, const.convertiblebondindex_code)

    risefitness_result          =  risefitness(funddf, csibondindexdf, 0.5)    
    declinefitness_result       =  declinefitness(funddf, csibondindexdf, 0.5)
    oscillationfitness_result   =  oscillationfitness(funddf, csibondindexdf, 0.5)    
    ratebondprefer_result       =  ratebondprefer(funddf, ratebondindexdf,0.5) 
    creditbondprefer_result     =  creditbondprefer(funddf, creditbondindexdf, 0.5)
    convertiblebondprefer_result=  convertiblebondprefer(funddf, convertiblebondindexdf, 0.5)

        #print 'rise'
        risefitness_set = set()
        for k,v in risefitness_result:
                risefitness_set.add(k)
                #print k, v

        #print
        declinefitness_set = set()
        #print 'decline'
        for k,v in declinefitness_result:
                declinefitness_set.add(k)
                #print k, v

        #print

        #print 'oscillation'
        oscillationfitness_set = set()
        for k,v in oscillationfitness_result:
                oscillationfitness_set.add(k)
                #print k, v
        #print


        #print 'ratebondprefer'
        ratebondprefer_set = set()
        for k,v in ratebondprefer_result:
                ratebondprefer_set.add(k)

        #print 'ratebondprefer'
        creditbondprefer_set = set()
        for k,v in creditbondprefer_result:
                creditbondprefer_set.add(k)


        convertiblebondprefer_set = set()
        for k,v in convertiblebondprefer_result:
                convertiblebondprefer_set.add(k)
Esempio n. 2
0
def bondfundfilter(start_date, end_date):

    indicator = {}

    funddf = data.bond_value(start_date, end_date)
    indexdf = data.bond_index_value(start_date, end_date, 'H11001.CSI')

    #按照基金创立时间过滤
    setuptime_data = fundsetuptimefilter(funddf.columns, start_date, data.bond_establish_data())


    #print setuptime_data
    #按照jensen测度过滤
    jensen_data    = jensenfilter(funddf, indexdf, rf, 0.5)
    #jensen_data    = sf.jensenfilter(funddf, indexdf, rf, 1.0)

    #按照索提诺比率过滤
    sortino_data   = sortinofilter(funddf, rf, 0.5)
    #sortino_data   = sf.sortinofilter(funddf, rf, 1.0)

    #按照ppw测度过滤
    ppw_data       = ppwfilter(funddf, indexdf, rf, 0.5)
    #ppw_data       = sf.ppwfilter(funddf, indexdf, rf, 1.0)
    #print ppw_data

    stability_data = stabilityfilter(funddf, 2.0 / 3)
    #stability_data = sf.stabilityfilter(funddf, 1.0)

    sharpe_data    = fi.fund_sharp_annual(funddf)

    #print stability_data

    #print 'jensen'
    jensen_dict = {}
    for k,v in jensen_data:
        jensen_dict[k] = v
    #print k, v
    #print
    #print 'sortino'


    sortino_dict = {}
    for k,v in sortino_data:
        sortino_dict[k] = v
    #print k,v

    #print
    #print 'ppw'
    ppw_dict = {}
    for k,v in ppw_data:
        ppw_dict[k] = v
    #print k,v

    #print
    #print 'statbility'
    stability_dict = {}
    for k,v in stability_data:
        stability_dict[k] = v
    #print k,v


    sharpe_dict = {}
    for k,v in sharpe_data:
        sharpe_dict[k] = v


    setuptime_set = set(setuptime_data)


    jensen_set = set()
    for k, v in jensen_data:
        jensen_set.add(k)


    sortino_set = set()
    for k, v in sortino_data:
        sortino_set.add(k)


    ppw_set = set()
    for k, v in ppw_data:
        ppw_set.add(k)


    stability_set = set()
    for k, v in stability_data:
        stability_set.add(k)

    codes = []



    for code in setuptime_set:
        if (code in jensen_set) and (code in sortino_set) and (code in ppw_set) and (code in stability_set):
            codes.append(code)


    for code in codes:
        ind = indicator.setdefault(code, {})
        ind['sharpe']    = sharpe_dict[code]
        ind['jensen']    = jensen_dict[code]
        ind['sortino']   = sortino_dict[code]
        ind['ppw']         = ppw_dict[code]
        ind['stability'] = stability_dict[code]


    indicator_set = set()
    for code in setuptime_set:
        indicator_set.add(code)

    indicator_codes = []
    indicator_datas = []


    for code in indicator_set:
        indicator_codes.append(code)
        indicator_datas.append([sharpe_dict.setdefault(code, None), jensen_dict.setdefault(code, None), sortino_dict.setdefault(code, None), ppw_dict.setdefault(code, None), stability_dict.setdefault(code, None)])


    indicator_df = pd.DataFrame(indicator_datas, index = indicator_codes, columns=['sharpe', 'jensen', 'sortino', 'ppw', 'stability'])
    indicator_df.to_csv('./tmp/bond_indicator_' + end_date + '.csv')


    f = open('./tmp/bondfilter_codes_' + end_date + '.csv','w')


    for code in codes:
        f.write(str(code) + '\n')

    f.flush()
    f.close()


    return codes, indicator
Esempio n. 3
0
    moneydf = data.bonds()
    moneydfr = moneydf.pct_change().fillna(0.0)

    dates = moneydf.index

    tag = {}

    for i in range(156, len(dates)):

        if (i - 156) % 13 == 0:

            start_date = dates[i - 52].strftime('%Y-%m-%d')
            allocation_start_date = dates[i - 13].strftime('%Y-%m-%d')
            end_date = dates[i].strftime('%Y-%m-%d')

            allocation_funddf = data.bond_value(allocation_start_date,
                                                end_date)
            fund_codes, tag = fs.select_money(allocation_funddf)

            #print tag
        #print tag
        # print fund_codes

        d = dates[i]
        print d.strftime('%Y-%m-%d'), ',', moneydfr.loc[
            d, tag['sharpe1']], ',', moneydfr.loc[d, tag['sharpe2']]

# print tag
# allocation_funddf      = allocation_funddf[fund_codes]
Esempio n. 4
0
def bondfilter(start_date, end_date):

    funddf = data.bond_value(start_date, end_date)

    indexdf = data.bond_index_value(start_date, end_date, 'H11001.CSI')

    #按照规模过滤
    #scale_data     = sf.scalefilter(2.0 / 3)

    #按照基金创立时间过滤
    setuptime_data = sf.fundsetuptimefilter(funddf.columns, start_date,
                                            data.bond_establish_data())

    #按照jensen测度过滤
    jensen_data = sf.jensenfilter(funddf, indexdf, rf, 0.5)

    #按照索提诺比率过滤
    sortino_data = sf.sortinofilter(funddf, rf, 0.5)

    #按照ppw测度过滤
    ppw_data = sf.ppwfilter(funddf, indexdf, rf, 0.5)
    #print ppw_data

    stability_data = sf.stabilityfilter(funddf, 2.0 / 3)
    #print stability_data
    '''
    scale_set = set()
    for k, v in scale_data:
        scale_set.add(k)
    '''

    setuptime_set = set(setuptime_data)

    jensen_set = set()
    for k, v in jensen_data:
        jensen_set.add(k)

    sortino_set = set()
    for k, v in sortino_data:
        sortino_set.add(k)

    ppw_set = set()
    for k, v in ppw_data:
        ppw_set.add(k)

    stability_set = set()
    for k, v in stability_data:
        stability_set.add(k)

    codes = []

    for code in setuptime_set:
        if (code in jensen_set) and (code in sortino_set) and (
                code in ppw_set) and (code in stability_set):
            codes.append(code)

    #按照业绩持续性过滤
    #stability_data = sf.stabilityfilter(funddf[codes], 2.0 / 3)
    #print stability_data

    #codes = []
    #for k, v in stability_data:
    #    codes.append(k)

    return codes
Esempio n. 5
0
def bondLabelAsset(dates, interval, funddf, indexdf):

    df = data.bonds()
    dfr = df.pct_change().fillna(0.0)

    funddfr = funddf.pct_change().fillna(0.0)
    indexdfr = indexdf.pct_change().fillna(0.0)

    pre_ratebond = ''
    pre_creditbond = ''
    pre_convertiblebond = ''

    tag = {}
    result_dates = []
    columns = []
    result_datas = []

    select_datas = []

    allcodes = []
    filtercodes = []
    poolcodes = []
    selectcodes = []

    for i in range(interval + 156, len(dates)):

        if (i - 156) % interval == 0:

            start_date = dates[i - 52].strftime('%Y-%m-%d')
            end_date = dates[i].strftime('%Y-%m-%d')
            allocation_start_date = dates[i - interval].strftime('%Y-%m-%d')

            allocationdf = data.bond_value(allocation_start_date, end_date)
            alldf = data.bond_value(start_date, end_date)

            codes, indicator = FundFilter.bondfundfilter(start_date, end_date)
            fund_pool, fund_tags = st.tagbondfund(start_date, end_date, codes)

            allocationdf = allocationdf[fund_pool]
            fund_code, tag = fund_selector.select_bond(allocationdf, fund_tags)

            allcodes = alldf.columns
            filtercodes = codes
            poolcodes = fund_pool
            selectcodes = fund_code

            if not tag.has_key('ratebond'):
                tag['ratebond'] = pre_ratebond
            else:
                pre_ratebond = tag['ratebond']
            if not tag.has_key('creditbond'):
                tag['creditbond'] = pre_creditbond
            else:
                pre_creditbond = tag['creditbond']
            if not tag.has_key('convertiblebond'):
                tag['convertiblebond'] = pre_convertiblebond
            else:
                pre_convertiblebond = tag['convertiblebond']

            print tag
            # print tag['largecap'] , tag['smallcap'], tag['rise'], tag['oscillation'], tag['decline'], tag['growth'], tag['value']

        d = dates[i]
        result_dates.append(d)
        result_datas.append([
            funddfr.loc[d, tag['ratebond']], funddfr.loc[d, tag['creditbond']],
            funddfr.loc[d, tag['convertiblebond']]
        ])
        print d.strftime('%Y-%m-%d'), funddfr.loc[
            d, tag['ratebond']], funddfr.loc[
                d, tag['creditbond']], funddfr.loc[d, tag['convertiblebond']]

        allcode_r = 0
        for code in allcodes:
            allcode_r = allcode_r + 1.0 / len(allcodes) * dfr.loc[d, code]

        filtercode_r = 0
        for code in filtercodes:
            filtercode_r = filtercode_r + 1.0 / len(filtercodes) * dfr.loc[
                d, code]

        poolcode_r = 0
        for code in poolcodes:
            poolcode_r = poolcode_r + 1.0 / len(poolcodes) * dfr.loc[d, code]

        selectcode_r = 0
        for code in selectcodes:
            selectcode_r = selectcode_r + 1.0 / len(selectcodes) * dfr.loc[
                d, code]

        select_datas.append(
            [allcode_r, filtercode_r, poolcode_r, selectcode_r])

    result_df = pd.DataFrame(
        result_datas,
        index=result_dates,
        columns=['ratebond', 'creditbond', 'convertiblebond'])
    result_df.to_csv('./tmp/bondlabelasset.csv')

    select_df = pd.DataFrame(
        select_datas,
        index=result_dates,
        columns=['allcodes', 'filtercodes', 'poolcode', 'selectcode'])
    select_df.to_csv('./tmp/bondselectasset.csv')

    return result_df
Esempio n. 6
0
    #for i in range(0 ,len(train_start_date)):

    for i in range(5, 6):

        #####################################################
        #训练和评测数据时间
        train_start = train_start_date[i]
        train_end = train_end_date[i]
        test_start = test_start_date[i]
        test_end = test_end_date[i]
        ####################################################

        ###################################################
        #评测数据
        funddf = data.bond_value(train_start, test_end)
        codes = funddf.columns
        evaluationdf = data.bond_value(test_start, test_end)
        evaluationdf = evaluationdf[codes]
        ###################################################

        ####################################################################
        #筛选基金池,基金打标签
        codes = bondfilter(train_start, train_end)
        fund_codes, bondtags = st.tagbonds(train_start, train_end, codes)
        ####################################################################

        #####################################################################################
        #blacklitterman 资产配置
        '''
        P = [[-1, 1]]