pub struct TradeClient { /* private fields */ }Expand description
Places, amends and cancels orders over the order entry stream
(/v5/trade), matching replies to requests by reqId.
Built on Stream: it authenticates on every (re)connect with the
credentials and ServerClock of the Config; requests are accepted
once Event::Authenticated arrived. Requests in flight when the
connection drops fail with Error::ConnectionLost and are not resent:
check the order (e.g. by orderLinkId) before retrying.
The order entry stream is not available on demo trading; use testnet.
use bybit::{
Category, Environment, OrderType, Side,
http::PlaceOrderRequest,
ws::{self, TradeClient},
};
use rust_decimal::dec;
let config = ws::Config::trade(Environment::Testnet)?.credentials("API_KEY", "API_SECRET");
let (trade, mut events) = TradeClient::new(config)?;
trade.connect().await?;
while let Some(event) = events.recv().await {
if matches!(event, ws::Event::Authenticated) {
break;
}
}
let mut order = PlaceOrderRequest::new(
Category::Linear,
"BTCUSDT",
Side::Buy,
OrderType::Limit,
dec!(0.001),
);
order.price = Some(dec!(10000));
let reply = trade.place_order(&order).await?;
println!("order id: {}", reply.data.order_id);Implementations§
Source§impl TradeClient
impl TradeClient
Sourcepub fn new(config: Config) -> Result<(Self, Receiver<Event>), Error>
pub fn new(config: Config) -> Result<(Self, Receiver<Event>), Error>
Spawn the stream driver (see Stream::new) and the reply router.
Returns the client and the events that are not replies to its
requests. Fails without credentials: the order entry stream requires
authentication.
Sourcepub async fn connect(&self) -> Result<(), Error>
pub async fn connect(&self) -> Result<(), Error>
Open the connection; requests are accepted after
Event::Authenticated.
Sourcepub async fn disconnect(&self) -> Result<(), Error>
pub async fn disconnect(&self) -> Result<(), Error>
Close the connection.
Sourcepub async fn place_order(
&self,
request: &PlaceOrderRequest,
) -> Result<TradeResponse<PlaceOrderResponse>, Error>
pub async fn place_order( &self, request: &PlaceOrderRequest, ) -> Result<TradeResponse<PlaceOrderResponse>, Error>
Create an order (order.create).
Sourcepub async fn amend_order(
&self,
request: &AmendOrderRequest,
) -> Result<TradeResponse<AmendOrderResponse>, Error>
pub async fn amend_order( &self, request: &AmendOrderRequest, ) -> Result<TradeResponse<AmendOrderResponse>, Error>
Amend an order (order.amend).
Sourcepub async fn cancel_order(
&self,
request: &CancelOrderRequest,
) -> Result<TradeResponse<CancelOrderResponse>, Error>
pub async fn cancel_order( &self, request: &CancelOrderRequest, ) -> Result<TradeResponse<CancelOrderResponse>, Error>
Cancel an order (order.cancel).
Sourcepub async fn place_order_batch(
&self,
request: &PlaceOrderBatchRequest,
) -> Result<TradeResponse<List<PlaceOrderBatchResult>>, Error>
pub async fn place_order_batch( &self, request: &PlaceOrderBatchRequest, ) -> Result<TradeResponse<List<PlaceOrderBatchResult>>, Error>
Create orders in a batch (order.create-batch); per-order results are
in TradeResponse::ret_ext_info.
Sourcepub async fn amend_order_batch(
&self,
request: &AmendOrderBatchRequest,
) -> Result<TradeResponse<List<AmendOrderBatchResult>>, Error>
pub async fn amend_order_batch( &self, request: &AmendOrderBatchRequest, ) -> Result<TradeResponse<List<AmendOrderBatchResult>>, Error>
Amend orders in a batch (order.amend-batch).
Sourcepub async fn cancel_order_batch(
&self,
request: &CancelOrderBatchRequest,
) -> Result<TradeResponse<List<CancelOrderBatchResult>>, Error>
pub async fn cancel_order_batch( &self, request: &CancelOrderBatchRequest, ) -> Result<TradeResponse<List<CancelOrderBatchResult>>, Error>
Cancel orders in a batch (order.cancel-batch).