Stock Backtesting Configuration

The stock asset market data types supported by the backtesting platform include: tick-by-tick or tick-by-tick + snapshot, snapshot, snapshot + tick-by-tick trade details, minute frequency, daily frequency, tick-by-tick (wide table), tick-by-tick + snapshot (wide table), snapshot + minute frequency.

Engine Configuration

Refer to the table below for the configuration of the config parameter of the createBacktester method and the userConfig parameter of the createBacktestEngine method (note that the strategy type for stock backtesting must be set to “stock”):

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; set to “stock”
"cash" Initial cash flow Required. DOUBLE type
"commission" Commission DOUBLE type; default 0.0
"tax" Stamp duty DOUBLE type; default 0.0
"dataType" Market data type Required. INT type. Possible values are:

0: Stock tick-by-tick (tick-by-tick orders and tick-by-tick trade details) + snapshot

1: Snapshot

2: Snapshot data + tick-by-tick trade details

3: Minute frequency

4: Daily frequency

5: Stock tick-by-tick (tick-by-tick orders and tick-by-tick trade details merged into a wide table)

6: Stock tick-by-tick + snapshot (tick-by-tick orders, tick-by-tick trade details, and snapshot merged into a wide table)

7: Snapshot + minute frequency (This mode is supported only by engines created through the createBacktester method and does not support JIT.)

“frequency”

Synthesize snapshots from tick-by-tick data at the specified frequency

Or synthesize bars from snapshot data at the specified frequency

INT type. Default is 0
  • When dataType=0 (tick-by-tick data) or dataType=5, you must set frequency>0. The system synthesizes snapshots from tick-by-tick data at the frequency interval and triggers the onSnapshot callback function accordingly.

  • When dataType=1 or 2, if you set frequency>0, the system synthesizes snapshot data into bar data at the frequency interval and triggers the onBar callback function accordingly.

"msgAsTable" Market data format BOOL type. Default is false

false: Dictionary

true: Table (you can create the engine only through the createBacktestEngine method)

“matchingMode“ Order matching mode INT type. Available values are:

1:

  • Daily: Orders are matched at the closing price.

  • Minute frequency: Orders are matched when the market data timestamp is later than the order timestamp

2:

  • Daily: Orders are matched at the opening price.

  • Minute frequency: When the market data timestamp is equal to the order time, orders are matched at the closing price of the current market data record. Subsequent unfilled orders are matched as described in mode 1.

3: Orders are filled at the order price.

Note:
  • When dataType=3 or 4, the default is order matching mode 1.
  • When dataType is not 3 or 4 (backtesting strategies based on high-frequency market data), if this parameter is set to 1 or 2, the order matching simulator matches orders according to price priority and time priority.
“benchmark” Benchmark instrument STRING

Shanghai Stock Exchange (SSE) symbols end with ".XSHG".

Shenzhen Stock Exchange (SZSE) symbols end with ".XSHE".

For example, “000300.XSHG”

"latency" Order latency INT type, in milliseconds. Simulates the latency between the time a user's order is submitted and the time it is processed. The default is 0, which means no delay.
“stockDividend” Dividend and Ex-Rights Basic Information Table

TABLE type; see below for field descriptions. Alternatively, use DICT type; if convertible bonds are included, DICT type is required:

dividend=dict(STRING,ANY)
dividend["stocks"]=tb1           //Stocks and funds
dividend["convertibleBonds"]=tb2 //Convertible bonds
“enableAlgoOrder” Whether to enable algorithmic orders BOOL type:

true: Enabled

false: Disabled

“enableIndicatorOptimize” Enable indicator optimization BOOL type. Default is false

true: Enabled

false: Disabled

“isBacktestMode” Whether it is in backtest mode BOOL type. Default is true

true: Backtesting

false: Matching

“dataRetentionWindow” Data retention window when indicator optimization starts STRING type. This parameter takes effect only when enableIndicatorOptimize=true.
  • When isBacktestMode=true, the default value is “None”. The available values are:

    • “None”: No data retained

    • "ALL": All data retained

    • "20d": Retains data by trading days (i.e., the number of trading days). For example, “20d” means 20 trading days.

    • "20": Retains data by record count. For example, “20” means retaining the latest 20 records for each symbol.

  • No configuration is required when isBacktestMode=false.

”addTimeColumnInIndicator“ Whether to add a time column to the indicator table when subscribing to indicators. BOOL type. Default is false

true: Include the time column

