Skip to content

Commit

Permalink
pr update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Mar 25, 2024
1 parent a903c22 commit 56a1239
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
20 changes: 3 additions & 17 deletions rqalpha/data/bundle.py
Expand Up @@ -633,16 +633,15 @@ def update_futures_trading_parameters(path, end_date):


class AutomaticUpdateBundle(object):
def __init__(self, path: str, filename: str, rqdata_api: Callable, fields: List[str], end_date: datetime.date, completion: bool =False) -> None:
def __init__(self, path: str, filename: str, api: Callable, fields: List[str], end_date: datetime.date) -> None:
if not os.path.exists(path):
os.makedirs(path)
self._file = os.path.join(path, filename)
self._trading_dates = None
self._filename = filename
self._rqdata_api = rqdata_api
self._api = api
self._fields = fields
self._end_date = end_date
self._completion = completion # 缓存 h5 文件时,是否需要对缺失数据的日期进行补 0
self.updated = []
self._env = Environment.get_instance()

Expand Down Expand Up @@ -709,7 +708,7 @@ def _auto_update_task(self, instrument: Instrument) -> None:
h5.close()

def _get_array(self, instrument: Instrument, start_date: datetime.date) -> Optional[np.ndarray]:
df = self._rqdata_api(instrument.order_book_id, start_date, self._end_date, self._fields)
df = self._api(instrument.order_book_id, start_date, self._end_date, self._fields)
if not (df is None or df.empty):
df = df[self._fields].loc[instrument.order_book_id] # rqdatac.get_open_auction_info get Futures's data will auto add 'open_interest' and 'prev_settlement'
record = df.iloc[0: 1].to_records()
Expand All @@ -722,18 +721,5 @@ def _get_array(self, instrument: Instrument, start_date: datetime.date) -> Optio
arr['trading_dt'] = trading_dt
for field in self._fields:
arr[field] = df[field].values
if self._completion:
arr = self._completion_zero(instrument, arr)
return arr
return None

def _completion_zero(self, instrument: Instrument, arr: np.ndarray) -> np.ndarray:
completion_start_date = max(instrument.listed_date.date(), datetime.date(2005, 1, 4))
trading_dates = self._env.data_proxy._data_source.get_trading_dates(completion_start_date, self._end_date)
trading_dates = convert_date_to_date_int(trading_dates)
completion_dt = np.array(list(set(trading_dates).difference(set(arr['trading_dt']))))
arr_zero = np.zeros((completion_dt.shape[0], ), dtype=arr.dtype)
arr_zero['trading_dt'] = completion_dt
arr = np.sort(np.concatenate((arr, arr_zero)), order="trading_dt")
return arr

3 changes: 1 addition & 2 deletions rqalpha/data/trading_dates_mixin.py
Expand Up @@ -116,8 +116,7 @@ def _get_future_trading_date(self, dt):

return td

def batch_get_trading_date(self, dt_index):
# type: (DatetimeIndex) -> DatetimeIndex
def batch_get_trading_date(self, dt_index: pd.DatetimeIndex):
# 获取 numpy.array 中所有时间所在的交易日
# 认为晚八点后为第二个交易日,认为晚八点至次日凌晨四点为夜盘
dt = dt_index - datetime.timedelta(hours=4)
Expand Down
Expand Up @@ -32,7 +32,7 @@ def init_fixture(self):
self._auto_update_bundle_module = AutomaticUpdateBundle(
path=self._path,
filename="open_auction_volume.h5",
rqdata_api=self._mock_get_open_auction_info,
api=self._mock_get_open_auction_info,
fields=['volume'],
end_date=datetime.date(2024, 2, 28),
)
Expand Down

0 comments on commit 56a1239

Please sign in to comment.