Skip to content

Commit d95ea74

Browse files
committed
last changes'
1 parent 7ff7b17 commit d95ea74

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

python/hip3_order.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
print(f"Could not fetch {COIN} mid price, using fallback")
1111
mid = 78.0
1212

13-
sz = round(10.0 / mid, 2)
13+
sz = round(11.0 / mid, 2)
1414
buy_px = round(mid * 1.03, 2)
1515

1616
print(f"{COIN} mid: ${mid:,.2f}")

rust/examples/modify_order.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use hyperliquid_api_examples::Client;
2+
use serde_json::json;
3+
4+
const COIN: &str = "BTC";
5+
6+
#[tokio::main]
7+
async fn main() {
8+
let client = Client::from_env();
9+
10+
let mid = client.get_mid(COIN).await;
11+
if mid == 0.0 {
12+
eprintln!("Could not fetch {COIN} mid price");
13+
std::process::exit(1);
14+
}
15+
16+
let sz = format!("{:.5}", 11.0 / mid);
17+
let rest_px = format!("{}", (mid * 0.97) as u64);
18+
19+
println!("{COIN} mid: ${mid:.2}");
20+
println!("Placing resting BUY {sz} @ {rest_px} (GTC, 3% below mid)\n");
21+
22+
// Place resting order
23+
let res = client
24+
.exchange(&json!({
25+
"action": {
26+
"type": "order",
27+
"orders": [{"asset": COIN, "side": "buy", "price": rest_px, "size": sz, "tif": "gtc"}],
28+
},
29+
}))
30+
.await;
31+
32+
let hash = res["hash"].as_str().unwrap();
33+
let sig = client.sign_hash(hash).await;
34+
35+
let result = client
36+
.exchange(&json!({
37+
"action": res["action"],
38+
"nonce": res["nonce"],
39+
"signature": sig,
40+
}))
41+
.await;
42+
43+
let exchange_resp = &result["exchangeResponse"];
44+
let statuses = exchange_resp["response"]["data"]["statuses"]
45+
.as_array()
46+
.expect("No statuses in response");
47+
48+
let oid = statuses
49+
.iter()
50+
.find_map(|s| s["resting"]["oid"].as_u64())
51+
.expect("Could not extract OID from resting order");
52+
53+
let new_px = format!("{}", (mid * 0.96) as u64);
54+
println!("Order resting (OID: {oid})");
55+
println!("Modifying price: {rest_px} -> {new_px}\n");
56+
57+
// Modify order
58+
let modify_action = json!({
59+
"type": "batchModify",
60+
"modifies": [{
61+
"oid": oid,
62+
"order": {"asset": COIN, "side": "buy", "price": new_px, "size": sz, "tif": "gtc"},
63+
}],
64+
});
65+
66+
let res = client.exchange(&json!({"action": modify_action})).await;
67+
68+
let hash = res["hash"].as_str().unwrap();
69+
let sig = client.sign_hash(hash).await;
70+
71+
let modify_result = client
72+
.exchange(&json!({
73+
"action": modify_action,
74+
"nonce": res["nonce"],
75+
"signature": sig,
76+
}))
77+
.await;
78+
79+
println!(
80+
"{}",
81+
serde_json::to_string_pretty(&modify_result["exchangeResponse"]).unwrap()
82+
);
83+
println!("\nOrder modified.");
84+
}

0 commit comments

Comments
 (0)