StrategyInterface#
- class swordfish.plugins.backtest.StrategyInterface#
- initialize(context)#
Strategy initialization callback function, triggered when the backtester is created. It is used for preparatory tasks such as loading parameters, initializing states, and registering indicators.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
- before_trading(context)#
The daily callback function is triggered before the market opens each trading day.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
- on_tick(context, msg, indicator)#
The callback function is triggered whenever the latest tick-by-tick order or trade data is received.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
msg (_type_) – Represents the tick-level market data. Each record is provided as either a dictionary object or a table (depending on the msg_as_table configuration). The specific fields vary by asset type. For details, refer to the msg data section below.
indicator (_type_) – A dictionary or nested dictionary with a structure consistent with the corresponding msg, containing the data subscribed to by the strategy.
Msg data#
Stock Tick / Margin Trading
Field
Type
Description
symbol
SYMBOL
Stock code: - Ends with “.XSHG” for Shanghai Stock Exchange - Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
“.XSHG” (Shanghai Stock Exchange) or “.XSHE” (Shenzhen Stock Exchange)
timestamp
TIMESTAMP
Timestamp
sourceType
INT
0 represents entrust data; 1 represents trade data
orderType
INT
entrust:
1: Market order
2: Limit order
3: Best own side price
10: Cancel order (Shanghai only, cancel records are in entrust)
trade:
0: Trade
1: Cancel order (Shenzhen only, cancel records are in trade)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
Original buy order number in trade; filled for entrust orders
sellNo
LONG
Original sell order number in trade; filled for entrust orders
direction
INT
1 (Buy) or 2 (Sell)
channelNo
INT
Channel number
seqNum
LONG
Tick data sequence number
Stock Tick or Tick + Snapshot
Field
Type
Description
symbol
SYMBOL
Stock code:
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
“.XSHG” (Shanghai Stock Exchange) or “.XSHE” (Shenzhen Stock Exchange)
timestamp
TIMESTAMP
Timestamp
sourceType
INT
0 represents entrust data; 1 represents trade data
orderType
INT
entrust:
1: Market order
2: Limit order
3: Best own side price
10: Cancel order (Shanghai only; cancel records are in entrust)
trade:
0: Trade
1: Cancel order (Shenzhen only; cancel records are in trade)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
Original buy order number in trade; filled for entrust orders
sellNo
LONG
Original sell order number in trade; filled for entrust orders
direction
INT
1 (Buy) or 2 (Sell)
channelNo
INT
Channel number
seqNum
LONG
Tick data sequence number
Stock Tick (Wide Table)
Field
Type
Description
symbol
SYMBOL
Stock code:
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
“.XSHG” (Shanghai Stock Exchange) or “.XSHE” (Shenzhen Stock Exchange)
timestamp
TIMESTAMP
Timestamp
sourceType
INT
0 represents entrust data; 1 represents trade data
orderType
INT
entrust:
1: Market order
2: Limit order
3: Best own side price
10: Cancel order (Shanghai only; cancel records are in entrust)
trade:
0: Trade
1: Cancel order (Shenzhen only; cancel records are in trade)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
Original buy order number in trade; filled for entrust orders
sellNo
LONG
Original sell order number in trade; filled for entrust orders
direction
INT
1 (Buy) or 2 (Sell)
channelNo
INT
Channel number
seqNum
LONG
Tick data sequence number
reserve1
DOUBLE
Reserved field 1 (for wide table)
Stock Tick + Snapshot (Wide Table)
Field
Type
Description
symbol
SYMBOL
Stock code:
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
“.XSHG” (Shanghai Stock Exchange) or “.XSHE” (Shenzhen Stock Exchange)
timestamp
TIMESTAMP
Timestamp
sourceType
INT
0 represents entrust data; 1 represents trade; 2 represents snapshot
orderType
INT
entrust:
1: Market order
2: Limit order
3: Best own side price
10: Cancel order (Shanghai only; cancel records are in entrust)
trade:
0: Trade
1: Cancel order (Shenzhen only; cancel records are in trade)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
Original buy order number in trade; filled for entrust orders
sellNo
LONG
Original sell order number in trade; filled for entrust orders
direction
INT
1 (Buy) or 2 (Sell)
channelNo
INT
Channel number
seqNum
LONG
Tick data sequence number
lastPrice
DOUBLE
Latest trade price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Interval buy quantity
totalOfferQty
LONG
Interval sell quantity
bidPrice
DOUBLE[]
List of buy prices
bidQty
LONG[]
List of buy quantities
offerPrice
DOUBLE[]
List of sell prices
offerQty
LONG[]
List of sell quantities
prevClosePrice
DOUBLE
Previous close price
reserve1
DOUBLE
Reserved field 1 (for wide table)
reserve2
DOUBLE
Reserved field 2 (for wide table)
- on_snapshot(context, msg, indicator)#
The callback function is triggered upon receiving snapshot market data.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
msg (_type_) – Represents the tick-level market data. Each record is provided as either a dictionary object or a table (depending on the msg_as_table configuration). The specific fields vary by asset type. For details, refer to the msg data section below.
indicator (_type_) – A dictionary or nested dictionary with a structure consistent with the corresponding msg, containing the data subscribed to by the strategy.
Msg data#
Stock Tick / Margin Trading
Field
Type
Description
symbol
SYMBOL
Stock code - Ends with “.XSHG” for Shanghai Stock Exchange - Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
SYMBOL
Stock market
“.XSHG”: Shanghai Stock Exchange
“.XSHE”: Shenzhen Stock Exchange
timestamp
TIMESTAMP
Timestamp
lastPrice
DOUBLE
Latest trade price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Total bid quantity
totalOfferQty
LONG
Total offer quantity
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of offer prices
offerQty
LONG[]
List of offer quantities
signal
DOUBLE[]
List of indicators
Stock tick or tick + snapshot
Name
Type
Description
symbol
SYMBOL
Stock code
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
Stock market
“.XSHG”: Shanghai Stock Exchange
“.XSHE”: Shenzhen Stock Exchange
timestamp
TIMESTAMP
Timestamp
lastPrice
DOUBLE
Latest trade price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Total bid quantity
totalOfferQty
LONG
Total offer quantity
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of offer prices
offerQty
LONG[]
List of offer quantities
signal
DOUBLE[]
Other indicators
open
DOUBLE
Open price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
close
DOUBLE
Close price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
low
DOUBLE
Lowest price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
high
DOUBLE
Highest price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
volume
LONG
Trading volume of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
Stock Snapshot + Tick
Field
Type
Description
symbol
SYMBOL
Stock code
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
Stock market identifier
“.XSHG”: Shanghai Stock Exchange
“.XSHE”: Shenzhen Stock Exchange
timestamp
TIMESTAMP
Timestamp
lastPrice
DOUBLE
Latest traded price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Total bid quantity executed
totalOfferQty
LONG
Total offer quantity executed
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of offer prices
offerQty
LONG[]
List of offer quantities
signal
DOUBLE[]
Other indicators
open
DOUBLE
Open price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
close
DOUBLE
Close price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
low
DOUBLE
Lowest price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
high
DOUBLE
Highest price of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
volume
LONG
Trading volume of the aggregated bar data (Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
Stock Tick (Wide Table)
Field
Type
Description
symbol
SYMBOL
Stock code
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
Stock market identifier
“.XSHG”: Shanghai Stock Exchange
“.XSHE”: Shenzhen Stock Exchange
timestamp
TIMESTAMP
Timestamp
sourceType
INT
Source type indicator
0: Entrust data (
entrust)1: Trade data (
trade)
orderType
INT
Order type
For
entrust:1 = Market order; 2 = Limit order; 3 = Best bid/ask; 10 = Cancel order (only for SSE, since cancellations are recorded in `entrust`)
For
trade:0 = Trade; 1 = Cancel order (only for SZSE, since cancellations are recorded in `trade`)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
For
trade: Corresponds to the original data. Forentrust: Populated with the entrust order number.sellNo
LONG
For
trade: Corresponds to the original data. Forentrust: Populated with the entrust order number.direction
INT
Trade direction: 1 = Buy, 2 = Sell
channelNo
INT
Channel number
seqNum
LONG
Tick-by-tick data sequence number
reserve1
DOUBLE
Reserved field 1 (for wide table)
Stock Tick + Snapshot (Wide Table)
Field
Type
Description
symbol
SYMBOL
Stock code
Ends with “.XSHG” for Shanghai Stock Exchange
Ends with “.XSHE” for Shenzhen Stock Exchange
symbolSource
STRING
Stock market identifier
“.XSHG”: Shanghai Stock Exchange
“.XSHE”: Shenzhen Stock Exchange
timestamp
TIMESTAMP
Timestamp
sourceType
INT
Source type indicator
0: Entrust data (
entrust)1: Trade data (
trade)2: Snapshot data (
snapshot)
orderType
INT
Order type
For
entrust:1 = Market order; 2 = Limit order; 3 = Best bid/ask; 10 = Cancel order (only for SSE, cancellations recorded in `entrust`)
For
trade:0 = Trade; 1 = Cancel order (only for SZSE, cancellations recorded in `trade`)
price
DOUBLE
Order price
qty
LONG
Order quantity
buyNo
LONG
For
trade: corresponds to the original data Forentrust: populated with the entrust order numbersellNo
LONG
For
trade: corresponds to the original data Forentrust: populated with the entrust order numberdirection
INT
Trade direction: 1 = Buy, 2 = Sell
channelNo
INT
Channel number
seqNum
LONG
Tick-by-tick data sequence number
lastPrice
DOUBLE
Last traded price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Total bid quantity in the interval
totalOfferQty
LONG
Total ask quantity in the interval
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of ask prices
offerQty
LONG[]
List of ask quantities
prevClosePrice
DOUBLE
Previous closing price
reserve1
DOUBLE
Reserved field 1 (for wide table)
reserve2
DOUBLE
Reserved field 2 (for wide table)
Options snapshot
Name
Type
Description
symbol
SYMBOL
Option code
symbolSource
STRING
Exchange
timestamp
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / Settlement date
lastPrice
DOUBLE
Latest trade price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Interval total buy quantity
totalOfferQty
LONG
Interval total sell quantity
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of ask prices
offerQty
LONG[]
List of ask quantities
highPrice
DOUBLE
Highest price
lowPrice
DOUBLE
Lowest price
signal
DOUBLE[]
List of other fields
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice | DOUBLE
Previous settlement price
underlyingPrice
DOUBLE
Underlying asset price
Theta
DOUBLE
/
Vega
DOUBLE
/
Gamma
DOUBLE
/
Delta
DOUBLE
/
IV
DOUBLE
/
open
DOUBLE
Open price of synthetic bar (available only if data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
close
DOUBLE
Close price of synthetic bar (available only if data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
low
DOUBLE
Lowest price of synthetic bar (available only if data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
high
DOUBLE
Highest price of synthetic bar (available only if data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
volume
LONG
Volume of synthetic bar (available only if data_type = 1 or 2, and callback_for_snapshot = 1 or 2)
Note
When generating bar data from snapshots (frequency > 0 and callback_for_snapshot = 1 or 2), the input parameter msg should additionally include five fields: “open”, “close”, “low”, “high”, “volume”.
Futures Snapshot
Field
Type
Description
symbol
SYMBOL
Futures contract code
symbolSource
STRING
Exchange
timestamp
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
lastPrice
DOUBLE
Last traded price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Total buy quantity in the interval
totalOfferQty
LONG
Total sell quantity in the interval
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of ask prices
offerQty
LONG[]
List of ask quantities
highPrice
DOUBLE
Highest price
lowPrice
DOUBLE
Lowest price
signal
DOUBLE[]
List of other fields
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice
DOUBLE
Previous settlement price
open
DOUBLE
Open price of the synthesized bar (available only when dataType = 1 or 2 and callbackForSnapshot = 1 or 2)
close
DOUBLE
Close price of the synthesized bar (available only when dataType = 1 or 2 and callbackForSnapshot = 1 or 2)
low
DOUBLE
Lowest price of the synthesized bar (available only when dataType = 1 or 2 and callbackForSnapshot = 1 or 2)
high
DOUBLE
Highest price of the synthesized bar (available only when dataType = 1 or 2 and callbackForSnapshot = 1 or 2)
volume
LONG
Volume of the synthesized bar (available only when dataType = 1 or 2 and callbackForSnapshot = 1 or 2)
Note
When generating bar data from snapshots (frequency > 0 and callbackForSnapshot = 1 or 2), the input parameter msg should additionally include five fields: “open”, “close”, “low”, “high”, “volume”.
Interbank Bond Snapshot
Field
Type
Description
symbol
SYMBOL
Instrument code
messageSource
SYMBOL
Market: Interbank “X_BOND”
byield
TIMESTAMP
Timestamp
ayield
DOUBLE[]
Bid yields
bmdEntryPrice
DOUBLE[]
Bid net price (CNY)
amdEntryPrice
DOUBLE[]
Ask net price (CNY)
bmdEntrySize
LONG[]
Bid quantity (CNY)
amdEntrySize
LONG[]
Ask quantity (CNY)
bsettlType
LONG[]
Buy settlement speed
asettlType
LONG[]
Sell settlement speed
settlType
LONG[]
Interval settlement speed
tradePrice
DOUBLE[]
List of interval trade prices
tradeYield
DOUBLE[]
Yield to maturity
tradeQty
LONG[]
List of interval trade quantities
Cryptocurrency Snapshot
Field
Type
Description
symbol
STRING
Instrument code
symbolSource
STRING
Exchange
timestamp
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
lastPrice
DECIMAL128(8)
Last traded price
upLimitPrice
DECIMAL128(8)
Upper limit price
downLimitPrice
DECIMAL128(8)
Lower limit price
totalBidQty
DECIMAL128(8)
Interval bid quantity
totalOfferQty
DECIMAL128(8)
Interval ask quantity
bidPrice
DECIMAL128(8)[]
List of bid prices
bidQty
DECIMAL128(8)[]
List of bid quantities
offerPrice
DECIMAL128(8)[]
List of ask prices
offerQty
DECIMAL128(8)[]
List of ask quantities
highPrice
DECIMAL128(8)
Highest price
lowPrice
DECIMAL128(8)
Lowest price
signal
DOUBLE[]
Other indicator fields
prevClosePrice
DECIMAL128(8)
Previous closing price
settlementPrice
DECIMAL128(8)
Settlement price
prevSettlementPrice
DECIMAL128(8)
Previous settlement price
contractType
INT
Instrument type:
0: Spot
1: Delivery contract
2: Perpetual contract
3: Option
- on_bar(context, msg, indicator)#
The callback function (minute or daily frequency) is triggered when subscribed snapshot data is aggregated into bar data.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
msg (_type_) – Represents the tick-level market data. Each record is provided as either a dictionary object or a table (depending on the msg_as_table configuration). The specific fields vary by asset type. For details, refer to the msg data section below.
indicator (_type_) – A dictionary or nested dictionary with a structure consistent with the corresponding msg, containing the data subscribed to by the strategy.
Msg data#
Stock snapshot / Snapshot + tick trade details / Minute or daily frequency
Field
Type
Description
symbol
SYMBOL
Stock code Ends with “.XSHG” for SSE Ends with “.XSHE” for SZSE
tradeTime
TIMESTAMP
Trading day
open
DOUBLE
Opening price
low
DOUBLE
Lowest price
high
DOUBLE
Highest price
close
DOUBLE
Closing price
volume
LONG
Trading volume
amount
DOUBLE
Trading amount
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
prevClosePrice
DOUBLE
Previous closing price
signal
DOUBLE[]
Other indicators / metrics
Option snapshot
When callback_for_snapshot = 1 or 2 is configured in the backtesting engine, the callback function on_bar will be triggered. The input parameter msg represents a K-line bar, with each K-line containing the following fields:
Field
Type
Description
symbol
SYMBOL
Option code
symbolSource
STRING
Exchange
tradeTime
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
open
DOUBLE
Opening price
low
DOUBLE
Lowest price
high
DOUBLE
Highest price
close
DOUBLE
Closing price
volume
LONG
Trading volume
amount
DOUBLE
Trading amount
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
signal
DOUBLE[]
Other indicators / metrics
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice
DOUBLE
Previous settlement price
underlyingPrice
DOUBLE
Underlying asset price
Theta
DOUBLE
/
Vega
DOUBLE
/
Gamma
DOUBLE
/
Delta
DOUBLE
/
IV
DOUBLE
/
Option minute or daily frequency
The input parameter msg represents a K-line bar, with each K-line containing the following fields:
Field
Type
Description
symbol
SYMBOL
Option code
symbolSource
STRING
Exchange
tradeTime
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
open
DOUBLE
Opening price
low
DOUBLE
Lowest price
high
DOUBLE
Highest price
close
DOUBLE
Closing price
volume
LONG
Trading volume
amount
DOUBLE
Trading amount
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
signal
DOUBLE[]
Other fields list
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice
DOUBLE
Previous settlement price
underlyingPrice
DOUBLE
Underlying asset price
Theta
DOUBLE
/
Vega
DOUBLE
/
Gamma
DOUBLE
/
Delta
DOUBLE
/
IV
DOUBLE
/
Futures snapshot
When callbackForSnapshot = 1, the callback function on_bar will be triggered. The input parameter msg represents a K-line bar, with each K-line containing the following fields:
Field
Type
Description
symbol
SYMBOL
Futures code
symbolSource
STRING
Exchange
timestamp
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
lastPrice
DOUBLE
Latest traded price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Interval buy quantity
totalOfferQty
LONG
Interval sell quantity
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of ask prices
offerQty
LONG[]
List of ask quantities
highPrice
DOUBLE
Highest price
lowPrice
DOUBLE
Lowest price
signal
DOUBLE[]
Other fields list
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice
DOUBLE
Previous settlement price
Futures minute or daily frequency
The input parameter msg represents a K-line bar, with each K-line containing the following fields:
Field
Type
Description
symbol
SYMBOL
Futures code
symbolSource
STRING
Exchange
timestamp
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
lastPrice
DOUBLE
Latest traded price
upLimitPrice
DOUBLE
Upper limit price
downLimitPrice
DOUBLE
Lower limit price
totalBidQty
LONG
Interval buy quantity
totalOfferQty
LONG
Interval sell quantity
bidPrice
DOUBLE[]
List of bid prices
bidQty
LONG[]
List of bid quantities
offerPrice
DOUBLE[]
List of ask prices
offerQty
LONG[]
List of ask quantities
highPrice
DOUBLE
Highest price
lowPrice
DOUBLE
Lowest price
signal
DOUBLE[]
Other fields list
prevClosePrice
DOUBLE
Previous closing price
settlementPrice
DOUBLE
Settlement price
prevSettlementPrice
DOUBLE
Previous settlement price
Digital currency minute or daily frequency
Field
Type
Description
symbol
SYMBOL
Instrument code
symbolSource
SYMBOL
Exchange
tradeTime
TIMESTAMP
Timestamp
tradingDay
DATE
Trading day / settlement date
open
DECIMAL128(8)
Opening price
low
DECIMAL128(8)
Lowest price
high
DECIMAL128(8)
Highest price
close
DECIMAL128(8)
Closing price
volume
DECIMAL128(8)
Trading volume
amount
DECIMAL128(8)
Trading amount
upLimitPrice
DECIMAL128(8)
Upper limit price
downLimitPrice
DECIMAL128(8)
Lower limit price
signal
DOUBLE[]
Other fields list
prevClosePrice
DECIMAL128(8)
Previous closing price
settlementPrice
DECIMAL128(8)
Settlement price
prevSettlementPrice
DECIMAL128(8)
Previous settlement price
contractType
INT
Instrument type
0: Spot
1: Delivery contract
2: Perpetual contract
3: Option
- on_transaction(context, msg, indicator)#
The callback function is triggered upon receiving tick-level trade details. It is supported only for bonds traded on the Shanghai Stock Exchange.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
msg (_type_) – Represents the market data. Each record is provided as either a dictionary object or a table (depending on the msg_as_table configuration).
indicator (_type_) – A dictionary or nested dictionary with a structure consistent with the corresponding msg, containing the data subscribed to by the strategy.
- on_order(context, orders)#
The order update notification function is triggered when an order status changes.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
orders (_type_) –
A dictionary containing order information. The field structure varies depending on the asset type (for example, Shanghai Stock Exchange bonds have a different order field structure than other asset classes).
Shanghai Stock Exchange Bonds
Field
Type
Description
orderId
LONG
Order ID
symbol
STRING
Security code
timestamp
TIMESTAMP
Order timestamp
bidQty
LONG
Bid quantity
bidPrice
DOUBLE
Bid price
bidTotalVolume
LONG
Executed bid quantity
askQty
LONG
Ask quantity
askPrice
DOUBLE
Ask price
askTotalVolume
LONG
Executed ask quantity
status
INT
Order status. Possible values:
4: Reported
0: Partially filled
1: Fully filled
2: Cancelled
-1: Rejected
-2: Cancel reject
-3: Unfilled
direction
INT
Order direction. Possible values:
1: Buy
2: Sell
3: Two-sided
bidTradeValue
DOUBLE
Executed buy value
askTradeValue
DOUBLE
Executed sell value
label
STRING
Label, used to add remarks to the order
updateTime
TIMESTAMP
Update time
Other Assets (excluding SSE Bonds)
Field
Type
Description
orderId
LONG
Order ID
symbol
STRING
Security code
symbolSource
STRING
Exchange (futures only)
timestamp
TIMESTAMP
Order timestamp
qty
LONG
Order quantity
price
DOUBLE
Order price
status
INT
Order status. Possible values:
4: Reported
0: Partially filled
1: Fully filled
2: Cancelled
-1: Rejected
-2: Cancel reject
direction
INT
Order direction. Possible values:
1: Open long
2: Open short
3: Close short
4: Close long
tradeQty
LONG
Total executed quantity
tradeValue
DOUBLE
Total executed value
label
STRING
Label, used to add remarks to the order
updateTime
TIMESTAMP
Update time
- on_trade(context, trades)#
The trade update notification function is triggered when an order is executed.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
trades (_type_) –
A dictionary containing trade information. The field definitions are as follows:
Field
Type
Description
orderId
LONG
Order ID
symbol
STRING
Security code
tradePrice
DOUBLE
Trade price of the current execution
tradeQty
LONG
Trade quantity of the current execution
tradeValue
DOUBLE
Trade value of the current execution
totalFee
DOUBLE
Total transaction fee
bidTotalQty
LONG
Cumulative buy quantity
bidTotalValue
DOUBLE
Cumulative buy value
askTotalQty
LONG
Cumulative sell quantity
askTotalValue
DOUBLE
Cumulative sell value
direction
INT
Order direction. Possible values:
1: Open long
2: Open short
3: Close short
4: Close long
tradeTime
TIMESTAMP
Trade time
orderPrice
DOUBLE
Order price
label
STRING
Label, used to add remarks to the order
- after_trading(context)#
The callback function is triggered at the end of each trading day. It can be used to summarize the day’s trades, positions, and other statistics.
Note: This function is not required for cryptocurrency strategies.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.
- finalize(context)#
The strategy termination callback function is triggered when the backtest completes.
- Parameters:
context (_type_) –
A dictionary representing the global context of the strategy. It is used to store and manage all user-defined variables within the strategy. In addition, the backtesting engine maintains four internal variables within the context:
context.tradeTime — Returns the latest market timestamp.
context.tradeDate — Returns the current trading date.
context.BarTime — Returns the current bar timestamp when market snapshots are aggregated into lower-frequency bars.
context.engine — Returns the internal instance handle of the backtesting engine.