Skip to main content

Canonical schemas for AmpyFin (generated Python protobufs)

Project description

ampy-proto

Canonical Protocol Buffer schemas for the AmpyFin trading system

License: MIT Go Version Python Version C++ Standard Protocol Buffers Buf

Build Status Tests Coverage Code Quality

Release Downloads Last Commit


๐Ÿ“‹ Table of Contents

๐ŸŽฏ What We're Solving

AmpyFin is building a self-learning, modular trading system that needs to handle data from multiple sources (DataBento, Tiingo, yfinance, etc.) across different languages (Go, Python, C++). This project provides:

  • ๐ŸŽฏ Unified schemas for all financial data (bars, ticks, fundamentals, news, etc.)
  • ๐ŸŒ Cross-language compatibility with generated code for Go, Python, and C++
  • ๐ŸŽฏ Precision guarantees using scaled decimal types instead of floating-point
  • โฐ Time discipline with UTC timestamps and clear event/ingest time separation
  • ๐Ÿ”„ Versioned contracts that evolve safely without breaking consumers

โšก Quick Start

๐Ÿ Python

# Install from PyPI (when published)
pip install ampy-proto

# Or install locally
pip install -e .
from ampy.bars.v1 import bars_pb2
from ampy.common.v1 import common_pb2

# Create a bar
bar = bars_pb2.Bar()
bar.security.symbol = "AAPL"
bar.security.mic = "XNAS"
bar.open.scaled = 1923450
bar.open.scale = 4
bar.high.scaled = 1925600
bar.high.scale = 4
bar.low.scaled = 1922200
bar.low.scale = 4
bar.close.scaled = 1924100
bar.close.scale = 4
bar.volume = 184230

print(f"AAPL bar: ${bar.close.scaled / (10 ** bar.close.scale):.2f}")

๐Ÿน Go

# Install all ampy-proto packages at once
go get github.com/AmpyFin/ampy-proto@v2.0.3

# Or install specific packages only
go get github.com/AmpyFin/ampy-proto/gen/go/ampy/bars/v1@v2.0.3
package main

import (
    "fmt"
    barspb "github.com/AmpyFin/ampy-proto/gen/go/ampy/bars/v1"
)

func main() {
    bar := &barspb.Bar{}
    bar.Security = &barspb.Security{
        Symbol: "AAPL",
        Mic:    "XNAS",
    }
    bar.Open = &barspb.Decimal{
        Scaled: 1923450,
        Scale:  4,
    }
    bar.High = &barspb.Decimal{
        Scaled: 1925600,
        Scale:  4,
    }
    bar.Low = &barspb.Decimal{
        Scaled: 1922200,
        Scale:  4,
    }
    bar.Close = &barspb.Decimal{
        Scaled: 1924100,
        Scale:  4,
    }
    bar.Volume = 184230

    fmt.Printf("AAPL bar: $%.2f\n", float64(bar.Close.Scaled)/float64(1e4))
}

โšก C++

# Build the library
cd gen/cpp
mkdir build && cd build
cmake .. && make
#include <iostream>
#include "ampy/bars/v1/bars.pb.h"

int main() {
    ampy::bars::v1::Bar bar;
    bar.mutable_security()->set_symbol("AAPL");
    bar.mutable_security()->set_mic("XNAS");
    bar.mutable_open()->set_scaled(1923450);
    bar.mutable_open()->set_scale(4);
    bar.mutable_high()->set_scaled(1925600);
    bar.mutable_high()->set_scale(4);
    bar.mutable_low()->set_scaled(1922200);
    bar.mutable_low()->set_scale(4);
    bar.mutable_close()->set_scaled(1924100);
    bar.mutable_close()->set_scale(4);
    bar.set_volume(184230);

    std::cout << "AAPL bar: $" 
              << (bar.close().scaled() / std::pow(10, bar.close().scale()))
              << std::endl;
    return 0;
}

๐Ÿ“Š Available Schemas

๐Ÿท๏ธ Domain ๐Ÿ“ Purpose ๐Ÿ”‘ Key Messages
๐Ÿ“ˆ bars OHLCV price bars Bar, BarBatch
๐Ÿ“Š ticks Trade and quote data Tick, TickBatch
๐Ÿ“‹ fundamentals Financial statements Fundamental, FundamentalBatch
๐Ÿ“ฐ news Market news and sentiment NewsItem
๐Ÿ’ฑ fx Foreign exchange rates FxRate
๐Ÿข corporate_actions Splits, dividends CorporateAction
๐ŸŒ universe Tradable securities lists Universe
๐ŸŽฏ signals Model outputs and signals Signal
๐Ÿ“‹ orders Order management Order, OrderRequest
โœ… fills Trade executions Fill
๐Ÿ’ผ positions Portfolio positions Position
๐Ÿ“Š metrics Operational metrics Metric
๐Ÿ”ง common Shared types Decimal, Money, Security, Meta

