I am trying to calculate the trade signal outlined in Avellaneda & Lee paper "Statistical Arbitrage in the US Equities Market".

They describe their approach in appendix. Here is my attempt on simulated data:

# Simulate returns
window = 60
stock_returns = np.random.normal(0.0005, 0.01, window)
etf_returns = np.random.normal(0.0004, 0.008, window)

# Standardize
stock_returns_standardised = (stock_returns - stock_returns.mean())/stock_returns.std()
etf_returns_standardised = (etf_returns - etf_returns.mean())/etf_returns.std()

# Run regression of stock returns on ETF returns
etf_returns_with_const = sm.add_constant(etf_returns_standardised)
model = sm.OLS(stock_returns_standardised, etf_returns_with_const)
results = model.fit()

# Calculate the residuals from the regression (idiosyncratic returns)
residuals = results.resid

# Fit an AR(1) model to the residuals
ar_model = AutoReg(residuals, lags=1)
ar_results = ar_model.fit()

# Obtain the autocorrelation coefficients 'a' and 'b' from the AR(1) model
a = ar_results.params[0] 
b = ar_results.params[1] 

# Calculate the signal
s_score = -a * np.sqrt(1 - b**2) / ((1 - b) * np.sqrt(np.var(residuals)))

There is some issue with this calculation as visually the signals time series does not make sense to me when I apply the logic to my actual data.

As many of the concepts here are new to me, I would appreciate any help with correcting my approach.

  • $\begingroup$ Please provide a DOI link to the paper $\endgroup$ Dec 9, 2023 at 17:49
  • $\begingroup$ What programming language is that? $\endgroup$ Dec 9, 2023 at 17:50
  • $\begingroup$ Just follow this post, it's very nicely written. $\endgroup$
    – quanted
    Jun 1 at 14:30

2 Answers 2


From memory they assume that the sum of residuals is a mean reverting process, whereas your code assumes a random walk in the residuals + no correlation between the stock and ETF return process. I would suggest attempting this using actual trading data from Yahoo or other free resources.

  • $\begingroup$ Thanks for the answer. What changes do I need to make to model is as a mean-reverting process? I have some actual data in place but trying to work out the correct calculation here $\endgroup$
    – arkon
    Nov 7, 2023 at 14:51
  • $\begingroup$ @arkon Look at the CIR process. Any type of process that has a random walk + mean reversion component will be type of mean reverting process. All you need basically is $dx_t = \alpha (\mu - x_t) d_t + \beta dW_t$. The $ \alpha (\mu - x_t) d_t $ component makes the process revert to $\mu$ at the rate of $\alpha$. $\endgroup$ Dec 8, 2023 at 0:02

if i'm reading your code correctly, you have look forward bias. in the second block of code, you use the mean and stdv of the entire return series, rather than a backwards looking measure of those metrics


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.