Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Binance Rust Algo Connector

Crates.io docs.rs Build Status License: MIT

This module provides the official Rust client for Binance's Algo Trading REST API, enabling developers to interact programmatically with Binance's Algo trading platform. The module provides tools to programmatically leverage Binance in-house algorithmic trading capability to automate order execution strategy, improve execution transparency and give users smart access to the available market liquidity through the REST API:

Table of Contents

Supported Features

  • REST API Endpoints:
    • /sapi/v1/algo/*
  • Inclusion of test cases and examples for quick onboarding.

Installation

Enable the algo feature in your Cargo.toml under binance-sdk:

[dependencies]
binance-sdk = { version = "1.0.0", features = ["algo"] }

In your code, import the algo client:

use binance_sdk::algo;

Documentation

REST APIs

All REST API endpoints are available through the rest_api module. Note that some endpoints require authentication using your Binance API credentials.

use binance_sdk::config::ConfigurationRestApi;
use binance_sdk::algo;

let configuration = ConfigurationRestApi::builder()
  .api_key("YOUR_API_KEY")
  .api_secret("YOUR_SECRET_KEY")
  .build()?;

let client = algo::AlgoRestApi::production(configuration);
let params = algo::rest_api::QueryHistoricalAlgoOrdersSpotAlgoParams::default();
let response = client.query_historical_algo_orders_spot_algo(params).await?;

let data = response.data().await?;
println!("{:#?}", data);

Configuration Options

The algo module can be configured with the following options via the ConfigurationRestApi builder pattern:

  • timeout (u64): Request timeout in milliseconds (default: 1000)
  • proxy (ProxyConfig): HTTP/HTTPS proxy settings
    • host (String): Proxy server hostname.
    • port (u16): Proxy server port.
    • protocol (String): Proxy protocol (http or https).
    • auth (ProxyAuth): Proxy authentication credentials:
      • username (String): Proxy username.
      • password (String): Proxy password.
  • keep_alive (bool): Enable HTTP keep-alive (default: true)
  • compression (bool): Enable response compression (default: true)
  • retries (u32): Number of retry attempts for failed requests (default: 3)
  • backoff (u64): Delay in milliseconds between retries (default: 1000)
  • agent (HttpAgent): Custom HTTP agent for advanced TLS configuration
  • private_key (PrivateKey): RSA or ED25519 private key for request signing (raw string or PEM file path)
  • private_key_passphrase (String): Passphrase for the private key, if encrypted

Refer to the configuration for more details.

Timeout

You can configure a timeout for requests in milliseconds. If the request exceeds the specified timeout, it will be aborted. See the Timeout example for detailed usage.

Proxy

The REST API supports HTTP/HTTPS proxy configurations. See the Proxy example for detailed usage.

Keep-Alive

Enable HTTP keep-alive for persistent connections. See the Keep-Alive example for detailed usage.

Compression

Enable or disable response compression. See the Compression example for detailed usage.

Retries

Configure the number of retry attempts and delay in milliseconds between retries for failed requests. See the Retries example for detailed usage.

HTTPS Agent

Customize the HTTPS agent for advanced TLS configurations. See the HTTPS Agent example for detailed usage.

Key Pair Based Authentication

The REST API supports key pair-based authentication for secure communication. You can use RSA or Ed25519 keys for signing requests. See the Key Pair Based Authentication example for detailed usage.

Certificate Pinning

To enhance security, you can use certificate pinning with the agent option in the configuration. This ensures the client only communicates with servers using specific certificates. See the Certificate Pinning example for detailed usage.

Error Handling

Errors are represented by the following types:

  • ConnectorClientError: General client error
  • UnauthorizedError: Invalid or missing authentication
  • ForbiddenError: Access to resource forbidden
  • TooManyRequestsError: Rate limit exceeded
  • RateLimitBanError: IP banned due to rate limits
  • ServerError: Internal server error
  • NetworkError: Network connectivity issues
  • NotFoundError: Resource not found
  • BadRequestError: Invalid request parameters

See the Error Handling example for detailed usage. Refer to the error module for more information.

Logging

This crate ships with an optional default logger that you can enable with a single call:

use binance_sdk::logger;

fn main() {
    // Initialize the default logger once at the start of your application
    logger::init();

    // ... rest of your code
}

The logger integrates with the Rust tracing ecosystem and behaves as follows:

  • If another global subscriber is already set, logger::init() is a no-op and does not override your existing logging setup.
  • If no subscriber is set yet, it installs the crate’s default global subscriber (with sensible defaults) so you immediately get structured logs from the connector.

Testing

To run the tests for the Algo module:

cargo test --features algo

Tests cover:

  • REST API endpoint integration
  • Error scenarios and edge cases

Migration Guide

If you are upgrading from a legacy, single-crate connector that included Algo support, see the Migration Guide for instructions on enabling the algo feature.

Contributing

Contributions are welcome!

Since this repository contains auto-generated code, we encourage you to start by opening a GitHub issue to discuss your ideas or suggest improvements. This helps ensure that changes align with the project's goals and auto-generation processes.

To contribute:

  1. Open a GitHub issue describing your suggestion or the bug you've identified.
  2. If it's determined that changes are necessary, the maintainers will merge the changes into the main branch.

Please ensure that all tests pass with --features algo if you're making a direct contribution. Submit a pull request only after discussing and confirming the change.

Thank you for your contributions!

License

This project is licensed under the MIT License. See the LICENSE file for details.