Note
The following documentation closely follows the article by Alexandr Lipton and Marcos Lopez de Prado: “A closed-form solution for optimal mean-reverting trading strategies”
As well as the book by Marcos Lopez de Prado: “Advances in Financial Machine Learning”
A closed-form solution for optimal mean-reverting trading strategies
As stated in the paper by Marcos Lopez de Prado: “When prices reflect all available information, they oscillate around an equilibrium level. This oscillation is the result of the temporary market impact caused by waves of buyers and sellers.” Which means that if we can consider the asset prices to be mean-reverted, then there is a clear toolkit for its approximation, namely, the Ornstein-Uhlenbeck process.
Attempting to monetize this oscillations market makers provide liquidity by entering the long position if the asset is underpriced - hence, the price is lower than the equilibrium, and short position if the situation is reversed. The position is then held until one of the three outcomes occurs:
they achieve a targeted profit
they experience a maximum tolerated loss
the position is held longer then the maximum tolerated horizon
The main problem that arises now is how to define the optimal profit-taking and stop-loss levels. In this module to obtain the solution we utilize the approach introduced by Alexandr Lipton and Marcos Lopez de Prado that applies the method of heat potentials, which is widely applied in physics, to Sharpe ratio maximization problem. The maximization is performed with respect to the border values of our exit corridor.
Problem definition
We suppose an investment strategy S invests in \(i = 1,...I\) opportunities or bets. At each opportunity \(i\), S takes a position of \(m_i\) units of security X, where \(m_i \in (-\infty; +\infty)\). The transaction that entered such opportunity was priced at a value \(m_i P_{i,0}\), where \(P_{i,0}\) is the average price per unit at which the \(m_i\) securities were transacted.
Note
In this approach, we use the volume clock metric instead of the time-based metric. More in that in the paper “The Volume Clock: Insights into the High Frequency Paradigm” by David Easley, Marcos Lopez de Prado, and Maureen O’Hara.
As other market participants continue to transact security X, we can mark-to-market (MtM) the value of that opportunity \(i\) after \(t\) observed transactions as \(m_i P_{1,t}\). Where \(m_i P_{1,t}\) represents the value of opportunity \(i\) if it were liquidated at the price observed in the market after \(t\) transactions. Accordingly, we can compute the MtM p/l of opportunity \(i\) after \(t\) transactions as \(\pi_{i,T_i}=m(P_{i,t}-P_{i,0})\) The exiting opportunity arises in the following scenarios:
\(\pi_{i,T_i}\geq \bar{\pi}\), where \(\bar{\pi}>0\) is a profit-taking threshold
\(\pi_{i,T_i}\leq \underline{\pi}\), where \(\underline{\pi}<0\) is a stop-loss level
Consider the OU process representing the \({P_i}\) series of prices
In order to calculate the Sharpe ratio we need to reformulate our problem in terms of heat potentials.
Suppose S - long investment strategy with p/l driven by the OU process:
and a trading rule \(R = \{ \bar{\pi}',\underline{\pi}',T' \}\). Now we transform it to use its steady-state by performing scaling to remove superfluous parameters.
and get
where \(\theta\) is an expected value and its standard deviation is given by \(\Omega=\frac{1}{\sqrt{2}}\) .
According to the trading rule, we exit the trade in one of the three scenarios:
Price hits a targeted profit \(\bar{\pi}\)
Price hits \(\underline{\pi}\) stop-loss level
The trade expires at \(t=T\)
Tip
Short strategy reverses the roles of \({\bar{\pi}',\underline{\pi}'}\):
\(-\underline{\pi}\) equals the profit taken when the price hits \(\underline{\pi}\) and
\(-\bar{\pi}\) losses are incurred while price hits \(-\bar{\pi}\)
Hence, we can restrict ourself to the case with \(\theta \geq 0\) .
Implementation
First of all the user has to use fit
for the parameters to be scaled to remove superfluous parameters,
and set up the delta for the grid calculation and maximum duration of the trade.
To separately perform the optimization process we use the optimal_levels
function.
There is also a possibility to calculate the Sharpe ratio for chosen optimal levels and the maximum duration of the trade of choice.
To view the optimal levels scaled back to initial parameters the description
function is used.
Example
# Importing modules
from arbitragelab.optimal_mean_reversion.ou_model import OrnsteinUhlenbeck
from arbitragelab.optimal_mean_reversion import HeatPotentials
import numpy as np
# Generating the sample OU data
ou_data = OrnsteinUhlenbeck()
data = ou_data.ou_model_simulation(n=1000, theta_given=0.03711, mu_given=65.3333,
sigma_given=0.3, delta_t_given=1/255)
# To get the model parameters we need to fit the OU model to the data.
# Assign the delta value
ou_data.delta_t = 1/252
# Model fitting
ou_data.fit_to_portfolio(data)
# Now we obtained the parameters to use for our optimization procedure
theta,mu,sigma = ou_data.theta, ou_data.mu, np.sqrt(ou_data.sigma_square)
# Establish the instance of the class
example = HeatPotentials()
# Fit the model and establish the maximum duration of the trade
example.fit(ou_params=(theta, mu, sigma), delta_grid=0.1, max_trade_duration=0.03)
# Calculate the initial optimal levels
levels = example.optimal_levels()
print(levels)
# We can also calculate the Sharpe ratio for given scaled parameters
sr = example.sharpe_calculation(max_trade_duration=1.9599, optimal_profit=5.07525, optimal_stop_loss=-3.41002)
print(sr)
# To get the results scaled back to our initial model we call the description function
example.description()
Research Notebook
The following research notebook can be used to better understand the concepts of the Heat Potentials module.