Correlation.

Correlation.

We will talk about correlation in the context of algorithmic trading.

Why is this necessary? How to calculate it? Possible applications in algorithmic trading?

Fig. 1. Interface for pair trading in OsEngine.

 

1. What is correlation?

Correlation in trading is a numerical measure of the relationship between different assets and indices. How synchronously two assets move.

The numerical value of correlation can range from +1 to -1, which is very convenient for calculations and using this indicator during trading.

1. +1 means very high synchronicity of assets. High correlation. Candles literally follow each other without deviations.

2. -1 indicates negative correlation. This means that assets move in different directions.

3. Everything between these values needs to be somehow interpreted in the code. Not always in an obvious way. But we will also talk about this.

 

2. Why is it important to know the correlation coefficient in pair trading?

1. Because it greatly influences the overall profitability of robots for pair and other types of arbitrage, in which two assets (or an asset to an index) are compared.

2. Changes in correlation dynamics can themselves provide excellent entry points. You can even not pay much attention to the charts of the instruments. Example:

3. How to calculate correlation?

Since this post will be read for the next 5 years, we simply must include formulas for mathematicians here. But that doesn't mean you have to delve into this and get scared) Don't be scared! Everything is much simpler!

Usually it is proposed to do this:

Or like this:

Of course, you don't need to do this. Everything has been written long before us. Even we didn't bother to figure it out. And you certainly don't need to.

You should use standard tools to calculate the correlation between two series built into the environment in which you create robots.

Within OsEngine, we have a layer for creating robots for trading pairs.

Advanced individuals can also use the CorrelationBuilder class:

Which provides us with methods for calculating the correlation:

1. ReloadCorrelation - input:

candles 1

candles 2

calculation length,

get an array of PairIndicatorValue values. 

1. ReloadCorrelationLast - same as above, but with one value based on the most recent candles.

 

4. In Conclusion

At this point, it is important to understand:

1. Correlation is very important when trading pair arbitrage or an asset to an index.

2. This thing generates trading signals on its own and can serve as a filter for entry. Because very low correlation in the moment may indicate that it is not worth trading on convergence at that moment.

3. You can calculate in any language using standard methods by throwing an array of candles or an array of type double into some class. There is no need to memorize formulas. Focus on trading logic. In OsEngine, everything has already been done for you, and in the pair trading layer, you will always have up-to-date data on correlation at your fingertips.

If something does not work out, or if you have any questions, write in the support chat, link.