
Cointegration and stationarity are properties of arrays of values invented by mathematicians in the times of computer absence, meaning that with the presence of cointegration or stationarity, in the context of algorithmic trading in the 21st century, we can profit from the movement of candles within these two arrays, as there is a certain clear pattern in their movements relative to each other.
In the context of algorithmic trading, knowing more is not necessary. And one should not delve into it at all. Since there are no stationary series in trading, there is only temporary cointegration and temporary stationarity on which one can try to make a profit.
You can read more about this on Habr, but it's better not to. It will melt your brain, and you won't gain any money.
What you need to know is the following:
After years of theorizing and paperwork in search of cointegration, mathematicians came up with a graph of the minimum residuals from the difference of two instruments with an optimal multiplier, which will help us make money.
1. Graph of the minimum residuals from the difference of two arrays of candles with an optimal multiplier.
The calculation is as follows:
Paper1 - (Paper2 * Multiplier).
We adjust the multiplier so that the standard deviation is minimal.

Fig. 1. The location of the graph in the Os Engine.
Essentially, it is the simplest indicator for two arrays of candles. C# managed to brute-force it in 20 minutes, and now it is included in OsEngine in the general assembly.
This graph allows you to dynamically observe the deviation of one instrument from another and trade around its values.
The two white bands on the graph are calculated as the standard deviation on the same, multiplied by another multiplier:

Fig. 2. Settings for the graph of the minimum residuals from the difference of two arrays of...
1. Deviation for the white lines on the graph.
2. Depth of the graph calculation.
These two settings are the subject of optimization in pair trading.
2. How to make sure that a pair is cointegrated or even stationary?
DO YOU NEED TO GRADUATE WITH A PHYSICS DEGREE AND SPEND 5 YEARS IN MATLAB STUDYING FORMULAS? No...
Much faster:
1. Take the free robots from OsEngine that trade based on this graph, or create your own. Then the "Graph of the minimum residuals from the difference of two arrays of candles with an optimal multiplier" will simply be called "Cointegration" in some places. This is not entirely scientific, but writing a lengthy definition of this graph everywhere is even more schizophrenic.
2. Test these robots on different pairs.
3. Experiment with depth and deviation settings.
4. Review robot logs after testing.
5. If the equity curve is going up, then it is often a cointegrated pair (there is some statistically significant regularity present). And if the equity curve is flat or going down, then it is not a cointegrated pair.
3. Conclusion.
The essence of this article was to introduce the "Graph of the minimum residuals from the difference of two arrays of candles with an optimal multiplier", which is carelessly labeled as "Cointegration" in OsEngine for brevity.
But the graph is clear, and its essence is clear.
It is already calculated for you. It is calculated dynamically on each candle. It can be accessed from robots, and three examples have already been written for this purpose.
Different uses of this graph in trading logic can help in tests:
1. Pairs for convergence.
2. Pairs for divergence.
3. Other strange patterns on the cointegration graph to profit from the movement of instruments.
If you have any difficulties or questions, please write to the support chat. Link.