@backtest-kit/mongo

中文文档
23.7.0发布于 2026/10/29MITTypeScriptESM + CJS近 30 天下载 2456

MongoDB + Redis persistence adapter for backtest-kit. Replaces the default file-based storage with a production-grade backend — O(1) reads via Redis, atomic writes via Mongoose.

原始使用文档
文档目录8 节
  1. Why
  2. Configuration
  3. API reference
  4. The 16 adapters
  5. How it works
  6. Internal architecture (complete source map)
  7. 🤝 Contribute
  8. 📜 License

💾 @backtest-kit/mongo

MongoDB + Redis persistence for backtest-kit. Swaps the default file storage for a production backend — durable, queryable, atomic, with O(1) cached reads — in one setup() call and zero strategy-code changes.

screenshot

Ask DeepWiki npm TypeScript

📚 Docs · 🌟 Reference implementation · 🐙 GitHub

npm install @backtest-kit/mongo backtest-kit mongoose ioredis
import { setup } from '@backtest-kit/mongo';
setup(); // reads connection settings from env; call once before any trading operation

That single call reimplements all 16 of backtest-kit's IPersist*Instance contracts against MongoDB (source of truth) with a Redis O(1) read cache. Your strategy code does not change.


Why

File storage is perfect on day one and a bottleneck the day you're doing thousands of context-keyed reads per second across parallel symbols. This package moves persistence to MongoDB without touching strategy logic: every read goes Redis-first for the Mongo _id (two O(1) hops), every write is one atomic findOneAndUpdate upsert (read-after-write guaranteed, concurrent duplicates rejected by the unique index), and adapters whose data drives decisions store the simulation timestamp so look-ahead protection is enforceable even inside the database.

  • 🗄️ MongoDB backend — all 16 IPersist*Instance contracts implemented with Mongoose.
  • ⚡ O(1) reads via Redis — one GET + one findById, no B-tree scans on the hot path.
  • 🔒 Atomic writes — findOneAndUpdate({ upsert:true, new:true }) guarantees read-after-write with no race.
  • 🛡️ Look-ahead protection — decision-affecting adapters store the simulation when.
  • 🪦 Soft delete — Measure / Interval / Memory carry a removed flag instead of being deleted (audit trail).
  • 🔌 Zero strategy changes — drop setup() into your entry point; everything else stays the same.

Configuration

Explicit parameters & environment variables
import { setup } from '@backtest-kit/mongo';
setup({
  CC_MONGO_CONNECTION_STRING: 'mongodb://mongo:27017/mydb',
  CC_REDIS_HOST: 'redis', CC_REDIS_PORT: 6379, CC_REDIS_PASSWORD: 'secret',
});
VariableDefaultDescription
CC_MONGO_CONNECTION_STRINGmongodb://localhost:27017/backtest-kit?wtimeoutMS=15000MongoDB connection string
CC_REDIS_HOST127.0.0.1Redis host
CC_REDIS_PORT6379Redis port
CC_REDIS_USER(empty)Redis username
CC_REDIS_PASSWORD(empty)Redis password

Values passed to setup() / setConfig() always take precedence over env vars. Within the CLI, put setup() in config/setup.config.ts — when present, the CLI skips its default file-adapter registration and your config owns persistence.


API reference

ExportDescription
setup(config?)Configure and register all 16 adapters in one call. Reads env when config omitted.
install()Register adapters only — when config was already applied via setConfig/env.
setConfig(config)Override individual connection parameters at runtime.
getConfig()The current merged configuration (env + any setConfig overrides).
setLogger(logger)Replace the internal logger with your own implementation.
getMongo()The connected Mongoose instance (lazy singleton).
getRedis()The connected ioredis instance (lazy singleton).

The 16 adapters

Each adapter covers one persistence slot in backtest-kit. The unique index is the compound key MongoDB enforces at the storage engine.

AdapterCollectionUnique index
Candlecandle-itemssymbol + interval + timestamp
Signalsignal-itemssymbol + strategyName + exchangeName
Strategystrategy-itemssymbol + strategyName + exchangeName
Scheduleschedule-itemssymbol + strategyName + exchangeName
Riskrisk-itemsriskName + exchangeName
Partialpartial-itemssymbol + strategyName + exchangeName + signalId
Breakevenbreakeven-itemssymbol + strategyName + exchangeName + signalId
Storagestorage-itemsbacktest + signalId
Notificationnotification-itemsbacktest + notificationId
Loglog-itemsentryId
Measuremeasure-itemsbucket + entryKey
Intervalinterval-itemsbucket + entryKey
Memorymemory-itemssignalId + bucketName + memoryId
Recentrecent-itemssymbol + strategyName + exchangeName + frameName + backtest
Statestate-itemssignalId + bucketName
Budgetbudget-itemsstrategyName + exchangeName + frameName + backtest + name
Sessionsession-itemsstrategyName + exchangeName + frameName
Write semantics — immutable, mutable, soft-delete
  • Candle is immutable — first write wins; subsequent writes to the same (symbol, interval, timestamp) are silently ignored via $setOnInsert (historical OHLCV never changes).
  • All others use $set — each write replaces the previous value.
  • Measure / Interval / Memory soft-delete — removeMeasureData / removeIntervalData / removeMemoryData set removed: true rather than deleting; listings filter on removed: false, keeping a full audit trail.

