PairToTrade - a pair for arbitrage. Overview of trading methods, properties, and events.

PairToTrade - a pair for arbitrage. Overview of trading methods, properties, and events.

In this post, we will talk about an individual pair within the source for pair trading in OsEngine.

We remind you that one BotTabPair (source for pair arbitrage) can contain multiple Pairs:

Fig. 1. Relationship between BotTabPair and PairToTrade.

This allows you to:

1. Apply common settings for pairs.

2. Quickly create a large number of pairs through auto-pair creation.

3. Handle dozens and hundreds of simultaneously calculated pairs with one code.

4. This is what BotTabPair was created for.

You can subscribe to events of correlation or cointegration change for a pair and wait for it to happen in the code. Link:

PairToTrade is located in the same file as the main class for pair arbitrage:

Fig. 2. PairToTrade placement in the project.

Within the file:

Fig. 3. PairToTrade placement in the file.

Regions within the PairToTrade class:

Fig. 4. Regions inside the PairToTrade class.

1. Service region, containing methods responsible for the proper functioning of the pair within the OsEngine architecture. It is not necessary for trading.

2. Properties and settings - trading settings, properties that can be requested in trading, sources with instruments. It is necessary to study.

3. Trading methods - methods for trading. Mandatory for study.

4. Delayed position opening on the second leg - implementation of delayed opening of a position on the second leg. Not necessary for trading.

5. Correlation calculation and storage - calculation of correlation and its storage. Of interest - event of correlation change for a pair. Mandatory for study.

6. Cointegration calculation and storage - calculation of cointegration and its storage. Of interest - two events about cointegration change and the location of the last deviation. Mandatory for study. Also available values:

a. Current values of the lines.

b. Last cointegration deviation value.

c. Multiplier used in the last recalculation of the minimum deviation table.

d. Standard deviation in the last minimum deviation table.

7. Event processing and indicator recalculation call - service region. Not of interest for trading.

8. Logging.

 

Properties and settings.

Fig. 5. Properties and settings region in PairToTrade.

1. Trading sources. Like BotTabSimple. Through them, everything available in the standard source is accessible. Adding indicators, all trading methods of the base source, and everything else you may need. Link to the article about the subject: https://o-s-a.net/os-engine-faq/index/40

2. Trading mode for corresponding sources. Modes can be:

a. Off - trading on the leg is disabled.

b. Limit - limit order. In this case, slippage will start working.

c. Market - market order.

d. Second - a position on this leg will be taken at the market immediately after a position on the other leg is opened.

3. Slippage mode for the corresponding source. There may be:

a. Percent - slippage will be calculated as a percentage of the current contract price.

b. Absolute - the absolute value entered by the user.

4. Slippage magnitude for the corresponding source.

5. Volume calculation mode for the corresponding instrument. Can be:

a. Currency - Contract price. If the contract is denominated in rubles, it will be in rubles. If in dollars, then in dollars. For example, if BTCUSDT is denominated in USDT, and this volume calculation type is chosen, it would mean that we have chosen 7 million dollars.

b. Contract - Number of contracts without conversion.

6. Volume magnitude for the corresponding source.

7. Whether this pair has positions. True - has. False - doesn't have.

 

Trading methods.

Fig. 6. Trading methods region in PairToTrade.

1. Buy instrument 1 and sell instrument 2. If working on cointegration for the convergence of instruments, this trading method should be called when the current difference between instruments is above the zero mark.

2. Sell instrument 1 and buy instrument 2. If working on cointegration for the convergence of instruments, this trading method should be called when the current difference between instruments is below the zero mark.

3. Close all current positions. Withdraw all orders from the market for the pair.

4. Method for calculating the volume for the corresponding security passed in the parameters.

5. Method for calculating the order price for the corresponding security passed in the parameters.

6. The last side of the position of the difference between instruments on the cointegration chart at the time of position opening.

 

Correlation calculation and storage.

Fig. 7. Correlation calculation and storage region in PairToTrade.

1. Object for correlation calculation. If interested in using it, it's better to see how this class interacts with it.

2. Configuration for the calculation length of correlation.

3. Array with the history of correlation changes.

4. The last correlation value between instruments.

5. Force recalculate correlation without waiting for the last candle to finish.

6. Private method for correlation recalculation. If interested in using correlation outside of this class, it's better to see how it works.

7. Correlation change event.

Cointegration calculation and storage.

Fig. 8. Cointegration calculation and storage region in PairToTrade.

1. Object for cointegration calculation (minimum differences between instruments with an optimal multiplier). If interested in using it, it's better to see how this class interacts with it.

2. Configuration for the calculation length of cointegration.

3. Deviation for line calculation on the cointegration chart. Used as a multiplier for the standard deviation of the current deviation array.

4. Array with the minimum differences between closing candles for instruments with an optimal multiplier multiplied by instrument 2 (cointegration).

5. The last value on the cointegration chart.

6. Optimal multiplier. This is the value in the formula for calculating the minimum differences between instruments, to which the second instrument is multiplied. The last relevant value is saved.

7. Standard deviation in the deviation array.

8. Position of the current difference 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…

9. Upper value of the cointegration line.

10. Lower value of the cointegration line.

11. Force recalculate the deviation array (cointegration) without waiting for the last candle to finish.

12. Private method for recalculation of the minimum differences array between instruments (cointegration). If interested in using cointegration outside of this class, it's better to see how it works.

13. Event of change in the array of minimum differences between instruments (cointegration).

14. Event of change in the position of the current difference on the last candle relative to the lines on the chart.

 

How to access PairToTrade from the robot? Directly take it from BotTabPair.

In the class that provides storage and management of pairs (BotTabPair), there is an array of type PairToTrade:

Fig. 8. Location of the PairToTrade array in BotTabPair.

You can directly request this array from the robot:

Fig. 9. Example of working with the PairToTrade array in the robot

1. Creating a trading pair source.

2. In any event or handler or thread, we have access to the list of pairs created by the user from the Pairs array. In this case, an example is shown of iterating through the array of pairs and taking values from all pairs in turn. This includes BotTabSimple - basic trading sources in OsEngine.

 

How to access PairToTrade from the robot? Through subscribing to BotTabPair events.

Fig. 10. Example of subscribing to an event that provides us with PairToTrade where it occurred.

1. Creating a trading pair source.

2. Subscribing to the event of the position of the last deviation between instruments relative to the lines on the cointegration chart.

3. Handler. And in the second parameter, the pair that the event occurred for is passed to us. After which we can do something with it.

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