arbitragelab.time_series_approach.ou_optimal_threshold_bertram

The module implements the Bertram class for OU Optimal Threshold Model.

Module Contents

Classes

OUModelOptimalThresholdBertram

This class implements the analytic solutions of the optimal trading thresholds for the series

class OUModelOptimalThresholdBertram

Bases: arbitragelab.time_series_approach.ou_optimal_threshold.OUModelOptimalThreshold

This class implements the analytic solutions of the optimal trading thresholds for the series with mean-reverting properties. The methods are described in the following publication: Bertram, W. K. (2010). Analytic solutions for optimal statistical arbitrage trading. Physica A: Statistical Mechanics and its Applications, 389(11): 2234–2243.

expected_trade_length(a: float, m: float) float

Calculates equation (9) in the paper to get the expected trade length.

Parameters:
  • a – (float) The entry threshold of the trading strategy.

  • m – (float) The exit threshold of the trading strategy.

Returns:

(float) The expected trade length of the trading strategy.

trade_length_variance(a: float, m: float) float

Calculates equation (10) in the paper to get the variance of trade length.

Parameters:
  • a – (float) The entry threshold of the trading strategy.

  • m – (float) The exit threshold of the trading strategy.

Returns:

(float) The variance of trade length of the trading strategy.

expected_return(a: float, m: float, c: float) float

Calculates equation (5) in the paper to get the expected return.

Parameters:
  • a – (float) The entry threshold of the trading strategy.

  • m – (float) The exit threshold of the trading strategy.

  • c – (float) The transaction costs of the trading strategy.

Returns:

(float) The expected return of the trading strategy.

return_variance(a: float, m: float, c: float) float

Calculates equation (6) in the paper to get the variance of return.

Parameters:
  • a – (float) The entry threshold of the trading strategy.

  • m – (float) The exit threshold of the trading strategy.

  • c – (float) The transaction costs of the trading strategy.

Returns:

(float) The variance of return of the trading strategy.

sharpe_ratio(a: float, m: float, c: float, rf: float) float

Calculates equation (15) in the paper to get the Sharpe ratio.

Parameters:
  • a – (float) The entry threshold of the trading strategy.

  • m – (float) The exit threshold of the trading strategy.

  • c – (float) The transaction costs of the trading strategy.

  • rf – (float) The risk free rate.

Returns:

(float) The Sharpe ratio of the strategy.

get_threshold_by_maximize_expected_return(c: float, initial_guess: float = None) tuple

Solves equation (13) in the paper to get the optimal trading thresholds.

Parameters:
  • c – (float) The transaction costs of the trading strategy.

  • initial_guess – (float) The initial guess of the entry threshold.

Returns:

(tuple) The values of the optimal trading thresholds.

get_threshold_by_maximize_sharpe_ratio(c: float, rf: float, initial_guess: float = None) tuple

Minimize -1 * Sharpe ratio to get the optimal trading thresholds.

Parameters:
  • c – (float) The transaction costs of the trading strategy.

  • rf – (float) The risk free rate.

  • initial_guess – (float) The initial guess of the entry threshold.

Returns:

(tuple) The values of the optimal trading thresholds.

plot_target_vs_c(target: str, method: str, c_list: list, rf: float = 0) matplotlib.pyplot.figure

Plots target versus transaction costs.

Parameters:
  • target – (str) The target values to plot. The options are [“a”, “m”, “expected_return”, “return_variance”, “sharpe_ratio”, “expected_trade_length”, “trade_length_variance”].

  • method – (str) The method for calculating the optimal thresholds. The options are [“maximize_expected_return”, “maximize_sharpe_ratio”].

  • c_list – (list) A list contains transaction costs.

  • rf – (float) The risk free rate. It is only needed when the target is “sharpe_ratio” or when the method is “maximize_sharpe_ratio”.

Returns:

(plt.figure) Figure that plots target versus transaction costs.

plot_target_vs_rf(target: str, method: str, rf_list: list, c: float) matplotlib.pyplot.figure

Plots target versus risk free rates.

Parameters:
  • target – (str) The target values to plot. The options are [“a”, “m”, “expected_return”, “return_variance”, “sharpe_ratio”, “expected_trade_length”, “trade_length_variance”].

  • method – (str) The method for calculating the optimal thresholds. The options are [“maximize_expected_return”, “maximize_sharpe_ratio”].

  • rf_list – (list) A list contains risk free rates.

  • c – (float) The transaction costs of the trading strategy.

Returns:

(plt.figure) Figure that plots target versus risk free rates.

construct_ou_model_from_given_parameters(theta: float, mu: float, sigma: float)

Initializes the O-U process from given parameters.

Parameters:
  • theta – (float) The long-term mean of the O-U process.

  • mu – (float) The speed at which the values will regroup around the long-term mean.

  • sigma – (float) The amplitude of randomness of the O-U process.

fit_ou_model_to_data(data: numpy.array | pandas.DataFrame, data_frequency: str)

Fits the O-U process to log values of the given data.

Parameters:
  • data – (np.array/pd.DataFrame) It could be a single time series or a time series of two assets prices. The dimensions should be either n x 1 or n x 2.

  • data_frequency – (str) Data frequency [“D” - daily, “M” - monthly, “Y” - yearly].