Beispiel #1
0
def get_adjust_point(startdate='2010-01-08', enddate=None, label_period=1):
    # 加载时间轴数据
    if not enddate:
        yesterday = (datetime.now() - timedelta(days=1))
        enddate = yesterday.strftime("%Y-%m-%d")

    index = base_trade_dates.load_trade_dates(startdate, end_date=enddate)
    index = index[index.td_type >= 8].index
    #index = DBData.trade_date_index(startdate, end_date=enddate)

    if label_period > 1:
        label_index = index[::label_period]
        if index.max() not in label_index:
            label_index = label_index.insert(len(label_index), index.max())
    else:
        label_index = index
    # label_index = pd.DatetimeIndex([
    #     '2010-01-08',
    #     '2010-07-09',
    #     '2011-01-07',
    #     '2011-07-08',
    #     '2012-01-13',
    #     '2012-07-20',
    #     '2013-01-25',
    #     '2013-08-02',
    #     '2014-01-30',
    #     '2014-08-01',
    #     '2015-01-30',
    #     '2015-07-31',
    #     '2016-01-29',
    #     '2016-08-05',
    #     '2016-11-05',
    # ])

    return label_index
Beispiel #2
0
    def trade_date(begin_date = None, end_date = None, lookback = None):

        if ATradeDate.__a_trade_date is None:
            ATradeDate.__a_trade_date = base_trade_dates.load_trade_dates().sort_index()
        td_date = ATradeDate.__a_trade_date.copy()
        if begin_date is not None:
            if lookback is not None:
                tmp_td_date = td_date[td_date.index <= begin_date]
                td_date = td_date[td_date.index >= tmp_td_date.index[-1 * lookback]]
            else:
                td_date = td_date[td_date.index >= begin_date]
        if end_date is not None:
            td_date = td_date[td_date.index <= end_date]
        return td_date.index
Beispiel #3
0
    def month_trade_date(begin_date = None, end_date = None, lookback = None):

        if ATradeDate.__a_trade_date is None:
            ATradeDate.__a_trade_date = base_trade_dates.load_trade_dates().sort_index()
        td_date = ATradeDate.__a_trade_date.copy()
        yesterday = td_date.iloc[[-2], :]
        td_date = td_date[(td_date.td_type & 0x08) > 0]
        if not yesterday.index in td_date.index:
            td_date = pd.concat([td_date, yesterday])
        if begin_date is not None:
            if lookback is not None:
                tmp_td_date = td_date[td_date.index <= begin_date]
                td_date = td_date[td_date.index >= tmp_td_date.index[-1 * lookback]]
            else:
                td_date = td_date[td_date.index >= begin_date]
        if end_date is not None:
            td_date = td_date[td_date.index <= end_date]
        return td_date.index
