Multi-Asset Backtesting Configuration

The backtesting engine supports backtesting multiple assets simultaneously, including stocks, options, futures, bonds, and general assets.

Engine Configuration

Configurations for config in createBacktester and createBacktestEngine:

Key Description Note
"startDate" Start date

Required. DATE type

(e.g., 2020.01.01).

"endDate" End date

Required. DATE type

(e.g., 2020.01.01).

"strategyGroup" Strategy type Required, must be "multiAsset"
"cash" Initial cash

Required. A Dictionary. You can use one or more accounts to manage funds. If several assets share the same account, separate their names with a comma (",") to indicate that they share the same account.

For example:

  • Multi-Asset Shared Funds:
    cashDict["futures, options"]=100000000.
    cashDict["futures, options, stock, bond"]=100000000.
  • Separate Funds for Each Asset:
    cashDict["futures"]=100000000.
    cashDict["options"]=100000000.
    cashDict["stock"]=100000000.
    cashDict["bonds"]=100000000.

When initial cash is configured with bonds/bond, dataType must be 1, multiAssetQuoteUnifiedInput must be false, and JIT is not supported.

"commission" Commissions on spot instruments such as stocks DOUBLE type. Default is 0.0.
"tax" Stamp duty for spot instruments such as stocks DOUBLE type. Default is 0.0.
"frequency" Aggregate snapshot data into OHLC bars at the frequency specified by frequency and trigger onBar INT type. Default is 0.
"callbackForSnapshot" Callback mode triggered for snapshot

INT type. Default is 0. Available values are:

0: Only the onSnapshot callback is triggered.

1: Both the onSnapshot and onBar callbacks are triggered.

2: Only the onBar callback is triggered.

"dataType"

Market data type:

1: Snapshot

3: Minute frequency

4: Daily frequency

When frequency>0 and dataType=1 or 2, the market data are snapshots; the engine internally aggregates snapshot data at the frequency specified by frequency to trigger onBar.
"matchingMode" Order matching mode

INT type. Available values are:

1:

  • Daily: Orders are matched at the closing price.

  • Minute frequency: Orders are matched when market time > order time.

2:

  • Daily: Orders are matched at the opening price.

  • Minute frequency: Orders are matched with the closing price when market time = order time and unfilled orders are matched with mode 1.

3: Orders are matched at the order price.

"benchmark" Benchmark instrument STRING type
"latency" Order latency INT type, in milliseconds. Simulates the latency between the time an user order is submitted and the time order matching begins. The default is 0, which means no latency.
"orderBookMatchingRatio" Fill ratio against the market order book DOUBLE type. Default is 1.0, valid range: [0-1.0].
"matchingRatio" Order matching ratio DOUBLE type. Default is 1.0, valid range: [0-1.0]. By default, it equals orderBookMatchingRatio.
"maintenanceMargin" Maintenance margin ratio DOUBLE type.
"stockDividend" Dividend and Ex-Rights Basic Information Table A table. See field descriptions below for details.
"multiAssetQuoteUnifiedInput" Specifies whether the input market data for multi-asset backtesting is provided in a single table combining multiple assets or separately for each asset. BOOL type. Default is true. In this case, the input market data multiAsset is a table combining different assets.
"msgAs​PiecesOnSnapshot" Specifies whether the onSnapshot callback function is triggered sequentially for each data record or simultaneously for all data records with the same timestamp. BOOL type. The default value is false and onSnapshot is triggered sequentially for each data record. When set to true, all records with the same timestamp trigger onSnapshot simultaneously.

Field descriptions of the stockDividend table:

Field Name Description
symbol Stock symbol
endDate Dividend year
annDate Announcement date
recordDate Record date
exDate Ex-rights/ex-dividend date
payDate Dividend payment date
divListDate Listing date for bonus shares
bonusRatio Bonus share ratio per share
capitalConversion Capital conversion ratio per share
afterTaxCashDiv Dividend per share (after tax)
allotPrice Rights issue price
allotRatio Rights issue ratio per share

Basic Information Table

