45 stable releases

new 2.0.5 Nov 16, 2024
2.0.1 Oct 23, 2024
1.0.43 Oct 31, 2024
1.0.29 Jul 26, 2024
1.0.1 Nov 21, 2023

#214 in Development tools

Download history 43/week @ 2024-07-28 314/week @ 2024-08-18 156/week @ 2024-08-25 10/week @ 2024-09-01 469/week @ 2024-09-15 550/week @ 2024-09-22 64/week @ 2024-09-29 9/week @ 2024-10-06 330/week @ 2024-10-13 368/week @ 2024-10-20 238/week @ 2024-10-27 49/week @ 2024-11-03 292/week @ 2024-11-10

959 downloads per month

MIT/Apache

345KB
7K SLoC

LongPort OpenAPI SDK for Rust

longport provides an easy-to-use interface for invokes LongPort OpenAPI.

Quickstart

Add dependencies to Cargo.toml

[dependencies]
longport = "1.0.0"

Setting environment variables(MacOS/Linux)

export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"

Setting environment variables(Windows)

setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"

Quote API (Get basic information of securities)

use std::sync::Arc;

use longport::{
    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 longport::{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 longport::{
    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,
        decimal!(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, longport gates certain features, all of which are disabled by default:

Feature Description
blocking Provides the blocking client API.

License

Licensed under either of

Dependencies

~15–27MB
~396K SLoC