How I decided to return to algorithmic trading after a 10-year break

The most important thing is that it has nothing to do with the word алко it doesn't have.
For several years I traded in combination with the AmiBroker technical analysis program + QUIK trading terminal via .tri and .trr files, mainly on futures on the derivatives market.
I can’t say that it was a bad experience, but over time I learned about Asset allocation and realized that it is very difficult to compete with a benchmark in the form of a stock index.

In theory, this means that you can buy an index and forget about it, go about your business, business, family – and the index grows (or falls, depending on what time it is) and you don’t need to take any action for this.
And the AmiBroker + QUIK combination had to be constantly monitored to see if the import adapter via the .tri file had fallen off. And also have several virtual machines with installed copies of Windows on each virtual machine on one physical computer for different brokers. It was possible to install only one pair of AmiBroker + QUIK on one Windows. I didn't really like all this.

In 2024, I wanted something lightweight – without Windows and modern – via an API interface. Preferably free for the user. Several years ago, I already tried to find out whether Russian brokers had an API to work with them, but I never got around to it. This fall, I began to actively search for information – which broker has an API for working with individuals. I couldn't find any pivot table and found only three options:

For subjective reasons, I chose to work with the T‑Bank Invest API (this is the former Tinkoff) through the JavaScript runtime environment Node.JS.

A little bit of my history, 2008-2012: AmiBroker + QUIK

AmiBroker is a technical analysis platform that allows users to create their own indicators and scripts. It can also integrate with other programs such as MetaTrader, NinjaTrader and even trading terminals including QUIK.

AmiBroker 3D Optimization Chart

AmiBroker 3D Optimization Chart

What I really liked about AmiBroker was its 3D Optimization Chart feature. In my opinion, it made it possible to avoid over-fitting the indicators to the history curve. Optimization in the back tester was supported by the optimize function. The syntax for this function was as follows:

переменная = optimize("Описание", default , min , max , step );

переменная is a regular variable in their AFL language that is assigned the value returned by the optimization function.
In optimization mode, the optimize function returns sequential values ​​from minimum to maximum (inclusive) in increments.
"Описание" is a string that is used to identify the optimization variable and appears as a column name in the list of optimization results.
default is the default value that optimizes function return in Explore, Indicator, Comment, Scan, and Normal Backtest modes.
min — minimum value of the variable being optimized
max — maximum value of the optimized variable
шаг is the interval used to increase the value from minimum to maximum.

QUIK is a trading information system designed to provide stock market participants with access to stock market data in real time. It is used by many brokers and traders to carry out transactions in the stock markets.

When AmiBroker connected to QUIK, it received real-time data from the QUIK trading system and used it to plot charts, create indicators, and perform other analytical tasks. The user could also configure the system to automatically execute trades through QUIK based on certain conditions.

To connect AmiBroker to QUIK, it was necessary to install the appropriate plugin or module. This allowed the AmiBroker platform to receive data from QUIK and process it for further work.

Example of work logs:

09.10.2009 11:00:31: [2208:2212] Получено уведомление о выполнении транзакции ,   TRANS_ID=807263520;CLASSCODE=SPBFUT;ACTION=NEW_ORDER;SECCODE=GMZ9;ACCOUNT=SPBFUT0087W;PRICE=37692;QUANTITY=2;OPERATION=S;CLIENT_CODE=48560/48560;TYPE=L;EXECUTION_CONDITION=;CHECK_LIMITS=;MARKET_MAKER_ORDER=;STATUS=3;TRANS_NAME="Ввод заявки"; DESCRIPTION="[FORTS] Заявка N 761722396 успешно зарегистрирована"; ORDER_NUMBER=761722396;
09.10.2009 14:04:30: [2208:1172] Получено уведомление об отправке транзакции ,   TRANS_ID=2087816784;CLASSCODE=SPBFUT;ACTION=NEW_ORDER;SECCODE=VBZ9;ACCOUNT=SPBFUT0087W;PRICE=6333;QUANTITY=12;OPERATION=S;CLIENT_CODE=48560/48560;TYPE=L;EXECUTION_CONDITION=;CHECK_LIMITS=;MARKET_MAKER_ORDER=;STATUS=0;TRANS_NAME="Ввод заявки"; DESCRIPTION="Отправлена транзакция";
09.10.2009 14:04:30: [2208:2212] Получено уведомление о выполнении транзакции ,   TRANS_ID=2087816784;CLASSCODE=SPBFUT;ACTION=NEW_ORDER;SECCODE=VBZ9;ACCOUNT=SPBFUT0087W;PRICE=6333;QUANTITY=12;OPERATION=S;CLIENT_CODE=48560/48560;TYPE=L;EXECUTION_CONDITION=;CHECK_LIMITS=;MARKET_MAKER_ORDER=;STATUS=3;TRANS_NAME="Ввод заявки"; DESCRIPTION="[FORTS] Заявка N 763167846 успешно зарегистрирована"; ORDER_NUMBER=763167846;
09.10.2009 15:10:10: [2208:1172] Завершен процесс отправления транзакций из файла ,   Входной файл-"C:\Program Files\Quik5\MTS\input.tri", Выходной файл-"C:\Program Files\Quik5\MTS\output.tro", Файл с журналом-"C:\Program Files\Quik5\MTS\log.trr", отправлено транзакций-3, выполнено транзакций-3

