Example #1
0
    def __init__(self, start, flag, fre, factor_filename, day_range, dirpath, date=None):
        self.sql = "select s_info_windcode,trade_dt,s_dq_freeturnover from AShareEODDerivativeIndicator"
        self.flag = flag  # 0 回测 1更新

        self.day_range = day_range  ##

        self.dirpath = dirpath  # 新建data文件夹 存各种因子数据
        self.factor_filename = factor_filename  # data文件夹里 具体因子的文件夹

        self.factor_path = self.dirpath + '/' + self.factor_filename
        # 确定运行日期
        if date == None:
            self.today = strftime("%Y%m%d", localtime())
        else:
            self.today = date
        self.initial = '20080101'  # 留点窗口 为了下面start往前推能取到日期
        self.start = start
        self.fre = fre

        self.all_trade_day = get_tradeDay.wind(self.initial, self.today, fre='day')  ##用于定位一段交易日区间
        self.trade_day = get_tradeDay.wind(self.start, self.today, fre=self.fre)
        #        self.trade_day = self.trade_day.iloc[:-1]
        self.getdb = client_db.read_db(type='ctquant2')

        if not os.path.exists(self.dirpath):
            os.mkdir(self.dirpath)

        if not os.path.exists('%s' % (self.factor_path)):
            os.mkdir('%s' % (self.factor_path))
Example #2
0
def run(output_path, start, end, flag, fre, dir_path):

    factor_name1 = [
        'Style.SIZE', 'Style.RESVOL', 'Style.SIZENL', 'Style.LIQUIDTY'
    ]
    factor_name = [
        'SIZE_barra', 'RESVOL_barra', 'SIZENL_barra', 'LIQUIDTY_barra'
    ]

    for i in factor_name:
        if not os.path.exists(output_path + '/' + i):
            os.mkdir(output_path + '/' + i)

    trade_day = get_tradeDay.wind(start, end, fre=fre)

    if flag == 0:
        for i in range(len(trade_day)):
            today = trade_day.iloc[i]
            save_day = today
            print(save_day)
            update(save_day, factor_name, factor_name1, dir_path, output_path)
    elif flag == 1:
        save_day = end
        print(save_day)
        update(save_day, factor_name, factor_name1, dir_path, output_path)
Example #3
0
def run(firstRun_update, start, end, fre, index_code, dir_path,
        beachmark_file):
    if not os.path.exists('%s' % beachmark_file):
        os.mkdir('%s' % beachmark_file)
    if not os.path.exists('%s/%s' % (beachmark_file, index_code)):
        os.mkdir('%s/%s' % (beachmark_file, index_code))
    if firstRun_update == 0:
        trade_day = get_tradeDay.wind(start, end, fre)
        for i in trade_day:
            print(i)
            df = pd.read_excel('%s/000846weightnextday%s.xls' % (dir_path, i),
                               dtype={u'成分券代码\nConstituent Code': str})
            df = df.iloc[:, [4, 16]]
            df.columns = ['code', 'weight']
            df['code'] = df['code'].apply(lambda x: x + '-CN')
            df.to_csv('%s/%s/benchmark_%s.csv' %
                      (beachmark_file, index_code, i),
                      index=None,
                      header=None)
    elif firstRun_update == 1:
        cal = Calendar('China.SSE')
        today = strftime("%Y%m%d", localtime())
        today = Date.strptime(today, '%Y%m%d')
        today = cal.advanceDate(today, Period('-1b'))
        today = today.strftime("%Y%m%d")
        df = pd.read_excel('%s/000846weightnextday%s.xls' % (dir_path, today),
                           dtype={u'成分券代码\nConstituent Code': str})
        df = df.iloc[:, [4, 16]]
        df.columns = ['code', 'weight']
        df['code'] = df['code'].apply(lambda x: x + '-CN')
        df.to_csv('%s/%s/benchmark_%s.csv' %
                  (beachmark_file, index_code, today),
                  index=None,
                  header=None)
Example #4
0
    def get_fcftar(self):
        self.trade_day = get_tradeDay.wind('20170701',
                                           self.today,
                                           fre=self.fre)
        if not os.path.exists(self.raw_dirpath):
            os.mkdir(self.raw_dirpath)

        for fre in self.fre_shift_list:
            file1 = self.raw_dirpath + '/' + self.factor_filename1 + '_' + self.cal_model1 + '_' + str(
                fre)
            file2 = self.raw_dirpath + '/' + self.factor_filename2 + '_' + self.cal_model2 + '_' + str(
                fre)

            new_factor_path = self.raw_dirpath + '/' + self.new_factor_name + str(
                fre)
            if not os.path.exists(new_factor_path):
                os.mkdir(new_factor_path)
            if self.flag == 0:
                for i in self.trade_day:
                    self.update(i, file1, self.factor_filename1,
                                self.cal_model1, file2, self.factor_filename2,
                                self.cal_model2, new_factor_path)
            elif self.flag == 1:
                self.update(self.today, file1, self.factor_filename1,
                            self.cal_model1, file2, self.factor_filename2,
                            self.cal_model2, new_factor_path)
