Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/backtest-kit/uzse-backtest-app/llms.txt

Use this file to discover all available pages before exploring further.

import-trades.ts is the second step in the data-ingestion pipeline. It reads all trades_page_*.html files written by download-trades.ts, parses each HTML trade table using regex-based extraction, builds a SHA1 deduplication hash for every row, and upserts records into the MongoDB trade-results collection. Rows already present in the database (identified by hash collision code 11000) are silently skipped. After processing, all HTML files are deleted from tmp/.

Synopsis

npx tsx scripts/import-trades.ts
This script takes no command-line arguments. It automatically discovers and processes all tmp/trades_page_*.html files left by download-trades.ts.

Behavior

The script performs the following steps in order:
  1. Reads and sorts files — scans tmp/ for files matching trades_page_*.html and sorts them numerically by page number.
  2. Parses HTML tables — for each file, iterates over <tr> elements using a regex, extracts <td> cell text (stripping inner tags), and skips rows with fewer than 10 cells.
  3. Parses Russian-format datetimes — converts date strings such as "17 апреля 2026, 16:02" to JavaScript Date objects using the Russian month name lookup table.
  4. Generates SHA1 hash — computes a deduplication key from the concatenation: symbol|time|tradePrice|quantity|volume|pageIndex|rowIndex|urlKey.
  5. Inserts to MongoDB — calls TradeModel.create() for each row. On duplicate key error (code 11000) the row is counted as skipped; any other error is logged.
  6. Cleans up tmp/ — deletes all .html files from tmp/ after all files have been processed.

Extracted Fields

Each parsed table row produces a document with the following fields:
FieldTypeSource columnDescription
timeDateColumn 0Transaction datetime, parsed from Russian locale string
symbolstringColumn 2ISIN code, e.g. UZ7011340005
issuerstringColumn 3Company name, e.g. Hamkorbank
securityTypestringColumn 4Security type, e.g. Простые акции
marketstringColumn 5Market sector code, e.g. STK
platformstringColumn 6Trading platform code, e.g. G1, G2
tradePricenumberColumn 7Price per unit
quantitynumberColumn 8Number of units traded
volumenumberColumn 9Total trade value (tradePrice × quantity)
hashstringComputedSHA1 deduplication key — unique index in the collection

Russian Month Lookup

The parser recognises the following Russian genitive month names:
RussianMonthRussianMonth
января0июля6
февраля1августа7
марта2сентября8
апреля3октября9
мая4ноября10
июня5декабря11

MongoDB

SettingValue
Databasebacktest
Collectiontrade-results
ConnectionMONGO_URI env var (default: mongodb://localhost:27017/backtest)
Unique indexhash field — prevents duplicate rows
The script relies on the MONGO_URI environment variable configured in config/setup.ts. Ensure MongoDB is running and reachable before executing.

Output

The script prints a progress line per file, then a summary on completion:
Found 3 file(s)
MongoDB connected
trades_page_1.html: 42 rows
trades_page_2.html: 42 rows
trades_page_3.html: 17 rows
Done. Inserted: 98, skipped (duplicates): 3
Tmp cleaned.

Exit Codes

CodeMeaning
0Parsing and import completed successfully
1No trades_page_*.html files found in tmp/, or fatal error
Running import-trades.ts multiple times against the same HTML files is safe. The SHA1 hash deduplication ensures previously imported rows are silently skipped without creating duplicates.

Build docs developers (and LLMs) love