pub struct Client { /* private fields */ }Expand description
客户端
Implementations§
Source§impl Client
impl Client
Sourcepub async fn new(
username: &str,
password: &str,
config: ClientConfig,
) -> Result<Self>
pub async fn new( username: &str, password: &str, config: ClientConfig, ) -> Result<Self>
创建新的客户端(使用默认配置)
这是一个便捷方法,等同于:
let client = ClientBuilder::new("username", "password")
.config(ClientConfig::default())
.build()
.await?;如需更多配置选项,请使用 ClientBuilder
Sourcepub fn builder(
username: impl Into<String>,
password: impl Into<String>,
) -> ClientBuilder
pub fn builder( username: impl Into<String>, password: impl Into<String>, ) -> ClientBuilder
创建客户端构建器
§示例
let client = Client::builder("username", "password")
.log_level("debug")
.view_width(5000)
.development(true)
.build()
.await?;Sourcepub async fn init_market(&mut self) -> Result<()>
pub async fn init_market(&mut self) -> Result<()>
初始化行情功能
Sourcepub async fn set_auth<A: Authenticator + 'static>(&mut self, auth: A)
pub async fn set_auth<A: Authenticator + 'static>(&mut self, auth: A)
设置认证器
允许在运行时更换认证器,例如切换账号或更新 token
§注意
- 更换认证器后,需要重新调用
init_market()来使用新的认证信息 - 已创建的
SeriesAPI和TradeSession仍使用旧的认证器
§示例
let mut client = Client::new("user1", "pass1", ClientConfig::default()).await?;
// 切换到另一个账号
let mut new_auth = TqAuth::new("user2".to_string(), "pass2".to_string());
new_auth.login().await?;
client.set_auth(new_auth).await;
// 重新初始化行情功能
client.init_market().await?;Sourcepub async fn get_auth(&self) -> RwLockReadGuard<'_, dyn Authenticator>
pub async fn get_auth(&self) -> RwLockReadGuard<'_, dyn Authenticator>
获取认证器的只读引用
用于检查当前的认证状态或权限
§示例
let auth = client.get_auth().await;
if auth.has_feature("futr") {
println!("有期货权限");
}Sourcepub async fn subscribe_quote(
&self,
symbols: &[&str],
) -> Result<Arc<QuoteSubscription>>
pub async fn subscribe_quote( &self, symbols: &[&str], ) -> Result<Arc<QuoteSubscription>>
订阅 Quote
Sourcepub async fn create_trade_session(
&self,
broker: &str,
user_id: &str,
password: &str,
) -> Result<Arc<TradeSession>>
pub async fn create_trade_session( &self, broker: &str, user_id: &str, password: &str, ) -> Result<Arc<TradeSession>>
创建交易会话(不自动连接)
§重要提示
由于 TradeSession 使用 broadcast 队列,建议按以下顺序使用:
// 1. 创建会话(不连接)
let session = client.create_trade_session("simnow", "user_id", "password").await?;
// 2. 先注册回调或订阅 channel(避免丢失消息)
session.on_account(|account| {
println!("账户: {}", account.balance);
}).await;
// 3. 最后连接
session.connect().await?;§参数
broker- 期货公司代码(如 “simnow”)user_id- 用户账号password- 密码
§返回
返回 TradeSession 实例,需要手动调用 connect() 连接
Sourcepub async fn register_trade_session(
&self,
key: &str,
session: Arc<TradeSession>,
)
pub async fn register_trade_session( &self, key: &str, session: Arc<TradeSession>, )
注册交易会话
Sourcepub async fn get_trade_session(&self, key: &str) -> Option<Arc<TradeSession>>
pub async fn get_trade_session(&self, key: &str) -> Option<Arc<TradeSession>>
获取交易会话
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
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
Mutably borrows from an owned value. Read more