Struct longport::trade::TradeContext
source · pub struct TradeContext { /* private fields */ }
Expand description
Trade context
Implementations§
source§impl TradeContext
impl TradeContext
sourcepub async fn try_new(
config: Arc<Config>
) -> Result<(Self, UnboundedReceiver<PushEvent>)>
pub async fn try_new( config: Arc<Config> ) -> Result<(Self, UnboundedReceiver<PushEvent>)>
Create a 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.longportapp.com/en/docs/trade/trade-push#subscribe
§Examples
use std::sync::Arc;
use longport::{
decimal,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType, TradeContext},
Config,
};
let config = Arc::new(Config::from_env()?);
let (ctx, mut receiver) = TradeContext::try_new(config).await?;
let opts = SubmitOrderOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
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.longportapp.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
Reference: https://open.longportapp.com/en/docs/trade/execution/history_executions
§Examples
use std::sync::Arc;
use longport::{
trade::{GetHistoryExecutionsOptions, TradeContext},
Config,
};
use time::macros::datetime;
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/execution/today_executions
§Examples
use std::sync::Arc;
use longport::{
trade::{GetTodayExecutionsOptions, TradeContext},
Config,
};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
let opts = GetTodayExecutionsOptions::new().symbol("700.HK");
let resp = ctx.today_executions(opts).await?;
println!("{:?}", resp);
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.longportapp.com/en/docs/trade/order/history_orders
§Examples
use std::sync::Arc;
use longport::{
trade::{GetHistoryOrdersOptions, OrderSide, OrderStatus, TradeContext},
Config, Market,
};
use time::macros::datetime;
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/order/today_orders
§Examples
use std::sync::Arc;
use longport::{
trade::{GetTodayOrdersOptions, OrderSide, OrderStatus, TradeContext},
Config, Market,
};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/order/replace
§Examples
use std::sync::Arc;
use longport::{
decimal,
trade::{ReplaceOrderOptions, TradeContext},
Config,
};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
let opts = ReplaceOrderOptions::new("709043056541253632", 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.longportapp.com/en/docs/trade/order/submit
§Examples
use std::sync::Arc;
use longport::{
decimal,
trade::{OrderSide, OrderType, SubmitOrderOptions, TimeInForceType, TradeContext},
Config,
};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
let opts = SubmitOrderOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
200,
TimeInForceType::Day,
)
.submitted_price(decimal!(50i32));
let resp = ctx.submit_order(opts).await?;
println!("{:?}", resp);
sourcepub async fn cancel_order(&self, order_id: impl Into<String>) -> Result<()>
pub async fn cancel_order(&self, order_id: impl Into<String>) -> Result<()>
Cancel order
Reference: https://open.longportapp.com/en/docs/trade/order/withdraw
§Examples
use std::sync::Arc;
use longport::{trade::TradeContext, Config};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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
Reference: https://open.longportapp.com/en/docs/trade/asset/account
§Examples
use std::sync::Arc;
use longport::{trade::TradeContext, Config};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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
Reference: https://open.longportapp.com/en/docs/trade/asset/cashflow
§Examples
use std::sync::Arc;
use longport::{
trade::{GetCashFlowOptions, TradeContext},
Config,
};
use time::macros::datetime;
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/asset/fund
§Examples
use std::sync::Arc;
use longport::{trade::TradeContext, Config};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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
Reference: https://open.longportapp.com/en/docs/trade/asset/stock
§Examples
use std::sync::Arc;
use longport::{trade::TradeContext, Config};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/asset/margin_ratio
§Examples
use std::sync::Arc;
use longport::{trade::TradeContext, Config};
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
let resp = ctx.margin_ratio("700.HK").await?;
println!("{:?}", resp);
sourcepub async fn order_detail(
&self,
order_id: impl Into<String>
) -> Result<OrderDetail>
pub async fn order_detail( &self, order_id: impl Into<String> ) -> Result<OrderDetail>
Get order detail
Reference: https://open.longportapp.com/en/docs/trade/order/order_detail
§Examples
use std::sync::Arc;
use longport::{
trade::{GetHistoryOrdersOptions, OrderSide, OrderStatus, TradeContext},
Config, Market,
};
use time::macros::datetime;
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
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.longportapp.com/en/docs/trade/order/estimate_available_buy_limit
§Examples
use std::sync::Arc;
use longport::{
trade::{EstimateMaxPurchaseQuantityOptions, OrderSide, OrderType, TradeContext},
Config,
};
use time::macros::datetime;
let config = Arc::new(Config::from_env()?);
let (ctx, _) = TradeContext::try_new(config).await?;
let resp = ctx
.estimate_max_purchase_quantity(EstimateMaxPurchaseQuantityOptions::new(
"700.HK",
OrderType::LO,
OrderSide::Buy,
))
.await?;
println!("{:?}", resp);
Trait Implementations§
source§impl Clone for TradeContext
impl Clone for TradeContext
source§fn clone(&self) -> TradeContext
fn clone(&self) -> TradeContext
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more