Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

客户端

Implementations§

Source§

impl Client

Source

pub async fn new( username: &str, password: &str, config: ClientConfig, ) -> Result<Self>

创建新的客户端(使用默认配置)

这是一个便捷方法,等同于:

let client = ClientBuilder::new("username", "password")
    .config(ClientConfig::default())
    .build()
    .await?;

如需更多配置选项,请使用 ClientBuilder

Source

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?;
Source

pub async fn init_market(&mut self) -> Result<()>

初始化行情功能

Source

pub async fn set_auth<A: Authenticator + 'static>(&mut self, auth: A)

设置认证器

允许在运行时更换认证器,例如切换账号或更新 token

§注意
  • 更换认证器后,需要重新调用 init_market() 来使用新的认证信息
  • 已创建的 SeriesAPITradeSession 仍使用旧的认证器
§示例
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?;
Source

pub async fn get_auth(&self) -> RwLockReadGuard<'_, dyn Authenticator>

获取认证器的只读引用

用于检查当前的认证状态或权限

§示例
let auth = client.get_auth().await;
if auth.has_feature("futr") {
    println!("有期货权限");
}
Source

pub fn series(&self) -> Result<Arc<SeriesAPI>>

获取 Series API

Source

pub async fn subscribe_quote( &self, symbols: &[&str], ) -> Result<Arc<QuoteSubscription>>

订阅 Quote

Source

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() 连接

Source

pub async fn register_trade_session( &self, key: &str, session: Arc<TradeSession>, )

注册交易会话

Source

pub async fn get_trade_session(&self, key: &str) -> Option<Arc<TradeSession>>

获取交易会话

Source

pub async fn close(&self) -> Result<()>

关闭客户端

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more