-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrigger_orders.ts
More file actions
67 lines (56 loc) · 2.21 KB
/
trigger_orders.ts
File metadata and controls
67 lines (56 loc) · 2.21 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
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
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* Trigger Orders Example
*
* Stop loss and take profit orders.
*
* Requires: PRIVATE_KEY environment variable
*/
import { HyperliquidSDK, Side } from '@quicknode/hyperliquid-sdk';
const PRIVATE_KEY = process.env.PRIVATE_KEY;
if (!PRIVATE_KEY) {
console.error("Set PRIVATE_KEY environment variable");
console.error("Example: export PRIVATE_KEY='0x...'");
process.exit(1);
}
async function main() {
const sdk = new HyperliquidSDK(undefined, { privateKey: PRIVATE_KEY });
const mid = await sdk.getMid("BTC");
console.log(`BTC mid: $${mid.toLocaleString()}`);
// Stop loss order (market) - triggers when price falls below stop price
// No limitPrice means market order when triggered
// const result = await sdk.stopLoss("BTC", {
// size: 0.001,
// triggerPrice: mid * 0.95, // 5% below current
// });
// console.log(`Stop loss (market): ${result}`);
// Stop loss order (limit) - triggers and places limit order at limitPrice
// const result = await sdk.stopLoss("BTC", {
// size: 0.001,
// triggerPrice: mid * 0.95,
// limitPrice: mid * 0.94,
// });
// console.log(`Stop loss (limit): ${result}`);
// Take profit order (market) - triggers when price rises above trigger
// const result = await sdk.takeProfit("BTC", {
// size: 0.001,
// triggerPrice: mid * 1.05, // 5% above current
// });
// console.log(`Take profit (market): ${result}`);
// Take profit order (limit)
// const result = await sdk.takeProfit("BTC", {
// size: 0.001,
// triggerPrice: mid * 1.05,
// limitPrice: mid * 1.06,
// });
// console.log(`Take profit (limit): ${result}`);
// For buy-side stop/TP (e.g., closing a short position), use side=Side.BUY
// const result = await sdk.stopLoss("BTC", { size: 0.001, triggerPrice: mid * 1.05, side: Side.BUY });
console.log("\nTrigger order methods available:");
console.log(" sdk.stopLoss(asset, { size, triggerPrice, limitPrice?, side? })");
console.log(" sdk.takeProfit(asset, { size, triggerPrice, limitPrice?, side? })");
console.log(" sdk.triggerOrder(TriggerOrder(...))");
console.log("\nNote: Omit limitPrice for market orders when triggered");
}
main().catch(console.error);