How it works

O(1) reads — DbService + CacheService per domain

Every domain is two layers: a DbService (MongoDB) and a CacheService (Redis). Reading state for a context key asks Redis for the Mongo _id first; a hit is two O(1) ops, a miss falls back to an indexed findOne and backfills Redis.

read signal for (BTCUSDT, my_strategy, binance)
  ├─ Redis GET → hit  → Mongo findById(_id)        ← O(1) + O(1)
  └─ Redis GET → miss → Mongo findOne(filter) → Redis SET → return

After every write the Redis entry is refreshed in the same call, so write-then-read always hits the cache.

Atomic writes — read-after-write with no race

backtest-kit requires that once write*Data() returns, the next read*Data() sees the new value. Every write is one findOneAndUpdate round-trip:

const document = await SignalModel.findOneAndUpdate(
  { symbol, strategyName, exchangeName },
  { $set: { payload } },
  { upsert: true, new: true, setDefaultsOnInsert: true },
);
await signalCacheService.setSignalId(readTransform(document.toJSON()));

The filter matches the unique compound index, so MongoDB rejects any concurrent duplicate insert at the storage engine; the returned document is written straight to Redis, making the next read O(1) on fresh data.

Look-ahead bias protection in the DB layer

Adapters whose data influences decisions (Risk, Partial, Breakeven, Recent, State, Budget, Session, Memory, Interval) store when: Number — the simulation timestamp in ms — alongside the payload, so backtest-kit can verify no read returns data written at a future simulation time. Measure is exempt because it caches LLM / external-API responses, where look-ahead bias is not meaningful.


Internal architecture (complete source map)

Layers & files

Public surface — functions/setup.ts (setup/install/setConfig/getConfig/setLogger), index.ts re-exports + getMongo/getRedis.

Adapter classes (classes/Persist*Instance.ts, 17) — each implements one backtest-kit IPersist*Instance contract and delegates to its domain DbService: PersistCandleInstance, PersistSignalInstance, PersistStrategyInstance, PersistScheduleInstance, PersistRiskInstance, PersistPartialInstance, PersistBreakevenInstance, PersistStorageInstance, PersistNotificationInstance, PersistLogInstance, PersistMeasureInstance, PersistIntervalInstance, PersistMemoryInstance, PersistRecentInstance, PersistStateInstance, PersistBudgetInstance, PersistSessionInstance.

Service layer (lib/services/):

  • base/ — MongoService (lazy Mongoose connection), RedisService (lazy ioredis), LoggerService.
  • db/ — one *DbService per domain: the Mongoose models, schemas, unique compound indexes, and findOneAndUpdate upsert logic.
  • cache/ — one *CacheService per domain (CandleCacheService, SignalCacheService, BreakevenCacheService, IntervalCacheService, LogCacheService, MeasureCacheService, MemoryCacheService, NotificationCacheService, PartialCacheService, RecentCacheService, …): Redis _id mapping for O(1) lookups.

Shared primitives (lib/common/) — BaseCRUD (the upsert/read/remove pattern every DbService reuses) and BaseMap (the Redis key-mapping pattern every CacheService reuses).

DI & config — lib/core/{di,provide,types}.ts (IoC container wiring Db/Cache/base services), lib/index.ts (container bootstrap), config/{mongo,redis,params}.ts (connection builders + merged params), interfaces/Logger.interface.ts.

🤝 Contribute

Fork / PR on GitHub.

📜 License

MIT © tripolskypetr

版本与发布记录

@backtest-kit/mongo 共发布 105 个版本,首次发布于 2026/05/16;过去 12 个月发布了 105 个版本。

版本发布日期距今
23.7.0latest2026/10/299 天前
23.6.02026/10/299 天前
23.5.02026/10/2613 天前
23.4.02026/10/2613 天前
23.3.02026/10/2514 天前
23.2.02026/10/2414 天前

关于 @backtest-kit/mongo 的常见问题

@backtest-kit/mongo 是什么?

MongoDB + Redis persistence adapter for backtest-kit. Replaces the default file-based storage with a production-grade backend — O(1) reads via Redis, atomic writes via Mongoose.

如何安装 @backtest-kit/mongo?

在项目目录运行 npm install @backtest-kit/mongo,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ioredis(^5.6.1)、mongoose(^8.23.0)、typescript(^5.0.0)、backtest-kit(^23.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@backtest-kit/mongo 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@backtest-kit/mongo 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@backtest-kit/mongo 可以商用吗?

@backtest-kit/mongo 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@backtest-kit/mongo 的下载量有多少?

根据 npm 官方下载统计,@backtest-kit/mongo 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,456 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@backtest-kit/mongo 还在维护吗?

最新版本 23.7.0 发布于 2026/10/29(9 天前);至今共发布 105 个版本,其中过去 12 个月发布了 105 个;首次发布于 2026/05/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。