-
Notifications
You must be signed in to change notification settings - Fork 11
/
buy_and_cancel.rs
88 lines (74 loc) · 2.36 KB
/
buy_and_cancel.rs
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
use anyhow::Error;
use fehler::throws;
use futures::{SinkExt, StreamExt};
use okex::websocket::{Channel, Command, OkExWebsocket};
use okex::{
enums::{InstType, Side, TdMode},
rest::{
AmendOrderRequest, BalanceRequest, CancelOrderRequest, OkExRest, OrderDetailsRequest,
PlaceOrderRequest,
},
};
use std::env::var;
use std::time::Duration;
use tokio::time::sleep;
#[throws(Error)]
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
env_logger::init();
let client = OkExRest::with_credential(
&var("OKEX_KEY")?,
&var("OKEX_SECRET")?,
&var("OKEX_PASSPHRASE")?,
);
let jh = tokio::spawn(async {
let mut client = OkExWebsocket::with_credential(
&var("OKEX_KEY")?,
&var("OKEX_SECRET")?,
&var("OKEX_PASSPHRASE")?,
)
.await?;
client.send(Command::login(&client)?).await?;
client.next().await;
client
.send(Command::subscribe(vec![Channel::Orders {
inst_type: InstType::Any,
uly: None,
inst_id: Some("IOTA-USDT".to_string()),
}]))
.await?;
while let Some(m) = client.next().await {
println!("Subscription: {:?}", m);
}
Result::<(), Error>::Ok(())
});
sleep(Duration::from_secs(3)).await;
let [resp] = client.request(BalanceRequest::multiple(&["BTC"])).await?;
println!("{:?}", resp);
let mut req = PlaceOrderRequest::limit("IOTA-USDT", TdMode::Cross, Side::Buy, 1.5, 10.);
req.set_ccy("USDT");
let [resp] = client.request(req).await?;
println!("{:?}", resp);
let ord_id = resp.ord_id;
let [resp] = client
.request(OrderDetailsRequest::ord_id("IOTA-USDT", &ord_id))
.await?;
println!("{:?}", resp);
let req = AmendOrderRequest::new_qty("IOTA-USDT", &ord_id, 20.);
let resp = client.request(req).await?;
println!("{:?}", resp);
let [resp] = client
.request(OrderDetailsRequest::ord_id("IOTA-USDT", &ord_id))
.await?;
println!("{:?}", resp);
let resp = client
.request(CancelOrderRequest::with_ord_id("IOTA-USDT", &ord_id))
.await?;
println!("{:?}", resp);
let [resp] = client
.request(OrderDetailsRequest::ord_id("IOTA-USDT", &ord_id))
.await?;
println!("{:?}", resp);
jh.await?
}