HKUDS/Vibe-Trading · error · KeyError

vwap(equity_cn) requires panel['amount'] and panel['volume']

Error message

vwap(equity_cn) requires panel['amount'] and panel['volume']

What it means

For Market.EQUITY_CN, vwap is computed as (amount * 1000) / (volume * 100 + 1) from CNY amount and share volume. If the panel lacks either 'amount' or 'volume' columns (and no precomputed 'vwap' column exists), KeyError is raised.

Source

Thrown at agent/src/factors/base.py:348

      suspended bars.
    - ``equity_us`` / ``equity_hk`` / ``equity_in`` / ``equity_kr`` /
      ``futures``: typical price ``(H + L + C + O) / 4`` when ``panel["vwap"]``
      is absent. India (NSE/BSE) bars from Yahoo and Korea (KRX) bars from
      pykrx carry raw price/volume (no Tushare 千元/手 scaling), so the
      typical-price form applies unchanged.
    - ``crypto``: prefer ``panel["vwap"]`` if provided, else typical price.

    Any missing required column → NaN propagation; never silent zero.
    """
    if isinstance(market, str):
        market = Market(market)

    if "vwap" in panel:
        return panel["vwap"]

    if market is Market.EQUITY_CN:
        if "amount" not in panel or "volume" not in panel:
            raise KeyError("vwap(equity_cn) requires panel['amount'] and panel['volume']")
        return safe_div(panel["amount"] * 1000.0, panel["volume"] * 100.0 + 1.0)

    required = ("open", "high", "low", "close")
    missing = [k for k in required if k not in panel]
    if missing:
        raise KeyError(f"vwap({market.value}) requires panel keys {required}; missing {missing}")
    return (panel["open"] + panel["high"] + panel["low"] + panel["close"]) / 4.0

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Include 'amount' and 'volume' columns in the CN panel
  2. Or supply a precomputed panel['vwap'] column which is returned as-is
  3. Ensure your data source (e.g. CN exchange feed) provides CNY amount

Example fix

# before
panel = load_ohlc(codes)  # no amount
vwap(panel, Market.EQUITY_CN)
# after
panel = load_ohlc_with_amount(codes)
vwap(panel, Market.EQUITY_CN)
Defensive patterns

Strategy: validation

Validate before calling

required = {'amount', 'volume'}
missing = required - set(panel.keys())
assert not missing, f'equity_cn panel missing: {missing}'

Type guard

def has_cn_vwap_inputs(panel: dict) -> bool:
    return 'vwap' in panel or ({'amount', 'volume'} <= set(panel.keys()))

Try / catch

try:
    v = vwap(panel, Market.EQUITY_CN)
except KeyError as e:
    log.warning(f'vwap unavailable: {e}'); v = None  # or fallback factor

Prevention

When it happens

Trigger: Calling vwap on an equity_cn panel missing panel['amount'] or panel['volume'], e.g. only OHLC bars were loaded.

Common situations: Data loaders that only fetch OHLCV close/volume but not CNY amount, switching market from US to CN without changing the data pipeline, or a precomputed 'vwap' column being dropped during preprocessing.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/512f0868a06a8204. Report an issue: GitHub.