def _handle_split(self, trading_date): import rqdatac for order_book_id, position in six.iteritems(self.portfolio.positions): split_df = rqdatac.get_split(order_book_id, start_date="2005-01-01", end_date="2099-01-01") if split_df is None: system_log.warn(_("no split data {}").foramt(order_book_id)) continue try: series = split_df.loc[trading_date] except KeyError: continue # 处理拆股 user_system_log.info(_("split {order_book_id}, {position}").format( order_book_id=order_book_id, position=position, )) ratio = series.split_coefficient_to / series.split_coefficient_from for key in ["_buy_order_quantity", "_sell_order_quantity", "_buy_trade_quantity", "_sell_trade_quantity"]: setattr(position, key, getattr(position, key) * ratio) user_system_log.info(_("split {order_book_id}, {position}").format( order_book_id=order_book_id, position=position, )) user_system_log.info(_("split {order_book_id}, {series}").format( order_book_id=order_book_id, series=series, ))
def gen_splits(d): stocks = rqdatac.all_instruments().order_book_id.tolist() split = rqdatac.get_split(stocks) split['split_factor'] = split['split_coefficient_to'] / split[ 'split_coefficient_from'] split = split[['split_factor']] split.reset_index(inplace=True) split.rename(columns={'ex_dividend_date': 'ex_date'}, inplace=True) split['ex_date'] = [convert_date_to_int(d) for d in split['ex_date']] split.set_index(['order_book_id', 'ex_date'], inplace=True) with h5py.File(os.path.join(d, 'split_factor.h5'), 'w') as h5: for order_book_id in split.index.levels[0]: h5[order_book_id] = split.loc[order_book_id].to_records()
def get_split(order_book_ids, start_date=None): # type: (Union[str, List[str]], Optional[Union[str, datetime.date]]) -> pd.DataFrame """ 获取某只股票到策略当前日期前一天的拆分情况(包含起止日期)。 :param order_book_ids: 证券代码,证券的独特的标识符,例如:'000001.XSHE' :param start_date: 开始日期,用户必须指定,需要早于策略当前日期 :return: 查询时间段内的某个股票的拆分数据 * ex_dividend_date: 除权除息日,该天股票的价格会因为拆分而进行调整 * book_closure_date: 股权登记日 * split_coefficient_from: 拆分因子(拆分前) * split_coefficient_to: 拆分因子(拆分后) 例如:每10股转增2股,则split_coefficient_from = 10, split_coefficient_to = 12. :example: .. code-block:: python3 :linenos: get_split('000001.XSHE', start_date='2010-01-04') #[Out] # book_closure_date payable_date split_coefficient_from \ #ex_dividend_date #2013-06-20 2013-06-19 2013-06-20 10 # split_coefficient_to #ex_dividend_date #2013-06-20 16.0 """ # order_book_id 支持list类型 env = Environment.get_instance() dt = env.trading_dt.date() - datetime.timedelta(days=1) start_date = to_date(start_date) if start_date > dt: raise RQInvalidArgument( _('in get_split, start_date {} is no earlier than the previous test day {}' ).format(start_date, dt)) if isinstance(order_book_ids, six.string_types): order_book_ids = [order_book_ids] order_book_ids = [assure_order_book_id(i) for i in order_book_ids] return rqdatac.get_split(order_book_ids, start_date, dt)