コード例 #1
0
ファイル: back_test.py プロジェクト: zllquant/ZllQuant
    def get_all_code_price(self, date, holding_stock_dict, to_buy, to_sell,
                           to_add):
        all_code_set = set()
        for code in holding_stock_dict.keys():
            all_code_set.add(code)
        all_code_set = all_code_set.union(to_sell).union(to_buy).union(to_add)
        if len(all_code_set) == 0:
            return None
        df_price = rqd.get_price(all_code_set,
                                 date,
                                 date,
                                 fields=['open', 'close'],
                                 expect_df=True).droplevel(1)
        is_suspend = rqd.is_suspended(all_code_set, date, date).squeeze()
        df_price['is_suspended'] = is_suspend

        return df_price
コード例 #2
0
# 数据导入
df_buy_sell = pd.read_csv(inputPath + "汇总个股买卖时点.csv",
                          index_col=0,
                          engine='python')
df_buy_sell.sort_values(by='buy_date', axis=0, ascending=True, inplace=True)
df_buy_sell = df_buy_sell.reset_index(drop=True)

# 剔除停牌及建仓日涨跌停个股
list_suspended_index = pd.DataFrame(index=df_buy_sell.index, columns=['index'])
for ind in df_buy_sell.index:
    ind_code = df_buy_sell.loc[ind, 'code']
    ind_date = df_buy_sell.loc[ind, 'buy_date']
    try:
        suspended_index = rq.is_suspended(ind_code,
                                          start_date=ind_date,
                                          end_date=ind_date)
    except:
        list_suspended_index.loc[ind, 'index'] = True
    else:
        if suspended_index is None:
            list_suspended_index.loc[ind, 'index'] = True
        else:
            list_suspended_index.loc[ind,
                                     'index'] = suspended_index.loc[ind_date,
                                                                    ind_code]

list_limit_index = pd.DataFrame(index=df_buy_sell.index, columns=['index'])
for ind in df_buy_sell.index:
    ind_code = df_buy_sell.loc[ind, 'code']
    ind_date = df_buy_sell.loc[ind, 'buy_date']
コード例 #3
0
def drop_suspended(universe, date):
    # squeeze(axis=0):DataFrames with a single column or a single row are squeezed to a Series
    is_suspended = rqd.is_suspended(universe, date, date).squeeze()
    assert isinstance(is_suspended, pd.Series), 'is_suspended is not series'
    return is_suspended.index[~is_suspended]
コード例 #4
0
    def analyze(self):
        dates = rqd.get_trading_dates(self.begin_date, self.end_date)
        top_group, bottom_group = [], []
        last_date = None
        df_profit = pd.DataFrame(columns=['top', 'bottom', 'benchmark'])
        # 间隔一段时间遍历交易日
        for index in range(0, len(dates), self.interval):
            date = dates[index]
            print("当前日期:", date)
            if last_date is None:
                df_profit.loc[date] = {'top': 0, 'bottom': 0, 'benchmark': 0}
            else:
                # 股票收盘价
                close = rqd.get_price(top_group + bottom_group,
                                      last_date,
                                      date,
                                      fields='close')
                # 基准收盘价
                bench_close = rqd.get_price(self.benchmark,
                                            last_date,
                                            date,
                                            fields='close')
                # 股票收益
                profit_series = close.iloc[-1] / close.iloc[0] - 1
                top_profit = profit_series[top_group].mean()
                bottom_profit = profit_series[bottom_group].mean()
                # 基准收益
                bench_profit = bench_close[-1] / bench_close[0] - 1
                df_profit.loc[date] = {
                    'top': top_profit,
                    'bottom': bottom_profit,
                    'benchmark': bench_profit
                }

            last_date = date
            # 选出当天可交易的股票
            universe = filter_stock_pool(date)
            factor_df = rqd.get_factor(universe, self.factor, date,
                                       date)[lambda x: x > 0]
            # 每档数量
            position_size = int(len(factor_df) / self.group)
            # 清空首档没有停牌的
            if len(top_group) > 0:
                top_suspend = []
                for code in top_group:
                    if rqd.is_suspended(code, date, date).squeeze():
                        top_suspend.append(code)
                top_group = top_suspend
            # 清空末档没有停牌的
            if len(bottom_group) > 0:
                bottom_suspend = []
                for code in bottom_group:
                    if rqd.is_suspended(code, date, date).squeeze():
                        bottom_suspend.append(code)
                bottom_group = bottom_suspend
            # 添加首档
            for code in factor_df.sort_values().index:
                if len(top_group) >= position_size:
                    break
                if code not in top_group:
                    top_group.append(code)
            # 添加末档
            for code in factor_df.sort_values(ascending=False).index:
                if len(bottom_group) >= position_size:
                    break
                if code not in bottom_group:
                    bottom_group.append(code)
        df_profit['top-bottom'] = df_profit['top'] - df_profit['bottom']
        df_cumprod_profit = (df_profit + 1).cumprod()
        df_cumprod_profit.plot()
        plt.show()