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:
| 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:
|
| "frequency" | INT | No | The frequency at which snapshot data is aggregated into OHLC
bars. The default is 0, in milliseconds.
|
| "msgAsTable" | BOOL | No | The market data format. The default is false.
|
| "matchingMode" | INT | No | The order matching mode. Valid values:
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.
|
| "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.
|
| "enableIndicatorOptimize" | BOOL | No | Specifies whether to enable indicator optimization. The default
is false.
|
| "isBacktestMode" | BOOL | No | Specifies whether to enable backtesting mode. The default is true.
|
| "dataRetentionWindow" | STRING | No | The data retention window. This configuration is effective only
when enableIndicatorOptimize = true.
|
| "addTimeColumnInIndicator" | BOOL | No | Specifies whether to add a time column to the indicator data
table when subscribing to indicators. The default is false.
|
| "context" | DICT | No | Global strategy variables, for
example: |
| "callbackForSnapshot" | INT | No | The callback trigger mode for snapshot data. This configuration
is effective only when dataType = 1. Valid values:
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. |
| "msgAsPiecesOnSnapshot" | BOOL | No | How the onSnapshot callback is triggered. The
default is false.
|
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:
| Column Name | Data Type | Description |
|---|---|---|
| symbol | SYMBOL | Contract symbol |
| underlyingAssetType | INT | Option category. Valid values:
|
| multiplier | DOUBLE | Contract multiplier |
| type | INT | Option type. Valid values:
|
| 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:
|
| lastTradingDay | DATE | Last trading date of the contract |
| exerciseDate | DATE | Option exercise date |
| exerciseSettlementDate | DATE | Exercise settlement date |
| exDate | DATE |
|
| 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.
| 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:
|
| 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 = 1orcallbackForSnapshot = 2, theonBarcallback 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:
| 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.
| 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.
