polars-bt 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ [build]
2
+ jobs = 128
3
+
4
+ [test]
5
+ jobs = 128
@@ -0,0 +1,67 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main, develop]
6
+ pull_request:
7
+ branches: [main, develop]
8
+
9
+ env:
10
+ CARGO_TERM_COLOR: always
11
+
12
+ jobs:
13
+ test:
14
+ name: Test
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ matrix:
18
+ python-version: ["3.9", "3.10", "3.11", "3.12"]
19
+
20
+ steps:
21
+ - uses: actions/checkout@v4
22
+
23
+ - name: Set up Python ${{ matrix.python-version }}
24
+ uses: actions/setup-python@v5
25
+ with:
26
+ python-version: ${{ matrix.python-version }}
27
+
28
+ - name: Set up Rust
29
+ uses: dtolnay/rust-toolchain@nightly
30
+ with:
31
+ components: rustfmt, clippy
32
+
33
+ - name: Cache cargo
34
+ uses: actions/cache@v4
35
+ with:
36
+ path: |
37
+ ~/.cargo/registry
38
+ ~/.cargo/git
39
+ target
40
+ key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
41
+
42
+ - name: Cache uv
43
+ uses: actions/cache@v4
44
+ with:
45
+ path: ~/.cache/uv
46
+ key: ${{ runner.os }}-uv-${{ hashFiles('**/pyproject.toml') }}
47
+
48
+ - name: Install uv
49
+ run: pip install uv
50
+
51
+ - name: Install dependencies with uv
52
+ run: uv pip install --system maturin polars pytest
53
+
54
+ - name: Run rustfmt
55
+ run: cargo +nightly fmt --all -- --check
56
+
57
+ - name: Run clippy
58
+ run: cargo +nightly clippy --package polars_bt_extension --all-features -- -D warnings
59
+
60
+ - name: Build wheels with maturin
61
+ run: maturin build --release
62
+
63
+ - name: Install wheels with uv
64
+ run: uv pip install --system target/wheels/*.whl --force-reinstall
65
+
66
+ - name: Run tests with uv
67
+ run: uv run pytest test_backtest.py -v
@@ -0,0 +1,78 @@
1
+ # Rust
2
+ /target/
3
+ **/*.rs.bk
4
+ *.pdb
5
+ Cargo.lock
6
+
7
+ # Python
8
+ __pycache__/
9
+ *.py[cod]
10
+ *$py.class
11
+ *.so
12
+ *.pyd
13
+ *.dll
14
+ *.pyc
15
+ .Python
16
+ build/
17
+ develop-eggs/
18
+ dist/
19
+ downloads/
20
+ eggs/
21
+ .eggs/
22
+ lib/
23
+ lib64/
24
+ parts/
25
+ sdist/
26
+ var/
27
+ wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ *.pyi
32
+ MANIFEST
33
+
34
+ # Virtual environments
35
+ venv/
36
+ ENV/
37
+ env/
38
+ .venv/
39
+ .ENV/
40
+ env.bak/
41
+ venv.bak/
42
+
43
+ # IDE
44
+ .vscode/
45
+ .idea/
46
+ *.swp
47
+ *.swo
48
+ *~
49
+ .DS_Store
50
+
51
+ # Testing
52
+ .pytest_cache/
53
+ .coverage
54
+ htmlcov/
55
+ .tox/
56
+ *.parquet
57
+
58
+ # Logs
59
+ logs/
60
+ *.log
61
+
62
+ # Maturin
63
+ *.whl
64
+ *.tar.gz
65
+
66
+ # Environment variables
67
+ .env
68
+ .env.local
69
+ .env.*.local
70
+
71
+ # Temporary files
72
+ tmp/
73
+ temp/
74
+ *.tmp
75
+
76
+ # OS
77
+ Thumbs.db
78
+ .DS_Store
@@ -0,0 +1,198 @@
1
+ # Project Structure
2
+
3
+ This document provides an overview of the polars_bt_extension project structure and organization.
4
+
5
+ ## Directory Layout
6
+
7
+ ```
8
+ polars_bt_extension/
9
+ ├── .github/
10
+ │ └── workflows/
11
+ │ ├── ci.yml # Continuous Integration workflow
12
+ │ └── release.yml # Release and PyPI publishing workflow
13
+ ├── polars_bt_extension/ # Python package
14
+ │ ├── __init__.py # Package initialization
15
+ │ └── backtest.py # Python API for backtest function
16
+ ├── src/ # Rust source code
17
+ │ ├── exchange/
18
+ │ │ ├── mod.rs # Exchange simulator and main backtest logic
19
+ │ │ └── matcher.rs # Order matching algorithms
20
+ │ ├── order.rs # Order management structures
21
+ │ ├── position.rs # Position tracking and P&L calculation
22
+ │ └── lib.rs # Library entry point
23
+ ├── .gitignore # Git ignore patterns
24
+ ├── .cargo/ # Cargo configuration
25
+ ├── Cargo.toml # Rust dependencies and metadata
26
+ ├── CHANGELOG.md # Version changelog
27
+ ├── CONTRIBUTING.md # Contribution guidelines
28
+ ├── LICENSE # MIT License
29
+ ├── Makefile # Development tasks
30
+ ├── pyproject.toml # Python package configuration
31
+ ├── README.md # Project documentation
32
+ ├── requirements.txt # Python development dependencies
33
+ ├── rust-toolchain.toml # Rust toolchain configuration
34
+ ├── rustfmt.toml # Rust formatting configuration
35
+ └── test_backtest.py # Python tests
36
+ ```
37
+
38
+ ## Core Modules
39
+
40
+ ### `src/exchange/`
41
+
42
+ **Purpose**: Low-frequency exchange simulator with order matching capabilities
43
+
44
+ **Key Components**:
45
+ - `LofiEx`: Main exchange simulator
46
+ - `Strategy`: Trading strategy implementation
47
+ - `backtest()`: Main Polars plugin function
48
+
49
+ **Dependencies**: `order`, `position`, `matcher`
50
+
51
+ ### `src/order.rs`
52
+
53
+ **Purpose**: Order management and direction handling
54
+
55
+ **Key Components**:
56
+ - `Order`: Order structure with price, quantity, direction, and timestamp
57
+ - `OrderDirection`: Long/Short direction enumeration
58
+
59
+ **Dependencies**: `chrono` (for timestamp formatting)
60
+
61
+ ### `src/position.rs`
62
+
63
+ **Purpose**: Position tracking and profit/loss calculation
64
+
65
+ **Key Components**:
66
+ - `Position`: Position management with P&L tracking
67
+ - `PositionDirection`: Long/Short/Flat position direction
68
+ - `Summary`: Backtest result summary
69
+
70
+ **Dependencies**: `order`
71
+
72
+ ### `src/exchange/matcher.rs`
73
+
74
+ **Purpose**: Pluggable order matching algorithms
75
+
76
+ **Key Components**:
77
+ - `Matcher`: Trait for order matching algorithms
78
+ - `DefaultMatcher`: Strict cross matching with limit restrictions
79
+ - `EasyMatcher`: Lenient matching without limit restrictions
80
+ - `MarketData`: Current market price information
81
+ - `DealResp`: Order matching result
82
+
83
+ **Dependencies**: `order`, `position`
84
+
85
+ ### `polars_bt_extension/` (Python Package)
86
+
87
+ **Purpose**: Python API interface
88
+
89
+ **Key Components**:
90
+ - `backtest()`: Python wrapper for Rust backtest function
91
+
92
+ **Dependencies**: `polars`, `pyo3-polars`
93
+
94
+ ## Build System
95
+
96
+ ### Rust (Cargo)
97
+
98
+ - **Cargo.toml**: Defines Rust dependencies and package metadata
99
+ - **rust-toolchain.toml**: Specifies Rust toolchain version
100
+ - **rustfmt.toml**: Rust code formatting rules
101
+
102
+ ### Python (Maturin)
103
+
104
+ - **pyproject.toml**: Python package configuration
105
+ - **requirements.txt**: Development dependencies
106
+
107
+ ### CI/CD
108
+
109
+ - **.github/workflows/ci.yml**: Runs tests on push/PR
110
+ - **.github/workflows/release.yml**: Builds and publishes on tag
111
+
112
+ ## Development Workflow
113
+
114
+ 1. **Code Changes**: Modify Rust or Python code
115
+ 2. **Testing**: Run `cargo test` and `python test_backtest.py`
116
+ 3. **Quality Checks**: Run `cargo clippy` and `cargo fmt`
117
+ 4. **Build**: Run `maturin develop` or `maturin develop --release`
118
+ 5. **Commit**: Follow conventional commit format
119
+ 6. **Push**: CI will automatically run tests
120
+
121
+ ## Key Design Decisions
122
+
123
+ ### Flat Directory Structure
124
+
125
+ - Removed `bt_lite/` intermediate directory for simpler structure
126
+ - Direct module organization under `src/`
127
+ - Easier navigation and maintenance
128
+
129
+ ### Separation of Concerns
130
+
131
+ - **Exchange**: Order matching and execution
132
+ - **Order**: Order data structures
133
+ - **Position**: Position tracking and P&L
134
+ - **Matcher**: Pluggable matching algorithms
135
+
136
+ ### Performance Optimization
137
+
138
+ - Zero-copy data transfer with Polars
139
+ - Native Rust implementation
140
+ - Efficient memory management with PolarsAllocator
141
+ - Array literals instead of `vec!` where possible
142
+
143
+ ### Code Quality
144
+
145
+ - All public APIs documented with English comments
146
+ - Clippy strict mode (`-D warnings`)
147
+ - Rustfmt for consistent formatting
148
+ - Comprehensive test coverage
149
+
150
+ ## Dependencies
151
+
152
+ ### Rust
153
+
154
+ - `polars`: DataFrame library
155
+ - `pyo3`: Python bindings
156
+ - `pyo3-polars`: Polars integration
157
+ - `serde`: Serialization
158
+ - `chrono`: Date/time handling
159
+
160
+ ### Python
161
+
162
+ - `polars`: DataFrame library
163
+ - `pytest`: Testing framework
164
+ - `maturin`: Build tool
165
+
166
+ ## Testing
167
+
168
+ ### Unit Tests
169
+
170
+ - Rust tests in `src/` modules
171
+ - Python tests in `test_backtest.py`
172
+
173
+ ### Integration Tests
174
+
175
+ - CI runs tests on multiple Python versions (3.9-3.12)
176
+ - CI runs tests on multiple OS (Linux, macOS, Windows)
177
+
178
+ ### Quality Checks
179
+
180
+ - `cargo clippy`: Linting
181
+ - `cargo fmt`: Formatting
182
+ - `cargo doc`: Documentation
183
+
184
+ ## Release Process
185
+
186
+ 1. Update version in `Cargo.toml`
187
+ 2. Update `CHANGELOG.md`
188
+ 3. Create git tag: `git tag -a v1.0.0 -m "Release v1.0.0"`
189
+ 4. Push tag: `git push origin v1.0.0`
190
+ 5. GitHub Actions builds wheels and publishes to PyPI
191
+
192
+ ## Contributing
193
+
194
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.
195
+
196
+ ## License
197
+
198
+ MIT License - see [LICENSE](LICENSE) file for details.
@@ -0,0 +1,37 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+ - Initial release of polars_bt_extension backtesting engine plugin
12
+ - High-performance order matching with DefaultMatcher and EasyMatcher
13
+ - Comprehensive position management with P&L tracking
14
+ - Support for trading limits (limit up/down)
15
+ - Python API integration with Polars
16
+ - Complete documentation and examples
17
+
18
+ ### Changed
19
+ - Refactored project structure for better maintainability
20
+ - Optimized code for performance with zero-copy data transfer
21
+ - Improved code quality with clippy and rustfmt
22
+
23
+ ### Fixed
24
+ - All clippy warnings resolved
25
+ - Documentation formatting issues fixed
26
+
27
+ ## [0.1.2] - 2025-03-04
28
+
29
+ ### Added
30
+ - Initial version
31
+ - Core backtesting functionality
32
+ - Order matching algorithms
33
+ - Position management
34
+ - Python API
35
+
36
+ [Unreleased]: https://github.com/yourusername/polars_bt_extension/compare/v0.1.2...HEAD
37
+ [0.1.2]: https://github.com/yourusername/polars_bt_extension/releases/tag/v0.1.2
@@ -0,0 +1,185 @@
1
+ # Contributing to polars_bt_extension
2
+
3
+ Thank you for your interest in contributing to polars_bt_extension! This document provides guidelines and instructions for contributing to the project.
4
+
5
+ ## Code of Conduct
6
+
7
+ Be respectful, inclusive, and constructive in all interactions. We aim to maintain a welcoming environment for all contributors.
8
+
9
+ ## Getting Started
10
+
11
+ ### Prerequisites
12
+
13
+ - Rust 1.70 or later
14
+ - Python 3.9 or later
15
+ - maturin
16
+ - Git
17
+
18
+ ### Setting Up Development Environment
19
+
20
+ ```bash
21
+ # Clone the repository
22
+ git clone https://github.com/yourusername/polars_bt_extension.git
23
+ cd polars_bt_extension
24
+
25
+ # Create a virtual environment
26
+ python -m venv .venv
27
+ source .venv/bin/activate # On Windows: .venv\Scripts\activate
28
+
29
+ # Install development dependencies
30
+ pip install maturin polars pytest
31
+
32
+ # Install the package in development mode
33
+ maturin develop
34
+ ```
35
+
36
+ ## Development Workflow
37
+
38
+ ### 1. Create a Branch
39
+
40
+ ```bash
41
+ git checkout -b feature/your-feature-name
42
+ # or
43
+ git checkout -b fix/your-bug-fix
44
+ ```
45
+
46
+ ### 2. Make Changes
47
+
48
+ - Write clean, readable code
49
+ - Follow existing code style and conventions
50
+ - Add tests for new functionality
51
+ - Update documentation as needed
52
+
53
+ ### 3. Code Quality Checks
54
+
55
+ Before submitting, ensure your code passes all quality checks:
56
+
57
+ ```bash
58
+ # Format code
59
+ cargo fmt
60
+
61
+ # Run clippy (linting)
62
+ cargo clippy --all-targets --all-features -- -D warnings
63
+
64
+ # Run tests
65
+ cargo test
66
+ python test_backtest.py
67
+
68
+ # Check documentation
69
+ cargo doc --no-deps --all-features
70
+ ```
71
+
72
+ ### 4. Commit Changes
73
+
74
+ ```bash
75
+ git add .
76
+ git commit -m "Add your commit message here"
77
+ ```
78
+
79
+ **Commit Message Guidelines:**
80
+
81
+ - Use the present tense ("Add feature" not "Added feature")
82
+ - Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
83
+ - Limit the first line to 72 characters or less
84
+ - Reference issues and pull requests liberally
85
+
86
+ Examples:
87
+ - `Add support for custom matchers`
88
+ - `Fix memory leak in position tracking`
89
+ - `Update documentation for API changes`
90
+
91
+ ### 5. Push and Create Pull Request
92
+
93
+ ```bash
94
+ git push origin feature/your-feature-name
95
+ ```
96
+
97
+ Then create a Pull Request on GitHub with:
98
+ - Clear title and description
99
+ - Reference any related issues
100
+ - Explain the changes and their purpose
101
+ - Include screenshots if applicable
102
+
103
+ ## Coding Standards
104
+
105
+ ### Rust Code
106
+
107
+ - Follow [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/)
108
+ - Use meaningful variable and function names
109
+ - Add doc comments for all public APIs
110
+ - Prefer `unwrap_or_default()` over manual `match` for Option handling
111
+ - Use array literals instead of `vec!` when possible
112
+
113
+ ### Python Code
114
+
115
+ - Follow [PEP 8](https://pep8.org/)
116
+ - Use type hints where appropriate
117
+ - Add docstrings for all public functions
118
+ - Keep functions focused and small
119
+
120
+ ### Documentation
121
+
122
+ - All public APIs must have documentation
123
+ - Use clear, concise language
124
+ - Include examples for complex functionality
125
+ - Keep documentation up to date with code changes
126
+
127
+ ## Testing
128
+
129
+ ### Writing Tests
130
+
131
+ - Write tests for all new functionality
132
+ - Test both success and failure cases
133
+ - Use descriptive test names
134
+ - Keep tests independent and fast
135
+
136
+ ### Running Tests
137
+
138
+ ```bash
139
+ # Rust tests
140
+ cargo test
141
+
142
+ # Python tests
143
+ pytest test_backtest.py -v
144
+
145
+ # Run specific test
146
+ cargo test test_name
147
+ pytest test_backtest.py::test_function -v
148
+ ```
149
+
150
+ ## Project Structure
151
+
152
+ ```
153
+ polars_bt_extension/
154
+ ├── src/
155
+ │ ├── exchange/ # Exchange simulator and matching logic
156
+ │ ├── order.rs # Order management
157
+ │ ├── position.rs # Position tracking
158
+ │ └── lib.rs # Library entry point
159
+ ├── polars_bt_extension/ # Python package
160
+ │ ├── __init__.py
161
+ │ └── backtest.py # Python API
162
+ ├── tests/ # Test files
163
+ ├── .github/ # GitHub workflows
164
+ ├── Cargo.toml # Rust dependencies
165
+ ├── pyproject.toml # Python configuration
166
+ └── README.md # Project documentation
167
+ ```
168
+
169
+ ## Release Process
170
+
171
+ Releases are handled by maintainers through GitHub Actions:
172
+
173
+ 1. Update version in `Cargo.toml`
174
+ 2. Create a git tag: `git tag -a v1.0.0 -m "Release version 1.0.0"`
175
+ 3. Push the tag: `git push origin v1.0.0`
176
+ 4. GitHub Actions will automatically build and publish to PyPI
177
+
178
+ ## Questions?
179
+
180
+ Feel free to:
181
+ - Open an issue for bugs or feature requests
182
+ - Start a discussion for questions or ideas
183
+ - Contact maintainers directly for urgent matters
184
+
185
+ Thank you for contributing to polars_bt_extension! 🚀