The createBacktester and setSecurityReference methods take a securityReference parameter. The fields of its basic information table are listed in the following table:

For futures assets (assetType=1), securityReference supports the following two modes:

  • Static mode: The table does not contain a tradeDate column. The configured basic information table is used throughout the backtesting.

  • Trading-date mode: The table contains a tradeDate column. Before the market opens on each trading day, the engine reads the basic information table for each futures contract according to the following rules:

    • If a record with a tradeDate equal to the current trading day exists, use that record.

    • Otherwise, use the latest record whose tradeDate is less than or equal to the current trading day.

Static mode and trading-date mode cannot be mixed within the same engine. Whether the securityReference passed when creating the engine contains tradeDate must be consistent with the basic information table subsequently passed through Backtest::setSecurityReference.

Field Name Data Type Description
symbol SYMBOL or STRING Asset symbol
assetType INT

Asset type

0: Stock

1: Futures

2: Commodity options

3: Stock index options

4: ETF options

5: General assets

6: Bonds

tradeDate DATE Trading date. Only used for futures assets and must not contain null values.
underlyingCode STRING Underlying code; only supported for options assets.
optType INT

Option type; only supported for options assets.

1: Call

2: Put

strikePrice DOUBLE Option strike price; only supported for options assets.
multiplier DOUBLE Contract multiplier
marginRatio DOUBLE Margin ratio
tradeUnit DOUBLE Contract unit
priceUnit DOUBLE Quote unit
priceTick DOUBLE Minimum price increment
commission DOUBLE Commission
deliveryCommissionMode INT

1: commission per lot

2: Trading value * commission

lastTradingDay DATE Last trading day of the contract; only supported for options assets.
exerciseDate DATE Option exercise date; only supported for options assets.
exerciseSettlementDate DATE Exercise settlement date; only supported for options assets.

In trading-date mode, you can update basic information for futures assets, such as marginRatio and commission, using records with different tradeDate values. For the same futures contract, multiplier and tradeUnit always use the values loaded initially. Subsequent changes do not take effect, and the system does not report an error.

Market Data

When you insert data into the engine through the appendQuotationMsg method, msg must be a dictionary:

  • When multiAssetQuoteUnifiedInput is true, the available key values for msg are:

    • "indicator": Historical indicator table.

    • "multiAsset": A table of market data for multiple asset classes. Different asset classes are distinguished by the assetType field.

  • When multiAssetQuoteUnifiedInput is false, the available key values for msg are:

    • "indicator": Historical indicator table.

    • "stock": Stock market data table.

    • "futures": Futures market data table.

    • "options": Options market data table.

Snapshot Data

The fields are described below:

Field Name Data Type Description
symbol SYMBOL

Asset symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (Shanghai Stock Exchange) or "XSHE" (Shenzhen Stock Exchange)
timestamp TIMESTAMP Timestamp
tradingDay DATE Trading date/settlement date
lastPrice DOUBLE Latest traded price
upLimitPrice DOUBLE Limit-up price
downLimitPrice DOUBLE Limit-down price
totalBidQty LONG Total bid quantity
totalOfferQty LONG Total offer quantity
bidPrice DOUBLE[] Bid price list
bidQty LONG[] Bid quantity list
offerPrice DOUBLE[] Ask price list
offerQty LONG[] Ask quantity list
highPrice DOUBLE High price
lowPrice DOUBLE Low price
underlyingPrice DOUBLE Underlying price (required only for options instruments)
assetType INT

Asset type:

0: Stock

1: Futures

2: Options

5: General assets

6: Bonds

The following are the table creation codes for different assets. See the table above for field descriptions.

The table creation statement is as follows:

stock

colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice",
        "downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
        "offerPrice","offerQty","highPrice","lowPrice"]
colType= ["STRING","STRING","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","DOUBLE",
          "DOUBLE","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE","DOUBLE"]

futures

colName=["symbol","symbolSource","timestamp","tradingDay","lastPrice",
         "upLimitPrice","downLimitPrice","totalBidQty","totalOfferQty",
         "bidPrice","bidQty","offerPrice","offerQty","highPrice","lowPrice"]