As far as I understand, AmiBroker (created in Poland) has not been updated for several years: last release in 2015, and updates in 2017.

I haven’t used QUIK for a very long time, but from the screenshots on the website I realized that the program looks has not changed much in recent years.

A little more of my history, 2008-2012: KB PAUK and Technical Analysis of STOCKS & COMMODITIES

I used to be an active user of the KB PAUK forum. I even found an HTML file with a topic that interested me, which had been lying around for 20 years:

Forex kbpauk forum http://forex.kbpauk.ru/ (no longer working in 2024)

I studied a lot about the topic dedicated to AmiBroker. And I asked there (hello Oleg 000). Posted examples from paper magazine Technical Analysis of STOCKS & COMMODITIES.

Now, in 2024, the forum is unavailable, but if anyone wants nostalgia, then I found an archived copy of it.

Forex kbpauk forum http://forex.kbpauk.ru/ (no longer working in 2024)

True, the archived copy is a little useless, because the most interesting sections were opened only after logging in with your username and password.

2024, now: T‑Bank Invest API + unofficial SDK Node.JS. Description of trading robot functions

Approximate robot structure

Approximate robot structure

In the fall of 2024, I decided to write a trading bot, create a program structure that would work through the broker's API.

A trading robot must contain:

  1. List of papers – with which he will work.

  2. Condition of purchase.

  3. Condition of sale.

  4. CSV accounting file

  5. Money management – entry percentage.

  6. Logging of all actions.

  7. Displaying information, most likely via a web server.

  8. Have a backtesting module.

T‑Bank Invest API + Node.JS:

I decided not to get involved in the futures market for now and try to work with the most traded shares on the Moscow Exchange. All that remains is to find them.

In this article I will describe how I created two modules on Node.js:

  1. Interaction with T‑Bank Invest APItinkoffClient.js

  2. A module that searches for stocks with the highest turnover over the last three months searchTradingVolumes.js

The project is presented on GitHub: https://github.com/empenoso/SilverFir-TradingBot.

SilverFir-TradingBot\src\grpc\tinkoffClient.js

Module tinkoffClient.js is a specialized client designed to interact with T‑Bank Invest API, a Russian brokerage platform. The main purpose of this module is to provide a Node.js-based bot with access to financial data for algorithmic trading or market analysis.

Here is its functionality:

Main functions:

  1. Authorization and setup:

  • The module uses API tokens for authentication.

  • It supports both test (sandbox) and production environments, although it uses the sandbox environment by default.

  • API headers include the required authorization token and content type for JSON communication.

  1. Universal API call mechanism:

  • Method callApi() Serves as a flexible utility for sending POST requests to Tinkoff API endpoints.

  • It handles payload submission and error reporting, providing detailed feedback on failures (via logs).

  1. Extracting Japanese candlestick data:

  • Method getCandles() retrieves candlestick data for a given financial instrument (ticker) at specified intervals.

  • It calculates a time range based on the provided interval and retrieves a series of candles from the market data service. This is necessary for market analysis, technical indicators and trading strategies.

  • Processes up to 1000 candles per request and provides error logging if the request fails or returns empty data.

Dependencies and utilities:

  • Moment.js: Used to format dates and calculate time ranges, which are critical when working with financial data over different time periods.

  • Axios: Processes HTTP requests to API endpoints.

  • Logging Services: implements a custom logging system for file and console output, helping with debugging and tracking operations.

SilverFir-TradingBot\src\searchTradingVolumes.js

Module searchTradingVolumes.js is part of SilverFir-TradingBot, designed to identify shares on the Moscow Exchange (MOEX) with the highest trading volumes over the past three months. Needed to discard illiquid goods.

Here is a detailed overview of the module's functions:

Main functions:

  1. Filtering shares from MOEX (Moscow Exchange):

  • The module uses the T‑Bank Invest API to obtain a complete list of available stocks.

  • Filters results to focus specifically on stocks traded on REAL_EXCHANGE_MOEXensuring that only Moscow Exchange shares will be considered.

  • The filtered list includes important information such as FIGI (Financial Instrument Global Identifier), ticker, ISIN and name, which are then logged for tracking and analysis.

  1. Extracting historical trading volume data:

  • For each stock identified during the filtering step, the module retrieves daily candle data (OHLCV – open, maximum, minimum, close, volume) using T‑Bank Invest API.

  • It retrieves data for the last three months, calculating the total trading volume for that period.

  • This step is critical to measuring market activity and identifying the most actively traded stocks.

  1. Determining the Top 15 Stocks by Volume:

  • After collecting trading volume data, the module ranks all stocks based on their total trading volume over a three-month period.

  • He then selects the top 15 stocks that are considered to have the highest turnover, making them the most active in the market.

  1. Logging and configuration preparation:

  • The top 15 stocks are registered with their respective tickers and FIGI IDs for future reference.

  • This data is also prepared for inclusion in the bot's configuration, allowing it to be easily integrated into other parts of the trading strategy.

Dependencies and utilities:

  • Moment.js: used for date manipulation, in particular to generate time ranges (three months) for historical data queries.

  • TinkoffClient: This is a dedicated bot client for interacting with the Tinkoff Invest API, facilitating the communication necessary to obtain data on stocks and candles.

  • Custom Logging Service: The module uses a logging system that logs information both to the console and to external files, helping to track stock filtering, volume calculations, and any errors encountered during interaction with the API.

The result of executing the searchTradingVolumes.js script

2024-10-06 07:25:48 [INFO]: Отфильтрованных акций 170 штук.
2024-10-06 07:25:50 [INFO]: Топ 15 акций по объему за последние три месяца: [
  {
    "ticker": "GRNT",
    "figi": "TCS10A0JV532",
    "totalVolume": 7092179306
  },
  {
    "ticker": "VTBR",
    "figi": "BBG004730ZJ9",
    "totalVolume": 5906896857
  },
  {
    "ticker": "MTLR",
    "figi": "BBG004S68598",
    "totalVolume": 5047115732
  },
  {
    "ticker": "UWGN",
    "figi": "BBG008HD3V85",
    "totalVolume": 4423660125
  },
  {
    "ticker": "RNFT",
    "figi": "BBG00F9XX7H4",
    "totalVolume": 1775061013
  },
  {
    "ticker": "EUTR",
    "figi": "TCS00A1002V2",
    "totalVolume": 1270913208
  },
  {
    "ticker": "SNGSP",
    "figi": "BBG004S681M2",
    "totalVolume": 1092911599
  },
  {
    "ticker": "GAZP",
    "figi": "BBG004730RP0",
    "totalVolume": 1027429605
  },
  {
    "ticker": "ROSN",
    "figi": "BBG004731354",
    "totalVolume": 983187493
  },
  {
    "ticker": "SBER",
    "figi": "BBG004730N88",
    "totalVolume": 940111321
  },
  {
    "ticker": "SGZH",
    "figi": "BBG0100R9963",
    "totalVolume": 929574444
  },
  {
    "ticker": "AFLT",
    "figi": "BBG004S683W7",
    "totalVolume": 632806638
  },
  {
    "ticker": "VKCO",
    "figi": "TCS00A106YF0",
    "totalVolume": 595704137
  },
  {
    "ticker": "RUAL",
    "figi": "BBG008F2T3T2",
    "totalVolume": 542242519
  },
  {
    "ticker": "TATN",
    "figi": "BBG004RVFFC0",
    "totalVolume": 538004844
  }
]
2024-10-06 07:25:50 [INFO]: 

Вставка в config.js:

2024-10-06 07:25:50 [INFO]: 
securitiesToMonitorTikerArray: ["GRNT","VTBR","MTLR","UWGN","RNFT","EUTR","SNGSP","GAZP","ROSN","SBER","SGZH","AFLT","VKCO","RUAL","TATN"]
2024-10-06 07:25:50 [INFO]: 
securitiesToMonitorFigiArray: ["TCS10A0JV532","BBG004730ZJ9","BBG004S68598","BBG008HD3V85","BBG00F9XX7H4","TCS00A1002V2","BBG004S681M2","BBG004730RP0","BBG004731354","BBG004730N88","BBG0100R9963","BBG004S683W7","TCS00A106YF0","BBG008F2T3T2","BBG004RVFFC0"]
2024-10-06 07:25:50 [INFO]: 

Для использования в скрипте download_md.sh в одну колонку:
TCS10A0JV532
BBG004730ZJ9
BBG004S68598
BBG008HD3V85
BBG00F9XX7H4
TCS00A1002V2
BBG004S681M2
BBG004730RP0
BBG004731354
BBG004730N88
BBG0100R9963
BBG004S683W7
TCS00A106YF0
BBG008F2T3T2
BBG004RVFFC0

Results

The project is presented on GitHub: https://github.com/empenoso/SilverFir-TradingBot. New modules will be uploaded as they are written and tested.

Module tinkoffClient.js for T‑Bank Invest the API is only partially presented so far.

Author: Mikhail Shardin

October 7, 2024

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *