Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions rqalpha/data/base_data_source/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def get_open_auction_bar(self, instrument, dt):
bar = dict.fromkeys(self.OPEN_AUCTION_BAR_FIELDS, np.nan)
else:
bar = {k: day_bar[k] if k in day_bar.dtype.names else np.nan for k in self.OPEN_AUCTION_BAR_FIELDS}
# Day-bar liquidity covers the full session and is unavailable at auction time.
for field in ("volume", "total_turnover"):
if field in bar:
bar[field] = np.nan
bar["last"] = bar["open"] # type: ignore
return bar

Expand Down Expand Up @@ -431,8 +435,10 @@ def get_algo_bar(self, id_or_ins: Union[str, Instrument], start_min: int, end_mi
raise NotImplementedError("open source rqalpha not support algo order")

def get_open_auction_volume(self, instrument: Instrument, dt: datetime):
volume = self.get_open_auction_bar(instrument, dt)['volume']
return volume
auction_bar = self.get_open_auction_bar(instrument, dt)
if auction_bar is None:
return np.nan
return auction_bar.get("volume", np.nan)

# deprecated
def register_instruments_store(self, instruments_store, market: MARKET = MARKET.CN):
Expand Down
63 changes: 63 additions & 0 deletions tests/unittest/test_data/test_base_data_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from datetime import date

import numpy as np

from rqalpha.data.base_data_source.data_source import BaseDataSource


def test_open_auction_bar_does_not_copy_day_liquidity():
bar_dtype = np.dtype(
[
("datetime", "u8"),
("open", "f8"),
("limit_up", "f8"),
("limit_down", "f8"),
("volume", "f8"),
("total_turnover", "f8"),
]
)
full_day_bar = np.array(
(20260908, 10.5, 11.5, 9.5, 123456.0, 789012.0), dtype=bar_dtype
)

class StubDataSource:
OPEN_AUCTION_BAR_FIELDS = BaseDataSource.OPEN_AUCTION_BAR_FIELDS
get_open_auction_bar = BaseDataSource.get_open_auction_bar

def get_bar(self, instrument, dt, frequency):
assert frequency == "1d"
return full_day_bar

auction_bar = BaseDataSource.get_open_auction_bar(
StubDataSource(), "000001.XSHE", date(2026, 9, 8)
)

assert auction_bar["open"] == full_day_bar["open"]
assert auction_bar["limit_up"] == full_day_bar["limit_up"]
assert auction_bar["limit_down"] == full_day_bar["limit_down"]
assert np.isnan(auction_bar["volume"])
assert np.isnan(auction_bar["total_turnover"])

assert np.isnan(
BaseDataSource.get_open_auction_volume(
StubDataSource(), "000001.XSHE", date(2026, 9, 8)
)
)

class AuctionVolumeOverride(StubDataSource):
def get_open_auction_bar(self, instrument, dt):
return {"volume": 321.0}

assert BaseDataSource.get_open_auction_volume(
AuctionVolumeOverride(), "000001.XSHE", date(2026, 9, 8)
) == 321.0

class MissingAuctionBar(StubDataSource):
def get_open_auction_bar(self, instrument, dt):
return None

assert np.isnan(
BaseDataSource.get_open_auction_volume(
MissingAuctionBar(), "000001.XSHE", date(2026, 9, 8)
)
)
20 changes: 20 additions & 0 deletions tests/unittest/test_mod/test_sys_simulation/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ def test_bar_matcher_fills_market_order_by_volume_limit_and_cancels_remainder(fa
assert order.status == ORDER_STATUS.CANCELLED


def test_bar_matcher_uses_explicit_open_auction_volume(fake_env):
fake_env.bar.volume = 900
matcher = DefaultBarMatcher(
fake_env,
make_mod_config(
MATCHING_TYPE.CURRENT_BAR_CLOSE,
volume_limit=True,
),
)
order = make_order(1000)

matcher.match(FakeAccount(), order, open_auction=True)

trades = trade_events(fake_env)
assert len(trades) == 1
assert trades[0].trade.last_quantity == 900
assert order.filled_quantity == 900
assert order.status == ORDER_STATUS.CANCELLED


def test_bar_matcher_leaves_non_crossed_limit_order_active(fake_env):
matcher = DefaultBarMatcher(fake_env, make_mod_config(MATCHING_TYPE.CURRENT_BAR_CLOSE))
order = make_order(100, style=LimitOrder(9.9))
Expand Down