colType= ["STRING","STRING","TIMESTAMP","DATE","DOUBLE","DOUBLE","DOUBLE",
          "DOUBLE","DOUBLE","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE",
          "DOUBLE"]

options

colName=["symbol","symbolSource","timestamp","tradingDay","lastPrice",
         "upLimitPrice","downLimitPrice","totalBidQty","totalOfferQty",
         "bidPrice","bidQty","offerPrice","offerQty","highPrice",
         "lowPrice","underlyingPrice"]
colType= ["STRING","STRING","TIMESTAMP","DATE","DOUBLE","DOUBLE","DOUBLE",
          "DOUBLE","DOUBLE","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE",
          "DOUBLE", "DOUBLE"]

multiAsset

colName=["symbol","symbolSource","timestamp","tradingDay","lastPrice","upLimitPrice",
        "downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty","offerPrice",
        "offerQty","highPrice","lowPrice","assetType"]
colType= ["STRING","STRING","TIMESTAMP","DATE","DOUBLE","DOUBLE","DOUBLE","DOUBLE","DOUBLE",
        "DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE","DOUBLE", "INT"]
messageTable=table(10000000:0, colName, colType)
Note:
  • If option assets exist, you must add a DOUBLE field underlyingPrice.
  • Field names must exactly match those in the table. The first column must be the symbol column. In addition, columns of type INT, DOUBLE, and STRING, or a column named signal of type DOUBLE ARRAY VECTOR, are supported as extension fields.

Minute-Level or Daily Market Data

The fields are described below:

Field Data Type Description
symbol SYMBOL Instrument symbol
symbolSource STRING "XSHG" (Shanghai Stock Exchange) or "XSHE" (Shenzhen Stock Exchange)
tradeTime TIMESTAMP Timestamp
tradingDay DATE Trading date/settlement date
lastPrice DOUBLE Latest traded price
upLimitPrice DOUBLE Limit-up price
downLimitPrice DOUBLE Limit-down price
totalBidQty LONG Total bid volume
totalOfferQty LONG Total ask volume
bidPrice DOUBLE[] Bid price
bidQty LONG[] Bid volume
offerPrice DOUBLE[] Ask price
offerQty LONG[] Ask volume
highPrice DOUBLE High price
lowPrice DOUBLE Low price
assetType INT

Asset type:

0: Stock

1: Futures

2: Options

5: General assets

6: Bonds

The following are the table creation codes for different assets. See the table above for field descriptions:

stock

colName=[`symbol,`symbolSource,`tradeTime,`open,`low,`high,`close,`volume,
         `upLimitPrice,`downLimitPrice]
colType=[SYMBOL,SYMBOL,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,DOUBLE,DOUBLE]

futures

colName=[`symbol,`symbolSource,`tradeTime,"tradingDay",`open,`low,`high,`close,
         `volume,`upLimitPrice,`downLimitPrice]
colType=[SYMBOL,SYMBOL,TIMESTAMP,DATE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,DOUBLE,
         DOUBLE]

options

colName=[`symbol,`symbolSource,`tradeTime,"tradingDay",`open,`low,`high,`close,
         `volume,`upLimitPrice,`downLimitPrice,`underlyingPrice]
colType=[SYMBOL,SYMBOL,TIMESTAMP,DATE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,DOUBLE,
         DOUBLE,DOUBLE]

multiAsset

colName=[`symbol,`symbolSource,`tradeTime,`tradingDay,`open,`low,`high,`close,`volume,`upLimitPrice,
        `downLimitPrice,`assetType]
colType=[SYMBOL,SYMBOL,TIMESTAMP,DATE,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,
DOUBLE,DOUBLE,INT]
messageTable=table(10000000:0, colName, colType)
Note:
  • If option assets exist, you must add a DOUBLE field underlyingPrice.
  • Field names must exactly match those in the table. The first column must be the symbol column. In addition, columns of type INT, DOUBLE, and STRING, or a column named signal of type DOUBLE ARRAY VECTOR, are supported as extension fields.