Backtester

class SingleAssetHftBacktest(local, exch)[source]

Single Asset HftBacktest.

Warning

This has to be constructed by HftBacktest().

Parameters:
  • local – Local processor.

  • exch – Exchange processor.

run

Whether a backtest has finished.

current_timestamp

Current timestamp

property position

Current position.

property balance

Current balance..

property orders

Orders dictionary.

property tick_size

Tick size

property lot_size

Lot size

property high_ask_tick

The highest ask price in the market depth in tick.

property low_bid_tick

The lowest bid price in the market depth in tick.

property best_bid_tick

The best bid price in tick.

property best_ask_tick

The best ask price in tick.

property best_bid

The best bid price.

property best_ask

The best ask price.

property bid_depth

Bid market depth.

property ask_depth

Ask market depth.

property mid

Mid-price of BBO.

property equity

Current equity value.

property last_trade

Last market trade. If None, no last market trade.

property last_trades

An array of last market trades.

submit_buy_order(order_id, price, qty, time_in_force, order_type=0, wait=False)[source]

Places a buy order.

Parameters:
  • order_id (int64) – 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 (int64) –

    Available Time-In-Force options vary depending on the exchange model. See to the exchange model for details.

    • GTX: Post-only

    • GTC: Good ‘till Cancel

    • FOK: Fill or Kill

    • IOC: Immediate or Cancel

  • order_type (int64) – Currently, only LIMIT is supported. To simulate a MARKET order, set the price very high.

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

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

submit_sell_order(order_id, price, qty, time_in_force, order_type=0, wait=False)[source]

Places a sell order.

Parameters:
  • order_id (int64) – 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 (int64) –

    Available Time-In-Force options vary depending on the exchange model. See to the exchange model for details.

    • GTX: Post-only

    • GTC: Good ‘till Cancel

    • FOK: Fill or Kill

    • IOC: Immediate or Cancel

  • order_type (int64) – Currently, only LIMIT is supported. To simulate a MARKET order, set the price very low.

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

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

modify(order_id, price, qty, wait=False)[source]

Modify the specified order.

  • If the adjusted total quantity(leaves_qty + executed_qty) is less than or equal to the quantity already executed, the order will be considered expired. Be aware that this adjustment doesn’t affect the remaining quantity in the market, it only changes the total quantity.

  • Modified orders will be reordered in the match queue.

Parameters:
  • order_id (int64) – Order ID to modify.

  • price (float64) – Order price.

  • qty (float64) – Quantity to sell.

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

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

cancel(order_id, wait=False)[source]

Cancel the specified order.

Parameters:
  • order_id (int64) – Order ID to cancel.

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

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

wait_order_response(order_id, timeout=-1)[source]

Wait for the specified order response by order ID.

Parameters:
  • order_id (int64) – The order ID to wait for.

  • timeout (int64) – Maximum waiting time; The default value of -1 indicates no timeout.

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

wait_next_feed(include_order_resp, timeout=-1)[source]

Waits until the next feed is received.

Parameters:
  • include_order_resp (bool) – Whether to include order responses in the feed to wait for.

  • timeout (int) – Maximum waiting time; The default value of -1 indicates no timeout.

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

clear_inactive_orders()[source]

Clear inactive(CANCELED, FILLED, EXPIRED, or REJECTED) orders from the local orders dictionary.

clear_last_trades()[source]

Clears the last trades(market trades) from the buffer.

get_user_data(event)[source]

Retrieve custom user event data.

Parameters:

event (int64) – Event identifier. Refer to the data documentation for details on incorporating custom user data with the market feed data.

Returns:

The latest event data for the specified event.

elapse(duration)[source]

Elapses the specified duration.

Parameters:

duration (float64) – Duration to elapse. Unit should be the same as the feed data’s timestamp unit.

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.

goto(timestamp, wait_order_response=-1)[source]

Goes to a specified timestamp.

This method moves to the specified timestamp, updating the backtesting state to match the corresponding time. If wait_order_response is provided, the method will stop and return when it receives the response for the specified order.

Parameters:
  • timestamp (float64) – The target timestamp to go to. The timestamp unit should be the same as the feed data’s timestamp unit.

  • wait_order_response (int64) – Order ID to wait for; the default value is WAIT_ORDER_RESPONSE_NONE, which means not waiting for any order response.

Returns:

True if the method reaches the specified timestamp within the data. If the end of the data is reached before the specified timestamp, it returns False.