Example #5
0
def run(ret_path, output_path, today):
    commission = 0.0023
    stamp_buy = 0.0001
    stamp_sell = 0.0001


    day = datetime.datetime.strptime(today, '%Y%m%d')
    pre_day = day - datetime.timedelta(days=40)
    day = day.strftime('%Y%m%d')
    pre_day = pre_day.strftime('%Y%m%d')

    tradeday = get_tradeDay.wind(pre_day, today, fre = 'day')
    day20 = tradeday.iloc[-20:]
    day5 = tradeday.iloc[-5:]


    srisk = get_data(ret_path, today)
    srisk = srisk[['Symbol','srisk']]

    sret5 = get_sret_5(ret_path, day5)
    close20 = get_close_20(ret_path,day20)

    df_tcost = pd.merge(srisk, sret5, on='Symbol')
    df_tcost = pd.merge(df_tcost, close20, on='Symbol')

    df_tcost['lincoeff'] = commission + 0.5*(stamp_buy + stamp_sell) + 2.5 * df_tcost['sret'] + 0.005/df_tcost['close']
    df_tcost['sqrtcoeff'] = 0.001*df_tcost['srisk']

    df_tcost = df_tcost[['Symbol','lincoeff','sqrtcoeff']]
    df_tcost = df_tcost.sort_values('Symbol')

    df_header = pd.DataFrame([[np.nan,'GROUP','GROUP'],['name','lincoeff','sqrtcoeff'],['unit','number','number'],[np.nan,np.nan,np.nan]],columns=['Symbol','lincoeff','sqrtcoeff'])

    df_final = pd.concat([df_header,df_tcost])
    df_final.to_csv('%s/coef_%s.csv'%(output_path,today),index=None, header= None)
Example #6
0
def get_delta_alpha(output_path, today):

    # today = '20180108'
    # output_path = '.'
    index_list = ['ESG100', 'HS300', 'ZZ500']

    day = datetime.datetime.strptime(today, '%Y%m%d')
    pre_day = day - datetime.timedelta(days=20)
    day = day.strftime('%Y%m%d')
    pre_day = pre_day.strftime('%Y%m%d')

    trade_day = get_tradeDay.wind(pre_day, day, fre='day')
    pre_day = trade_day[trade_day < day].iloc[-1]

    df1 = pd.read_csv(
        'Z:/daily_data/alpha/neut/alphaALL/alphaALL_neut_CN_%s.csv' % day,
        skiprows=3)
    df1.columns = [
        u'stock_code', u'alphaALL_T', u'alphaALL_ESG100_T',
        u'alphaALL_HS300_T', u'alphaALL_ZZ500_T', u'Value_T', u'Quality_T',
        u'Revision_T', u'Fndsurp_T', u'IU_T', u'Mktmmt_T', u'Insider_T',
        u'lincoef', u'sqrtcoef', u'UNIVERSE'
    ]
    df1 = df1[[
        u'stock_code', u'alphaALL_ESG100_T', u'alphaALL_HS300_T',
        u'alphaALL_ZZ500_T'
    ]]
    df2 = pd.read_csv(
        'Z:/daily_data/alpha/neut/alphaALL/alphaALL_neut_CN_%s.csv' % pre_day,
        skiprows=3)
    df2.columns = [
        u'stock_code', u'alphaALL_T-1', u'alphaALL_ESG100_T-1',
        u'alphaALL_HS300_T-1', u'alphaALL_ZZ500_T-1', u'Value_T-1',
        u'Quality_T-1', u'Revision_T-1', u'Fndsurp_T-1', u'IU_T-1',
        u'Mktmmt_T-1', u'Insider_T-1', u'lincoef', u'sqrtcoef', u'UNIVERSE'
    ]
    df2 = df2[[
        u'stock_code', u'alphaALL_ESG100_T-1', u'alphaALL_HS300_T-1',
        u'alphaALL_ZZ500_T-1'
    ]]

    df = pd.merge(df1, df2, on='stock_code')

    for input1 in index_list:
        df['delta_alpha_%s' %
           input1] = df['alphaALL_%s_T' % input1] - df['alphaALL_%s_T-1' %
                                                       input1]

    col = ['delta_alpha_%s' % i for i in index_list]
    df = df[['stock_code'] + col]

    df_header = pd.DataFrame(
        [[np.nan] + ['GROUP'] * len(col), ['name'] + col,
         ['unit'] + ['number'] * len(col), [np.nan] + [np.nan] * len(col)],
        columns=['stock_code'] + col)

    df_final = pd.concat([df_header, df])
    df_final.to_csv('%s/delta_alpha_%s.csv' % (output_path, today),
                    index=None,
                    header=None)
