Options Backtesting Configuration

The backtesting plugin supports backtesting the following types of options data: snapshot, minute-level data, and daily data.

Engine Configuration

You can create a backtesting engine using createBacktester or createBacktestEngine. The config parameter is used to configure the engine. config is a dictionary. See the following table for the supported key-value pairs:

Table 1. Table 1-1 Description of config Parameter
Key Value Type Required Description
"startDate" DATE Yes The date on which backtesting starts, for example, 2020.01.01.
"endDate" DATE Yes The date on which backtesting ends, for example, 2020.06.01.
"strategyGroup" STRING Yes The strategy type. For options backtesting, set this to "option".
"cash" DOUBLE Yes Initial capital.
"dataType" INT Yes The market data type. Valid values:
  • 1: Snapshot
  • 3: Minute-level data
  • 4: Daily data
"frequency" INT No The frequency at which snapshot data is aggregated into OHLC bars. The default is 0, in milliseconds.
  • This configuration is effective only when dataType = 1.
  • This configuration is ineffective when frequency = 0.
  • When frequency > 0, the OHLC bars aggregated at the specified frequency trigger the onBar callback function.
"msgAsTable" BOOL No The market data format. The default is false.
  • false: dictionary.
  • true: table. In this case, the engine must be created using createBacktestEngine.
"matchingMode" INT No The order matching mode. Valid values:
  • 1:
    • Daily data: Matches orders at the closing price.
    • Minute-level data: Matches orders when the market data timestamp is later than the order timestamp.
  • 2:
    • Daily data: Matches orders at the opening price.
    • Minute-level data: When the market data timestamp equals the order timestamp, the order is matched at the closing price of the current market data. Subsequent matching of unfilled orders follows matchingMode = 1.
  • 3: Orders are matched at the order price.
Note:When dataType = 1, setting this configuration to 1 or 2 has no effect. The order matching simulator is always used to match orders according to price priority and time priority.
"enableSettlementPrice" BOOL No Specifies whether to use the settlement price to calculate the equity after each trading day.
  • true: When getDailyTotalPortfolios is called, or when getTotalPortfolios is called from the afterTrading callback function, the settlement price is used instead of the latest price to calculate metrics. When getTotalPortfolios is called from other callback functions, it continues to use the latest price.
  • false: Use the latest price to calculate metrics.
Note:The settlement price is taken from the latest settlementPrice field value for the instrument in the market data table. After this configuration is enabled, the snapshot, minute-level data, and daily data tables must all contain this field; otherwise, an error is returned.
"benchmark" STRING No The benchmark instrument used by getReturnSummary to compare return performance. For example, "A2305".
"latency" INT No The latency between submitting an order and processing it, in milliseconds. The default is 0, which indicates no latency.
"maintenanceMargin" DOUBLE No The maintenance margin ratio. Valid Range: [0, 1.0]. The default is 1.0.
"enableAlgoOrder" BOOL No Specifies whether to enable algorithmic orders. The default is false.
  • true: Enables the feature.
  • false: Disables the feature.
"enableIndicatorOptimize" BOOL No Specifies whether to enable indicator optimization. The default is false.
  • true: Enables the feature.
  • false: Disables the feature.
"isBacktestMode" BOOL No Specifies whether to enable backtesting mode. The default is true.
  • true: Backtesting
  • false: Simulated trading
"dataRetentionWindow" STRING No The data retention window. This configuration is effective only when enableIndicatorOptimize = true.
  • When isBacktestMode = true, the valid values are as follows:
    • "None" (default): Does not retain data.
    • "ALL": Retains all data.
    • "{num}d": Retains data by number of trading days. {num} must be a positive integer. For example, "20d" retains 20 trading days of data.
    • "{num}": Retains data by number of records. {num} must be a positive integer. For example, "20" retains the latest 20 records for each symbol.
  • This configuration does not need to be set when isBacktestMode = false.

"addTimeColumnInIndicator" BOOL No Specifies whether to add a time column to the indicator data table when subscribing to indicators. The default is false.
  • true: Adds the time column.
  • false: Omits the time column.
"context" DICT No Global strategy variables, for example:
context=dict(STRING,ANY)
context["buySignalRSI"]=70.
userConfig["context"]=context
"callbackForSnapshot" INT No The callback trigger mode for snapshot data. This configuration is effective only when dataType = 1. Valid values:
  • 0 (default): Triggers only onSnapshot.
  • 1: Triggers both onSnapshot and onBar.
  • 2: Triggers only onBar.
Note:When frequency > 0, onBar must be triggered. Set callbackForSnapshot to 1 or 2.
"orderBookMatchingRatio" DOUBLE No The fill ratio against the market order book. This configuration is effective only when dataType = 1. Valid range:[0, 1.0]. The default is 1.0.
"matchingRatio" DOUBLE No The order matching ratio. This configuration is effective only when dataType = 1. Valid range:[0, 1.0]. If not specified, it takes the value of orderBookMatchingRatio. If both are unspecified, the default is 1.0.
"msgAs​PiecesOnSnapshot" BOOL No How the onSnapshot callback is triggered. The default is false.
  • false: Each record triggers onSnapshot in sequence.
  • true: All records with the same timestamp trigger onSnapshot simultaneously.
Note:

The engine configuration varies depending on the option market data type (dataType). frequency, callbackForSnapshot, orderBookMatchingRatio, and matchingRatio take effect only when dataType = 1.

Basic Information Table Description

The securityReference parameter of createBacktester specifies the contract's basic information table. Its schema is as follows:

Table 2. Table 1-2 Basic Information Table
Column Name Data Type Description
symbol SYMBOL Contract symbol
underlyingAssetType INT Option category. Valid values:
  • 1: Commodity option
  • 2: Stock index option
  • 3: ETF option
