-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplace_order.ts
More file actions
39 lines (30 loc) · 1.05 KB
/
place_order.ts
File metadata and controls
39 lines (30 loc) · 1.05 KB
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
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* Limit Order Example
*
* Place a limit order that rests on the book until filled or cancelled.
*/
import { HyperliquidSDK } from '@quicknode/hyperliquid-sdk';
const PRIVATE_KEY = process.env.PRIVATE_KEY;
if (!PRIVATE_KEY) {
console.error("Set PRIVATE_KEY environment variable");
process.exit(1);
}
async function main() {
const sdk = new HyperliquidSDK(undefined, { privateKey: PRIVATE_KEY });
// Get current price
const mid = await sdk.getMid("BTC");
console.log(`BTC mid price: $${mid.toLocaleString()}`);
// Place limit buy 3% below mid (GTC = Good Till Cancelled)
const limitPrice = Math.floor(mid * 0.97);
const order = await sdk.buy("BTC", { notional: 11, price: limitPrice, tif: "gtc" });
console.log("Placed limit order:");
console.log(` OID: ${order.oid}`);
console.log(` Price: $${limitPrice.toLocaleString()}`);
console.log(` Status: ${order.status}`);
// Clean up - cancel the order
await order.cancel();
console.log("Order cancelled.");
}
main().catch(console.error);