Skip to content

Commit

Permalink
支持指数上市前的行情
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Apr 9, 2024
1 parent 8b865d9 commit 2ad8af0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
7 changes: 6 additions & 1 deletion rqalpha/data/base_data_source/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
ExchangeTradingCalendarStore, FutureDayBarStore,
FutureInfoStore, FuturesTradingParametersStore,InstrumentStore,
ShareTransformationStore, SimpleFactorStore,
YieldCurveStore)
YieldCurveStore, FuturesTradingParameters)


BAR_RESAMPLE_FIELD_METHODS = {
Expand Down Expand Up @@ -405,3 +405,8 @@ def get_open_auction_volume(self, instrument, dt):
# type: (Instrument, datetime.datetime) -> float
volume = self.get_open_auction_bar(instrument, dt)['volume']
return volume

def get_index_available_data_range(self, instrument: Instrument) -> Optional[tuple]:
if instrument.type == INSTRUMENT_TYPE.INDX:
s, e = self._day_bars[INSTRUMENT_TYPE.INDX].get_date_range(instrument.order_book_id)
return convert_int_to_date(s).date(), convert_int_to_date(e).date()
4 changes: 3 additions & 1 deletion rqalpha/data/data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,7 @@ def get_algo_bar(self, id_or_ins, order_style, dt):
return np.nan, 0
bar = self._data_source.get_algo_bar(id_or_ins, order_style.start_min, order_style.end_min, dt)
return (bar[order_style.TYPE], bar["volume"]) if bar else (np.nan, 0)


def get_index_available_data_range(self, instrument: Instrument) -> Optional[tuple]:
return self._data_source.get_index_available_data_range(instrument)

11 changes: 11 additions & 0 deletions rqalpha/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,17 @@ def get_algo_bar(self, id_or_ins, start_min, end_min, dt):
# type: (Union[str, Instrument], int, int, datetime) -> Optional[numpy.void]
# 格式: (date, VWAP, TWAP, volume) -> 案例 (20200102, 16.79877183, 16.83271429, 144356044)
raise NotImplementedError

def get_index_available_data_range(self, instrument):
"""
获取指数行情可用数据区间(指数上市前也可能存在行情)
:param instrument: 合约对象
:type instrument: :class:`~Instrument`
:return: (earliest, latest)
"""
raise NotImplementedError


class AbstractBroker(with_metaclass(abc.ABCMeta)):
Expand Down
18 changes: 13 additions & 5 deletions rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,19 @@ def _subscribe_events(self, event):
raise RuntimeError(
_("benchmark {} not exists, please entry correct order_book_id").format(order_book_id)
)
if ins.listed_date.date() > _s or ins.de_listed_date.date() <= _e:
raise RuntimeError(
_("benchmark {} listed date {} > backtest start date {} or de_listed date {} <= backtest end "
"date {}").format(order_book_id, ins.listed_date.date(), _s, ins.de_listed_date.date(), _e)
)
if ins.type == INSTRUMENT_TYPE.INDX:
available_s, available_e = self._env.data_proxy.get_index_available_data_range(ins)
if available_s > _s or available_e <= _e:
raise RuntimeError(
_("benchmark {} available data start date {} > backtest start date {} or end date {} <= backtest end "
"date {}").format(order_book_id, available_s, _s, available_e, _e)
)
else:
if ins.listed_date.date() > _s or ins.de_listed_date.date() <= _e:
raise RuntimeError(
_("benchmark {} listed date {} > backtest start date {} or de_listed date {} <= backtest end "
"date {}").format(order_book_id, ins.listed_date.date(), _s, ins.de_listed_date.date(), _e)
)

self._env.event_bus.add_listener(EVENT.TRADE, self._collect_trade)
self._env.event_bus.add_listener(EVENT.ORDER_CREATION_PASS, self._collect_order)
Expand Down

0 comments on commit 2ad8af0

Please sign in to comment.