Skip to content

Commit b33b310

Browse files
committed
Enhance DataProxy and BaseDataSource to improve instrument retrieval and factor handling. Introduce error handling for missing instruments in DataProxy and refine factor filtering in BaseDataSource to ensure only relevant data is returned based on listing dates.
1 parent e14ac9c commit b33b310

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

rqalpha/data/base_data_source/data_source.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from rqalpha.const import INSTRUMENT_TYPE, MARKET, TRADING_CALENDAR_TYPE
3333
from rqalpha.interface import AbstractDataSource, ExchangeRate
3434
from rqalpha.model.instrument import Instrument
35-
from rqalpha.utils.datetime_func import (convert_date_to_int, convert_int_to_date, convert_int_to_datetime)
35+
from rqalpha.utils.datetime_func import (convert_date_to_int, convert_int_to_date, convert_int_to_datetime, convert_dt_to_int)
3636
from rqalpha.utils.exception import RQInvalidArgument
3737
from rqalpha.utils.functools import lru_cache
3838
from rqalpha.utils.typing import DateLike
@@ -266,13 +266,26 @@ def _are_fields_valid(fields, valid_fields):
266266
return False
267267
return True
268268

269+
@lru_cache(1024)
269270
def get_ex_cum_factor(self, instrument: Instrument):
270271
try:
271272
ex_factor_store = self._ex_factor_stores[instrument.type, instrument.market]
272273
except KeyError:
273274
return None
274-
275-
return ex_factor_store.get_factors(instrument.order_book_id)
275+
factors = ex_factor_store.get_factors(instrument.order_book_id)
276+
if factors is None:
277+
return None
278+
# 考虑代码复用的情况,需要过滤掉不在上市日期范围内到数据
279+
factors = factors[
280+
(factors["start_date"] >= convert_dt_to_int(instrument.listed_date)) &
281+
(factors["start_date"] <= convert_dt_to_int(instrument.de_listed_date))
282+
]
283+
if len(factors) == 0:
284+
return None
285+
if factors["start_date"][0] != 0:
286+
# kind of dirty,强行设置初始值为 1
287+
factors = np.concatenate([np.array([(0, 1.0)], dtype=factors.dtype), factors])
288+
return factors
276289

277290
def _update_weekly_trading_date_index(self, idx):
278291
env = Environment.get_instance()

rqalpha/data/base_data_source/storage_interface.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,5 @@ def get_dividend(self, order_book_id):
6161

6262
class AbstractSimpleFactorStore:
6363
@abc.abstractmethod
64-
def get_factors(self, order_book_id):
65-
# type: (str) -> np.ndarray
64+
def get_factors(self, order_book_id: str) -> Optional[np.ndarray]:
6665
raise NotImplementedError

rqalpha/data/data_proxy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ def history_bars(
217217
adjust_type: str = 'pre',
218218
adjust_orig: Optional[datetime] = None
219219
):
220-
instrument = self.instrument_not_none(order_book_id)
220+
instruments = self.get_instrument_history(order_book_id, dt)
221+
if len(instruments) == 0:
222+
raise InstrumentNotFound(_("No instrument found at {dt}: {id_or_sym}").format(dt=dt, id_or_sym=order_book_id))
223+
instrument = instruments[-1]
221224
if adjust_orig is None:
222225
adjust_orig = dt
223226
return self._data_source.history_bars(instrument, bar_count, frequency, field, dt,

0 commit comments

Comments
 (0)