
I emphasize on this concept for the second time, and it is not without reason. The significance of the correlation between a stock and an index can filter out the main part of unnecessary inputs in positions, reducing the profitability of any strategy focused on trading with reference to the index. Last time we talked about correlation in the context of pair trading.
1. What is correlation?
Correlation in trading is a numerical measure of the relationship between different assets and indexes. How synchronously two assets move.
The numerical value of correlation ranges 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 move one after the other without deviations.
2. -1 means negative correlation. This means that assets move in opposite directions.
3. Everything between these values needs to be interpreted in the code somehow. Not always in an obvious way. But we will also talk about this in this series of articles.
2. Why is it necessary to know the correlation coefficient when trading from the index?
1. As we learned from the previous article in the series, the Index is candle data merged according to a certain formula. As a result, we also have candles at the output. Consequently, we can very easily calculate the correlation between the index and the stock we want to trade.
2. This significantly affects the profitability of robots for index arbitrage.
3. Changes in correlation dynamics can themselves provide excellent entry points.
3. We will use the correlation calculation directly from the source code.
In the layer for creating robots for pair trading, we can see the correlation on the chart with our eyes:

This is all great, but due to the abundance of possible uses of the index in trading, a visual interface will not be built for each one. Therefore, we will use the calculation of correlation directly in the source code without visualization.
The CorrelationBuilder object is responsible for calculating the correlation between two series of candles:

CorrelationBuilder provides us with methods for calculating correlation:

1. ReloadCorrelation - input candles 1, candles 2, calculation length, output an array of PairIndicatorValue values.
2. ReloadCorrelationLast - the same, but outputting a single value based on the latest candles.
4. Summary.
At this point, it is important to understand:
1. Correlation is very important when trading a pair of instruments or a pair of candle data. This includes knowing the current state of correlation between the series when comparing an index with a particular stock.
2. This thing generates trading signals on its own and can serve as a filter for entry, as extreme correlation indicators at the moment can indicate that it is not worth trading at the moment.
3. Calculated in just two lines of code using special classes within OsEngine. We will actively apply this when creating strategies with reference to the index.
Good luck with your algorithms!
If you have any difficulties or questions, please write to the support chat. Link
OsEngine: https://github.com/AlexWan/OsEngine