Futures#

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

Engine Configuration#

The following table describes the config parameter in createBacktester and the userConfig parameter in createBacktestEngine.

Key

Description

Notes

“startDate”

start date

DATE type, required. e.g. “2020.01.01”.

“endDate”

end date

DATE type, required. e.g. “2020.01.01”.

“strategyGroup”

strategy type

required, must be “futures”

“cash”

initial capital

DOUBLE type, required.

“dataType”

market data type

INT type, required.
Options:

  • 1: snapshot;
  • 3: minute-level;
  • 4: daily data.

“frequency”

bar frequency for snapshot data

INT type, default = 0. Applicable only when dataType = 1.

  • When frequency = 0, this parameter is ignored.
  • When dataType = 1 and frequency > 0, the engine internally calculates bars at the specified frequency and triggers the onBar callback.

“msgAsTable”

market data format

BOOL type, default = false.

  • false: dictionary format
  • true: table format (only supported when creating the engine via createBacktestEngine).

“matchingMode”

order matching mode

INT type, options:
1:

  • daily data: match at close
  • minute-level data: orders will be matched with market data whose time is later than the order time
2:
  • daily data: match at open.
  • minute-level data: match when data time = order time, using the close price
3:
match orders at the order price.
Note: When dataType = 1, values 1 and 2 are invalid; orders are matched by the simulated matching engine by default.

“benchmark”

benchmark instrument

STRING or SYMBOL type, e.g. “A2305”.

“latency”

order latency (ms)

INT type, the latency from order submission to execution.

“maintenanceMargin”

maintenance margin ratio

DOUBLE type, default = 1.0. value range: 0–1.0.

“enableAlgoOrder”

enable algorithmic orders

  • true: enabled;
  • false: disabled.

“futuresType”

futures category, e.g., index futures, commodity futures

STRING or SYMBOL type. Currently supports futures only

“enableIndicatorOptimize”

enable indicator optimization

  • true: enabled;
  • false (default): disabled.

“isBacktestMode”

backtest mode flag

  • true (default): backtest mode;
  • false: simulated trading mode.

“dataRetentionWindow”

data retention policy for indicator optimization

STRING/INT type. Effecti ve only when both enableIndicatorOptimize = true and isBacktestMode = true:

  • ”None”(default): Retain no data.
  • ”ALL”: Retain all data.
  • Retain data by trading days, e.g., “20d” retains 20 trading days.
  • Retain data by row count, e.g., “20” retains the latest 20 rows per symbol.

“addTimeColumnInIndicator”

add time column to indicator subscription results

  • false (default): omit time column
  • true: add time column

“context”

strategy context structure

A dictionary consisting of global strategy variables, for example:

context = {}
context[“buySignalRSI”] = 30.0
config[“context”] = context

“callbackForSnapshot”

snapshot callback mode

Available only for dataType=1. INT type, default 0.

  • 0: trigger onSnapshot only
  • 1: trigger both onSnapshot and onBar
  • 2: trigger onBar only
If frequency > 0, must trigger onBar (callbackForSnapshot = 1 or 2).

“orderBookMatchingRatio”

the proportion of an order that gets filled

DOUBLE type, default = 1.0, value range: 0–1.0. Applicable only when dataType = 1.

“matchingRatio”

matching ratio within price intervals

DOUBLE type, default 1.0, value range: 0–1.0. Applicable only when dataType = 1.

Note:

The available engine configuration parameters differ depending on the data type (dataType).

The parameters callbackForSnapshot, orderBookMatchingRatio, and matchingRatio are only applicable when dataType = 1.

Reference Table Description#

The securityReference parameter in both the createBacktester and createBacktestEngine interfaces should follow the field definitions below:

Field

Type

Description

symbol

SYMBOL or STRING

futures contract code

multiplier

DOUBLE

contract multiplier

marginRatio

DOUBLE

margin ratio

tradeUnit

DOUBLE

contract unit

priceUnit

DOUBLE

quotation unit

priceTick

DOUBLE

minimum price fluctuation

commission

DOUBLE

commission fee

deliveryCommissionMode

INT

commission calculation mode:

  • 1: commission × number of contracts traded
  • 2: commission × trade value

Snapshot Data#

Market Data Structure Description#

The data structure of the market snapshot table written via Backtest::appendQuotationMsg is as follows:

message_table = sf.table(
    types={
        "symbol": "STRING",
        "symbolSource": "STRING",
        "timestamp": "TIMESTAMP",
        "tradingDay": "DATE",
        "lastPrice": "DOUBLE",
        "upLimitPrice": "DOUBLE",
        "downLimitPrice": "DOUBLE",
        "totalBidQty": "LONG",
        "totalOfferQty": "LONG",
        "bidPrice": "DOUBLE[]",
        "bidQty": "LONG[]",
        "offerPrice": "DOUBLE[]",
        "offerQty": "LONG[]",
        "highPrice": "DOUBLE",
        "lowPrice": "DOUBLE",
        "prevClosePrice": "DOUBLE",
        "settlementPrice": "DOUBLE",
        "prevSettlementPrice": "DOUBLE"
    },
    size=0,
    capacity=10000000
)

Notes:

  • The structure above applies to snapshot data (callbackForSnapshot = 0), i.e., not aggregated into bar data.

  • Column names must match exactly as listed. The order is flexible except the first column, which must be symbol.

  • Additional columns of types INT, DOUBLE, STRING, or an extended field signal of type DOUBLE ARRAY VECTOR are supported.

Required Fields for Snapshot Data Table#

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

timestamp

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

lastPrice

DOUBLE

latest traded price

upLimitPrice

DOUBLE

limit up price

downLimitPrice

DOUBLE

limit down price

totalBidQty

LONG

cumulative buy quantity

totalOfferQty

LONG

cumulative sell quantity

bidPrice

DOUBLE[]

bid prices

bidQty

LONG[]

bid quantities

offerPrice

DOUBLE[]

ask prices

offerQty

LONG[]

ask quantities

highPrice

DOUBLE

highest price

lowPrice

DOUBLE

lowest price

signal

DOUBLE[]

extended field list (required in JIT mode)

prevClosePrice

DOUBLE

previous closing price (required in JIT mode)

settlementPrice

DOUBLE

settlement price (required in JIT mode)

prevSettlementPrice

DOUBLE

previous settlement price (required in JIT mode)

When frequency > 0 and callbackForSnapshot = 1 or 2, the table structure is as follows:

message_table = sf.table(
    types={
        "symbol": "STRING",
        "symbolSource": "STRING",
        "timestamp": "TIMESTAMP",
        "tradingDay": "DATE",
        "lastPrice": "DOUBLE",
        "upLimitPrice": "DOUBLE",
        "downLimitPrice": "DOUBLE",
        "totalBidQty": "LONG",
        "totalOfferQty": "LONG",
        "bidPrice": "DOUBLE[]",
        "bidQty": "LONG[]",
        "offerPrice": "DOUBLE[]",
        "offerQty": "LONG[]",
        "highPrice": "DOUBLE",
        "lowPrice": "DOUBLE",
        "signal": "DOUBLE",
        "prevClosePrice": "DOUBLE",
        "settlementPrice": "DOUBLE",
        "prevSettlementPrice": "DOUBLE",
        "open": "DOUBLE",
        "close": "DOUBLE",
        "low": "DOUBLE",
        "high": "DOUBLE",
        "volume": "LONG"
    },
    size=0,
    capacity=10000000
)

Notes:

  • When snapshot is aggregated into bars (callbackForSnapshot = 1 or 2), five additional fields must be included: "open", "close", "low", "high", and "volume".

  • Column names must match exactly as listed. Column order is flexible except for the first column, which must be symbol.

  • Additional columns of types INT, DOUBLE, STRING, or an extended field signal of type DOUBLE ARRAY VECTOR are supported.

Required fields for snapshot data (aggregated into bar) table

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

timestamp

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

lastPrice

DOUBLE

latest traded price

upLimitPrice

DOUBLE

limit up price

downLimitPrice

DOUBLE

limit down price

totalBidQty

LONG

cumulative buy quantity

totalOfferQty

LONG

cumulative sell quantity

bidPrice

DOUBLE[]

bid prices

bidQty

LONG[]

bid quantities

offerPrice

DOUBLE[]

ask prices

offerQty

LONG[]

ask quantities

highPrice

DOUBLE

highest price

lowPrice

DOUBLE

lowest price

signal

DOUBLE[]

extended field list (required in JIT mode)

prevClosePrice

DOUBLE

previous closing price (required in JIT mode)

settlementPrice

DOUBLE

settlement price (required in JIT mode)

prevSettlementPrice

DOUBLE

previous settlement price (required in JIT mode)

open

DOUBLE

open price of the generated bar

close

DOUBLE

close price of the generated bar

low

DOUBLE

lowest price of the generated bar

high

DOUBLE

highest price of the generated bar

volume

LONG

trading volume of the generated bar

At the end of the backtest data replay, send a message with symbol = "END":

message_table = sf.sql(
    "SELECT TOP 1 * FROM messageTable",
    vars={
        'messageTable': message_table
    }
)

sf.sql(
    "UPDATE messageTable SET symbol = `END",
    vars={'messageTable': message_table}
)

backtester.append_data(message_table)

Callback Functions#

Snapshot callback onSnapshot: input parameter: msg

When msg is a dictionary, it is keyed by symbol, and each value contains the following fields:

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

timestamp

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

lastPrice

DOUBLE

latest traded price

upLimitPrice

DOUBLE

limit-up price

downLimitPrice

DOUBLE

limit-down price

totalBidQty

LONG

cumulative buy quantity

totalOfferQty

LONG

cumulative sell quantity

bidPrice

DOUBLE[]

bid prices

bidQty

LONG[]

bid quantities

offerPrice

DOUBLE[]

ask prices

offerQty

LONG[]

ask quantities

highPrice

DOUBLE

highest price

lowPrice

DOUBLE

lowest price

signal

DOUBLE[]

extended field list

prevClosePrice

DOUBLE

previous closing price

settlementPrice

DOUBLE

settlement price

prevSettlementPrice

