1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
use reqwest::{Url, Client}; use crate::param::{ Parameters, OrderType, Side, TimeInForce, ID }; use crate::builder::ParamBuilder; use crate::types::*; use crate::client::*; /// Client for dealing with orders #[derive(Clone)] pub struct AccountClient { api_key: String, secret_key: String, url: Url, client: Client } impl AccountClient { /// Creates new client instance. /// # Example /// /// ```no_run /// use tokio_binance::{AccountClient, BINANCE_US_URL}; /// /// #[tokio::main] /// async fn main() -> Result<(), Box<dyn std::error::Error>> { /// let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// Ok(()) /// } /// ``` pub fn connect<A, S, U>(api_key: A, secret_key: S, url: U) -> crate::error::Result<Self> where A: Into<String>, S: Into<String>, U: Into<String> { Ok(Self { api_key: api_key.into(), secret_key: secret_key.into(), url: url.into().parse::<Url>()?, client: Client::new() }) } /// Place a new limit order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::{Side::Sell, TimeInForce::Fok, OrderRespType::Full}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// // false will send as test, true will send as a real order. /// .place_limit_order("BNBUSDT", Sell, 20.00, 5.00, false) /// // optional: lifetime of order; default is Gtc. /// .with_time_in_force(Fok) /// // optional: unique id; auto generated by default. /// .with_new_client_order_id("<uuid>") /// // optional: splits quantity; sets time in force to Gtc. /// .with_iceberg_qty(1.00) /// // optional: output verbosity; default is Ack. /// .with_new_order_resp_type(Full) /// // optional: converts Limit to Stop-Limit; triggers when price hits below 21.00. /// .with_stop_loss_limit(21.00) /// // optional: converts Limit to Stop-Limit; triggers when price hits above 21.00. /// .with_take_profit_limit(21.00) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // optional: converts Limit to Limit-Maker; consumes builder and returns a different one. /// .into_limit_maker_order() /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn place_limit_order<'a>( &self, symbol: &'a str, side: Side, price: f64, quantity: f64, execute: bool ) -> ParamBuilder<'a, '_, LimitOrderParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = if execute { url.join("/api/v3/order").unwrap() } else { url.join("/api/v3/order/test").unwrap() }; ParamBuilder::new( Parameters { symbol: Some(symbol), side: Some(side), order_type: Some(OrderType::Limit), price: Some(price), quantity: Some(quantity), time_in_force: Some(TimeInForce::Gtc), ..Parameters::default() }, client.post(url), Some(api_key), Some(secret_key) ) } /// Place a new market order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::{Side::Sell, TimeInForce::Fok, OrderRespType::Full}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// // false will send as test, true will send as a real order. /// .place_market_order("BNBUSDT", Sell, 5.00, false) /// // optional: unique id; auto generated by default. /// .with_new_client_order_id("<uuid>") /// // optional: output verbosity; default is Ack. /// .with_new_order_resp_type(Full) /// // optional: converts Market to Stop-Loss; triggers when price hits below 21.00. /// .with_stop_loss(21.00) /// // optional: converts Market to Stop-Loss; triggers when price hits above 21.00. /// .with_take_profit(21.00) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn place_market_order<'a>( &self, symbol: &'a str, side: Side, quantity: f64, execute: bool ) -> ParamBuilder<'a, '_, MarketOrderParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = if execute { url.join("/api/v3/order").unwrap() } else { url.join("/api/v3/order/test").unwrap() }; ParamBuilder::new( Parameters { symbol: Some(symbol), side: Some(side), order_type: Some(OrderType::Market), quantity: Some(quantity), ..Parameters::default() }, client.post(url), Some(api_key), Some(secret_key) ) } /// Get order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::ID; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .get_order("BNBUSDT", ID::ClientOId("<uuid>")) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_order<'a>(&self, symbol: &'a str, id: ID<'a>) -> ParamBuilder<'a, '_, OrderStatusParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/order").unwrap(); let order_id = if let ID::OrderId(id) = id { Some(id) } else { None }; let orig_client_order_id = if let ID::ClientOId(id) = id { Some(id) } else { None }; ParamBuilder::new( Parameters { symbol: Some(symbol), order_id, orig_client_order_id, ..Parameters::default() }, client.get(url), Some(api_key), Some(secret_key) ) } /// Cancel order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::ID; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .cancel_order("BNBUSDT", ID::ClientOId("<uuid>")) /// // optional: unique id; auto generated by default. /// .with_new_client_order_id("<uuid>") /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn cancel_order<'a>(&self, symbol: &'a str, id: ID<'a>) -> ParamBuilder<'a, '_, CancelOrderParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/order").unwrap(); let order_id = if let ID::OrderId(id) = id { Some(id) } else { None }; let orig_client_order_id = if let ID::ClientOId(id) = id { Some(id) } else { None }; ParamBuilder::new( Parameters { symbol: Some(symbol), order_id, orig_client_order_id, ..Parameters::default() }, client.delete(url), Some(api_key), Some(secret_key) ) } /// Get open orders. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .get_open_orders() /// // optional: filter by symbol; gets all symbols by default. /// .with_symbol("BNBUSDT") /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_open_orders(&self) -> ParamBuilder<'_, '_, OpenOrderParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/openOrders").unwrap(); ParamBuilder::new( Parameters::default(), client.get(url), Some(api_key), Some(secret_key) ) } /// Get all orders. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use chrono::{Utc, Duration}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let end = Utc::now(); /// let start = end - Duration::hours(23); /// /// let response = client /// .get_all_orders("BNBUSDT") /// // optional: filter by orders greater than or equal to the provided id. /// // If supplied, neither startTime or endTime can be provided /// .with_order_id(1230494) /// // optional: get orders from; pass 24 hours of orders is the default. /// .with_start_time(start) /// // optional: get orders until; default is now. /// .with_end_time(end) /// // optional: limit the amount of orders; default 500; max 1000. /// .with_limit(100) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_all_orders<'a>(&self, symbol: &'a str) -> ParamBuilder<'a, '_, AllOrdersParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/allOrders").unwrap(); ParamBuilder::new( Parameters { symbol: Some(symbol), ..Parameters::default() }, client.get(url), Some(api_key), Some(secret_key) ) } /// Place a new oco order. /// # Price Restrictions: /// - SELL: Limit Price > Last Price > Stop Price /// - BUY: Limit Price < Last Price < Stop Price /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::{Side::Sell, TimeInForce::Gtc, OrderRespType::Full}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// // Limit to sell at 30.00 and Stop-Loss at 20.00; One cancels the other. /// .place_oco_order("BNBUSDT", Sell, 30.00, 20.00, 5.00) /// // optional: A unique Id for the entire orderList; auto generated by default. /// .with_list_client_order_id("<uuid>") /// // optional: A unique Id for the limit order; auto generated by default. /// .with_limit_client_order_id("<uuid>") /// // optional: splits quantity for the limit order leg; /// .with_limit_iceberg_qty(1.00) /// // optional: A unique Id for the stop loss/stop loss limit leg; auto generated by default. /// .with_stop_client_order_id("<uuid>") /// // optional: Converts Stop-Loss to Stop-Limit; triggers bellow 20.00. /// .with_stop_limit_price(19.00, Gtc) /// // optional: splits quantity for the stop order leg; /// .with_stop_iceberg_qty(1.00) /// // optional: output verbosity; default is Ack. /// .with_new_order_resp_type(Full) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn place_oco_order<'a>( &self, symbol: &'a str, side: Side, price: f64, stop_price: f64, quantity: f64, ) -> ParamBuilder<'a, '_, OcoParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/order/oco").unwrap(); ParamBuilder::new( Parameters { symbol: Some(symbol), side: Some(side), price: Some(price), stop_price: Some(stop_price), quantity: Some(quantity), ..Parameters::default() }, client.post(url), Some(api_key), Some(secret_key) ) } /// Cancel oco order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::ID; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .cancel_oco_order("BNBUSDT", ID::ClientOId("<uuid>")) /// // optional: unique id; auto generated by default. /// .with_new_client_order_id("<uuid>") /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn cancel_oco_order<'a>(&self, symbol: &'a str, id: ID<'a>) -> ParamBuilder<'a, '_, CancelOcoParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/orderList").unwrap(); let order_list_id = if let ID::OrderId(id) = id { Some(id) } else { None }; let list_client_order_id = if let ID::ClientOId(id) = id { Some(id) } else { None }; ParamBuilder::new( Parameters { symbol: Some(symbol), order_list_id, list_client_order_id, ..Parameters::default() }, client.delete(url), Some(api_key), Some(secret_key) ) } /// Get oco order. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use tokio_binance::ID; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .get_oco_order(ID::ClientOId("<uuid>")) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_oco_order<'a>(&self, id: ID<'a>) -> ParamBuilder<'a, '_, OcoStatusParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/orderList").unwrap(); let order_list_id = if let ID::OrderId(id) = id { Some(id) } else { None }; let orig_client_order_id = if let ID::ClientOId(id) = id { Some(id) } else { None }; ParamBuilder::new( Parameters { order_list_id, orig_client_order_id, ..Parameters::default() }, client.get(url), Some(api_key), Some(secret_key) ) } /// Get all oco orders. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use chrono::{Utc, Duration}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let end = Utc::now(); /// let start = end - Duration::hours(23); /// /// let response = client /// .get_all_oco_orders() /// // optional: filter by orders greater than or equal to the provided id. /// // If supplied, neither startTime or endTime can be provided /// .with_from_id(1230494) /// // optional: get orders from; pass 24 hours of orders is the default. /// .with_start_time(start) /// // optional: get orders until; default is now. /// .with_end_time(end) /// // optional: limit the amount of orders; default 500; max 1000. /// .with_limit(100) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_all_oco_orders(&self) -> ParamBuilder<'_, '_, AllOcoParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/allOrderList").unwrap(); ParamBuilder::new( Parameters::default(), client.get(url), Some(api_key), Some(secret_key) ) } /// Get open oco orders. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .get_open_oco_orders() /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_open_oco_orders(&self) -> ParamBuilder<'_, '_, OpenOcoParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/openOrderList").unwrap(); ParamBuilder::new( Parameters::default(), client.get(url), Some(api_key), Some(secret_key) ) } /// Get current account information. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let response = client /// .get_account() /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_account(&self) -> ParamBuilder<'_, '_, AccountParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/account").unwrap(); ParamBuilder::new( Parameters::default(), client.get(url), Some(api_key), Some(secret_key) ) } /// Get trades for a specific account and symbol. /// # Example /// /// ```no_run /// # use tokio_binance::{AccountClient, BINANCE_US_URL}; /// use chrono::{Utc, Duration}; /// use serde_json::Value; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box<dyn std::error::Error>> { /// # let client = AccountClient::connect("<api-key>", "<secret-key>", BINANCE_US_URL)?; /// let end = Utc::now(); /// let start = end - Duration::hours(23); /// /// let response = client /// .get_account_trades("BNBUSDT") /// // optional: filter by orders greater than or equal to the provided id. /// // If supplied, neither startTime or endTime can be provided /// .with_from_id(1230494) /// // optional: get orders from; pass 24 hours of orders is the default. /// .with_start_time(start) /// // optional: get orders until; default is now. /// .with_end_time(end) /// // optional: limit the amount of orders; default 500; max 1000. /// .with_limit(100) /// // optional: processing time for request; default is 5000, can't be above 60000. /// .with_recv_window(8000) /// // /// .json::<Value>() /// .await?; /// # Ok(()) /// # } /// ``` pub fn get_account_trades<'a>(&self, symbol: &'a str) -> ParamBuilder<'a, '_, AccountTradesParams>{ let Self { ref api_key, ref secret_key, url, client } = self; let url = url.join("/api/v3/myTrades").unwrap(); ParamBuilder::new( Parameters { symbol: Some(symbol), ..Parameters::default() }, client.get(url), Some(api_key), Some(secret_key) ) } /// Helper method for getting a withdraw client instance. pub fn to_withdraw_client(&self) -> WithdrawalClient { WithdrawalClient { api_key: self.api_key.clone(), secret_key: self.secret_key.clone(), url: self.url.clone(), client: self.client.clone() } } /// Helper method for getting a market client instance. pub fn to_market_data_client(&self) -> MarketDataClient { MarketDataClient { api_key: self.api_key.clone(), url: self.url.clone(), client: self.client.clone() } } /// Helper method for getting a general client instance. pub fn to_general_client(&self) -> GeneralClient { GeneralClient { url: self.url.clone(), client: self.client.clone() } } }