false: Do not include the time column

"context" Structure of the market data logic context class DICT type. A dictionary of market data global variables, for example:
context=dict(STRING,ANY)
context["buySignalRSI"]=70.
userConfig["context"]=context
”callbackForSnapshot“ Callback mode triggered by snapshot market data Available only when dataType=1.

INT type. Default is 0. Available values are:

0: Triggers onSnapshot only.

1: Triggers both onSnapshot and onBar.

2: Triggers onBar only.

When frequency>0, the onBar callback must be triggered; that is, callbackForSnapshot must be set to 1 or 2.

“enableSubscriptionToTickQuotes” Whether to subscribe to tick-by-tick market data Available only when dataType=0, 5, or 6.

BOOL type. Default is false. Must be set to true when dataType is 0 or 5, and the onTick market data callback is used.

true: Subscribe.

false: Do not subscribe.

“outputQueuePosition”

Whether to obtain the order's position in the market.

If this information is output, the following five metrics are added to the trade details and unfilled order methods:

  • Total quantity of unfilled orders in the market at prices better than the order price

  • Total quantity of unfilled orders in the market at prices worse than the order price

  • Total quantity of unfilled orders in the market at the order price

  • Total quantity of unfilled orders in the market at the order price that arrived before the user's order time

  • Number of market depth levels at prices better than the order price

Available only when dataType=0, 5, or 6 and enableSubscriptionToTickQuotes=true.

INT type. Available values are:

0: Default. Does not output the information.

1: Includes the latest market data record in the order book when calculating the preceding metrics for order matching and execution.

2: Excludes the latest market data record from the order book when calculating the preceding metrics for order matching and execution. In other words, the metrics reflect the order's position before order matching is calculated.

“prevClosePrice” Previous Closing Price Data Table Available only when dataType=0, 5, or 6.

A table with the following three columns:

[symbol, tradeDate,prevClose]

For tick-by-tick market data from the SZSE, the previous closing price of ChiNext stocks must be provided; otherwise, order matching results may not meet expectations.

“orderBookMatchingRatio” Fill percentage against the market order book Available only when dataType=0, 1, 2, 5, or 6.

DOUBLE type; default 0.2; valid range: 0–1.0.

“matchingRatio” Interval order matching ratio Available only when dataType=0, 1, 2, 5, or 6.

DOUBLE type; default 0.2; valid range: 0–1.0. By default, this value is the same as orderBookMatchingRatio.

“setLastDayPosition” Set core position TABLE type; sets the core position for each stock in the selected pool.
"enableMinimumPerTransactionFee" Whether to enable a minimum transaction fee for each stock trade
  • true: When the transaction fee for a single trade is less than 5, the system automatically charges 5.
  • false: trading value * commission
"enableSellCloseRestrict" Whether to restrict the closing of positions opened on the current day
  • false (default): No restriction
  • true: Positions opened today via buy to open cannot be closed via sell to close; only previously held positions can be closed.
"msgAsPiecesOnSnapshot" Specifies whether the onSnapshot callback function is triggered sequentially for each data record or simultaneously for all data records with the same timestamp during backtesting. BOOL type. The default value is false. When set to false, onSnapshot is triggered sequentially for each data record.
"mutualTrigger" Set the trigger method for order confirmation and cancellation
  • false (default): Trigger based on the latest market data time for the instrument received by the order matching simulator;

  • true: Trigger based on the latest market data time received by the order matching simulator;

Supported only for dataType=1, 2, 5, and 6.

Set the core position using “setLastDayPosition”. The fields of “setLastDayPosition” in the engine configuration are described below:

Field Name Type Description
symbol SYMBOL Stock symbol
longPosition LONG Long Position Quantity
costPrice DOUBLE Average Position Price
closePrice DOUBLE Previous closing price

Note:

Engine configuration parameters differ by stock market data type (dataType):

  • The “callbackForSnapshot” parameter for the snapshot market data callback trigger mode is set only for snapshot market data;

  • The “enableSubscriptionToTickQuotes” parameter for whether to subscribe to tick-by-tick market data, the “outputQueuePosition” parameter for whether to obtain the order’s position in the market data, and the “prevClosePrice” previous closing price data table can be set only for market data types that contain tick-by-tick data, namely dataType=0, 5, and 6;

  • The “orderBookMatchingRatio” matching percentage against the market data order book and the “matchingRatio” interval order matching ratio can be set only for market data types that contain snapshots, namely dataType=0, 1, 2, 5, and 6.

