Backtester

class HashMapMarketDepthBacktest[source]
Parameters:

ptr (void*)

property current_timestamp: int64

In backtesting, this timestamp reflects the time at which the backtesting is conducted within the provided data.

depth(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the market depth will be retrieved.

Returns:

The depth of market of the specific asset.

Return type:

HashMapMarketDepth

property num_assets: uint64

Returns the number of assets.

position(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the position will be retrieved.

Returns:

The quantity of the held position.

Return type:

float64

state_values(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the state values will be retrieved.

Returns:

The state’s values.

Return type:

StateValues

last_trades(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the trades will be retrieved.

Returns:

An array of Event representing trades occurring in the market for the specific asset.

Return type:

ndarray[Any, dtype([(‘ev’, ‘<u8’), (‘exch_ts’, ‘<i8’), (‘local_ts’, ‘<i8’), (‘px’, ‘<f8’), (‘qty’, ‘<f8’), (‘order_id’, ‘<u8’), (‘ival’, ‘<i8’), (‘fval’, ‘<f8’)], align=True)]

clear_last_trades(asset_no)[source]

Clears the last trades occurring in the market from the buffer for last_trades().

Parameters:

asset_no (uint64) – Asset number at which this command will be executed. If ALL_ASSETS, all last trades in any assets will be cleared.

Return type:

None

orders(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which orders will be retrieved.

Returns:

An order dictionary where the keys are order IDs and the corresponding values are Order.

Return type:

OrderDict

submit_buy_order(asset_no, order_id, price, qty, time_in_force, order_type, wait)[source]

Submits a buy order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – The unique order ID; there should not be any existing order with the same ID on both local and exchange sides.

  • price (float64) – Order price.

  • qty (float64) – Quantity to buy.

  • time_in_force (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • order_type (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • wait (bool) – If True, wait until the order placement response is received.

Returns:

  • 0 when it successfully submits an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

submit_sell_order(asset_no, order_id, price, qty, time_in_force, order_type, wait)[source]

Submits a sell order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – The unique order ID; there should not be any existing order with the same ID on both local and exchange sides.

  • price (float64) – Order price.

  • qty (float64) – Quantity to sell.

  • time_in_force (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • order_type (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • wait (bool) – If True, wait until the order placement response is received.

Returns:

  • 0 when it successfully submits an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

cancel(asset_no, order_id, wait)[source]

Cancels the specified order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – Order ID to cancel.

  • wait (bool) – If True, wait until the order cancel response is received.

Returns:

  • 0 when it successfully cancels an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

clear_inactive_orders(asset_no)[source]

Clears inactive orders from the local order dictionary whose status is neither NEW nor PARTIALLY_FILLED.

Parameters:

asset_no (uint64) – Asset number at which this command will be executed. If ALL_ASSETS, all inactive orders in any assets will be cleared.

Return type:

None

wait_order_response(asset_no, order_id, timeout)[source]

Waits for the response of the order with the given order ID until timeout.

Parameters:
  • asset_no (uint64) – Asset number where an order with order_id exists.

  • order_id (uint64) – Order ID to wait for the response.

  • timeout (int64) – Timeout for waiting for the order response. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it receives an order response for the specified order ID of the specified asset number, or reaches the timeout.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

wait_next_feed(include_order_resp, timeout)[source]

Waits until the next feed is received, or until timeout.

Parameters:
  • include_order_resp (bool) – If set to True, it will return when any order response is received, in addition to the next feed.

  • timeout (int64) – Timeout for waiting for the next feed or an order response. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it receives a feed or an order response, or reaches the timeout.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

elapse(duration)[source]

Elapses the specified duration.

Parameters:

duration (uint64) – Duration to elapse. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it successfully elapses the given duration.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

elapse_bt(duration)[source]

Elapses time only in backtesting. In live mode, it is ignored. (Supported only in the Rust implementation)

The elapse method exclusively manages time during backtesting, meaning that factors such as computing time are not properly accounted for. So, this method can be utilized to simulate such processing times.

Parameters:

duration (int64) – Duration to elapse. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it successfully elapses the given duration.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

close()[source]

Closes this backtester or bot.

Returns:

  • 0 when it successfully closes the bot.

  • Otherwise, an error occurred.

Return type:

int64

feed_latency(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the last feed latency will be retrieved.

Returns:

The last feed’s exchange timestamp and local receipt timestamp if a feed has been received; otherwise, returns None.

Return type:

Tuple[int64, int64] | None

order_latency(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the last order latency will be retrieved.

Returns:

The last order’s request timestamp, exchange timestamp, and response receipt timestamp if there has been an order submission; otherwise, returns None.

Return type:

Tuple[int64, int64, int64] | None

class HashMapMarketDepth[source]
Parameters:

ptr (void*)

property best_bid_tick: int64

Returns the best bid price in ticks.

property best_ask_tick: int64

Returns the best ask price in ticks.

property best_bid: float64

Returns the best bid price.

property best_ask: float64

Returns the best ask price.

property tick_size: float64

Returns the tick size.

property lot_size: float64

Returns the lot size.

bid_qty_at_tick(price_tick)[source]

Returns the quantity at the bid market depth for a given price in ticks.

Parameters:

price_tick (int64) – Price in ticks.

Returns:

The quantity at the specified price.

Return type:

float64

ask_qty_at_tick(price_tick)[source]

Returns the quantity at the ask market depth for a given price in ticks.

Parameters:

price_tick (int64) – Price in ticks.

Returns:

The quantity at the specified price.

Return type:

float64

class ROIVectorMarketDepthBacktest[source]
Parameters:

ptr (void*)

property current_timestamp: int64

In backtesting, this timestamp reflects the time at which the backtesting is conducted within the provided data.

depth(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the market depth will be retrieved.

Returns:

The depth of market of the specific asset.

Return type:

ROIVectorMarketDepth

property num_assets: uint64

Returns the number of assets.

position(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the position will be retrieved.

Returns:

The quantity of the held position.

Return type:

float64

state_values(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the state values will be retrieved.

Returns:

The state’s values.

Return type:

StateValues

last_trades(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the trades will be retrieved.

Returns:

An array of Event representing trades occurring in the market for the specific asset.

Return type:

ndarray[Any, dtype([(‘ev’, ‘<u8’), (‘exch_ts’, ‘<i8’), (‘local_ts’, ‘<i8’), (‘px’, ‘<f8’), (‘qty’, ‘<f8’), (‘order_id’, ‘<u8’), (‘ival’, ‘<i8’), (‘fval’, ‘<f8’)], align=True)]

clear_last_trades(asset_no)[source]

Clears the last trades occurring in the market from the buffer for last_trades().

Parameters:

asset_no (uint64) – Asset number at which this command will be executed. If ALL_ASSETS, all last trades in any assets will be cleared.

Return type:

None

orders(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which orders will be retrieved.

Returns:

An order dictionary where the keys are order IDs and the corresponding values are Order.

Return type:

OrderDict

submit_buy_order(asset_no, order_id, price, qty, time_in_force, order_type, wait)[source]

Submits a buy order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – The unique order ID; there should not be any existing order with the same ID on both local and exchange sides.

  • price (float64) – Order price.

  • qty (float64) – Quantity to buy.

  • time_in_force (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • order_type (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • wait (bool) – If True, wait until the order placement response is received.

Returns:

  • 0 when it successfully submits an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

submit_sell_order(asset_no, order_id, price, qty, time_in_force, order_type, wait)[source]

Submits a sell order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – The unique order ID; there should not be any existing order with the same ID on both local and exchange sides.

  • price (float64) – Order price.

  • qty (float64) – Quantity to sell.

  • time_in_force (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • order_type (uint8) –

    Available options vary depending on the exchange model. See to the exchange model for details.

  • wait (bool) – If True, wait until the order placement response is received.

Returns:

  • 0 when it successfully submits an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

cancel(asset_no, order_id, wait)[source]

Cancels the specified order.

Parameters:
  • asset_no (uint64) – Asset number at which this command will be executed.

  • order_id (uint64) – Order ID to cancel.

  • wait (bool) – If True, wait until the order cancel response is received.

Returns:

  • 0 when it successfully cancels an order.

  • 1 when it reaches the end of the data, if wait is True.

  • Otherwise, an error occurred.

Return type:

int64

clear_inactive_orders(asset_no)[source]

Clears inactive orders from the local order dictionary whose status is neither NEW nor PARTIALLY_FILLED.

Parameters:

asset_no (uint64) – Asset number at which this command will be executed. If ALL_ASSETS, all inactive orders in any assets will be cleared.

Return type:

None

wait_order_response(asset_no, order_id, timeout)[source]

Waits for the response of the order with the given order ID until timeout.

Parameters:
  • asset_no (uint64) – Asset number where an order with order_id exists.

  • order_id (uint64) – Order ID to wait for the response.

  • timeout (int64) – Timeout for waiting for the order response. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it receives an order response for the specified order ID of the specified asset number, or reaches the timeout.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

wait_next_feed(include_order_resp, timeout)[source]

Waits until the next feed is received, or until timeout.

Parameters:
  • include_order_resp (bool) – If set to True, it will return when any order response is received, in addition to the next feed.

  • timeout (int64) – Timeout for waiting for the next feed or an order response. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it receives a feed or an order response, or reaches the timeout.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

elapse(duration)[source]

Elapses the specified duration.

Parameters:

duration (uint64) – Duration to elapse. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it successfully elapses the given duration.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

elapse_bt(duration)[source]

Elapses time only in backtesting. In live mode, it is ignored. (Supported only in the Rust implementation)

The elapse method exclusively manages time during backtesting, meaning that factors such as computing time are not properly accounted for. So, this method can be utilized to simulate such processing times.

Parameters:

duration (int64) – Duration to elapse. Nanoseconds is the default unit. However, unit should be the same as the data’s timestamp unit.

Returns:

  • 0 when it successfully elapses the given duration.

  • 1 when it reaches the end of the data.

  • Otherwise, an error occurred.

Return type:

int64

close()[source]

Closes this backtester or bot.

Returns:

  • 0 when it successfully closes the bot.

  • Otherwise, an error occurred.

Return type:

int64

feed_latency(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the last feed latency will be retrieved.

Returns:

The last feed’s exchange timestamp and local receipt timestamp if a feed has been received; otherwise, returns None.

Return type:

Tuple[int64, int64] | None

order_latency(asset_no)[source]
Parameters:

asset_no (uint64) – Asset number from which the last order latency will be retrieved.

Returns:

The last order’s request timestamp, exchange timestamp, and response receipt timestamp if there has been an order submission; otherwise, returns None.

Return type:

Tuple[int64, int64, int64] | None

class ROIVectorMarketDepth[source]
Parameters:

ptr (void*)

property best_bid_tick: int64

Returns the best bid price in ticks.

property best_ask_tick: int64

Returns the best ask price in ticks.

property best_bid: float64

Returns the best bid price.

property best_ask: float64

Returns the best ask price.

property tick_size: float64

Returns the tick size.

property lot_size: float64

Returns the lot size.

bid_qty_at_tick(price_tick)[source]

Returns the quantity at the bid market depth for a given price in ticks.

Parameters:

price_tick (int64) – Price in ticks.

Returns:

The quantity at the specified price.

Return type:

float64

ask_qty_at_tick(price_tick)[source]

Returns the quantity at the ask market depth for a given price in ticks.

Parameters:

price_tick (int64) – Price in ticks.

Returns:

The quantity at the specified price.

Return type:

float64

property bid_depth: ndarray[Any, float64]

Returns the bid market depth array, which contains the quantity at each price. Its length is ROI upper bound in ticks + 1 - ROI lower bound in ticks, the array contains the quantities at prices from the ROI lower bound to the ROI upper bound. The index is calculated as price in ticks - ROI lower bound in ticks. Respectively, the price is (index + ROI lower bound in ticks) * tick_size.

property ask_depth: ndarray[Any, float64]

Returns the ask market depth array, which contains the quantity at each price. Its length is ROI upper bound in ticks + 1 - ROI lower bound in ticks, the array contains the quantities at prices from the ROI lower bound to the ROI upper bound. The index is calculated as price in ticks - ROI lower bound in ticks. Respectively, the price is (index + ROI lower bound in ticks) * tick_size.

class OrderDict[source]

This is a wrapper for the order dictionary. It only supports get() method, in operator through __contains__(), and values() method for iterating over values. Please note the limitations of the values iterator.

Parameters:

ptr (void*)

values()[source]

Since numba does not support __next__ method in njit, you need to manually iterate using the next, which returns the next order value if it exists; otherwise, it returns None.

Example

values = order_dict.values()
while True:
    order = values.next()
    if order is None:
        break
    # Do what you need with the order.

Alternatively, has_next returns True if there is a next element and False otherwise, while also moving the iterator to the next element internally. get method then returns the element moved to by the has_next method. Since has_next internally moves the iterator, it should not be used solely to check if there is a next element.

Example

values = order_dict.values()
while values.has_next():
    order = values.get()
    # Do what you need with the order.
Return type:

Values

get(order_id)[source]
Parameters:

order_id (uint64) – Order ID

Returns:

Order with the specified order ID; None if it does not exist.

Return type:

Order | None

class Order[source]
Parameters:

arr (Array(Record([('qty', {'type': float64, 'offset': 0, 'alignment': None, 'title': None, }), ('leaves_qty', {'type': float64, 'offset': 8, 'alignment': None, 'title': None, }), ('exec_qty', {'type': float64, 'offset': 16, 'alignment': None, 'title': None, }), ('exec_price_tick', {'type': int64, 'offset': 24, 'alignment': None, 'title': None, }), ('price_tick', {'type': int64, 'offset': 32, 'alignment': None, 'title': None, }), ('tick_size', {'type': float64, 'offset': 40, 'alignment': None, 'title': None, }), ('exch_timestamp', {'type': int64, 'offset': 48, 'alignment': None, 'title': None, }), ('local_timestamp', {'type': int64, 'offset': 56, 'alignment': None, 'title': None, }), ('order_id', {'type': uint64, 'offset': 64, 'alignment': None, 'title': None, }), ('_q1', {'type': uint64, 'offset': 72, 'alignment': None, 'title': None, }), ('_q2', {'type': uint64, 'offset': 80, 'alignment': None, 'title': None, }), ('maker', {'type': bool, 'offset': 88, 'alignment': None, 'title': None, }), ('order_type', {'type': uint8, 'offset': 89, 'alignment': None, 'title': None, }), ('req', {'type': uint8, 'offset': 90, 'alignment': None, 'title': None, }), ('status', {'type': uint8, 'offset': 91, 'alignment': None, 'title': None, }), ('side', {'type': int8, 'offset': 92, 'alignment': None, 'title': None, }), ('time_in_force', {'type': uint8, 'offset': 93, 'alignment': None, 'title': None, })], 96, True), 1, 'A', False, aligned=True))

property price: float64

Returns the order price.

property exec_price: float64

Returns the executed price. This is only valid if status is FILLED or PARTIALLY_FILLED.

property cancellable: bool

Returns whether this order can be canceled. The order can be canceled only if it is active, meaning its status should be NEW or PARTIALLY_FILLED. It is not necessary for there to be no ongoing requests on the order to cancel it. However, HftBacktest currently enforces that there are no ongoing requests to cancel this order to simplify the implementation.

property qty: float64

Returns the order quantity.

property leaves_qty: float64

Returns the remaining active quantity after the order has been partially filled. In backtesting, this is only valid in exchange models that support partial fills, such as PartialFillExchange model.

property price_tick: int64

Returns the order price in ticks.

property tick_size: float64

Returns the tick size.

property exch_timestamp: int64

Returns the timestamp when the order is processed by the exchange.

property local_timestamp: int64

Returns the timestamp when the order request is made by the local.

property exec_price_tick: int64

Returns the executed price in ticks. This is only valid if status is FILLED or PARTIALLY_FILLED.

property exec_qty: float64

Returns the executed quantity. This is only valid if status is FILLED or PARTIALLY_FILLED.

property order_id: uint64

Returns the order ID.

property order_type: uint8

Returns the order type. This can be one of the following values, but may vary depending on the exchange model.

property req: uint8

Returns the type of the current ongoing request. This can be one of the following values, but may vary depending on the exchange model.

  • NONE for no ongoing request.

  • NEW for submitting a new order.

  • CANCELED for canceling the order.

property status: uint8

Returns the order status. This can be one of the following values, but may vary depending on the exchange model.

property side: uint8

Returns the order side.

property time_in_force: uint8

Returns the Time-In-Force of the order. This can be one of the following values, but may vary depending on the exchange model.