๐Ÿ—๏ธ Key Design Principles

1. ๐ŸŽฏ Precision with Scaled Decimals

Instead of floating-point numbers, we use scaled decimals:

message Decimal {
  int64 scaled = 1;  // The actual value
  int32 scale = 2;   // Decimal places (e.g., 4 = 4 decimal places)
}

Example: scaled: 1923450, scale: 4 represents 192.3450

2. โฐ Time Discipline

All timestamps are UTC with clear semantics:

  • ๐Ÿ• event_time: When the market event actually happened
  • ๐Ÿ“ฅ ingest_time: When our system received it
  • ๐ŸŽฏ as_of: Logical timestamp for downstream processing

3. ๐Ÿท๏ธ Security Identification

Use proper security identifiers, not just tickers:

message Security {
  string symbol = 1;    // e.g., "AAPL"
  string mic = 2;       // Market identifier code, e.g., "XNAS"
  string figi = 3;      // Financial Instrument Global Identifier (optional)
  string isin = 4;      // International Securities Identification Number (optional)
}

4. ๐Ÿ” Metadata for Traceability

Every message includes metadata for lineage:

message Meta {
  string run_id = 1;           // Unique run identifier
  string source = 2;           // Data source (e.g., "yfinance-go")
  string producer = 3;         // Producer instance ID
  string schema_version = 4;   // Schema version used
  string checksum = 5;         // Optional message checksum
}

๐Ÿ› ๏ธ Development

๐Ÿ“‹ Prerequisites

  • ๐Ÿ”ง Buf for protobuf management
  • ๐Ÿน Go 1.23+ for Go code generation
  • ๐Ÿ Python 3.9+ for Python code generation
  • โšก CMake and C++17 compiler for C++ code generation

๐Ÿ”จ Building

# Generate code for all languages
buf generate proto

# Lint protobuf files
buf lint proto

# Check for breaking changes
buf breaking proto --against '.git#branch=main'

# Build Python package
python -m build

# Build C++ library
cd gen/cpp && mkdir build && cd build && cmake .. && make

๐Ÿงช Testing

# Run roundtrip tests
python tests/roundtrip/python_roundtrip.py

# Test Go imports
go run examples/go/smoke/main.go

# Test C++ compilation
cd examples/cpp && g++ -I../../gen/cpp smoke.cpp -L../../gen/cpp/build -lampy_proto -lprotobuf -o smoke && ./smoke

๐Ÿ“ˆ Versioning

This project follows semantic versioning:

  • ๐Ÿ”ด Major versions (v2, v3): Breaking changes requiring migration
  • ๐ŸŸก Minor versions (v1.1, v1.2): Additive changes, backward compatible
  • ๐ŸŸข Patch versions (v1.0.1, v1.0.2, v1.0.3): Bug fixes, backward compatible

Current version: v2.0.3

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Make your changes and add tests
  4. Run buf lint and buf breaking to ensure compatibility
  5. Submit a pull request

๐Ÿ“‹ Schema Evolution Rules

  • โœ… Add new optional fields with default values
  • โœ… Add new enum values (append only)
  • โœ… Add new messages or services
  • โŒ Never change field numbers of existing fields
  • โŒ Never change field types of existing fields
  • โŒ Never remove fields (mark as deprecated instead)
  • โŒ Never renumber enum values

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ’ฌ Support



This project is part of the AmpyFin ecosystem - a self-learning, modular trading system.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ampy_proto-2.0.3.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ampy_proto-2.0.3-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file ampy_proto-2.0.3.tar.gz.

File metadata

  • Download URL: ampy_proto-2.0.3.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ampy_proto-2.0.3.tar.gz
Algorithm Hash digest
SHA256 57df7782ac7efc6c81f38f738e1653cfc297fdb6271f927add401e0769767164
MD5 cb25f56af4c934eb6e22c78b1b2c9673
BLAKE2b-256 a52179f3ca23fa4052180db338bdedc3f045d8710e6cbb719d8bafd41056bb39

See more details on using hashes here.

File details

Details for the file ampy_proto-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: ampy_proto-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ampy_proto-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 38ada512409f2a04eee03391fe91495c61c2d7774147d0b60acdcb84ee072abd
MD5 3048f9356e04b4e26866b7952719d39a
BLAKE2b-256 b231665eb7017a8cc822297102fb2b06dd4c62f658af03281fa6ea0de1b2e002

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page