Dividend and Ex-Rights Basic Information Table Description

The fields of the stock or fund dividend and ex-rights basic information table, “stockDividend”, are described below:

Field Name
symbol Stock symbol
endDate Dividend year
annDate Proposal announcement date
recordDate Record date
exDate Ex-rights/ex-dividend date
payDate Dividend payment date
divListDate Bonus share listing date
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

The fields of the convertible bond dividend and ex-rights basic information table, “stockDividend”, are described below:

Field Description
symbol Convertible bond code
recordDate Record date
payDate Convertible bond interest payment date
afterTaxCashDiv Convertible bond coupon rate

Convertible Bond Interest Payment Table

The "stockDividend" parameter supports configuration as a dictionary, where you can use "convertibleBonds" to specify the convertible bond interest payment table, for example:

dividend=dict(STRING,ANY)
dividend["convertibleBonds"]=tb2  // Convertible bond interest payment table

The convertible bond interest payment table contains the following fields:

Field Description
symbol Convertible bond code
recordDate Record date
payDate Convertible bond interest payment date
afterTaxCashDiv Convertible bond coupon rate

Margin Trading and Securities Lending

Engine Configuration

Regarding the configuration of the config parameter of the createBacktester method and the userConfig parameter of the createBacktestEngine method, in addition to the basic parameter configuration described above, you can add the parameter configuration required for margin trading and securities lending market data according to the table below. The strategy type must be changed to "securityCreditAccount", and only snapshot, minute, and daily market data types are supported. In addition, the market data and market data callback function descriptions for margin trading and securities lending strategies are the same as those for stocks. Margin trading and securities lending strategies support dividend and ex-rights settings; for the specific schema, refer to the description of the dividend and ex-rights table for stocks.

key Description Note
"strategyGroup" Strategy type Required; "securityCreditAccount"
"dataType" Market data type Required. INT type. Possible values are:

1: Snapshot

3: Minute frequency

4: Daily frequency

7: Snapshot + minute frequency (This mode is supported only by engines created through the createBacktester method and does not support JIT.)

“lineOfCredit” Line of credit Required; DOUBLE type; maximum margin financing and securities lending quota (margin financing + securities lending)
“marginTradingInterestRate” Margin interest rate Required; DOUBLE type; for example, 0.15
“secuLendingInterestRate” Securities lending rate Required; DOUBLE type; can be set to a value different from the margin interest rate, such as 0.15
“maintenanceMargin” Maintenance margin ratio Required; DOUBLE array type; for example, [1.45,1.3,1.2]; the three elements are the warning line, call line, and liquidation line, respectively
“longConcentration”

Net long concentration

The concentration is calculated as follows:

Market value of stock_i holdings / total market value of holdings

Optional; DOUBLE array type; controls the long position purchase amount, for example, [1.0,0.85,0.6]; the three elements control the three warning lines, respectively. The last element is the uppermost line, and the first element is the lowest line.
“shortConcentration“

Net short liability concentration

The concentration is calculated as follows:

Market value of stock_i holdings / total market value of holdings

Optional; DOUBLE array type; controls the short sale amount, for example, [1.0,0.85, 0.6]. A lower concentration indicates that the portfolio reduces risk.
“outputOrderInfo” Whether to output risk control logs (in the order details table) BOOL type

true: output

false (default): do not output

“repayWithoutMarginBuy“ Controls whether securities purchased on margin can offset securities sold short BOOL type

true: offsetting is allowed

false (default): offsetting is not allowed

“setLastDayPosition” Set core position TABLE type; sets the core position for each stock in the selected pool.
  • To set the core position, use "setLastDayPosition". The specific fields of "setLastDayPosition" in the engine configuration are described as follows:

Field Type Note
symbol SYMBOL Instrument symbol
marginSecuPosition LONG Collateral purchase position quantity
marginSecuAvgPrice DOUBLE Average buy execution price
marginPosition LONG Margin purchase position quantity
marginBuyValue DOUBLE Margin purchase amount
secuLendingPosition LONG Short sale position quantity
secuLendingSellValue DOUBLE Short sale amount
closePrice DOUBLE Closing price
conversionRatio DOUBLE Collateral discount rate
tradingMargin DOUBLE Margin financing ratio
lendingMargin DOUBLE Securities lending margin ratio

Basic Information Table Description

