Example of a one-legged index arbitrage strategy to return to the mean. Trading based on index.

Example of a one-legged index arbitrage strategy to return to the mean. Trading based on index.

Trading idea:

Take instruments that deviate from the broad market without momentum and trade them back to the index.

1. Robot's sources.

1. Index. BotTabIndex for generating the index.

2. Screener. BotTabScreener to take N instruments from the market and trade them all simultaneously.

 

2. Indicators.

1. Correlation, needed for dynamic filtering of instruments.

2. Chart "Minimum residuals from the difference between instruments with optimal multiplier".

 

3. Robot's logic.

1. Build a broad index by selecting securities by volume, weighing by price.

2. On each update of the index, check each instrument for correlation.

3. If the instrument passes, check each instrument for deviation from the index based on "Minimum residuals".

4. If the instrument deviates more than the standard deviation multiplied by the multiplier, trade for convergence back.

5. Exit on a reverse signal / crossing the zero mark on the chart of "maximum residuals" / exiting to the neutral zone.

 

4. Source code in the project.

Link to GitHub: https://github.com/AlexWan/OsEngine

Constructor:

1. Creating an index (BotTabIndex) and subscribing to its update event. Position opening logic is in this event.

2. Creating a screener (BotTabScreener) and subscribing to the candle update event for any instrument loaded into it. Position closing logic is in this event.

3. Initializing strategy parameters.

Where to look in the code:

Blocks with position opening and closing logic are highlighted with comments.

 

5. Robot's settings.

1. Regime. Operating mode:

a. On - all trading modes are enabled.

b. Off - disabled.

c. OnlyLong - only long positions allowed.

d. OnlyShort - only short positions allowed.

2. Regime Close Position. Position closing type:

a. Reverse signal. Reverse signal type on the chart of minimum deviations with the optimal multiplier.

b. No signal. Neutral position on the chart of minimum deviations with the optimal multiplier.

3. Max poses count. Maximum allowed number of positions open simultaneously.

4. Percent depo on positions. Percentage of available funds per position.

5. Asset in portfolio. Name of the currency in the portfolio. If Prime, the total reference unit available in the tester and some types of connections to the Moscow Exchange will be taken. In other cases, select the currency name as it is called in your portfolio.

6. Slippage. Slippage for order in %.

7. Correlation candles look back. Period for calculating correlation between the index and the security in trades.

8. Cointegration candles look back. Period for calculating the minimum residuals chart between the security and the index with the optimal multiplier.

9. Deviation mult. Deviation for standard deviation on the minimum residuals chart from the difference with the optimal multiplier.

10. Correlation min value. Minimum correlation value to open a position on the security.

 

6. Running the robot in the tester.

In the exchange emulator settings, I have connected a set of 9 futures with Binance. Of course, you can connect many more:

Create the robot. Open its chart and configure the sources:

All securities from the source are added to the Index:

The index also has an auto-formula set up:

All securities are connected to the screener:

 

7. One of the testing options.

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

OsEngine: https://github.com/AlexWan/OsEngine