Bonds#

The backtesting plugin supports bonds, including interbank bonds and bonds listed on the Shanghai Stock Exchange.

Interbank Bonds#

Configuration#

The configurations are as follows:

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 group

must be “CFETSBond”

“cash”

initial cash flow

DOUBLE type, required

“data_type”

the type of market data

INT type, required, which can be:

  • 2: snapshot + tick data
  • 3: minute-level data (matching_mode must be set to 3 when used)
  • 4: daily data

“matching_mode”

matching mode

INT type.
daily data:

  • 1: matching with the closing price.
  • 2: matching with the open price.
minute-level data:
  • 1: matching when market time > order time.
  • 2: matching with the closing price when market time = order time; matching unfilled orders with mode 1.
snapshot data:
matching with the order price.

“msg_as_table”

process the input market data as table or dictionary

  • false (default): dictionary
  • true: table (the backtesting engine can only be created via the createBacktestEngine interface)

“benck_mark”

benchmark instrument

used in getReturnSummary (e.g., “000300.XSHG”)

“latency”

the latency from order submission to execution

DOUBLE type, in milliseconds

“enable_indicator_optimize”

enable indicator calculation optimization

  • true: enable
  • false (default): disable

“add_time_columnIn_indicator”

add time column to indicator subscription results

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

“isBacktestMode”

backtesting mode

  • true (default): backtesting
  • false: matching

“data_retention_window”

data retention policy for indicator optimization

STRING/INT type. Effective only when both enable_indicator_optimize = true and isBacktestMode = true:

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

“context”

strategy context structure

a dictionary consisting of global strategy variables, for example:
context = {"buySignalRSI": 70} userConfig["context"] = context

“orderbook_matching_ratio”

the proportion of an order that gets filled

DOUBLE type, default is 1.0, valid range: 0 to 1.0.

“matchingRatio”

matching ratio within price intervals

DOUBLE type, valid range: 0 to 1.0. By default, it is equal to the orderbook_matching_ratio.

Schema of security Reference#

Field Name

Data Type

Description

symbol

STRING

underlying asset

couponRate

DOUBLE

coupon rate

frequency

INT

coupon frequency

valueDate

DATE

value date

maturityDate

DATE

maturity Date

Market Data#

When appending data into the backtest engine via the append_data interface:

col_type = {
    "symbol": "SYMBOL", "symbolSource": "SYMBOL", "timestamp": "TIMESTAMP", "bidSettlType": "INT[]", 
    "bidQty": "LONG[]", "bidPrice": "DOUBLE[]", "bidYield": "DOUBLE[]", "askSettlType": "INT[]", 
    "askQty": "LONG[]", "askPrice": "DOUBLE[]", "askYield": "DOUBLE[]", "tradePrice": "DOUBLE[]", 
    "tradeQty": "LONG[]", "settlType": "INT[]", "yield": "DOUBLE[]"
}

message_table = sf.streaming.table(types=col_type, size=0, capacity=10000000)

Schema for snapshot + tick data (“trades”):

Field

Data Type

Description

symbol

SYMBOL

symbol

symbolSource

SYMBOL

market: interbank “X_BOND”

timestamp

TIMESTAMP

timestamp

bidSettlType

INT[]

bid settlement type

bidQty

LONG[]

bid quantity

bidPrice

DOUBLE[]

bid clean price

bidYield

DOUBLE[]

bid yield

askSettlType

INT[]

ask settlement type

askQty

LONG[]

ask quantity

askPrice

DOUBLE[]

ask clean price

askYield

DOUBLE[]

ask yield

tradePrice

DOUBLE[]

trade price list

tradeQty

LONG[]

trade quantity list

settlType

INT[]

settlement type

yield

DOUBLE[]

yield to maturity

Minute-level data:

Field

Data Type

Description

symbol

SYMBOL

symbol

symbolName

STRING

symbol name

tradeDate

TIMESTAMP

trade date

maturityDate

DOUBLE

maturity date

tradeClean

DOUBLE

trade clean price

tradeDirty

DOUBLE

trade dirty price

tradeAccruedInterest

DOUBLE

accrued interest

yield

DOUBLE

yield to maturity

tradeVolume

DOUBLE

trade volume

When the backtest ends, a message with symbol=”END” is sent:

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)

Strategy Callback Function#

Snapshot market callback function on_snapshot: input parameter msg.

If msg is a dictionary, it is a snapShot data dictionary with symbol=”key”, where each snapShot object contains the following fields:

Field

Data Type

Notes

symbol

SYMBOL

symbol

messageSource

SYMBOL

market: interbank “X_BOND”

byield

TIMESTAMP

timestamp

ayield

DOUBLE[]

bid yield to maturity

bmdEntryPrice

DOUBLE[]

bid clean price

amdEntryPrice

DOUBLE[]

ask clean price

bmdEntrySize

LONG[]

bid quantity

amdEntrySize

LONG[]

ask quantity

bsettlType

LONG[]

bid settlement type

asettlType

LONG[]

ask settlement type

settlType

LONG[]

settlement type

tradePrice

DOUBLE[]

trade price list

tradeYield

DOUBLE[]

yield to maturity

tradeQty

LONG[]

trade quantity list

SSE Bonds#

Configuration#

The parameters of the Backtester interface are as follows:

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 group

must be “XSHGBond”

“cash”

initial cash flow

DOUBLE type, required

“commission”

commission

DOUBLE type, required

“tax”

stamp duty

DOUBLE type, required

“data_type”

the type of market data

INT type, required, which can be:

  • 2: snapshot + trades details
  • 4: daily data

“orderbook_matching_ratio”

the proportion of an order that gets filled

DOUBLE type, default is 1.0, valid range: 0 to 1.0

“matchingRatio”

matching ratio within price intervals

DOUBLE type, valid range: 0 to 1.0. By default, it is equal to the orderbook_matching_ratio

“setLastDayPosition”

base position settings

base position information table, see the table schema below

The schema of the base position information table is as follows:

Field

Data Type

Description

symbol

SYMBOL

stock code

longPosition

LONG/INT

long position size

costPrice

DOUBLE

cost price

Schema of Security Reference#

Field

Data Type

Description

symbol

STRING

symbol

couponRate

DOUBLE

coupon rate

frequency

INT

payment frequency

valueDate

DATE

value date

maturityDate

DATE

maturity date

Market Data#

The input market data supports a table composed of “firm quote market data” and “trade details,” with the following schema:

Field

Data Type

Notes

symbol

SYMBOL/STRING

symble

timestamp

TIMESTAMP

timestamp

msgType

INT

market type, which can be:

  • 0: firm quote market data
  • 1: trade details

symbolName

STRING

symbol name

bidId

INT

bid order id

bidTime

TIMESTAMP

bid time

bidParty

SYMBOL/STRING

bid party

bidClean

DOUBLE

bid clean price

bidQty

LONG

bid quantity

bidDirty

DOUBLE

bid dirty price

bidYield

DOUBLE

bid yield to maturity

askId

INT

ask order id

askTime

TIMESTAMP

ask time

askParty

SYMBOL/STRING

ask party

askClean

DOUBLE

ask clean price

askQty

LONG

ask quantity

askDirty

DOUBLE

ask dirty price

askYield

DOUBLE

ask yield to maturity

accruedInterest

DOUBLE

accrued interest

tradeTime

TIMESTAMP

trade time

tradeClean

DOUBLE

trade clean price

tradeAccruedInterest

DOUBLE

trade accrued interest

tradeDirty

DOUBLE

trade dirty price

yield

DOUBLE

yield to maturity

tradeVolume

INT

trade volume

tradeValue

DOUBLE

trade value

tradeType

INT

trading type, which can be:

  • 1: firm quote
  • 2: pending quote
  • 3: request for quote
  • 4: negotiated trade
  • 6: negotiated trade (including mixed declarations)
  • 7: bidding