Basic information table for the securityReference parameter of the createBacktester method and the securityReference parameter of the setSecurityReference method: The fields in the basic information table for daily tradable securities are described as follows:

Field Type Note
symbol SYMBOL Instrument symbol
tradeDate DATE Trading day
symbolSource SYMBOL Exchange
securityRating SYMBOL Security classification
marginable BOOL Whether it is a marginable security

true: buying with collateral is allowed

false: buying with collateral is not allowed

conversionRatio DOUBLE Collateral discount rate

(affects the available margin balance)

tradingMargin DOUBLE Margin financing ratio

(affects the available margin balance)

lendingMargin DOUBLE Securities lending margin ratio

(affects the available margin balance)

eligibleForMarginTrading BOOL Whether margin buying is allowed

true: margin buying is allowed

false: margin buying is not allowed

eligibleForLending BOOL Whether short selling is allowed

true: short selling is allowed

false: short selling is not allowed

Tick-by-Tick or Tick-by-Tick + Snapshot

Market Data Schema

When you insert data into the engine through the appendQuotationMsg method, the msg schema is:

colName=`msgTime`msgType`msgBody`symbol`channelNo`seqNum
colType= [TIMESTAMP, SYMBOL, BLOB,STRING,INT,LONG]
messageTable=streamTable(10000000:0, colName, colType)

When tick-by-tick market data is available, msgType has four types: "entrust", "trade", "snapshot", and "END". When using tick-by-tick data to synthesize snapshot data at a specified frequency, you do not need to use market data with msgType "snapshot". In this case, the system generates snapshot data at the corresponding frequency based on tick-by-tick trade data and order data. Tick-by-tick data tables with different msgType values are replayed using the replayDS function and replay output to obtain the messageTable table required for backtesting. The following sections describe the schemas of the various tick-by-tick data tables:

Tick-by-tick data entrust and trade table schemas:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 indicates entrust data; 1 indicates trade data
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG trade corresponds to its raw data;

Populated with the order number in entrust

sellNo LONG trade corresponds to its raw data;

Populated with the order number in entrust

direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number

Snapshot table schema:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
lastPrice DOUBLE Latest traded price
upLimitPrice DOUBLE Limit up price
downLimitPrice DOUBLE Limit down price
totalBidQty LONG Total bid quantity during the interval
totalOfferQty LONG Total offer quantity during the interval
bidPrice DOUBLE[] Bid price list
bidQty LONG[] Bid quantity list
offerPrice DOUBLE[] Offer price list
offerQty LONG[] Offer quantity list
signal DOUBLE[] Indicator list
seqNum LONG Tick-by-tick data sequence number
prevClosePrice DOUBLE Previous closing price

When the backtest market data replay ends, add a message with msgType "END". The following example:

messageTable=select top 1* from messageTable where msgTime=max(msgTime)
update messageTable set msgType="END"
update messageTable set msgTime=concatDateTime(msgTime.date(),16:00:00)
Backtest::appendQuotationMsg(engine,messageTable)

Market Data Callback Function Description

Tick-by-tick market data callback function onTick: input parameter msg

When msg is a dictionary, it is a tick-by-tick data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each tick object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 indicates order data entrust; 1 indicates the trade table (trade)
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, use its original data; for entrust, populate with the order number from entrust.
sellNo LONG For trade, use its original data; for entrust, populate with the order number from entrust.
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number

Snapshot market data callback function onSnapshot: input parameter msg

When msg is a dictionary, it is a snapshot data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each snapShot object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING Stock market

".XSHG" (SSE) or ".XSHE" (SZSE)

timestamp TIMESTAMP Timestamp
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[] Buy order quantity list
offerPrice DOUBLE[] Offer price list
offerQty LONG[] Sell order quantity list
signal DOUBLE[] Indicator list

Snapshot

Market Data Schema

The schema of the market data table written by Backtest::appendQuotationMsg is as follows:

colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice","downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
"offerPrice","offerQty","prevClosePrice"]
colType= ["SYMBOL","SYMBOL","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","LONG",
"LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]", "DOUBLE"]
messageTable=table(10000:0, colName, colType)

Note:

  • The underlying symbol must end with an exchange identifier (".XSHG" or ".XSHE"), for example, 600000.XSHG. Otherwise, an error is raised.

  • The above is the input table schema for snapshot market data (frequency=0, callbackForSnapshot=0), that is, not for bar market data synthesized from snapshots.

  • Field names must match the table below exactly. Except for the first column, which must be the symbol column, there are no other requirements for field order. In addition, columns of type INT, DOUBLE, or STRING, or a column of type DOUBLE ARRAY VECTOR named signal, can be used as extension fields.

