Skip to content
All library documents

Monte Carlo Simulation for Cointegrated Pairs Trading

Article Quant Q&A · Author: Michal

Summary

The question asks how to simulate two cointegrated asset prices to evaluate a spread trading rule. It considers modeling only the spread, simulating both prices, or using a vector autoregression, while noting that the individual assets need not be mean reverting. One response suggests estimating the hedge relationship on rolling data segments and proposes adding stochastic and nonlinear terms to the spread, but does not give a validated simulation design. Other answers recommend fitting time series models, generating paths, and evaluating strategy parameters out of sample, or emphasize that simulated outcomes depend on assumptions imposed on the paths.

The exchange illustrates that simulation cannot independently establish that a pair will converge: results depend on how dependence, convergence, and trading costs are modeled. Its suggestions are uneven; the nonlinear construction is speculative, and the categorical claim that Monte Carlo cannot test a strategy is too strong. A sound simulation needs an explicit joint model that preserves cointegration, followed by careful out-of-sample and robustness evaluation.

Key ideas

  • A pairs strategy trades a spread formed from a regression relationship between two assets.
  • Simulating both prices requires a joint model that preserves the relationship between them.
  • Monte Carlo results depend on assumptions about convergence, noise, and model parameters.
  • Rolling estimation can expose changes in the hedge relationship, but does not by itself define a Monte Carlo path model.
  • Out-of-sample evaluation and robustness checks are needed to assess a simulated trading rule.

Tags

Full text
# Cointegration pair trading - how to test a trading rule using Monte Carlo?


# Cointegration pair trading - how to test a trading rule using Monte Carlo?












I am doing a research exercise where I have two price series $X_t, Y_t$ which I regress against each other and test for cointegration. Once I confirm that they are cointegrated (using CADF or Johansen test) I derive the equation for the spread as $$spread_t = Y_t - (\alpha + \beta X_t)$$

or alternatively $$spread_t = Y_t - \beta X_t$$

where $\alpha$ is the intercept and $\beta$ is the slope coefficient from the regression of $Y_t$ on $X_t$.

As a next step I want to do Monte Carlo simulations to test the simple trading rule.

- if spread above 2 st.dev wrt equilibrium buy X and sell Y;

- if spread below 2 st.dev wrt equilibrium sell X and buy Y;

but don't know how to construct the Monte Carlo paths for the simulation.

Could anybody explain how the simulation could be carried out? Most of the textbooks and research papers explain just how to find asset pairs and test them for the cointegration. Some explain the back-testing on the historical data. I haven't found any explanations how the Monte Carlo simulation could be used to test such trading strategy. Should be only the spread modeled in the AR(1) like process? It seems that one would need to simulate the asset prices anyway. Should I maybe use the Vector Auto-regressive Model (VAR)? My understanding here is that this would give me some equation again where the spread would be expressed in therms of lagged asset prices. Each of the two assets doesn't have to be mean reverting. If I model the prices independently how then the relation (cointegration) between them would be preserved?

## Answer by Madhur (score 1, accepted)

https://quant.stackexchange.com/a/30017

I am somewhat missing the objectivity or the distinctness of the objective of the question. Nevertheless, here are the conclusions on what I have understood.

STEP I: Define and ascertain the minimum number of the data-points you need, for determination of $\alpha$ and $\beta$ and let this minimum number be called as a CHUNK or QUANTUM or BUNDLE. Also define the incremental stepping size e.g. $\mathscr{step\_size}$, by which the chunk would move forward in time. For a $\mathscr{chunk\_size}$ = 1000 and $\mathscr{step\_size}$ = 200, the input data moves from 0 to 1000; 200 to 1200; 400 to 1400; 600 to 1600 and likewise, and let's call these SEGMENTS.

STEP II: Each SEGMENT (after testing for the cointegration), would give somewhat different values for the $\alpha$ and $\beta$ which would give different values for $\mathscr{spread_t}$ form a set of random numbers, though a very closely seeded one. From these SEGMENT values, the decision making can be done.

Note 1: I think it is the first step for which you wish to create MonteCarlo paths for the simulations. With C++, it can be accomplished using OpenMP, though it is quite stone-age primitive for something like MonteCarlo but with some clever tricks, it usually acquiesces to programmer's wishes. Note 2: The crux of the approach is keeping the $\mathscr{step\_size}$ sufficiently short; through which a lot of closely-spaced and random(which is a paradox; hence quasi-random) values for $\mathscr{spread_t}$ can be generated. Note 3: MonteCarlo simulations are fundamentally used where the parametric form of the conditioning has been already established and a large number of experiments needed to be done to ascertain the value of the desired variable. In this case, it can be adjudicated that the parametric form for determining $\mathscr{spread_t}$ has been ascertained and hence a MonteCarlo procedure can be carried out, with introduction of stochastic non-linear weight function in the $\mathscr{spread_t}$ (see below). Please note that, these $\mathscr{chunk\_size}$ and $\mathscr{step\_size}$ themselves can be made random, which would perhaps require more number of experiments to conduct.

Given this problem, my personal approach would be different, somewhat along the lines of:

STEP I: Modifying the spread, after the test for the cointegration has been confirmed, as this establishes the linearity of the relationship. Introducing a function such as, $f(t) = k+e^{-\zeta(t, \sigma(t), \eta(t), \delta, \gamma, \cdots)}$, where $k$ is a real number, the shift factor, $\delta$ is a function that gives random number between 0 and 1, accounting for the normalized market-noise (this function mimics the so-called $\epsilon_i$, the random component, in regression analysis) and where $\eta(t)$ is simply the causal function. I would introduce another multiplicative variable $\gamma$ depending upon the historical weights and major share-holders' quantifiable decision making patterns. (It would be much easier, if the $X_t$ and $Y_t$ are the components of the DOW or S&P 500, as the major shareholding figures and the information about shareholders themselves, are easily available for such companies). Please notice that I am deliberately introducing the controlled non-linearity in this relationship for dynamic evaluation of $\mathscr{spread_t}$. Here I have constructed a function for $\eta(t)$ for the NASDAQ:AAPL with only 8 data points, though it is extremely primitive, it does illustrate the way and future trend for the equity in the neighborhood of AUG.19.2016. $$\eta(\tau) = \sin \left( 0.04545\tau \right) \times e^{1.22\sin \tau + \cos \tau} - 0.00840 \times \sin ({\tau}) \times \mathscr{e} ^{-\tau \sin(\tau)}$$ I have used $\tau$ for time-variable for visual clarity in equation.

Given below is a shifted-variation of this as $\eta(\tau-0.77)$ and here is the final output(though it is very crude due to lack of HP Computing, so the pin-pointing the exact timing is not very reliable.

STEP II: Now, from here, I could go on and follow the above standard procedure, with writing OpenMP loops in C++ to conduct as many possible iterations as I could, without getting the cores hanged, to get a lock on the $\mathscr{spread\_t}$. And once, a lock in the predefined trading bandwidth is accomplished, the automated trading can be performed, as per the desired conditions.

Note 1 The idea of cointegration in pairs-trading (and in various other stochastic binary-system analysis/estimate procedures) is basically derived from the fact that, in the long run, the assets, or, in general, the cointegrated pair of variables, usually stabilize toward their equilibrium(or the mean regression); the so-called "long-run-equilibrium"(please note that this is just an informal analogy). And now, the MonteCarlo simulations, work backwards, that is, from the volume or the space of all possible outcomes, and interpreting this volume or the space as the probability; though the analysis and the usual pedagogy of Monte Carlo usually goes to show the equivalence of a Monte Carlo estimate to the Expected value of all possible outcomes for a given well-defined and distinct condition(invoking subsequently the "law of large numbers") and from thereon the usual statistical analysis takes charge. This explains the difficulty in finding Monte Carlo simulation for pairs-trading strategy.

> I haven't found any explanations how the Monte Carlo simulation could be used to test such trading strategy.

Also, since the cointegration, establishes a linear relationship between two random walks and Monte Carlo simulations generally don't work well with systems with a linear model. This is why I have introduced a transcedental non-linearity in my approach. Nevertheless, there has been some progress, please see this link. Now, to answer your first question, in theory it is possible to model an asset in isolation (without cointegration and the regressive features, dynamic or otherwise, and various other dependent-variable statistical tools, just using the time-series properties cleverly) but this doesn't generate realistic results. I had used, NASDAQ Composite and NASDAQ:AAPL in the first example. And to answer the second question, in my opinion, it is more of a choice between preserving linearity established by cointegration tests and the realistic results in estimation of future prices. I chose the latter, as it is evident in my approach. Please note that I haven't seen any Monte Carlo simulations for a linear binary system, as of yet. And I could be very well wrong.

Note 2 For a parametric form of relationship which is linear, and its constants of proportionality, whose determination is a key issue in all types of simulation, when forced to a MonteCarlo simulation, requires at least the relation to be dependent not trivially on the non-stochastic terms and functions, for it limits the paths of simulations for such a procedure and yields unnecessary time and memory complexities. Any attempt to force genuine MonteCarlo eradicates the linearity of base relationship. The various methods presented to simulate Cointegrated Systems seem to be good, and can be programmed, in their original form, without invoking MonteCarlo method to get the desired outputs.

## Answer by milkmotel (score 1)

https://quant.stackexchange.com/a/30015

You can't use Monte Carlo simulation to test a trading strategy. The parameters (assumptions) you put in the simulation paths will either confirm or reject your hypothesis.

You can't have an agnostic Monte Carlo test. If you do a stochastic test and write out the code for it, you wouldn't need to run it to know what the results would be. Either X and Y are assumed to converge or they aren't. If they converge, the test will yield positive results. If they don't, your portfolio sucks. That's it. If your simulation paths include a rule to converge at z standard deviations, then the portfolio rules will work. If they don't have any rules to converge, then your portfolio rules will suck.

That is why they use historical data. Historical data does not require you to impose any assumptions on the simulation. Either they converge past 2 SD or they don't, but you don't know. You run the test to find out if two securities are cointegrated enough to run a stat arb trading strategy on them successfully.

## Answer by TomHan (score 0)

https://quant.stackexchange.com/a/30003

I think you may confuse with Monte Carlo and time series models you are using. As you said some papers show back test of pair trading strategy, in practice it will do it. After fitting the spread series in in-sample data set (of cause co-integration test valid as well) then optimize parameters, like the trading threshold and stop-loss, then put this strategy in out-sample data set to do the testing. If it performs good and stable, then it's a OK strategy ready to use.

For the Monte Carlo part it's hard to say how you can use directly. In general, you fit the two series Xs & Ys(depends what process you use, usually stocks use lognormal process, then run Monte Carlo to produce sort of "out-sample" data, then fit the spread series on the simulated series, after that do your best to find parameters(too much) that makes a strategy (usually find good parameters around simulation means, and control losses when extremes happen).

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.