Robot for pair trading based on an index. Trading from index.

Robot for pair trading based on an index. Trading from index.

Trading idea: Take N trading platforms for futures trading and one instrument. Build an equally weighted index from this instrument and trade deviations in pairs from it. No more than one position at a time.

1. Robot sources.

1. Index. BotTabIndex for generating the index.

2. Five BotTabSimple for papers that we will trade.

 

2. Indicators.

1. None.

 

3. Robot logic.

1. Take 3 (can easily be expanded) futures trading venues. Take one instrument. Build an equally weighted index from this instrument.

2. Entry into position:

a. Have a paper that has deviated from the index upward.

b. Have a paper that has deviated from the index downward.

c. The deviation of paper 1 from the index is not less than N%.

d. The distance between the papers is not less than M%.

e. Buy the spread between the papers.

3. Exit from the position:

a. The distance between the papers has decreased to F%.

 

4. Source code in the project.

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

Constructor:

2. Creating an index (BotTabIndex) and subscribing to the event of its update. In this event, the position opening logic.

3. Creating sources for trading individual papers.

4. Initializing strategy parameters.

Where to look in the code:

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

 

5. Robot settings.

1. Regime. Operating mode:

a. On - all trading modes are enabled.

b. Off - disabled.

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

3. Asset in portfolio. Name of the monetary unit in the portfolio. If Prime, the total unit of calculation available in the tester and some types of connections to the Moscow Exchange will be used. In other cases, you need to choose the name of the currency as it is called in your portfolio.

4. Min Deviation SecToIndex To Entry. The minimum deviation of a paper from the index in order to consider it necessary to buy or short it.

5. Min Deviation SecToSec To Entry. The minimum deviation between two papers in a pair in order to consider that their spread can be bought.

6. Min Deviation To Exit. Deviation between papers, upon reaching which the spread between the papers should be sold.

 

6. Running the robot in the tester.

In the exchange emulator settings, I have a folder with trading data from three exchanges: Binance, BingX, ByBit:

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

Three papers from different exchanges have been added to the Index. Auto-Formula is disabled. Their average is used as the index formula:

Three versions of one paper from different exchanges are connected. This list can be expanded to 5. With a little code change to 20+. In this case, like this:

7. One of the testing scenarios.

Which exact papers were traded from the log:

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

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