Example #7
0
def run(start, end, today, dirpath, output_path, flag, fre):
    getdb = read_db(type='wind')
    tradeday = get_tradeDay.wind(start, end, fre=fre)
    tradeday = tradeday.iloc[:-1]
    # if not os.path.exists(dirpath):
    #    os.mkdir(dirpath)
    if flag == 0:
        for day in tradeday:
            print(day)
            get_holding(day, dirpath, output_path, getdb)
    else:
        get_holding(today, dirpath, output_path, getdb)
Example #8
0
def run(start, end, today, dirpath, flag, fre):
    getdb = client_db.read_db()
    tradeday = get_tradeDay.wind(start, end, fre=fre)
    tradeday = tradeday.iloc[:-1]
    if not os.path.exists(dirpath):
        os.mkdir(dirpath)
    if flag == 0:
        for day in tradeday:
            print(day)
            suspend(day, dirpath, getdb)
    else:
        suspend(today, dirpath, getdb)
Example #9
0
    def __init__(self, start, end, fre, factor_name_list, type, benchmark,
                 input_path, universe_path, n_quantile, outputpath):
        self.start = start
        self.end = end

        self.fre = fre
        self.factor_name_list = factor_name_list
        self.tradeday = get_tradeDay.wind(self.start, self.end, fre=self.fre)

        self.daily = get_tradeDay.wind(self.start, self.end, fre='day')

        self.type = type
        self.benchmark = benchmark

        self.input_path = input_path
        self.universe_path = universe_path

        self.n_quantile = n_quantile
        self.pct_quantiles = 1 / float(self.n_quantile)

        self.outputpath0 = outputpath
        self.getdb = client_db.read_db(type='wind')
Example #10
0
    def __init__(self,
                 start,
                 sql,
                 flag,
                 factor_filename,
                 day_range,
                 date=None):
        self.sql = sql
        self.flag = flag  # 0 回测 1更新

        self.day_range = day_range  ##

        self.dirpath = 'raw_data'  # 新建data文件夹 存各种因子数据
        self.factor_filename = factor_filename  # data文件夹里 具体因子的文件夹

        self.factor_path = self.dirpath + '/' + self.factor_filename
        # 确定运行日期
        if date == None:
            self.today = strftime("%Y%m%d", localtime())
        else:
            self.today = date
        self.initial = '20170101'  # 留点窗口 为了下面start往前推能取到日期
        self.start = start

        self.all_trade_day = get_tradeDay.wind(self.initial,
                                               self.today,
                                               fre='day')  ##用于定位一段交易日区间
        self.trade_day = get_tradeDay.wind(self.start, self.today, fre='day')
        #        self.trade_day = self.trade_day.iloc[:-1]

        self.getdb = client_db.read_db(type='ctquant2')

        if not os.path.exists(self.dirpath):
            os.mkdir(self.dirpath)

        if not os.path.exists('%s' % (self.factor_path)):
            os.mkdir('%s' % (self.factor_path))
Example #11
0
    def event_data_prepare(self):
        daily = get_tradeDay.wind('20060104', '20171109', fre='day')
        daily = daily.values

        df_factor = self.get_factor()
        df_index_daily = self.get_index_daily()
        df_close_daily = self.get_close_daily()

        df_ret_daily = df_close_daily.pct_change().shift(-1)
        df_index_daily['ret'] = df_index_daily['close'].pct_change()
        df_index_daily = df_index_daily[['trade_day', 'ret']]
        df_index_daily['ret'] = df_index_daily['ret'].shift(-1).values
        df_index_daily = df_index_daily.set_index('trade_day')
        df_index_daily.columns = ['benchmark']
        return df_factor, daily, df_ret_daily, df_index_daily
Example #12
0
def run(start, end, today, output_path, flag, fre):
    config_path = 'Z:/daily_data/config/config.yaml'
    with open(config_path) as f:
        config = yaml.load(f.read())
    account = config['account']
    for key, sub_account in account.items():
        if sub_account['type'] == 'p':
            account_name = sub_account['subaccount'].split('account-')[1]
            getdb = read_db(type='wind')
            tradeday = get_tradeDay.wind(start, end, fre=fre)
            tradeday = tradeday.iloc[:-1]
            if flag == 0:
                for day in tradeday:
                    print(day)
                    get_new_stock(day, account_name, output_path, getdb)
            else:
                get_new_stock(today, account_name, output_path, getdb)
def run(start, end, flag, dirpath, output1, output2):
    if not os.path.exists(output1):
        os.mkdir(output1)
    if not os.path.exists(output2):
        os.mkdir(output2)

    if flag == 0:
        fre = 'day'
        tradeday = get_tradeDay.wind(start, end, fre=fre)
        for day in tradeday:
            print(day)
            get_exposure(dirpath, output1, day)
            get_cov(dirpath, output2, day)
    else:
        today = end
        # today = datetime.strftime(today.toDateTime(), "%Y%m%d")
        get_exposure(dirpath, output1, today)
        get_cov(dirpath, output2, today)
Example #14
0
def run(start, end, flag, dirpath, output1, output2):
    if not os.path.exists(output1):
        os.mkdir(output1)
    if not os.path.exists(output2):
        os.mkdir(output2)

    if flag == 0:
        fre = 'day'
        tradeday = get_tradeDay.wind(start, end, fre=fre)
        for day in tradeday:
            print(day)
            get_exposure(dirpath, output1, day)
            get_cov(dirpath, output2, day)
    else:
        end = Date.strptime(end, '%Y%m%d')
        today = cal.advanceDate(end, Period('-1b'))
        today = today.strftime("%Y%m%d")
        # today = datetime.strftime(today.toDateTime(), "%Y%m%d")
        get_exposure(dirpath, output1, today)
        get_cov(dirpath, output2, today)
Example #15
0
start = '20080401'
end = '20170510'

df_index = DataAPI.MktIdxdGet(tradeDate=u"",
                              indexID=u"",
                              ticker=u"000300",
                              beginDate=start,
                              endDate=end,
                              exchangeCD=u"XSHE,XSHG",
                              field=['tradeDate', 'closeIndex'],
                              pandas="1")
df_index.columns = ['trade_day', 'close']

fre = 'month'
tradeday = get_tradeDay.wind(start, end, fre=fre)

factor_name = 'RAND1'
factor_path = 'Z:/axioma_data/alpha/%s' % factor_name
# factor_path = 'E:/QUANT/alpha_factor/risk_data/SIZE_uqer'

close_path = 'Z:/backtest_data/close_data/gogo_data'
df_close = pd.DataFrame([])
for i in tradeday:
    print(i)
    close = pd.read_csv('%s/close_%s.csv' % (close_path, i),
                        dtype={'ticker': str})
    close['trade_day'] = i
    df_close = pd.concat([df_close, close])

df_close = df_close.pivot(index='trade_day',
Example #16
0
from tools import get_tradeDay

reload(sys)

start = '20080401'
end = '20170510'

df_index = DataAPI.MktIdxdGet(tradeDate=u"", indexID=u"", ticker=u"000300", beginDate=start, endDate=end,
                              exchangeCD=u"XSHE,XSHG", field=['tradeDate', 'closeIndex'], pandas="1")
df_index.columns = ['trade_day', 'close']
df_index['trade_day'] = df_index['trade_day'].apply(lambda x: x[:4] + x[5:7] + x[8:])



fre = 'month'
tradeday = get_tradeDay.wind(start, end, fre=fre)

daily = get_tradeDay.wind(start, end, fre='day')

factor_name = 'fcff_sign_r2'
# factor_path = 'Z:/axioma_data/alpha/%s' % factor_name
factor_path = 'E:/factor_data/filter_data/%s'%factor_name
universe_path = 'Z:/axioma_data/universe'

outputpath = 'E:/QUANT/analyse_factor/fig2'
outputpath = '%s/%s' % (outputpath, factor_name)

if not os.path.exists(outputpath):
    os.mkdir(outputpath)

close_path = 'Z:/backtest_data/close_data/gogo_data'
Example #17
0
    df = pd.merge(df1, df2, on='stock_code')

    for input1 in index_list:
        df['delta_alpha_%s' %
           input1] = df['alphaALL_%s_T' % input1] - df['alphaALL_%s_T-1' %
                                                       input1]

    col = ['delta_alpha_%s' % i for i in index_list]
    df = df[['stock_code'] + col]

    df_header = pd.DataFrame(
        [[np.nan] + ['GROUP'] * len(col), ['name'] + col,
         ['unit'] + ['number'] * len(col), [np.nan] + [np.nan] * len(col)],
        columns=['stock_code'] + col)

    df_final = pd.concat([df_header, df])
    df_final.to_csv('%s/delta_alpha_%s.csv' % (output_path, today),
                    index=None,
                    header=None)


if __name__ == '__main__':
    start = '20180103'
    end = '20180402'
    output_path = 'Z:/daily_data/tcost_model'
    trade_day = get_tradeDay.wind(start, end, fre='day')

    for today in trade_day:
        print(today)
        get_delta_alpha(output_path, today)