How I looked for analyst recommendations for buying Russian securities in JSON format

Four years ago I wrote a search system for finding undervalued American stocks using data from Yahoo Finance, because more than 10 thousand securities are traded on the American market, of which about 4 thousand securities have analyst recommendations on the predicted price. These are big numbers that are difficult to work with. But what about Russia?

I sluggishly tried to find a system that would also give recommendations from analysts for Russian companies, until I recently found such an API. For example, here are some recommendations for electric scooter rental operator WUSH:

{
  "targets": [
    {
      "uid": "b993e814-9986-4434-ae88-b086066714a0",
      "ticker": "WUSH",
      "company": "SberCIB Investment Research",
      "recommendation": "RECOMMENDATION_HOLD",
      "recommendationDate": "2024-10-02T00:00:00Z",
      "currency": "rub",
      "currentPrice": {
        "units": "192",
        "nano": 0
      },
      "targetPrice": {
        "units": "250",
        "nano": 0
      },
      "priceChange": {
        "units": "58",
        "nano": 0
      },
      "priceChangeRel": {
        "units": "30",
        "nano": 210000000
      },
      "showName": "Whoosh"
    },
    {
      "uid": "b993e814-9986-4434-ae88-b086066714a0",
      "ticker": "WUSH",
      "company": "Финам",
      "recommendation": "RECOMMENDATION_HOLD",
      "recommendationDate": "2024-09-26T00:00:00Z",
      "currency": "rub",
      "currentPrice": {
        "units": "192",
        "nano": 0
      },
      "targetPrice": {
        "units": "250",
        "nano": 0
      },
      "priceChange": {
        "units": "58",
        "nano": 0
      },
      "priceChangeRel": {
        "units": "30",
        "nano": 210000000
      },
      "showName": "Whoosh"
    },
    {
      "uid": "b993e814-9986-4434-ae88-b086066714a0",
      "ticker": "WUSH",
      "company": "Газпромбанк",
      "recommendation": "RECOMMENDATION_HOLD",
      "recommendationDate": "2024-09-24T00:00:00Z",
      "currency": "rub",
      "currentPrice": {
        "units": "192",
        "nano": 0
      },
      "targetPrice": {
        "units": "355",
        "nano": 0
      },
      "priceChange": {
        "units": "163",
        "nano": 0
      },
      "priceChangeRel": {
        "units": "84",
        "nano": 900000000
      },
      "showName": "Whoosh"
    }
  ],
  "consensus": {
    "uid": "b993e814-9986-4434-ae88-b086066714a0",
    "ticker": "WUSH",
    "recommendation": "RECOMMENDATION_HOLD",
    "currency": "rub",
    "currentPrice": {
      "units": "192",
      "nano": 0
    },
    "consensus": {
      "units": "285",
      "nano": 0
    },
    "minTarget": {
      "units": "250",
      "nano": 0
    },
    "maxTarget": {
      "units": "355",
      "nano": 0
    },
    "priceChange": {
      "units": "93",
      "nano": 0
    },
    "priceChangeRel": {
      "units": "48",
      "nano": 440000000
    }
  }
}

True, there is one nuance in quantity. There are 170 securities represented on the Moscow Exchange, of which only 89 shares have recommendations.

There is a much smaller number of papers, but the API provides specific names of the companies that made recommendations, as well as the date the forecast was given and the forecast price. Theoretically, it is possible to compile lists of the most accurate analysts by collecting prices over time and comparing them with forecast ones.

But I was more interested in creating a summary table of all 89 stocks available.

The code is presented on GitHub.

What does the code do?

I wrote my code in Node.js and accessed the T‑Bank Invest API to get the data. Here is its functionality:

  1. Initialization and configuration:

  • The script starts by importing the necessary modules such as configuration (secrets), logging utilities (logService) and Tinkoff client (tinkoffClient), which facilitates interaction with the Tinkoff Invest API.

  • API_TOKEN from the configuration file is used to authenticate API requests.

  1. Step 1: Obtaining stock data:

  • Function getStockData requests InstrumentsService Tinkoff for a list of available shares. She filters this data to select stocks listed on the MOEX exchange (REAL_EXCHANGE_MOEX).

  • The filtered list and the full stock list are logged and returned. Key stock data such as figi, ticker, uid And logoNameare extracted for further processing.

  1. Step 2: Extract analyst forecasts:

  • Function getForecastsForStocks walks through a filtered list of stocks and retrieves analyst forecasts using the endpoint InstrumentsService/GetForecastBy.

  • For each stock, it checks to see if there are forecasts available. If there are any, the forecast data (current price, consensus price, price change and number of recommendations) is stored in an array. If no forecasts are found, this increases the count of papers without recommendations by plus one.

  • The system provides a 600ms delay between API calls to comply with the frequency limits (100 requests per minute).

  • Forecasts are sorted based on potential price change, with the largest price increase at the top.

  1. Step 3: Generating HTML Table:

  • Function generateHTMLTable creates an HTML file to visually display data in a table.

  • It uses Google Charts to visualize a table of stock logos, prices, consensus prices, and number of analyst recommendations.

  • The table is saved as an HTML file, allowing the user to easily view it in a browser.

  1. Final execution:

  • The code wraps everything in an asynchronous function that first retrieves stock data, then retrieves forecasts, and finally generates an HTML table summarizing the analysts' recommendations.

  • The output is an HTML file with real-time stock data from the T-Bank API: T‑Investments, sorted by potential price change, and also added stock logos and country of risk.

To summarize, we can say that this script is an analytics tool that extracts current stock data from the T-Bank – T-Investments API, processes analyst recommendations and generates a clear, visual report for decision-making.

What are the results of the script?

With work log can be found on GitHub. Ready table you can download from it.

Generated table

Generated table

  • Each column header can be sorted.

  • For numeric columns such as current price, consensus price And potential price changesorting is simple: ascending or descending.

  • For text columns such as Name And tickersorting will be alphabetical.

  • Percentage sorting (e.g. potential price change) helps quickly identify stocks with the highest growth potential.

Results

The script allows you to find companies that have analyst recommendations. However, this selection does not constitute individual investment advice and may not meet your investment objectives. This is due to the fact that the script is just a tool, and the decision to purchase specific securities is made by the person himself after studying the issuer.

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

Author: Mikhail Shardin

October 14, 2024

Similar Posts

Leave a Reply

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