35 releases

Uses new Rust 2021

new 0.2.42 Nov 30, 2022
0.2.41 Nov 9, 2022
0.2.40 Oct 27, 2022
0.2.36 Sep 28, 2022
0.2.8 May 30, 2022

#330 in Development tools

Download history 20/week @ 2022-08-08 21/week @ 2022-08-15 66/week @ 2022-08-22 18/week @ 2022-08-29 3/week @ 2022-09-05 33/week @ 2022-09-12 45/week @ 2022-09-19 25/week @ 2022-09-26 92/week @ 2022-10-03 25/week @ 2022-10-10 1/week @ 2022-10-17 83/week @ 2022-10-24 16/week @ 2022-10-31 26/week @ 2022-11-07 68/week @ 2022-11-14 46/week @ 2022-11-21

156 downloads per month

MIT/Apache

260KB
5.5K SLoC

Longbridge OpenAPI SDK for Rust

longbridge provides an easy-to-use interface for invokes Longbridge OpenAPI.

Quickstart

Add dependencies to Cargo.toml

[dependencies]
longbridge = "0.1.0"

Setting environment variables(MacOS/Linux)

export LONGBRIDGE_APP_KEY="App Key get from user center"
export LONGBRIDGE_APP_SECRET="App Secret get from user center"
export LONGBRIDGE_ACCESS_TOKEN="Access Token get from user center"

Setting environment variables(Windows)

setx LONGBRIDGE_APP_KEY "App Key get from user center"
setx LONGBRIDGE_APP_SECRET "App Secret get from user center"
setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center"

Quote API (Get basic information of securities)

use std::sync::Arc;

use longbridge::{
    decimal,
    trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType},
    Config, QuoteContext, TradeContext,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_env()?);

    // Create a context for quote APIs
    let (ctx, _) = QuoteContext::try_new(config.clone()).await?;

    // Get basic information of securities
    let resp = ctx
        .quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])
        .await?;
    println!("{:?}", resp);

    Ok(())
}

Quote API (Subscribe quotes)

use std::sync::Arc;

use longbridge::{quote::SubFlags, Config, QuoteContext};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_env()?);

    // Create a context for quote APIs
    let (ctx, mut receiver) = QuoteContext::try_new(config).await?;

    // Subscribe
    ctx.subscribe(["700.HK"], SubFlags::QUOTE, true).await?;

    // Receive push events
    while let Some(event) = receiver.recv().await {
        println!("{:?}", event);
    }

    Ok(())
}

Trade API (Submit order)

use std::sync::Arc;

use longbridge::{
    decimal,
    trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType},
    Config, TradeContext,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load configuration from environment variables
    let config = Arc::new(Config::from_env()?);

    // Create a context for trade APIs
    let (ctx, _) = TradeContext::try_new(config).await?;

    // Submit order
    let opts = SubmitOrderOptions::new(
        "700.HK",
        OrderType::LO,
        OrderSide::Buy,
        500,
        TimeInForceType::Day,
    )
    .submitted_price(decimal!(50i32))
    .remark("Hello from Rust SDK".to_string());

    let resp = ctx.submit_order(opts).await?;
    println!("{:?}", resp);

    Ok(())
}

Crate features

To avoid compiling unused dependencies, Longbridge gates certain features, all of which are disabled by default:

Feature Description
blocking Provides the blocking client API.

License

Licensed under either of

Dependencies

~17–26MB
~571K SLoC