Skip to content

feat: native Go day merge replacing external datatool binary - #60

Merged
jing2uo merged 1 commit into
jing2uo:mainfrom
Abelonx:feat/native-day-merge
Mar 20, 2026
Merged

jing2uo merged 1 commit into
jing2uo:mainfrom
Abelonx:feat/native-day-merge

Conversation

@Abelonx

@Abelonx Abelonx commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace the external datatool binary (Linux x86 ELF only) with a native Go implementation for the day subcommand, enabling cross-platform support including macOS arm64.

Problem

The current datatool binary embedded via //go:embed is a closed-source Linux x86 ELF. It cannot run on:

  • macOS arm64 (Apple Silicon) — Rosetta only translates x86 Mach-O, not Linux ELF
  • Windows
  • Linux arm64

This means tdx2db cron fails with exec format error on any non-Linux-x86 platform.

Solution

Reverse-engineered the TDX .md1 and .cod binary formats:

File Record Size Purpose
.cod 150 bytes Maps stock codes to sequence numbers
.md1 512 bytes Stores OHLCV data per block

Mapping: md1_offset = cod.seq_num * 512

The native merger (NativeDayMerge) reads .md1+.cod file pairs from vipdoc/refmhq/ and appends standard 32-byte .day records to per-stock files.

Backward Compatibility

  • "day" subcommand: uses native Go merger (cross-platform)
  • "tick" and "min" subcommands: unchanged, still use the embedded datatool binary as fallback

Verification

Cross-validated against original datatool output across all three exchanges:

Exchange Merged Skipped
SH 5,037 21,776
SZ 4,747 18,035
BJ 301 21

Spot-checked multiple stocks (OHLCV prices and volumes match exactly):

  • sh600519: O=1489.00, H=1496.50, L=1463.15, C=1468.80
  • sh601398: O=7.39, H=7.46, L=7.33, C=7.39
  • sz300750: O=405.36, H=406.00, L=396.77, C=402.02
  • bj920002: O=87.60, H=88.64, L=86.09, C=87.70

Date-based deduplication prevents duplicate records on re-runs.

Files Changed

  • tdx/merge.go — New: native .md1/.cod to .day merge implementation
  • tdx/merge_test.go — New: unit tests
  • tdx/datatool.go — Modified: route day to native merge, keep fallback for tick/min

Test plan

  • TestParseIncrFilename — filename parsing for all exchanges
  • TestMakeDayRecord — .day record encoding with known prices
  • TestAppendDayRecordDedup — dedup for same/older/newer dates
  • TestParseCodEntries — synthetic .cod file parsing
  • TestReadMd1Block — synthetic .md1 block extraction
  • End-to-end: tdx2db cron on macOS arm64 completes successfully

Replace the external datatool binary (Linux x86 ELF only) with a native
Go implementation for the "day" subcommand, enabling cross-platform
support including macOS arm64.

The TDX .md1 and .cod binary formats were reverse-engineered:
- .cod (150 bytes/record): maps stock codes to sequence numbers
- .md1 (512 bytes/block): stores OHLCV data per block
- Mapping: md1 offset = cod.seq_num * 512

The native merger reads .md1+.cod file pairs from vipdoc/refmhq/ and
appends 32-byte .day records to per-stock files, with date-based
deduplication to prevent duplicate appends on re-runs.

Cross-validated against original datatool output for multiple stocks
across SH/SZ/BJ exchanges — prices and volumes match exactly.

"tick" and "min" subcommands continue to use the embedded datatool
binary as fallback.
@jing2uo

jing2uo commented Mar 20, 2026

Copy link
Copy Markdown
Owner

Thanks for your work; it's a huge improvement. 🙏 🚀

@jing2uo
jing2uo merged commit 6244ca3 into jing2uo:main Mar 20, 2026
rains31 pushed a commit to rains31/tdx2db that referenced this pull request Nov 5, 2026
feat: native Go day merge replacing external datatool binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants