Futures#
The backtesting engine supports the following types of market data: snapshot, minute-level data, and daily-level data.
Engine Configuration#
Key |
Description |
Notes |
|---|---|---|
“start_date” |
start date |
DATE type, required. e.g. “2020.01.01”. |
“end_date” |
end date |
DATE type, required. e.g. “2020.01.01”. |
“asset_type” |
strategy type |
required, must be “futures” |
“cash” |
initial capital |
DOUBLE type, required. |
“data_type” |
market data type |
INT type, required.
|
“frequency” |
bar frequency for snapshot data |
INT type, default = 0. Applicable only when
|
“msgAsTable” |
market data format |
BOOL type, default =
|
“matchingMode” |
order matching mode |
INT type, options:
match orders at the order price. Note: When data_type = 1, values 1 and 2 are invalid; orders are matched by the simulated matching engine by default. |
“bench_mark” |
benchmark instrument |
STRING or SYMBOL type, e.g. “A2305”. |
“latency” |
order latency (ms) |
INT type, the latency from order submission to execution. |
“maintenance_margin” |
maintenance margin ratio |
DOUBLE type, default = 1.0. value range: 0–1.0. |
“enable_algo_order” |
enable algorithmic orders |
|
“futures_type” |
futures category, e.g., index futures, commodity futures |
STRING or SYMBOL type. Currently supports futures only |
“enable_indicator_optimize” |
enable indicator optimization |
|
“isBacktestMode” |
backtest mode flag |
|
“dataRetentionWindow” |
data retention policy for indicator optimization |
STRING/INT type. Effecti ve only when both enable_indicator_optimize = true and isBacktestMode = true:
|
“add_time_column_in_indicator” |
add time column to indicator subscription results |
|
“context” |
strategy context structure |
A dictionary consisting of global strategy variables, for example: |
“callback_for_snapshot” |
snapshot callback mode |
Available only for
frequency > 0, must trigger on_bar (callback_for_snapshot = 1 or 2). |
“orderbook_matching_ratio” |
the proportion of an order that gets filled |
DOUBLE type, default = 1.0, value range: 0–1.0. Applicable only when |
“matching_ratio” |
matching ratio within price intervals |
DOUBLE type, default 1.0, value range: 0–1.0. Applicable only when |
Note:
The available engine configuration parameters differ depending on the data type (data_type).
The parameters callback_for_snapshot, orderbook_matching_ratio, and matching_ratio are only applicable when data_type = 1.
Security Reference Table Description#
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:
|
Snapshot Data#
Market Data Structure Description#
The data structure of the market snapshot table written via append_data 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 (
callback_for_snapshot = 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
signalof 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 callback_for_snapshot = 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 (
callback_for_snapshot = 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
signalof 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 on_snapshot: 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 |
close |
DOUBLE |
close price of the aggregated bar (only if |
low |
DOUBLE |
the lowest price of the aggregated bar (only if |
high |
DOUBLE |
the highest price of the aggregated bar (only if |
volume |
LONG |
trading volume of the aggregated bar (only if |
Notes:
When snapshot data is aggregated into bars (
frequency > 0andcallback_for_snapshot = 1 or 2), themsgobject must include five additional fields:"open","close","low","high", and"volume".When
callback_for_snapshot = 1, theon_barfunction must also be triggered, and the inputmsgstructure is as follows.
Bar callback function: on_bar: 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 append_data 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
signalof 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 function on_bar: 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 |