34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
"""vwap 派生列:turnover/volume,volume=0 → null."""
|
|
import sys, os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "vnpy_v4.4.0")))
|
|
|
|
from datetime import datetime
|
|
from vnpy.trader.object import BarData
|
|
from vnpy.trader.constant import Exchange, Interval
|
|
from sanguo_factor.data_adapter import convert_bars_to_alpha_df
|
|
|
|
|
|
def _bar(symbol, dt, close=10.0, volume=100.0, turnover=1000.0):
|
|
return BarData(
|
|
symbol=symbol, exchange=Exchange.SSE, datetime=dt, interval=Interval.DAILY,
|
|
open_price=9.9, high_price=10.1, low_price=9.8, close_price=close,
|
|
volume=volume, turnover=turnover, open_interest=0, gateway_name="T",
|
|
)
|
|
|
|
|
|
def test_vwap_derived():
|
|
df = convert_bars_to_alpha_df([_bar("600000", datetime(2024, 1, 5))])
|
|
assert df["vwap"][0] == 10.0 # 1000 / 100
|
|
|
|
|
|
def test_vwap_null_when_volume_zero():
|
|
df = convert_bars_to_alpha_df([_bar("600000", datetime(2024, 1, 5), volume=0.0, turnover=0.0)])
|
|
assert df["vwap"][0] is None
|
|
|
|
|
|
def test_empty_schema_has_vwap():
|
|
df = convert_bars_to_alpha_df([])
|
|
assert "vwap" in df.columns
|
|
assert df.height == 0
|