1 unstable release
0.1.0 | Sep 24, 2024 |
---|
#109 in Finance
48KB
919 lines
Tradier: Rust Library for Tradier Broker API
Table of Contents
- Introduction
- Features
- Project Structure
- Setup Instructions
- Library Usage
- Usage Examples
- Development
- Contribution and Contact
Introduction
tradier is a comprehensive Rust library for managing trades and market data using the Tradier broker API. This powerful toolkit enables developers, traders, and financial applications to:
- Execute trades efficiently
- Retrieve real-time quotes
- Manage portfolios
- Access historical market data
The library leverages Rust's performance and concurrency advantages, making it suitable for high-frequency trading applications and data-intensive financial processing.
Features
- Trade Execution: Implement order placement, modification, and cancellation.
- Real-time Market Data: Access live quotes, order book data, and trade information.
- Portfolio Management: Retrieve account information, positions, and performance metrics.
- Historical Data: Fetch and analyze historical price and volume data.
- Streaming Data: Utilize WebSocket connections for real-time data feeds.
- Authentication: Securely manage API keys and authentication tokens.
- Error Handling: Robust error handling and logging for reliability.
- Rate Limiting: Implement rate limiting to comply with API usage restrictions.
- Concurrent Processing: Leverage Rust's async capabilities for efficient data handling.
- Data Serialization: Use Serde for efficient JSON parsing and serialization.
Project Structure
The project is structured as follows:
-
Root Directory:
Cargo.lock
andCargo.toml
: Rust package manager files for dependency management.Docker
: Directory containing Docker-related files for containerization.LICENSE
: The license file for the project.Makefile
: Contains commands for common development tasks.README.md
: The main documentation file you're currently reading.coverage
: Directory for test coverage reports.rust-toolchain.toml
: Specifies the Rust toolchain version for the project.tarpaulin-report.html
: HTML report of test coverage generated by cargo-tarpaulin.
-
Documentation (
doc/
):images/
: Directory for storing images used in documentation.
-
Examples (
examples/
):auth_example.rs
: Example of how to use authentication in the library.auth_websocket_example.rs
: Example of using authenticated WebSocket connections.
-
Source Code (
src/
):- Configuration (
config/
):base.rs
: Defines the base configuration structure for the library.mod.rs
: Module declaration file for the config module.
constants.rs
: Defines constant values used throughout the library.lib.rs
: The main library file that exposes the public API.- Utilities (
utils/
):error.rs
: Defines custom error types for the library.logger.rs
: Sets up logging functionality.mod.rs
: Module declaration file for the utils module.tests.rs
: Contains tests for utility functions.
- WebSocket Session (
wssession/
):account.rs
: Handles account-related WebSocket sessions.market.rs
: Manages market data WebSocket sessions.mod.rs
: Module declaration file for the wssession module.session.rs
: Defines the base WebSocket session structure and functionality.
- Configuration (
-
Tests (
tests/
):- Unit Tests (
unit/
):mod.rs
: Module file for unit tests.
- Unit Tests (
This structure organizes the project into logical components:
- The root directory contains project-wide configuration and documentation files.
- The
examples
directory provides sample code for users to understand how to use the library. - The
src
directory contains the main library code, divided into modules for configuration, utilities, and WebSocket session management. - The
tests
directory is set up for containing unit tests, ensuring the reliability of the library's components.
Each module and file has a specific purpose, contributing to a well-organized and maintainable codebase for the TradierRust library.
Setup Instructions
-
Add tradier to your
Cargo.toml
:[dependencies] tradier = "0.1.0"
-
Set up your Tradier API credentials:
- Create a
.env
file in your project root:TRADIER_ACCESS_TOKEN=your_api_key_here TRADIER_CLIENT_ID=your_account_id_here
- Create a
-
Build your project:
cargo build
Library Usage
To use the library in your project:
use tradier::config::base::Config;
use tradier::utils::logger::setup_logger;
use tradier::wssession::market::MarketSession;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
setup_logger();
let config = Config::new();
let market_session = MarketSession::new(&config).await?;
// Use the market_session to interact with the Tradier API
Ok(())
}
Usage Examples
Here's an example of how to use the library for streaming market data:
use std::error::Error;
use tracing::{error, info};
use tradier::config::base::Config;
use tradier::utils::logger::setup_logger;
use tradier::wssession::account::AccountSession;
use tradier::wssession::market::{MarketSession, MarketSessionFilter, MarketSessionPayload};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
setup_logger();
let config = Config::new();
loop {
match MarketSession::new(&config).await {
Ok(market_session) => {
info!(
"Market streaming session created with id: {}",
market_session.get_session_id()
);
let payload = MarketSessionPayload {
symbols: vec!["AAPL".to_string(), "MSFT".to_string()],
filter: Some(vec![MarketSessionFilter::QUOTE, MarketSessionFilter::TRADE]),
session_id: market_session.get_session_id().to_string(),
linebreak: Some(true),
valid_only: Some(true),
advanced_details: None,
};
if let Err(e) = market_session.ws_stream(payload).await {
error!("Streaming error: {}. Reconnecting...", e);
}
}
Err(e) => {
error!(
"Failed to create market streaming session: {}. Retrying...",
e
);
}
}
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
}
}
Development
This project includes a Makefile for common development tasks:
make build
: Build the projectmake test
: Run testsmake fmt
: Format the codemake lint
: Run Clippy for lintingmake clean
: Clean the projectmake check
: Run pre-push checks (test, format check, lint)make coverage
: Generate test coverage reportmake doc
: Generate and open documentation
To run a specific task, use make <task_name>
. For example:
make test
Contribution and Contact
We welcome contributions to this project! If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure that the project still builds and all tests pass.
- Commit your changes and push your branch to your forked repository.
- Submit a pull request to the main repository.
If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:
Joaquín Béjar García
- Email: jb@taunais.com
- GitHub: joaquinbejar
We appreciate your interest and look forward to your contributions!
Dependencies
~11–25MB
~365K SLoC