Beispiel #4
0
def fc_update_bflayer_nav(ctx, optid):

    engine = database.connection('asset')
    Session = sessionmaker(bind=engine)
    session = Session()

    sql1 = session.query(factor_cluster_asset.fc_asset_id).filter(
        factor_cluster_asset.globalid == optid)
    asset_ids = [asset[0] for asset in sql1.all()]
    assets = {}
    for asset_id in asset_ids:
        print asset_id
        assets[asset_id] = load_nav_series(asset_id)

    start_date = '2010-01-01'
    # end_date = '2018-12-01'
    end_date = datetime.datetime.now().strftime('%Y-%m-%d')
    # trade_dates = DBData.trade_dates(start_date = '2010-01-01', end_date = '2018-12-01')
    # trade_dates = base_trade_dates.load_trade_dates(begin_date = '2010-01-01', end_date = '2018-12-01').index
    trade_dates = base_trade_dates.load_trade_dates(begin_date='2010-01-01',
                                                    end_date=end_date).index
    df_assets = pd.DataFrame(assets)
    df_assets = df_assets[df_assets.index >= start_date]
    df_assets = df_assets[df_assets.index <= end_date]
    df_assets = df_assets.reindex(trade_dates).dropna()
    df_ret = df_assets.pct_change().dropna()

    sql2 = session.query(distinct(barra_stock_factor_valid_factor.trade_date))
    select_dates = [select_date[0] for select_date in sql2.all()]
    select_dates = select_dates[20:]

    sql3 = session.query(
        barra_stock_factor_valid_factor.trade_date,
        barra_stock_factor_valid_factor.bf_layer_id,
    ).statement
    valid_factors = pd.read_sql(sql3,
                                session.bind,
                                index_col=['trade_date'],
                                parse_dates=True)

    df_new = pd.DataFrame(
        columns=['date', 'nav', 'fc_cluster_id', 'factor_selected_date'])
    for date in select_dates:
        print date
        bf_ids = valid_factors.loc[date].values.ravel()
        factors_ret = df_ret.loc[:, bf_ids]
        layer_ret = factors_ret.mean(1)
        layer_nav = (1 + layer_ret).cumprod()
        layer_nav = layer_nav.to_frame(name='nav')
        layer_nav.index.name = 'date'
        layer_nav['fc_cluster_id'] = '%s.%d' % (optid, 2)
        layer_nav = layer_nav.reset_index()

        layer_nav['factor_selected_date'] = date
        df_new = pd.concat([df_new, layer_nav])

    df_new['globalid'] = optid
    df_new = df_new.set_index(
        ['globalid', 'fc_cluster_id', 'date', 'factor_selected_date'])

    db = database.connection('asset')
    metadata = MetaData(bind=db)
    t = Table('factor_cluster_nav', metadata, autoload=True)

    df_old = asset_factor_cluster_nav.load_nav(optid)
    database.batch(db, t, df_new, df_old)
Beispiel #5
0
def fc_update_nav(ctx, optid):

    engine = database.connection('asset')
    Session = sessionmaker(bind=engine)
    session = Session()

    sql1 = session.query(factor_cluster_asset.fc_asset_id).filter(
        factor_cluster_asset.globalid == optid)
    asset_ids = [asset[0] for asset in sql1.all()]
    assets = {}
    for asset_id in asset_ids:
        assets[asset_id] = load_nav_series(asset_id)

    start_date = '2010-01-01'
    end_date = '2018-12-01'
    # trade_dates = DBData.trade_dates(start_date = '2010-01-01', end_date = '2018-12-01')
    trade_dates = base_trade_dates.load_trade_dates(
        begin_date='2010-01-01', end_date='2018-12-01').index
    df_assets = pd.DataFrame(assets)
    df_assets = df_assets[df_assets.index >= start_date]
    df_assets = df_assets[df_assets.index <= end_date]
    df_assets = df_assets.reindex(trade_dates).dropna()
    df_ret = df_assets.pct_change().dropna()

    df_layer_result = pd.read_csv('df_layer_result.csv',
                                  index_col=0,
                                  parse_dates=True)
    dates = df_layer_result.index.drop_duplicates()
    df_new = pd.DataFrame(
        columns=['date', 'nav', 'fc_cluster_id', 'factor_selected_date'])
    for date in dates:
        print date
        df = df_layer_result.loc[date]

        layers = np.unique(df.layer)
        df_layer = pd.DataFrame(columns=['date', 'nav', 'fc_cluster_id'])
        for layer in layers:
            factors = df[df.layer == layer]
            factors = factors.factor.values
            factors_ret = df_ret.loc[:, factors]
            layer_ret = factors_ret.mean(1)
            layer_nav = (1 + layer_ret).cumprod()
            layer_nav = layer_nav.to_frame(name='nav')
            layer_nav.index.name = 'date'
            layer_nav['fc_cluster_id'] = optid + '.%d' % layer
            layer_nav = layer_nav.reset_index()
            df_layer = pd.concat([df_layer, layer_nav])

        df_layer['factor_selected_date'] = date
        df_new = pd.concat([df_new, df_layer])

    df_new['globalid'] = optid
    df_new = df_new.set_index(
        ['globalid', 'fc_cluster_id', 'date', 'factor_selected_date'])

    db = database.connection('asset')
    metadata = MetaData(bind=db)
    t = Table('factor_cluster_nav', metadata, autoload=True)

    df_old = asset_factor_cluster_nav.load_nav(optid)
    database.batch(db, t, df_new, df_old)