Documentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-docs/llms.txt
Use this file to discover all available pages before exploring further.
@backtest-kit/mongo is a production persistence layer for strategies that outgrow the default file-based ./dump/ storage. It implements all 15 IPersist*Instance contracts from backtest-kit using MongoDB as the source of truth and Redis as an O(1) lookup cache. A single setup() call wires everything up before the first persistence call — strategy code stays unchanged.
Install
Setup
Callsetup() from your config/setup.config.ts file. The CLI loads this file once before any module hooks or strategy code run, so the adapters are registered before backtest-kit makes its first persistence call:
Environment Variables
| Variable | Default | Description |
|---|---|---|
CC_MONGO_CONNECTION_STRING | mongodb://localhost:27017/backtest-kit?wtimeoutMS=15000 | MongoDB connection string |
CC_REDIS_HOST | 127.0.0.1 | Redis host |
CC_REDIS_PORT | 6379 | Redis port |
CC_REDIS_USER | (empty) | Redis username |
CC_REDIS_PASSWORD | (empty) | Redis password |
Persistence Adapters
Each of the 15 adapters covers one persistence slot inbacktest-kit. Every write uses findOneAndUpdate with upsert: true on the compound unique index for that domain:
| Adapter | MongoDB Collection | Unique Index |
|---|---|---|
| Candle | candle-items | symbol + interval + timestamp |
| Signal | signal-items | symbol + strategyName + exchangeName |
| Schedule | schedule-items | symbol + strategyName + exchangeName |
| Risk | risk-items | riskName + exchangeName |
| Partial | partial-items | symbol + strategyName + exchangeName + signalId |
| Breakeven | breakeven-items | symbol + strategyName + exchangeName + signalId |
| Storage | storage-items | backtest + signalId |
| Notification | notification-items | backtest + notificationId |
| Log | log-items | entryId |
| Measure | measure-items | bucket + entryKey |
| Interval | interval-items | bucket + entryKey |
| Memory | memory-items | signalId + bucketName + memoryId |
| Recent | recent-items | symbol + strategyName + exchangeName + frameName + backtest |
| State | state-items | signalId + bucketName |
| Session | session-items | strategyName + exchangeName + frameName |
(symbol, interval, timestamp) are silently ignored via $setOnInsert. All other adapters use $set, replacing the previous value on each write.
O(1) Redis Caching
Every domain has two layers: aDbService that talks to MongoDB and a CacheService that talks to Redis. On each read, the cache service checks Redis for the MongoDB _id of the document. A cache hit means two O(1) operations total — one Redis GET and one MongoDB findById. A cache miss falls back to a regular indexed MongoDB query, then writes the _id to Redis so the next call is instant.
backtest-kit performs thousands of context-keyed reads per second — Redis eliminates the per-request B-tree traversal and makes repeated reads effectively free.
Atomic Writes
backtest-kit requires that once write*Data() returns, the very next read*Data() must see the new value. Every write is a single findOneAndUpdate round-trip:
Look-Ahead Bias Protection
Adapters that influence trading decisions — Risk, Partial, Breakeven, Recent, State, Session, Memory, and Interval — store awhen: Number field alongside the payload. This is the simulation timestamp in milliseconds. backtest-kit uses it to verify that no read returns data written at a future simulation time, preserving temporal correctness across backtest runs.
Measure is exempt from this requirement because it caches LLM and external API responses, where look-ahead bias is not meaningful.
Soft Delete
Measure, Interval, and Memory adapters support soft delete. Calling their respectiveremove*Data() functions sets removed: true on the document instead of physically deleting it. Listing operations filter on removed: false, so logically deleted records are excluded from queries while the full audit trail is preserved in MongoDB.