Crate longbridge
source · [−]Expand description
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::{error::Error, sync::Arc};
use longbridge::{
decimal,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType},
Config, QuoteContext, TradeContext,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn 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 anyhow::Result;
use longbridge::{quote::SubFlags, Config, QuoteContext};
#[tokio::main]
async fn main() -> Result<()> {
// 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 anyhow::Result;
use longbridge::{
decimal,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType},
Config, TradeContext,
};
#[tokio::main]
async fn main() -> Result<()> {
// 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!(500i32),
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
- Apache License, Version 2.0,(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Modules
Macros
A macro to construct decimal.
Structs
Configuration options for Longbridge sdk
Decimal represents a 128 bit representation of a fixed-precision decimal number.
The finite set of values of type Decimal are of the form m / 10e,
where m is an integer such that -296 < m < 296, and e is an integer
between 0 and 28 inclusive.
Quote context
Trade context
Enums
Market