Exemplo n.º 1
0
def get_price_change_rate(
    order_book_ids,  # type: Union[str, List[str]]
    count=1,  # type: Optional[int]
    expect_df=False  # type: Optional[bool]
):  # type: (...) -> Union[pd.DataFrame, pd.Series]
    """
    获取股票/指数截止T-1日的日涨幅

    :param order_book_ids: 可输入 order_book_id, order_book_id list, symbol, symbol list
    :param count: 回溯获取的数据个数。默认为当前能够获取到的最近的数据
    :param expect_df: 是否期望始终返回 DataFrame。pandas 0.25.0 以上该参数应设为 True,以避免因试图构建 Panel 产生异常

    当 expect_df 为 False 时,返回值的类型如下:

        *  传入多个order_book_id,函数会返回pandas DataFrame
        *  传入一个order_book_id,函数会返回pandas Series

    :example:

    获取平安银行以及沪深300指数一段时间的涨跌幅情况:

    ..  code-block:: python3
        :linenos:

        get_price_change_rate(['000001.XSHE', '510050.XSHG'], 1)
        # [Out]
        # 2016-06-01 15:30:00.00  INFO   order_book_id  000001.XSHE  510050.XSHG
        #                                date
        #                                2016-05-31        0.026265     0.033964
        # 2016-06-02 15:30:00.00  INFO   order_book_id  000001.XSHE  510050.XSHG
        #                                date
        #                                2016-06-01       -0.006635    -0.008308

    """
    env = Environment.get_instance()
    data_proxy = env.data_proxy

    if isinstance(order_book_ids, six.string_types):
        order_book_ids = assure_order_book_id(order_book_ids)
    else:
        order_book_ids = [assure_order_book_id(i) for i in order_book_ids]

    end_date = data_proxy.get_previous_trading_date(env.trading_dt)

    if count == 1:
        start_date = end_date
    else:
        start_date = data_proxy.get_previous_trading_date(end_date, count - 1)

    return rqdatac.get_price_change_rate(order_book_ids,
                                         start_date,
                                         end_date,
                                         expect_df=expect_df)
Exemplo n.º 2
0
 def get_score(self, helper):
     order_book_ids = helper.order_book_ids
     weight = np.repeat(1.0 / len(order_book_ids), len(order_book_ids))
     risk = weight.dot(helper.get_cov_matrix())
     rtn = self.expected_returns
     if rtn is None:
         start_date = rqdatac.trading_date_offset(helper.prev_trading_date,
                                                  -self.window)
         rtn = rqdatac.get_price_change_rate(
             order_book_ids, start_date,
             helper.prev_trading_date).mean() * 252
     rtn = rtn.reindex(order_book_ids, fill_value=0.0)
     return rtn / risk
Exemplo n.º 3
0
    def get_score(self, helper):
        order_book_ids = helper.order_book_ids
        weight = np.repeat(1.0 / len(order_book_ids), len(order_book_ids))
        not_in_pool = len(helper.union_components) - len(helper.order_book_ids)
        if not_in_pool > 0:
            active_weight = np.hstack([weight,
                                       np.repeat(0.0, not_in_pool)
                                       ]) - helper.benchmark_weight.values
        else:
            active_weight = weight - helper.benchmark_weight.values
        active_risk = active_weight.dot(helper.get_union_cov_matrix())

        rtn = self._expected_active_returns
        if rtn is None:
            start_date = rqdatac.trading_date_offset(helper.prev_trading_date,
                                                     -self.window)
            rtn = rqdatac.get_price_change_rate(
                order_book_ids + [helper.benchmark], start_date,
                helper.prev_trading_date).mean() * 252
            benchmark_returns = rtn[helper.benchmark]
            rtn -= benchmark_returns

        rtn = rtn.reindex(order_book_ids, fill_value=0.0)
        return rtn / active_risk[:len(order_book_ids)]
Exemplo n.º 4
0
holding = pd.read_sql(sql, conn)
holding['stock_code'] = holding['stock_code'].apply(rq.id_convert)

sql = f'''
SELECT * FROM jarvis.lgt_inst_north_holding_em
WHERE inst_code in {str(inst_code_all)}
AND as_of_date BETWEEN '{date_start}' AND '{date_end}'
'''
holding_all = pd.read_sql(sql, conn)
holding_all['stock_code'] = holding_all['stock_code'].apply(rq.id_convert)

#所有股票代码
instruments = holding['stock_code'].drop_duplicates().sort_values().to_list()
#历史涨跌幅
pct_change = rq.get_price_change_rate(instruments,
                                      start_date=date_start,
                                      end_date=date_end)
#获取历史价格
md = rq.get_price(instruments,
                  start_date=date_start,
                  end_date=date_end,
                  fields=['close'])

ind_data = w.wsd(
    "CI005003.WI,CI005005.WI,CI005006.WI,CI005001.WI,CI005002.WI,CI005012.WI,CI005013.WI,CI005010.WI,CI005011.WI,CI005016.WI,CI005018.WI,CI005009.WI,CI005017.WI,CI005014.WI,CI005015.WI,CI005019.WI,CI005020.WI,CI005027.WI,CI005025.WI,CI005026.WI,CI005028.WI,CI005022.WI,CI005030.WI,CI005021.WI,CI005007.WI,CI005023.WI,CI005008.WI,CI005024.WI,CI005004.WI,CI005029.WI",
    "pct_chg", date, date, "")
citics_group = {
    '交通运输': '基地',
    '传媒': '科技',
    '农林牧渔': '消费',
    '医药': '医药',