arbitragelab.time_series_approach.regime_switching_arbitrage_rule

The module implements a statistical arbitrage strategy based on the Markov regime-switching model.

Module Contents

Classes

RegimeSwitchingArbitrageRule

This class implements a statistical arbitrage strategy described in the following publication:

class RegimeSwitchingArbitrageRule(delta: float, rho: float)

This class implements a statistical arbitrage strategy described in the following publication: Bock, M. and Mestel, R. (2009). A regime-switching relative value arbitrage rule. Operations Research Proceedings 2008, pages 9–14. Springer.

change_strategy(regime: str, direction: str, action: str, rule: Callable)

This function is used for changing the default strategy.

Parameters:
  • regime – (str) Rule’s regime. It could be either “High” or “Low”.

  • direction – (str) Rule’s direction. It could be either “Long” or “Short”.

  • action – (str) Rule’s action. It could be either “Open” or “Close”.

  • rule – (Callable) A new rule to replace the original rule. The parameters of the rule should be the subset of (Xt, mu, delta, sigma, prob, rho).

get_signal(data: numpy.array | pandas.Series | pandas.DataFrame, switching_variance: bool = False, silence_warnings: bool = False) numpy.array

The function will first fit the Markov regime-switching model to all the input data, then calculate the trading signal at the last timestamp based on the strategy.

Parameters:
  • data – (np.array/pd.Series/pd.DataFrame) A time series for fitting the Markov regime-switching model. The dimensions should be n x 1.

  • switching_variance – (bool) Whether the Markov regime-switching model has different variances in different regimes.

  • silence_warnings – (bool) Flag to silence warnings from failing to fit the Markov regime-switching model to the input data properly.

Returns:

(np.array) The trading signal at the last timestamp of the given data. The returned array will contain four Boolean values, representing whether to open a long trade, close a long trade, open a short trade and close a short trade.

get_signals(data: numpy.array | pandas.Series | pandas.DataFrame, window_size: int, switching_variance: bool = False, silence_warnings: bool = False) numpy.array

The function will fit the Markov regime-switching model with a rolling time window, then calculate the trading signal at the last timestamp of the window based on the strategy.

Parameters:
  • data – (np.array/pd.Series/pd.DataFrame) A time series for fitting the Markov regime-switching model. The dimensions should be n x 1.

  • window_size – (int) Size of the rolling time window.

  • switching_variance – (bool) Whether the Markov regime-switching model has different variances in different regimes.

  • silence_warnings – (bool) Flag to silence warnings from failing to fit the Markov regime-switching model to the input data.

Returns:

(np.array) The array containing the trading signals at each timestamp of the given data. A trading signal at any timestamp will have four Boolean values, representing whether to open a long trade, close a long trade, open a short trade and close a short trade. The returned array dimensions will be n x 4.

get_trades(signals: numpy.array) numpy.array

The function will decide the trade actions at each timestamp based on the signal at time t and the position at time t - 1. The position at time 0 is assumed to be 0.

Parameters:

signals – (np.array) The array containing the trading signals at each timestamp. A trading signal at any timestamp should have four Boolean values, representing whether to open a long trade, close a long trade, open a short trade and close a short trade. The input array dimensions should be n x 4.

Returns:

(np.array) The array containing the trade actions at each timestamp. A trade action at any timestamp will have four Boolean values, representing whether to open a long trade, close a long trade, open a short trade and close a short trade. The returned array dimensions will be n x 4.

static plot_trades(data: numpy.array | pandas.Series | pandas.DataFrame, trades: numpy.array, marker_size: int = 12) matplotlib.pyplot.figure

Plots the trades on the given data.

Parameters:
  • data – (np.array/pd.Series/pd.DataFrame) The time series to plot the trades on. The dimensions should be n x 1.

  • trades – (np.array) The array containing the trade actions at each timestamp of the given data. A trade action at any timestamp should have four Boolean values, representing whether to open a long trade, close a long trade, open a short trade and close a short trade. The input array dimensions will be n x 4.

  • marker_size – (int) Marker size of the plot.

Returns:

(plt.figure) Figure that plots trades on the given data.