The required fields of the snapshot market data table are as follows:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Offer price list
offerQty LONG[] Offer quantity list

Snapshot market data table schema (frequency>0, callbackForSnapshot=1 or 2):

colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice",
"downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
"offerPrice","offerQty","prevClosePrice","open", "close","low","high","volume"]
colType= ["STRING","STRING","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","LONG",
"LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE",DOUBLE, DOUBLE,DOUBLE,DOUBLE,LONG]
messageTable=table(10000000:0, colName, colType)

Note:

  • When synthesizing OHLC bar market data from snapshots (frequency>0 and callbackForSnapshot=1 or 2), five additional fields are required: "open", "close", "low", "high", "volume".

  • Field names must match the table below exactly. Except for the first column, which must be the symbol column, there are no other requirements for field order. In addition, columns of type INT, DOUBLE, or STRING, or a column of type DOUBLE ARRAY VECTOR named signal, can be used as extension fields.

The required fields for the snapshot market data table (when synthesizing OHLC bar market data from snapshots) are as follows:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Offer price list
offerQty LONG[] Offer quantity list
open DOUBLE Opening price of the synthesized OHLC bar
close DOUBLE Closing price of the synthesized OHLC bar
low DOUBLE Low price of the synthesized OHLC bar
high DOUBLE High price of the synthesized OHLC bar
volume LONG Trading volume of the synthetic OHLC bar

When backtest market data playback ends, the system sends a message whose symbol is “END”:

messageTable=select top 1 * from messageTable
update messageTable set symbol="END"
Backtest::appendQuotationMsg(engine,messageTable)

Market Data Callback Function Description

Snapshot market data callback function onSnapshot: input parameter msg

When msg is a dictionary, it is a snapshot data dictionary keyed by symbol. The value is the market data for the corresponding stock. Each snapShot object contains the following fields:

Name Type Description
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Buy order quantity list
offerPrice DOUBLE[] Offer price list
offerQty LONG[] Sell order quantity list
signal DOUBLE[] Other
open DOUBLE Opening price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

close DOUBLE Closing price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

low DOUBLE Low price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

high DOUBLE High price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

volume LONG Trading volume of the synthetic OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

Note:

  • When synthesizing OHLC bar market data from snapshots (frequency>0 and callbackForSnapshot=1 or 2), the input parameter msg also requires five additional fields: "open", "close", "low", "high", "volume".

  • When callbackForSnapshot=1 or 2, the onBar function is also invoked. The data structure of the input parameter msg is as follows.

OHLC bar market data callback function onBar: input parameter msg

When msg is a dictionary, it is a dictionary of minute-frequency OHLC bar data keyed by symbol. Each OHLC bar contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

tradeTime TIMESTAMP Trading day
open DOUBLE Opening price
low DOUBLE Low price
high DOUBLE High price
close DOUBLE Closing price
volume LONG Trading volume
amount DOUBLE Trading value
upLimitPrice DOUBLE Limit up price
downLimitPrice DOUBLE Limit down price
prevClosePrice DOUBLE Previous closing price
signal DOUBLE[] Other

Snapshot + Tick-by-Tick Trades

Market Data Schema

Table schema for market data with snapshot + tick-by-tick trades (frequency=0, callbackForSnapshot=0):

colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice",
                "downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
                "offerPrice","offerQty","tradePrice","tradeQty","prevClosePrice"]
colType= ["STRING","STRING","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","LONG",
                "LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE"]
messageTable=table(10000000:0, colName, colType)

Note:

  • The symbol must end with an exchange identifier (".XSHG", ".XSHE"), for example, 600000.XSHG; otherwise, an error is reported.

  • The above is the input table schema for snapshot and tick-by-tick trades market data (frequency=0, callbackForSnapshot=0), that is, market data that is not synthesized into OHLC bars from snapshots.

The fields are described in detail below: (Compared with snapshot market data, two fields, tradePrice and tradeQty, are added.)

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Offer price list
offerQty LONG[] Offer quantity list
tradePrice DOUBLE[] Trade price
tradeQty LONG[] Trade quantity
signal DOUBLE[] Indicator list
prevClosePrice DOUBLE Previous closing price

Table schema for snapshot + tick-by-tick trade detail market data (frequency>0, callbackForSnapshot=1 or 2):

colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice",
                "downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
                "offerPrice","offerQty","tradePrice","tradeQty","prevClosePrice","open", "close","low","high","volume"]
colType= ["STRING","STRING","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","LONG",
                "LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE","DOUBLE", "DOUBLE","DOUBLE","DOUBLE","LONG"]
messageTable=table(10000000:0, colName, colType)

Note:

When synthesizing OHLC bar market data from snapshots (frequency>0 and callbackForSnapshot=1 or 2), five additional fields are required: "open", "close", "low", "high", "volume".

The fields are described in detail below: (Compared with snapshot market data, two fields, tradePrice and tradeQty, are added.)

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Offer price list
offerQty LONG[] Offer quantity list
tradePrice DOUBLE[] Trade price
tradeQty LONG[] Trade quantity
signal DOUBLE[] Indicator list
prevClosePrice DOUBLE Previous closing price
open DOUBLE Opening price of the synthesized OHLC bar
close DOUBLE Closing price of the synthesized OHLC bar
low DOUBLE Low price of the synthesized OHLC bar
high DOUBLE High price of the synthesized OHLC bar
volume LONG Trading volume of the synthetic OHLC bar

When backtest market data playback ends, send a message with symbol “END”:

messageTable=select top 1 * from messageTable
update messageTable set symbol="END"
Backtest::appendQuotationMsg(engine, messageTable)

Market Data Callback Function Description

Snapshot market data callback function onSnapshot: input parameter msg

When msg is a dictionary, it is a snapshot data dictionary keyed by symbol. The value is the market data for the corresponding stock. Each snapShot object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
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[] Buy order quantity list
offerPrice DOUBLE[] Offer price list
offerQty LONG[] Sell order quantity list
signal DOUBLE[] Other
open DOUBLE Opening price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

close DOUBLE Closing price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

low DOUBLE Low price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

high DOUBLE High price of the synthesized OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

volume LONG Trading volume of the synthetic OHLC bar

(Available only when dataType=1 or 2 and callbackForSnapshot=1 or 2)

Note:

  • When synthesizing OHLC bar market data from snapshots (frequency>0 and callbackForSnapshot=1 or 2), the input parameter msg also requires five additional fields: "open", "close", "low", "high", "volume".

  • When callbackForSnapshot=1 or 2, the onBar function is also invoked. The data structure of the input parameter msg is as follows.

OHLC bar market data callback function onBar: input parameter msg

When msg is a dictionary, it is a dictionary of minute-frequency OHLC bar data keyed by symbol. Each OHLC bar contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

tradeTime TIMESTAMP Trading day
open DOUBLE Opening price
low DOUBLE Low price
high DOUBLE High price
close DOUBLE Closing price
volume LONG Trading volume
amount DOUBLE Trading value
upLimitPrice DOUBLE Limit up price
downLimitPrice DOUBLE Limit down price
prevClosePrice DOUBLE Previous closing price
signal DOUBLE[] Other

Minute Frequency or Daily Frequency

Market Data Schema

The schema of the market data table written by Backtest::appendQuotationMsg is as follows:

colName=`symbol`tradeTime`open`low`high`close`volume`amount`upLimitPrice`downLimitPrice`prevClosePrice
colType=[SYMBOL,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,DOUBLE,DOUBLE,DOUBLE,DOUBLE]
messageTable=table(10000000:0, colName, colType)

Note:

Field names must match the table below exactly. Except for the first column, which must be the symbol column, there are no other requirements for field order. In addition, columns of type INT, DOUBLE, or STRING, or a column of type DOUBLE ARRAY VECTOR named signal, can be used as extension fields.

The required fields for a minute-frequency market data table are as follows:

Field Type Note
symbol SYMBOL Stock symbol
tradeTime TIMESTAMP Timestamp
open DOUBLE Opening price
low DOUBLE Low price
high DOUBLE High price
close DOUBLE Closing price
volume LONG Trading volume
upLimitPrice DOUBLE Limit up price
downLimitPrice DOUBLE Limit down price

When backtest market data playback ends, the system sends a message whose symbol is “END”:

messageTable=select top 1 * from messageTable
update messageTable set symbol="END"
Backtest::appendQuotationMsg(engine,messageTable)

Market Data Callback Function Description

OHLC bar market data callback function onBar: input parameter msg

When msg is a dictionary, it is a dictionary of minute-frequency OHLC bar data keyed by symbol. Each OHLC bar contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

tradeTime TIMESTAMP Trading day
open DOUBLE Opening price
low DOUBLE Low price
high DOUBLE High price
close DOUBLE Closing price
volume LONG Trading volume
amount DOUBLE Trading value
upLimitPrice DOUBLE Limit up price
downLimitPrice DOUBLE Limit down price
prevClosePrice DOUBLE Previous closing price
signal DOUBLE[] Other

Tick-by-Tick (Wide Table)

Market Data Schema

When you run a backtest, the schema of the input table messageTable is: Backtest::appendQuotationMsg(engine,messageTable)

colName=[`symbol,`symbolSource,`timestamp,`sourceType,`orderType,`price,`qty,`buyNo,
    `sellNo,`direction,`channelNo,`seqNum,`reserve1]
