Skip to content

callthingsoff/tr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tr (scaffold)

This is a minimal Go project scaffold modeled after tr to get you started with multi-timeframe OHLC aggregation and streaming processing.

Structure

  • cmd/tr - main executable
  • internal/config - config loader (simple)
  • internal/server - minimal HTTP server (health route)
  • pkg/trader - demo trader with aggregators and forwarding
  • pkg/timeframe - timeframe definitions (M1, M5, M15, M30, H1, H4, D1, WK, MN)
  • pkg/ohlc - OHLC bar aggregation (batch and streaming)

Quick Start

Build

cd /path/to/repo
go build ./...

Run

go run ./cmd/tr -port 8080

Open http://localhost:8080/ or http://localhost:8080/health

Tests

Run all tests:

go test ./...

Run pkg/ohlc tests specifically:

go test ./pkg/ohlc -v

Expected output: 8 passed, 1 skipped

Performance Benchmarks

Run benchmarks with memory stats:

go test ./pkg/ohlc -bench . -benchmem -run ^$

Typical results (Apple M1):

BenchmarkAggregator_AddTick-8       47M ops/sec    23.05 ns/op    1 B/op    0 allocs/op
BenchmarkChain_M1ToH1-8             42M ops/sec    28.34 ns/op    1 B/op    0 allocs/op

Performance notes:

  • Single aggregator: ~23 nanoseconds per tick
  • Full chain (M1→M5→M15→M30→H1): ~28 nanoseconds per tick
  • Zero heap allocations (GC-friendly)
  • Memory overhead: 1 byte per operation (buffered channel semantics)

Recommended Settings for Benchmarking

For reproducible results on multi-core systems:

# Run with single CPU core
GOMAXPROCS=1 go test ./pkg/ohlc -bench . -benchmem

# Run with specific concurrency
GOMAXPROCS=4 go test ./pkg/ohlc -bench . -benchmem

Hardware recommendations:

  • Development: Any modern CPU (M1/Intel/AMD)
  • Benchmarking: Run on consistent hardware (avoid thermal throttling)
  • Production: 2+ cores recommended for typical tick rates

Multi-Timeframe Aggregation

The pkg/ohlc package provides streaming OHLC aggregation with hierarchical forwarding:

// Create aggregators for each timeframe
m1 := ohlc.NewAggregator(timeframe.M1)
m5 := ohlc.NewAggregator(timeframe.M5)
h1 := ohlc.NewAggregator(timeframe.H1)

// Wire forwarding: M1 bars → M5 → H1
go func() {
    for bar := range m1.Completed {
        m5.AddBar(bar)
    }
}()
go func() {
    for bar := range m5.Completed {
        h1.AddBar(bar)
    }
}()

// Feed ticks
tick := ohlc.Tick{Time: now, Price: 100.5, Volume: 10}
m1.AddTick(tick)

// Process completed bars
for bar := range h1.Completed {
    // Strategy logic here
}

Test Coverage

  • Unit tests: OHLC aggregation (batch & stream), boundary alignment
  • Integration tests:
    • M1→H1 hierarchical chain (61 min window)
    • M1→D1 cross-midnight (跨日)
    • D1→MN cross-month (跨月)
  • Benchmarks: Throughput and memory efficiency

Run specific integration test:

go test ./pkg/ohlc -run TestHierarchicalAggregation_M1ToH1 -v
go test ./pkg/ohlc -run TestCrossDayBoundary_ChainedToD1 -v
go test ./pkg/ohlc -run TestCrossMonthBoundary_ChainedToMN_Simplified -v

Next Steps

  • Add CSV/Parquet data replay mode in cmd/tr
  • Persist high-level bars to TSDB (e.g., InfluxDB)
  • Implement trading strategy examples
  • Add configuration file (YAML/TOML)
  • Add CI pipeline (GitHub Actions)
  • Extend benchmarks for high-concurrency scenarios

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages