Stat

class Stat(hbt, utc=True, unit='us', allocated=100000)[source]

Calculates performance statistics and generates a summary of performance metrics.

Parameters:
  • hbt (HftBacktest) – An instance of the HftBacktest class.

  • utc (bool) – If True, timestamps are in UTC.

  • unit (Literal['s', 'ms', 'us', 'ns']) – The unit of the timestamp.

  • allocated (int) – The preallocated size of recorded time series.

annualised_return(denom=None, include_fee=True, trading_days=365)[source]

Calculates annualised return.

Parameters:
  • denom (float | None) – If provided, annualised return will be calculated in percentage terms by dividing by the specified denominator.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

  • trading_days (int) – The number of trading days per year used for annualisation.

Returns:

Annaulised return.

daily_trade_amount()[source]

Retrieves the average value of daily trades.

Returns:

Average value of daily trades.

daily_trade_num()[source]

Retrieves the average number of daily trades.

Returns:

Average number of daily trades.

daily_trade_volume()[source]

Retrieves the average quantity of daily trades.

Returns:

Average quantity of daily trades.

datetime()[source]

Converts and returns a DateTime series from the timestamp.

Returns:

DateTime series by converting from the timestamp.

drawdown(resample=None, include_fee=True)[source]

Retrieves Draw Down time-series.

Parameters:
  • resample (str | None) – The resampling period, such as ‘1s’, ‘5min’.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

Returns:

Draw down time-series.

equity(resample=None, include_fee=True, datetime=True)[source]

Calculates equity values.

Parameters:
  • resample (str | None) – If provided, equity values will be resampled based on the specified period.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

  • datetime (bool) – If set to True, the timestamp is converted to a DateTime, which takes a long time. If you want fast computation, set it to False.

Returns:

the calculated equity values.

maxdrawdown(denom=None, include_fee=True)[source]

Retrieves Maximum Draw Down.

Parameters:
  • denom (float | None) – If provided, MDD will be calculated in percentage terms by dividing by the specified denominator.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

Returns:

Maximum Draw Down.

property recorder

Returns a Recorder instance to record performance statistics.

riskreturnratio(include_fee=True)[source]

Calculates Risk-Return Ratio, which is Annualized Return / Maximum Draw Down over the entire period.

Parameters:

include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

Returns:

Risk-Return Ratio

sharpe(resample, include_fee=True, trading_days=365)[source]

Calculates the Sharpe Ratio without considering benchmark rates.

Parameters:
  • resample (str) – The resampling period, such as ‘1s’, ‘5min’.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

  • trading_days (int) – The number of trading days per year used for annualisation.

Returns:

The calculated Sharpe Ratio.

sortino(resample, include_fee=True, trading_days=365)[source]

Calculates Sortino Ratio.

Parameters:
  • resample (str) – The resampling period, such as ‘1s’, ‘5min’.

  • include_fee (bool) – If set to True, fees will be included in the calculation; otherwise, fees will be excluded.

  • trading_days (int) – The number of trading days per year used for annualisation.

Returns:

Sortino Ratio

summary(capital=None, resample='5min', trading_days=365)[source]

Generates a summary of performance metrics.

Parameters:
  • capital (float | None) – The initial capital investment for the strategy. If provided, it is used as the denominator to calculate annualized return and MDD in percentage terms. Otherwise, absolute values are displayed.

  • resample (str) – The resampling period, such as ‘1s’, ‘5min’.

  • trading_days (int) – The number of trading days per year used for annualisation.

class Recorder(timestamp, mid, balance, position, fee, trade_num, trade_qty, trade_amount)[source]
Parameters:
  • timestamp (ListType(int64)) –

  • mid (ListType(float64)) –

  • balance (ListType(float64)) –

  • position (ListType(float64)) –

  • fee (ListType(float64)) –

  • trade_num (ListType(int64)) –

  • trade_qty (ListType(float64)) –

  • trade_amount (ListType(float64)) –

record(hbt)[source]

Records the current stats.

Parameters:

hbt – An instance of the HftBacktest class.