colType=["SYMBOL","INT","TIMESTAMP","INT","INT","DOUBLE","LONG","LONG",
    "LONG","INT","INT","LONG","DOUBLE"]
messageTable=table(1000:0,colName,colType)

The schema of the tick-by-tick (wide table) market data table is as follows:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource INT 0 (SSE) or 1 (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 indicates order data entrust; 1 indicates the trade table (trade)
orderType INT

entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
sellNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
reserve1 DOUBLE Reserved field 1 (wide table)

Compared with tick-by-tick data, the tick-by-tick (wide table) adds a reserved field named "reserve1".

At the end of market data replay during backtesting, a message with symbol "END" is added. The following example:

messageTable=select top 1 * from messageTable
update messageTable set symbol="END"
Backtest::appendQuotationMsg(engine,messageTable)

Market Data Callback Function Description

Tick-by-tick market data callback function onTick: input parameter msg

When msg is a dictionary, it is a tick-by-tick data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each tick object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 indicates order data entrust; 1 indicates the trade table (trade)
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
sellNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
reserve1 DOUBLE Reserved field 1 (wide table)

Snapshot market data callback function onSnapshot: input parameter msg

When msg is a dictionary, it is a snapshot data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each snapShot object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 indicates order data entrust; 1 indicates the trade table (trade)
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
sellNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
reserve1 DOUBLE Reserved field 1 (wide table)

Tick-by-Tick and Snapshot (Wide Table)

Market Data Schema

Data schema of the input table messageTable when running a backtest: Backtest::appendQuotationMsg(engine,messageTable)

colName=[`symbol,`symbolSource,`timestamp,`sourceType,`orderType,`price,`qty,`buyNo,
`sellNo,`direction,`channelNo,`seqNum,"lastPrice","upLimitPrice",
"downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
"offerPrice","offerQty","prevClosePrice","reserve1","reserve2"]
colType=["SYMBOL","INT","TIMESTAMP","INT","INT","DOUBLE","LONG","LONG",
"LONG","INT","INT","LONG","DOUBLE","DOUBLE","DOUBLE","LONG",
"LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]","DOUBLE","DOUBLE","DOUBLE"]
messageTable=table(1000:0,colName,colType)

Market data table schema for tick-by-tick and snapshot (wide table):

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource INT 0 (SSE) or 1 (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 represents entrust data; 1 represents trade data; 2 represents snapshot data
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG trade corresponds to its raw data;

Populated with the order number in entrust

sellNo LONG trade corresponds to its raw data;

Populated with the order number in entrust

direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
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[] Offer price list
offerQty LONG[] Offer quantity list
prevClosePrice DOUBLE Previous closing price
reserve1 DOUBLE Reserved field 1 (wide table)
reserve2 DOUBLE Reserved field 2 (wide table)

When backtest market data playback ends, the system sends a message whose symbol is “END”:

messageTable=select top 1* from messageTable
update messageTable set symbol="END"
Backtest::appendQuotationMsg(engine,messageTable)

Market Data Callback Function Description

Tick-by-tick market data callback function onTick: input parameter msg

When msg is a dictionary, it is a tick-by-tick data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each tick object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 represents entrust data entrust; 1 represents the trade table (trade); 2 represents snapshot
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
sellNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
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[] Offer price list
offerQty LONG[] Offer quantity list
prevClosePrice DOUBLE Previous closing price
reserve1 DOUBLE Reserved field 1 (wide table)
reserve2 DOUBLE Reserved field 2 (wide table)

Snapshot market data callback function onSnapshot: input parameter msg

When msg is a dictionary, it is a snapshot data dictionary keyed by symbol. The value contains the market data for this stock and the indicator calculation results defined in initialize. Each snapShot object contains the following fields:

Field Type Note
symbol SYMBOL Stock symbol

SSE symbols end with ".XSHG".

SZSE symbols end with ".XSHE".

symbolSource STRING ".XSHG" (SSE) or ".XSHE" (SZSE)
timestamp TIMESTAMP Timestamp
sourceType INT 0 represents entrust data entrust; 1 represents the trade table (trade); 2 represents snapshot
orderType INT entrust: 1 market; 2 limit; 3 best own-side; 10 cancellation (SSE only, i.e., SSE cancellation records are in entrust)

trade: 0 execution; 1 cancellation (SZSE only, i.e., SZSE cancellation records are in trade)

price DOUBLE Order price
qty LONG Order quantity
buyNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
sellNo LONG For trade, this corresponds to its original data; for entrust, it is populated with the order number,
direction INT 1 (buy) or 2 (sell)
channelNo INT Channel number
seqNum LONG Tick-by-tick data sequence number
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[] Offer price list
offerQty LONG[] Offer quantity list
prevClosePrice DOUBLE Previous closing price
reserve1 DOUBLE Reserved field 1 (wide table)
reserve2 DOUBLE Reserved field 2 (wide table)

Snapshot + Minute Frequency

Market Data Schema

When you insert data into the engine using appendQuotationMsg, msg is a dictionary whose key can be one of three values: 'ohlc', 'snapshot', or 'indicator':

  • 'ohlc' is a minute-frequency market data table with the following schema:
    colName=`symbol`tradeTime`open`low`high`close`volume`amount`upLimitPrice`downLimitPrice`prevClosePrice
    colType=[SYMBOL,TIMESTAMP,DOUBLE,DOUBLE,DOUBLE,DOUBLE,LONG,DOUBLE,DOUBLE,DOUBLE,DOUBLE]
    messageTable=table(10000000:0, colName, colType)
    Note:

    Field names must match the table below exactly. Except for the first column, which must be the symbol column, there are no other requirements for field order. In addition, columns of type INT, DOUBLE, or STRING, or a column of type DOUBLE ARRAY VECTOR named signal, can be used as extension fields.

  • 'snapshot' is a snapshot market data table with the following schema:
    colName=["symbol","symbolSource","timestamp","lastPrice","upLimitPrice","downLimitPrice","totalBidQty","totalOfferQty","bidPrice","bidQty",
    "offerPrice","offerQty","prevClosePrice"]
    colType= ["SYMBOL","SYMBOL","TIMESTAMP","DOUBLE","DOUBLE","DOUBLE","LONG",
    "LONG","DOUBLE[]","LONG[]","DOUBLE[]","LONG[]", "DOUBLE"]
    messageTable=table(10000:0, colName, colType)
    Note:
    • The underlying symbol must end with an exchange identifier (".XSHG" or ".XSHE"), for example, 600000.XSHG. Otherwise, an error is raised.

    • The above is the input table schema for snapshot market data (frequency=0, callbackForSnapshot=0), that is, not for bar market data synthesized from snapshots.

    • Field names must match the table below exactly. Except for the first column, which must be the symbol column, there are no other requirements for field order. In addition, columns of type INT, DOUBLE, or STRING, or a column of type DOUBLE ARRAY VECTOR named signal, can be used as extension fields.

    The required fields of the snapshot market data table are as follows:
    Field Type Note
    symbol SYMBOL Stock symbol

    SSE symbols end with ".XSHG".

    SZSE symbols end with ".XSHE".

    symbolSource STRING "XSHG" (SSE) or "XSHE" (SZSE)
    timestamp TIMESTAMP Timestamp
    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[] Offer price list
    offerQty LONG[] Offer quantity list
    The required fields for a minute-frequency market data table are as follows:
    Field Type Note
    symbol SYMBOL Stock symbol
    tradeTime TIMESTAMP Timestamp
    open DOUBLE Opening price
    low DOUBLE Low price
    high DOUBLE High price
    close DOUBLE Closing price
    volume LONG Trading volume
    upLimitPrice DOUBLE Limit up price
    downLimitPrice DOUBLE Limit down price
  • 'indicator' is the historical indicator data table for the onBar callback. When this table is set, you cannot also use subscribeIndicator to subscribe to indicators of type ohlc/kline. The table must include the fields symbol and timestamp; these can be followed by extension fields of type INT, DOUBLE, or STRING.