arbitragelab.time_series_approach.ou_optimal_threshold_zeng

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

Module Contents

Classes

OUModelOptimalThresholdZeng

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

class OUModelOptimalThresholdZeng

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: Zeng, Z. and Lee, C.-G. (2014). Pairs trading: optimal thresholds and profitability. Quantitative Finance, 14(11): 1881–1893.

expected_trade_length(a: float, b: float) float

Calculates the expected trade length.

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

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

Returns:

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

trade_length_variance(a: float, b: float) float

Calculates the expected trade length.

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

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

Returns:

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

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

Calculates the expected return.

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

  • b – (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, b: float, c: float) float

Calculates the variance of return.

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

  • b – (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, b: float, c: float, rf: float) float

Calculates the Sharpe ratio.

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

  • b – (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_conventional_optimal_rule(c: float, initial_guess: float = None) tuple

Solves equation (20) 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 for a short position in the dimensionless system.

Returns:

(tuple) The values of the optimal trading thresholds.

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

Solves equation (23) 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 for a short position in the dimensionless system.

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_s”, “b_s”, “a_l”, “b_l”, “expected_return”, “return_variance”, “sharpe_ratio”, “expected_trade_length”, “trade_length_variance”].

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

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

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

Returns:

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

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

Plots the Sharpe ratio versus risk free rates.

Parameters:
  • method – (str) The method for calculating the optimal thresholds. The options are [“conventional_optimal_rule”, “new_optimal_rule”]

  • 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].