Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/theonetrade/uzse-backtest-app/llms.txt

Use this file to discover all available pages before exploring further.

The exchange module is the integration layer between the MongoDB candle store and the backtest-kit toolchain. Rather than hardwiring a connection to a live exchange API, backtest-kit exposes an addExchangeSchema registration function that lets you supply any async data source — in this case, a MongoDB query against locally built OHLCV candles. Every tool in the project (the visual editor, the Pine Script runner, and the data dump utility) registers the same "mongo-exchange" schema through this mechanism, so all three share an identical data contract.

The Three Module Files

All three module files in the modules/ directory are thin wrappers that call addExchangeSchema with the same implementation. They exist as separate entry points so each tool can load only what it needs at startup:

editor.module.ts

Loaded by the visual editor when you run npm start. Registers the "mongo-exchange" schema so the chart UI can fetch candles for any selected symbol and timeframe.

pine.module.ts

Loaded when running Pine Script backtests programmatically. Provides the same MongoDB-backed candle feed to the @backtest-kit/pinets execution engine.

dump.module.ts

Loaded for data export and dump operations. Allows external scripts to pull OHLCV data from MongoDB through the standard backtest-kit candle interface.

Full Module Code

Each of the three module files contains the following implementation (shown here as editor.module.ts):
import { addExchangeSchema } from "backtest-kit";
import { CandleModel } from "../schema/Candle.schema";
import "../config/setup";

addExchangeSchema({
  exchangeName: "mongo-exchange",
  getCandles: async (symbol, interval, since, limit) => {
    const candles = await CandleModel.find(
      { symbol, interval, timestamp: { $gte: since.getTime() } },
      { timestamp: 1, open: 1, high: 1, low: 1, close: 1, volume: 1, _id: 0 }
    )
      .sort({ timestamp: 1 })
      .limit(limit)
      .lean();

    return candles.map(({ timestamp, open, high, low, close, volume }) => ({
      timestamp,
      open,
      high,
      low,
      close,
      volume,
    }));
  },
});
The import "../config/setup" line at the top of each module file handles the MongoDB connection automatically. config/setup.ts calls mongoose.connect() using the MONGO_URI environment variable when it is imported, so no explicit connection management is needed in the module itself. The database is ready by the time getCandles is first invoked.

addExchangeSchema API Reference

exchangeName
string
required
A unique string identifier for this data source within the backtest-kit registry. This project uses "mongo-exchange". The visual editor references this name when resolving which getCandles function to call for a given chart.
getCandles
async function
required
An async function that accepts four parameters and returns an array of OHLCV candle objects. backtest-kit calls this function whenever the editor or backtest engine needs market data.Parameters:
symbol
string
required
The ticker symbol to query, e.g. "HMKB" or "UZ7011340005". Must match the symbol field stored in the candle-items collection.
interval
string
required
The candlestick timeframe string, e.g. "1m", "1h", "1d". Must match the interval field in the candle-items collection. See Timeframes Reference for all valid values.
since
Date
required
A JavaScript Date object representing the earliest timestamp to include. The query converts this to milliseconds via since.getTime() and applies a $gte filter on the timestamp field.
limit
number
required
The maximum number of candles to return. Passed directly to Mongoose’s .limit() method. The editor controls this value based on the visible chart window.
Return value: An array of plain objects with the following shape, matching the Candle Schema:
{
  timestamp: number; // Unix milliseconds
  open:      number;
  high:      number;
  low:       number;
  close:     number;
  volume:    number;
}

MongoDB Query Details

The getCandles implementation constructs a Mongoose query with the following characteristics:
AspectDetail
Filter{ symbol, interval, timestamp: { $gte: since.getTime() } }
ProjectionReturns only timestamp, open, high, low, close, volume; excludes _id
SortAscending by timestamp ({ timestamp: 1 })
LimitCapped at the limit argument provided by the caller
Lean.lean() is called to return plain JS objects instead of Mongoose documents, improving throughput
The timestamp field is stored as a Unix millisecond integer in MongoDB. since.getTime() converts the Date argument to the same unit before comparison.
The query does not apply an upper-bound timestamp filter — it returns all candles from since up to limit records. If limit is very large and the since date is far in the past, the query may return a significant amount of data. The editor manages this automatically, but custom scripts using dump.module.ts should set a reasonable limit.

Adapting to a Different Data Source

The addExchangeSchema contract only requires that getCandles returns an array of { timestamp, open, high, low, close, volume } objects. The internal implementation can use any data retrieval mechanism.
To swap MongoDB for a different backend, replace the body of getCandles with any async logic that produces the correct return shape. Examples:
// Read from a local CSV file
getCandles: async (symbol, interval, since, limit) => {
  const rows = await parseCsv(`./data/${symbol}_${interval}.csv`);
  return rows
    .filter(r => r.timestamp >= since.getTime())
    .slice(0, limit);
}

// Fetch from a REST API
getCandles: async (symbol, interval, since, limit) => {
  const res = await fetch(`https://api.example.com/candles?symbol=${symbol}&interval=${interval}&since=${since.getTime()}&limit=${limit}`);
  return res.json();
}
The exchangeName value and the return object shape must remain unchanged — everything else is up to you.

Visual Editor

Start the backtest-kit chart editor that loads editor.module.ts on launch.

Pine Script

Apply Pine Script v6 indicators to the candle data served by this module.

Candle Schema

See the full MongoDB document structure for the OHLCV records queried here.

MongoDB Infrastructure

Configure and run the MongoDB instance that backs the exchange module.

Build docs developers (and LLMs) love