Skip to main content

Backtest Data Preparation Failure: ClickHouse Credential Precedence Fix

Symptom​

prepare_backtest_data reported:

  • missing candles
  • adapter attempts failed (cTrader / Twelve Data)
  • ClickHouse insert failed

Root Cause​

The ClickHouse analytics adapter used a different credential precedence than the rest of the backend:

  • services/backend/src/adapters/analytics/clickhouse.ts preferred CLICKHOUSE_API_KEY/SECRET first.
  • Other ClickHouse paths preferred CLICKHOUSE_USERNAME/PASSWORD first.

In environments where API keys are read-only:

  • queryOHLCV can succeed (so check_candle_data_availability returns missing normally).
  • replaceOHLCVRange fails on ALTER/INSERT (so prepare_backtest_data fails with ClickHouse insert error).

Fix​

Updated adapter credential resolution to prefer service username/password first, then API key/secret fallback.

File changed:

  • services/backend/src/adapters/analytics/clickhouse.ts

Validation​

  • pnpm typecheck passed.
  • pnpm lint passed with existing warning-only baseline.

Deployments​

  • Dev backend version: defc133b-371f-4a64-8d8c-0072391df9e6
  • Production backend version: d12a200b-8bde-4e39-b9ce-4eaca8d6d9d2

Expected Outcome​

prepare_backtest_data should now be able to stage candles into ClickHouse (assuming credentials/provider APIs are otherwise valid).