Function addExchangeSchema

  • Registers an exchange data source in the framework.

    The exchange provides:

    • Historical candle data via getCandles
    • Price/quantity formatting for the exchange
    • VWAP calculation from last 5 1m candles

    Parameters

    • exchangeSchema: IExchangeSchema

      Exchange configuration object

      Exchange schema registered via addExchange(). Defines candle data source and formatting logic.

      • Optionalcallbacks?: Partial<IExchangeCallbacks>

        Optional lifecycle event callbacks (onCandleData)

      • exchangeName: string

        Unique exchange identifier for registration

      • OptionalformatPrice?: (symbol: string, price: number, backtest: boolean) => Promise<string>

        Format price according to exchange precision rules.

        Optional. If not provided, defaults to Bitcoin precision on Binance (2 decimal places).

      • OptionalformatQuantity?: (symbol: string, quantity: number, backtest: boolean) => Promise<string>

        Format quantity according to exchange precision rules.

        Optional. If not provided, defaults to Bitcoin precision on Binance (8 decimal places).

      • OptionalgetAggregatedTrades?: (
            symbol: string,
            from: Date,
            to: Date,
            backtest: boolean,
        ) => Promise<IAggregatedTradeData[]>

        Fetch aggregated trades for a trading pair. Optional. If not provided, throws an error when called.

        // Backtest implementation: returns historical aggregated trades for the time range
        const backtestAggregatedTrades = async (symbol: string, from: Date, to: Date, backtest: boolean) => {
        if (backtest) {
        return await database.getAggregatedTrades(symbol, from, to);
        }
        return await exchange.fetchAggregatedTrades(symbol);
        };

        // Live implementation: ignores from/to when not in backtest mode
        const liveAggregatedTrades = async (symbol: string, _from: Date, _to: Date, backtest: boolean) => {
        return await exchange.fetchAggregatedTrades(symbol);
        };
      • getCandles: (
            symbol: string,
            interval: CandleInterval,
            since: Date,
            limit: number,
            backtest: boolean,
        ) => Promise<IPublicCandleData[]>

        Fetch candles from data source (API or database).

      • OptionalgetOrderBook?: (
            symbol: string,
            depth: number,
            from: Date,
            to: Date,
            backtest: boolean,
        ) => Promise<IOrderBookData>

        Fetch order book for a trading pair.

        Optional. If not provided, throws an error when called.

        // Backtest implementation: returns historical order book for the time range
        const backtestOrderBook = async (symbol: string, depth: number, from: Date, to: Date, backtest: boolean) => {
        if (backtest) {
        return await database.getOrderBookSnapshot(symbol, depth, from, to);
        }
        return await exchange.fetchOrderBook(symbol, depth);
        };

        // Live implementation: ignores from/to when not in backtest mode
        const liveOrderBook = async (symbol: string, depth: number, _from: Date, _to: Date, backtest: boolean) => {
        return await exchange.fetchOrderBook(symbol, depth);
        };
      • Optionalnote?: string

        Optional developer note for documentation

    Returns void

    addExchange({
    exchangeName: "binance",
    getCandles: async (symbol, interval, since, limit) => {
    // Fetch from Binance API or database
    return [{
    timestamp: Date.now(),
    open: 50000,
    high: 51000,
    low: 49000,
    close: 50500,
    volume: 1000,
    }];
    },
    formatPrice: async (symbol, price) => price.toFixed(2),
    formatQuantity: async (symbol, quantity) => quantity.toFixed(8),
    });