DOUBLE

previous settlement price

open

DOUBLE

open price of the aggregated bar (only if dataType = 1 or 2 and callbackForSnapshot = 1 or 2)

close

DOUBLE

close price of the aggregated bar (only if dataType = 1 or 2 and callbackForSnapshot = 1 or 2)

low

DOUBLE

the lowest price of the aggregated bar (only if dataType = 1 or 2 and callbackForSnapshot = 1 or 2)

high

DOUBLE

the highest price of the aggregated bar (only if dataType = 1 or 2 and callbackForSnapshot = 1 or 2)

volume

LONG

trading volume of the aggregated bar (only if dataType = 1 or 2 and callbackForSnapshot = 1 or 2)

Notes:

  • When snapshot data is aggregated into bars (frequency > 0 and callbackForSnapshot = 1 or 2), the msg object must include five additional fields: "open", "close", "low", "high", and "volume".

  • When callbackForSnapshot = 1, the onBar function must also be triggered, and the input msg structure is as follows.

Bar callback function: onBar: input parameter: msg

When msg is a dictionary, it is keyed by symbol, and each bar object contains the following fields:

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

timestamp

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

lastPrice

DOUBLE

latest traded price

upLimitPrice

DOUBLE

limit-up price

downLimitPrice

DOUBLE

limit-down price

totalBidQty

LONG

cumulative buy quantity

totalOfferQty

LONG

cumulative sell quantity

bidPrice

DOUBLE[]

bid prices

bidQty

LONG[]

bid quantities

offerPrice

DOUBLE[]

ask prices

offerQty

LONG[]

ask quantities

highPrice

DOUBLE

the highest price

lowPrice

DOUBLE

the lowest price

signal

DOUBLE[]

extended field list

prevClosePrice

DOUBLE

previous closing price

settlementPrice

DOUBLE

settlement price

prevSettlementPrice

DOUBLE

previous settlement price

Minute-Level or Daily Data#

Market Data Structure Description#

The market data table structure written via Backtest::appendQuotationMsg is as follows:

message_table = sf.table(
    types={
        "symbol": "SYMBOL",
        "symbolSource": "SYMBOL",
        "tradeTime": "TIMESTAMP",
        "tradingDay": "DATE",
        "open": "DOUBLE",
        "low": "DOUBLE",
        "high": "DOUBLE",
        "close": "DOUBLE",
        "volume": "LONG",
        "amount": "DOUBLE",
        "upLimitPrice": "DOUBLE",
        "downLimitPrice": "DOUBLE",
        "prevClosePrice": "DOUBLE",
        "settlementPrice": "DOUBLE",
        "prevSettlementPrice": "DOUBLE"
    },
    size=0,
    capacity=10000000
)

Notes:

  • Column names must match exactly as listed.

  • The order of columns is flexible except that the first column must be symbol.

  • Additional columns of type INT, DOUBLE, or STRING are supported, as well as an extended field named signal of type DOUBLE ARRAY VECTOR.

Required Fields for Minute-level or Daily Data#

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

tradeTime

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

open

DOUBLE

opening price

low

DOUBLE

the lowest price

high

DOUBLE

the highest price

close

DOUBLE

closing price

volume

LONG

trading volume

amount

DOUBLE

trading amount (required in JIT mode only)

upLimitPrice

DOUBLE

limit-up price

downLimitPrice

DOUBLE

limit-down price

signal

DOUBLE[]

extended field list (required in JIT mode only)

prevClosePrice

DOUBLE

previous closing price (required in JIT mode only)

settlementPrice

DOUBLE

settlement price (required in JIT mode only)

prevSettlementPrice

DOUBLE

previous settlement price (required in JIT mode only)

At the end of the backtest data replay, send a message with symbol = "END":

message_table = sf.sql(
    "SELECT TOP 1 * FROM messageTable,
    vars={
        'messageTable': message_table
    }
)

sf.sql(
    "UPDATE messageTable SET symbol = `END",
    vars={'messageTable': message_table}
)

backtester.append_data(message_table)

Callback Function#

OHLC data callback functiononBar: input parameter msg

When msg is a dictionary, it is keyed by symbol, and each value contains a minute-level (or daily-level) object with the following fields:

Field

Type

Description

symbol

SYMBOL

futures contract code

symbolSource

STRING

exchange name

timestamp

TIMESTAMP

timestamp

tradingDay

DATE

trading day / settlement date

lastPrice

DOUBLE

latest traded price

upLimitPrice

DOUBLE

limit-up price

downLimitPrice

DOUBLE

limit-down price

totalBidQty

LONG

cumulative buy quantity

totalOfferQty

LONG

cumulative sell quantity

bidPrice

DOUBLE[]

bid prices

bidQty

LONG[]

bid quantities

offerPrice

DOUBLE[]

ask prices

offerQty

LONG[]

ask quantities

highPrice

DOUBLE

highest price

lowPrice

DOUBLE

lowest price

signal

DOUBLE[]

extended field list

prevClosePrice

DOUBLE

previous closing price

settlementPrice

DOUBLE

settlement price

prevSettlementPrice

DOUBLE

previous settlement price