multiplier DOUBLE Contract multiplier
type INT Option type. Valid values:
  • 1: Call
  • 2: Put
strikePrice DOUBLE Strike price
marginRatio DOUBLE Margin ratio
tradeUnit DOUBLE Contract unit
priceUnit DOUBLE Quote unit
priceTick DOUBLE Minimum price increment
commission DOUBLE Commission rate or commission per lot, depending on the value of deliveryCommissionMode.
deliveryCommissionMode INT Commission calculation method. Valid values:
  • 1: Calculated per lot: commission = commission * number of lots
  • 2: Calculated as a percentage of the trading value: commission = commission * trading value
lastTradingDay DATE Last trading date of the contract
exerciseDate DATE Option exercise date
exerciseSettlementDate DATE Exercise settlement date
exDate DATE
  • exDate: Dividend or interest payment date
  • adjStrikePrice: Adjusted strike price
  • adjMultiplier: Adjusted contract multiplier
Note:When the trading date is on or after exDate, use adjStrikePrice and adjMultiplier for the strike price and contract multiplier.
adjStrikePrice DOUBLE
adjMultiplier DOUBLE
intradayClosingFee DOUBLE Intraday position-closing fee

Snapshot

Market Data Table Schema

The table schema for snapshot data (dataType = 1) is as follows:

Note:

The column names must match those in the table below exactly. The first column must be symbol; the remaining columns may appear in any order. You can also add columns of type INT, DOUBLE, or STRING, or a column named signal with type DOUBLE ARRAY VECTOR.

Table 3. Table 1-3 Table Schema of Snapshot Data
Column Name Data Type Description
symbol STRING/SYMBOL Option symbol
symbolSource STRING/SYMBOL 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 during the interval
totalOfferQty LONG Total offer quantity during the interval
bidPrice DOUBLE[] Bid price
bidQty LONG[] Bid volume
offerPrice DOUBLE[] Ask price
offerQty LONG[] Ask volume
highPrice DOUBLE High price
lowPrice DOUBLE Low price
underlyingPrice DOUBLE Underlying price
open DOUBLE When callbackForSnapshot = 1 or callbackForSnapshot = 2, add the following five columns for the OHLC bars generated from snapshot data:
  • open: opening price
  • close: closing price
  • low: low price
  • high: high price
  • volume: trading volume
close DOUBLE
low DOUBLE
high DOUBLE
volume LONG
settlementPrice DOUBLE Optional. When enableSettlementPrice = true and the settlement price is used for valuation or output, this column is required and its values must be positive.
amount DOUBLE Optional. Trading value
prevClosePrice DOUBLE Optional. Previous closing price
prevSettlementPrice DOUBLE Optional. Previous settlement price
Theta DOUBLE Sensitivity of the option price to changes in time
Vega DOUBLE Sensitivity of the option price to the volatility of the underlying asset
Gamma DOUBLE Sensitivity of the option Delta to the price of the underlying asset
Delta DOUBLE Sensitivity of the option price to the price of the underlying asset
IV DOUBLE Implied volatility

Callback Function Description

  • Snapshot data callback function onSnapshot: input parameter msg. When msg is a dictionary, each key is a symbol and its value is the corresponding snapshot data (snapShot). For the fields in each snapShot object, see Table 1-3 Table Schema of Snapshot Data.
  • When callbackForSnapshot = 1 or callbackForSnapshot = 2, the onBar callback function is also required.

    OHLC bar callback function onBar: input parameter msg. When msg is a dictionary, each key is a symbol and its value is the corresponding OHLC bar data. Each OHLC bar contains the following fields:

Table 4. Table 1-4 OHLC Data Fields
Field Name Data Type Description
symbol STRING/SYMBOL Option symbol
symbolSource STRING/SYMBOL Exchange
tradeTime TIMESTAMP Timestamp
tradingDay DATE Trading date/settlement date
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
signal DOUBLE[] Other fields
prevClosePrice DOUBLE Previous closing price
settlementPrice DOUBLE Settlement price
prevSettlementPrice DOUBLE Previous settlement price
underlyingPrice DOUBLE Underlying price

Minute-Level and Daily Data

Market Data Table Schema

The table schema for minute-level (dataType = 3) and daily (dataType = 4) market data is as follows:

Note:

The column names must match those in the table below exactly. The first column must be symbol; the remaining columns may appear in any order. You can also add columns of type INT, DOUBLE, or STRING, or a column named signal with type DOUBLE ARRAY VECTOR.

Table 5. Table 1-5 Table Schema of Minute-Level and Daily Data
Column Name Data Type Description
symbol STRING/SYMBOL Option symbol
symbolSource STRING/SYMBOL Exchange
tradeTime TIMESTAMP Timestamp
tradingDay DATE Trading date/settlement date
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
underlyingPrice DOUBLE Underlying price
settlementPrice DOUBLE Optional. When enableSettlementPrice = true and the settlement price is used for valuation or output, this column is required and its values must be positive.
amount DOUBLE Optional. Trading value
prevClosePrice DOUBLE Optional. Previous closing price
prevSettlementPrice DOUBLE Optional. Previous settlement price
Theta DOUBLE Sensitivity of the option price to changes in time
Vega DOUBLE Sensitivity of the option price to the volatility of the underlying asset
Gamma DOUBLE Sensitivity of the option Delta to the price of the underlying asset
Delta DOUBLE Sensitivity of the option price to the price of the underlying asset
IV DOUBLE Implied volatility

Callback Function Description

OHLC bar callback function onBar: input parameter msg. When msg is a dictionary, each key is a symbol and its value is the corresponding OHLC bar data. For details about the fields in each OHLC bar, see Table 1-4 OHLC Data Fields.