Using correlation and cointegration from the common layers of robot creation in OsEngine.

Using correlation and cointegration from the common layers of robot creation in OsEngine.

An article about how to use CorrelationBuilder and CointegrationBuilder in bots, by throwing candles at them. These classes were made exclusively for BotTabPair, but the calculation of these indicators is universal.

To use them, you need candles from two sources and this article...

Location of the indicators in OsEngine.

CorrelationBuilder code overview.

1. Method that recalculates the correlation between candle arrays. Takes candles and calculation depth as input. IMPORTANT! Candles must be of the same TF, otherwise there will be errors.

2. Private list with the results of the last recalculation.

3. Private method for calculations.

4. Private method for calculations.

The class is very simple. There is no need to customize anything. You input candles of the same TF and the calculation length. You get an array of correlation values in return.

CointegrationBuilder code overview.

In general, the logic of working with it is similar to correlation. However! Before throwing candles in there, you need to configure this class. And after working with it, you need to take the results from its fields and properties.

1. Configuration. CointegrationLookBack - length of the indicator calculation.

2. Configuration. CointegrationDeviation - multiplier for calculating deviation lines on the chart.

3. Field. After the indicator is recalculated, it contains an array of minimum differences between instruments.

4. Property. Read-only. After recalculation, it contains a multiplier for the second instrument in the formula for finding minimum residuals. (Candles1[i].Close - Candles2[i].Close * CointegrationMult).

5. Property. Read-only. After recalculation, it contains the standard deviation of residuals, taken in absolute value for counting.

6. Property. Read-only. After recalculation, it contains the direction of the current difference placement on the last candle relative to the lines on the chart.

a. Up - deviation above the current line.

b. Down - deviation below the current line.

c. No...

7. Property. Read-only. After recalculation, it contains the upper line on the deviation chart.

8. Property. Read-only. After recalculation, it contains the lower line on the deviation chart.

9. Method to call for recalculation of the indicator. Takes two arrays of candles (which must be with the same TF!!!) and a boolean value, indicating whether the class needs to trim insignificant values from the deviation chart (False - for the tester, as it is expensive. True - for the live robot, as it looks nice on the chart afterwards).

10. Private methods for calculating the indicator.

Example of usage.

It is recommended to create both classes for calculating these indicators in the constructor of the robot. There is no point in constantly creating new objects of these classes.

You can store them as regular fields, next to the strategy parameters and event handlers.

You can and should use them after the candle is completed, making sure that you have two arrays of candles of the same timeframe.

1. Call the object to calculate cointegration. Pass in two arrays of candles and the StrategyParametrInt integer value for the length of the indicator. Immediately save the indicator values in an array for further use.

2. Set the settings for the object calculating cointegration.

3. Call the cointegration calculation. Pass in two arrays of candles to the object, and false to prevent unnecessary calculations and data trimming.

4. Get the data from the cointegration object.

5. And then some arbitrary robot logic.

If you have any difficulties or questions, please write to the support chat. Link