feat(portfolio): LocalUnifiedProvider get_price+前复权(Task1)
This commit is contained in:
@@ -143,3 +143,140 @@ class TestBuildQfqFactor:
|
||||
assert len(f) == 2
|
||||
assert abs(f.iloc[0] - 1.0) < 1e-6
|
||||
assert abs(f.iloc[1] - 1.0) < 1e-6
|
||||
|
||||
|
||||
# ======================== Task 1: get_price fixture ========================
|
||||
@pytest.fixture
|
||||
def unified_provider(tmp_path):
|
||||
"""造小样本 sqlite fixture: dbbardata 日线 + bs_adjust_factor。"""
|
||||
db = tmp_path / "quant_trading.db"
|
||||
c = sqlite3.connect(str(db))
|
||||
c.execute(
|
||||
"CREATE TABLE dbbardata(symbol TEXT, exchange TEXT, datetime TEXT, "
|
||||
"interval TEXT, volume REAL, turnover REAL, open_interest REAL, "
|
||||
"open_price REAL, high_price REAL, low_price REAL, close_price REAL)"
|
||||
)
|
||||
# 600519: 除权日 2024-06-19 raw close 1000 → 900 跳水
|
||||
rows = [
|
||||
("600519", "SSE", "2024-06-18 00:00:00", "d", 1000, 1e6, 0,
|
||||
1000.0, 1010.0, 990.0, 1000.0),
|
||||
("600519", "SSE", "2024-06-19 00:00:00", "d", 1000, 1e6, 0,
|
||||
900.0, 910.0, 890.0, 900.0),
|
||||
("600519", "SSE", "2024-06-20 00:00:00", "d", 1000, 1e6, 0,
|
||||
910.0, 920.0, 900.0, 910.0),
|
||||
]
|
||||
c.executemany("INSERT INTO dbbardata VALUES(?,?,?,?,?,?,?,?,?,?,?)", rows)
|
||||
# 复权因子: 2024-06-19 起除权, factor=0.9
|
||||
c.execute(
|
||||
"CREATE TABLE bs_adjust_factor(code TEXT, dividOperateDate TEXT, "
|
||||
"foreAdjustFactor REAL, backAdjustFactor REAL, adjustFactor REAL)"
|
||||
)
|
||||
c.execute(
|
||||
"INSERT INTO bs_adjust_factor VALUES('sh.600519','2024-06-19',0.9,0,0)"
|
||||
)
|
||||
c.commit()
|
||||
c.close()
|
||||
return LocalUnifiedProvider({"db_path": str(db), "data_dir": str(tmp_path)})
|
||||
|
||||
|
||||
# ======================== Task 1: get_price daily ========================
|
||||
class TestGetPrice:
|
||||
def test_raw_keeps_original_prices(self, unified_provider):
|
||||
# raw: 除权日 close=900 跳水(原值)
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
start_date="2024-06-18",
|
||||
end_date="2024-06-20",
|
||||
fq="raw",
|
||||
)
|
||||
assert len(df) == 3
|
||||
assert abs(df.loc["2024-06-19", "close"] - 900.0) < 1e-6
|
||||
|
||||
def test_qfq_earlier_date_uses_earliest_factor(self, unified_provider):
|
||||
# 2024-06-18 早于除权日 06-19 → factor=0.9 → 1000*0.9=900
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
start_date="2024-06-18",
|
||||
end_date="2024-06-20",
|
||||
fq="qfq",
|
||||
)
|
||||
assert abs(df.loc["2024-06-18", "close"] - 900.0) < 1e-6
|
||||
|
||||
def test_qfq_after_event_uses_event_factor(self, unified_provider):
|
||||
# 2024-06-19/20 ≥ 除权日 → factor=0.9 → 900*0.9=810, 910*0.9=819
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
start_date="2024-06-19",
|
||||
end_date="2024-06-20",
|
||||
fq="qfq",
|
||||
)
|
||||
assert abs(df.loc["2024-06-19", "close"] - 810.0) < 1e-6
|
||||
assert abs(df.loc["2024-06-20", "close"] - 819.0) < 1e-6
|
||||
|
||||
def test_panel_false_returns_long_table(self, unified_provider):
|
||||
# panel=False → 长表含 time + code 列
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
end_date="2024-06-20",
|
||||
count=2,
|
||||
panel=False,
|
||||
fields=["close"],
|
||||
)
|
||||
assert "code" in df.columns
|
||||
assert "time" in df.columns
|
||||
assert len(df) == 2
|
||||
assert "600519.XSHG" in set(df["code"])
|
||||
|
||||
def test_fields_with_missing_column_fills_nan(self, unified_provider):
|
||||
# high_limit 不在 dbbardata → NaN 降级(策略 prepare_stock_list 涨停识别降级)
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
end_date="2024-06-20",
|
||||
count=1,
|
||||
panel=False,
|
||||
fields=["close", "high_limit"],
|
||||
)
|
||||
assert "high_limit" in df.columns
|
||||
# high_limit NaN(不崩)
|
||||
assert pd.isna(df.iloc[0]["high_limit"]) or df.iloc[0]["high_limit"] != df.iloc[0]["high_limit"]
|
||||
|
||||
def test_minute_frequency_returns_empty(self, unified_provider):
|
||||
# 1m 频率无数据 → 返空 DataFrame
|
||||
df = unified_provider.get_price(
|
||||
"600519.XSHG",
|
||||
end_date="2024-06-20",
|
||||
frequency="1m",
|
||||
count=1,
|
||||
panel=False,
|
||||
)
|
||||
assert isinstance(df, pd.DataFrame)
|
||||
assert df.empty
|
||||
|
||||
def test_multi_stocks_panel_false(self, tmp_path):
|
||||
# 多股 panel=False → 长表含 code 列区分
|
||||
db = tmp_path / "t.db"
|
||||
c = sqlite3.connect(str(db))
|
||||
c.execute(
|
||||
"CREATE TABLE dbbardata(symbol TEXT, exchange TEXT, datetime TEXT, "
|
||||
"interval TEXT, volume REAL, turnover REAL, open_interest REAL, "
|
||||
"open_price REAL, high_price REAL, low_price REAL, close_price REAL)"
|
||||
)
|
||||
rows = [
|
||||
("600519", "SSE", "2024-06-19 00:00:00", "d", 1000, 1e6, 0,
|
||||
900.0, 910.0, 890.0, 900.0),
|
||||
("000001", "SZSE", "2024-06-19 00:00:00", "d", 1000, 1e6, 0,
|
||||
10.0, 10.5, 9.8, 10.2),
|
||||
]
|
||||
c.executemany("INSERT INTO dbbardata VALUES(?,?,?,?,?,?,?,?,?,?,?)", rows)
|
||||
c.commit()
|
||||
c.close()
|
||||
p = LocalUnifiedProvider({"db_path": str(db), "data_dir": str(tmp_path)})
|
||||
df = p.get_price(
|
||||
["600519.XSHG", "000001.XSHE"],
|
||||
end_date="2024-06-19",
|
||||
count=1,
|
||||
panel=False,
|
||||
fields=["close"],
|
||||
)
|
||||
assert len(df) == 2
|
||||
assert set(df["code"]) == {"600519.XSHG", "000001.XSHE"}
|
||||
|
||||
Reference in New Issue
Block a user