pub struct TradeContext(/* private fields */);Expand description
Trade context
Implementations§
Source§impl TradeContext
impl TradeContext
Sourcepub async fn subscribe<I>(&self, topics: I) -> Result<()>where
I: IntoIterator<Item = TopicType>,
pub async fn subscribe<I>(&self, topics: I) -> Result<()>where
I: IntoIterator<Item = TopicType>,
Subscribe
Reference: https://open.longbridge.com/en/docs/trade/trade-push#subscribe
§Examples
use std::sync::Arc;
use longbridge::{
Config, decimal,
oauth::OAuthBuilder,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType, TradeContext},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, mut receiver) = TradeContext::new(config);
let opts = SubmitOrderOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
decimal!(200),
TimeInForceType::Day,
)
.submitted_price(decimal!(50i32));
let resp = ctx.submit_order(opts).await?;
println!("{:?}", resp);
while let Some(event) = receiver.recv().await {
println!("{:?}", event);
}
Sourcepub async fn unsubscribe<I>(&self, topics: I) -> Result<()>where
I: IntoIterator<Item = TopicType>,
pub async fn unsubscribe<I>(&self, topics: I) -> Result<()>where
I: IntoIterator<Item = TopicType>,
Unsubscribe
Reference: https://open.longbridge.com/en/docs/trade/trade-push#cancel-subscribe
Sourcepub async fn history_executions(
&self,
options: impl Into<Option<GetHistoryExecutionsOptions>>,
) -> Result<Vec<Execution>>
pub async fn history_executions( &self, options: impl Into<Option<GetHistoryExecutionsOptions>>, ) -> Result<Vec<Execution>>
Get history executions
side may be Unknown on Longbridge US accounts.
Reference: https://open.longbridge.com/en/docs/trade/execution/history_executions
§Examples
use std::sync::Arc;
use longbridge::{
oauth::OAuthBuilder,
trade::{GetHistoryExecutionsOptions, TradeContext},
Config,
};
use time::macros::datetime;
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = GetHistoryExecutionsOptions::new()
.symbol("700.HK")
.start_at(datetime!(2022-05-09 0:00 UTC))
.end_at(datetime!(2022-05-12 0:00 UTC));
let resp = ctx.history_executions(opts).await?;
println!("{:?}", resp);Sourcepub async fn today_executions(
&self,
options: impl Into<Option<GetTodayExecutionsOptions>>,
) -> Result<Vec<Execution>>
pub async fn today_executions( &self, options: impl Into<Option<GetTodayExecutionsOptions>>, ) -> Result<Vec<Execution>>
Get today executions
Reference: https://open.longbridge.com/en/docs/trade/execution/today_executions
§Examples
use std::sync::Arc;
use longbridge::{
Config,
oauth::OAuthBuilder,
trade::{GetTodayExecutionsOptions, TradeContext},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = GetTodayExecutionsOptions::new().symbol("700.HK");
let resp = ctx.today_executions(opts).await?;
println!("{:?}", resp);Sourcepub async fn all_executions(
&self,
options: impl Into<Option<GetAllExecutionsOptions>>,
) -> Result<AllExecutionsResponse>
pub async fn all_executions( &self, options: impl Into<Option<GetAllExecutionsOptions>>, ) -> Result<AllExecutionsResponse>
Get all executions
AP accounts only: US data-center accounts get a region error; use
history_executions instead.
Reference: https://open.longbridge.com/en/docs/trade/execution/all_executions
Sourcepub async fn history_orders(
&self,
options: impl Into<Option<GetHistoryOrdersOptions>>,
) -> Result<Vec<Order>>
pub async fn history_orders( &self, options: impl Into<Option<GetHistoryOrdersOptions>>, ) -> Result<Vec<Order>>
Get history orders
Reference: https://open.longbridge.com/en/docs/trade/order/history_orders
§Examples
use std::sync::Arc;
use longbridge::{
oauth::OAuthBuilder,
trade::{GetHistoryOrdersOptions, OrderSide, OrderStatus, TradeContext},
Config, Market,
};
use time::macros::datetime;
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = GetHistoryOrdersOptions::new()
.symbol("700.HK")
.status([OrderStatus::Filled, OrderStatus::New])
.side(OrderSide::Buy)
.market(Market::HK)
.start_at(datetime!(2022-05-09 0:00 UTC))
.end_at(datetime!(2022-05-12 0:00 UTC));
let resp = ctx.history_orders(opts).await?;
println!("{:?}", resp);Sourcepub async fn today_orders(
&self,
options: impl Into<Option<GetTodayOrdersOptions>>,
) -> Result<Vec<Order>>
pub async fn today_orders( &self, options: impl Into<Option<GetTodayOrdersOptions>>, ) -> Result<Vec<Order>>
Get today orders
Reference: https://open.longbridge.com/en/docs/trade/order/today_orders
§Examples
use std::sync::Arc;
use longbridge::{
Config, Market,
oauth::OAuthBuilder,
trade::{GetTodayOrdersOptions, OrderSide, OrderStatus, TradeContext},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = GetTodayOrdersOptions::new()
.symbol("700.HK")
.status([OrderStatus::Filled, OrderStatus::New])
.side(OrderSide::Buy)
.market(Market::HK);
let resp = ctx.today_orders(opts).await?;
println!("{:?}", resp);Sourcepub async fn replace_order(&self, options: ReplaceOrderOptions) -> Result<()>
pub async fn replace_order(&self, options: ReplaceOrderOptions) -> Result<()>
Replace order
Reference: https://open.longbridge.com/en/docs/trade/order/replace
§Examples
use std::sync::Arc;
use longbridge::{
Config, decimal,
oauth::OAuthBuilder,
trade::{ReplaceOrderOptions, TradeContext},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts =
ReplaceOrderOptions::new("709043056541253632", decimal!(100)).price(decimal!(300i32));
let resp = ctx.replace_order(opts).await?;
println!("{:?}", resp);Sourcepub async fn submit_order(
&self,
options: SubmitOrderOptions,
) -> Result<SubmitOrderResponse>
pub async fn submit_order( &self, options: SubmitOrderOptions, ) -> Result<SubmitOrderResponse>
Submit order
Reference: https://open.longbridge.com/en/docs/trade/order/submit
§Examples
use std::sync::Arc;
use longbridge::{
Config, decimal,
oauth::OAuthBuilder,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType, TradeContext},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = SubmitOrderOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
decimal!(200),
TimeInForceType::Day,
)
.submitted_price(decimal!(50i32));
let resp = ctx.submit_order(opts).await?;
println!("{:?}", resp);Sourcepub async fn submit_multileg(
&self,
options: SubmitMultiLegOrderOptions,
) -> Result<SubmitOrderResponse>
pub async fn submit_multileg( &self, options: SubmitMultiLegOrderOptions, ) -> Result<SubmitOrderResponse>
Submit a multi-leg option combination order (such as vertical spreads, straddles, strangles, collars, etc.). All legs are submitted together as a single strategy order.
§Examples
use std::sync::Arc;
use longbridge::{
Config, decimal,
oauth::OAuthBuilder,
trade::{
MultiLegStrategy, OrderSide, OrderType, SubmitMultiLegOrderLeg,
SubmitMultiLegOrderOptions, TradeContext,
},
};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = SubmitMultiLegOrderOptions::new(
OrderSide::Buy,
OrderType::LO,
decimal!(1i32),
MultiLegStrategy::VerticalCallSpread,
[
SubmitMultiLegOrderLeg::new("QQQ260731C764000.US", decimal!(1i32)),
SubmitMultiLegOrderLeg::new("QQQ260731C767000.US", decimal!(1i32)),
],
)
.submitted_price(decimal!(1.5));
let resp = ctx.submit_multileg(opts).await?;
println!("{:?}", resp);Sourcepub async fn cancel_order(
&self,
options: impl Into<CancelOrderOptions>,
) -> Result<()>
pub async fn cancel_order( &self, options: impl Into<CancelOrderOptions>, ) -> Result<()>
Cancel order
Reference: https://open.longbridge.com/en/docs/trade/order/withdraw
§Examples
use std::sync::Arc;
use longbridge::{Config, oauth::OAuthBuilder, trade::TradeContext};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
ctx.cancel_order("709043056541253632").await?;Sourcepub async fn account_balance(
&self,
currency: Option<&str>,
) -> Result<Vec<AccountBalance>>
pub async fn account_balance( &self, currency: Option<&str>, ) -> Result<Vec<AccountBalance>>
Get account balance
AP accounts only: US data-center accounts get a region error; use
us_cash_and_buy_power (or us_asset_overview) instead.
Reference: https://open.longbridge.com/en/docs/trade/asset/account
§Examples
use std::sync::Arc;
use longbridge::{Config, oauth::OAuthBuilder, trade::TradeContext};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.account_balance(None).await?;
println!("{:?}", resp);Sourcepub async fn cash_flow(
&self,
options: GetCashFlowOptions,
) -> Result<Vec<CashFlow>>
pub async fn cash_flow( &self, options: GetCashFlowOptions, ) -> Result<Vec<CashFlow>>
Get cash flow
AP accounts only: US data-center accounts get a region error; use
us_transaction_history instead.
Reference: https://open.longbridge.com/en/docs/trade/asset/cashflow
§Examples
use std::sync::Arc;
use longbridge::{
oauth::OAuthBuilder,
trade::{GetCashFlowOptions, TradeContext},
Config,
};
use time::macros::datetime;
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let opts = GetCashFlowOptions::new(datetime!(2022-05-09 0:00 UTC), datetime!(2022-05-12 0:00 UTC));
let resp = ctx.cash_flow(opts).await?;
println!("{:?}", resp);Sourcepub async fn fund_positions(
&self,
opts: impl Into<Option<GetFundPositionsOptions>>,
) -> Result<FundPositionsResponse>
pub async fn fund_positions( &self, opts: impl Into<Option<GetFundPositionsOptions>>, ) -> Result<FundPositionsResponse>
Get fund positions
Reference: https://open.longbridge.com/en/docs/trade/asset/fund
AP accounts only: US data-center accounts have no fund holdings and get a region error. No US equivalent.
§Examples
use std::sync::Arc;
use longbridge::{Config, oauth::OAuthBuilder, trade::TradeContext};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.fund_positions(None).await?;
println!("{:?}", resp);Sourcepub async fn stock_positions(
&self,
opts: impl Into<Option<GetStockPositionsOptions>>,
) -> Result<StockPositionsResponse>
pub async fn stock_positions( &self, opts: impl Into<Option<GetStockPositionsOptions>>, ) -> Result<StockPositionsResponse>
Get stock positions
AP accounts only: US data-center accounts get a region error; use
us_asset_overview (stock_list) instead.
Reference: https://open.longbridge.com/en/docs/trade/asset/stock
§Examples
use std::sync::Arc;
use longbridge::{Config, oauth::OAuthBuilder, trade::TradeContext};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.stock_positions(None).await?;
println!("{:?}", resp);Sourcepub async fn margin_ratio(
&self,
symbol: impl Into<String>,
) -> Result<MarginRatio>
pub async fn margin_ratio( &self, symbol: impl Into<String>, ) -> Result<MarginRatio>
Get margin ratio
Reference: https://open.longbridge.com/en/docs/trade/asset/margin_ratio
AP accounts only: US data-center accounts get a region error. No US
equivalent; for US margin info see us_cash_and_buy_power
(margin_status).
§Examples
use std::sync::Arc;
use longbridge::{Config, oauth::OAuthBuilder, trade::TradeContext};
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.margin_ratio("700.HK").await?;
println!("{:?}", resp);Sourcepub async fn order_detail(
&self,
options: impl Into<GetOrderDetailOptions>,
) -> Result<OrderDetail>
pub async fn order_detail( &self, options: impl Into<GetOrderDetailOptions>, ) -> Result<OrderDetail>
Get order detail
AP accounts only: US data-center accounts get a region error; use
us_order_detail instead.
Reference: https://open.longbridge.com/en/docs/trade/order/order_detail
§Examples
use std::sync::Arc;
use longbridge::{
Config, Market,
oauth::OAuthBuilder,
trade::{GetHistoryOrdersOptions, OrderSide, OrderStatus, TradeContext},
};
use time::macros::datetime;
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.order_detail("701276261045858304").await?;
println!("{:?}", resp);Sourcepub async fn estimate_max_purchase_quantity(
&self,
opts: EstimateMaxPurchaseQuantityOptions,
) -> Result<EstimateMaxPurchaseQuantityResponse>
pub async fn estimate_max_purchase_quantity( &self, opts: EstimateMaxPurchaseQuantityOptions, ) -> Result<EstimateMaxPurchaseQuantityResponse>
Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options
Reference: https://open.longbridge.com/en/docs/trade/order/estimate_available_buy_limit
§Examples
use std::sync::Arc;
use longbridge::{
Config,
oauth::OAuthBuilder,
trade::{EstimateMaxPurchaseQuantityOptions, OrderSide, OrderType, TradeContext},
};
use time::macros::datetime;
let oauth = OAuthBuilder::new("your-client-id")
.build(|url| println!("Visit: {url}"))
.await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx
.estimate_max_purchase_quantity(EstimateMaxPurchaseQuantityOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
))
.await?;
println!("{:?}", resp);Sourcepub async fn us_query_orders(
&self,
opts: GetUSHistoryOrders,
) -> Result<QueryUSOrdersResponse>
pub async fn us_query_orders( &self, opts: GetUSHistoryOrders, ) -> Result<QueryUSOrdersResponse>
Query the paginated US order list.
Path: POST /v1/us/orders/query
US token required.
Sourcepub async fn us_order_detail(
&self,
order_id: impl Into<String>,
) -> Result<USOrderDetailResponse>
pub async fn us_order_detail( &self, order_id: impl Into<String>, ) -> Result<USOrderDetailResponse>
Get US order detail.
Path: GET /v1/us/orders/{order_id}
US token required.
Sourcepub async fn us_asset_overview(&self) -> Result<USAssetOverview>
pub async fn us_asset_overview(&self) -> Result<USAssetOverview>
Get the full US account asset snapshot (stocks, options, crypto, buying power).
Path: GET /v1/us/assets/overview
US token required.
Sourcepub async fn us_realized_pl(
&self,
opts: GetUSRealizedPLOptions,
) -> Result<USRealizedPL>
pub async fn us_realized_pl( &self, opts: GetUSRealizedPLOptions, ) -> Result<USRealizedPL>
Get realized profit-and-loss for the US account.
currency: required, e.g. "USD".
category: optional filter — "ALL", "STOCK", "OPTION", or
"CRYPTO".
Path: GET /v1/us/assets/pl/realized
US token required.
Sourcepub async fn us_cash_and_buy_power(
&self,
currency: Option<&str>,
) -> Result<USCashAndBuyPower>
pub async fn us_cash_and_buy_power( &self, currency: Option<&str>, ) -> Result<USCashAndBuyPower>
Get cash, buying power and margin status for the US account.
currency: optional, e.g. "USD".
Path: GET /v1/us/assets/cash-and-buy-power
US token required.
Sourcepub async fn us_available_holding(
&self,
opts: GetUSAvailableHoldingOptions,
) -> Result<USAvailableHolding>
pub async fn us_available_holding( &self, opts: GetUSAvailableHoldingOptions, ) -> Result<USAvailableHolding>
Get the quantity of a US holding that is available to trade.
Path: GET /v1/us/assets/available-holding
US token required.
Sourcepub async fn us_transaction_history(
&self,
opts: GetUSTransactionHistoryOptions,
) -> Result<USTransactionHistory>
pub async fn us_transaction_history( &self, opts: GetUSTransactionHistoryOptions, ) -> Result<USTransactionHistory>
Get the US account transaction history (deposits, withdrawals, trades, corporate actions, transfers and so on), one page at a time.
Path: POST /v1/us/assets/transaction-history
US token required.
Sourcepub async fn us_exchange_rates(&self) -> Result<USExchangeRates>
pub async fn us_exchange_rates(&self) -> Result<USExchangeRates>
Get the exchange rates used by the US account.
The upstream two-level map is flattened into
USExchangeRates::exchanges; each entry keeps the two upstream map
keys as universal_key and spec_key.
Path: GET /v1/us/assets/exchange-rates
US token required.
Trait Implementations§
Source§impl Clone for TradeContext
impl Clone for TradeContext
Source§fn clone(&self) -> TradeContext
fn clone(&self) -> TradeContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for TradeContext
impl !UnwindSafe for TradeContext
impl Freeze for TradeContext
impl Send for TradeContext
impl Sync for TradeContext
impl Unpin for TradeContext
impl UnsafeUnpin for TradeContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more