-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfo_candles.ts
More file actions
83 lines (73 loc) · 2.51 KB
/
info_candles.ts
File metadata and controls
83 lines (73 loc) · 2.51 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* Historical Candles Example
*
* Shows how to fetch historical candlestick (OHLCV) data.
*
* Note: candleSnapshot may not be available on all QuickNode endpoints.
* Check the QuickNode docs for method availability.
*
* Usage:
* export QUICKNODE_ENDPOINT="https://your-endpoint.hype-mainnet.quiknode.pro/YOUR_TOKEN"
* npx ts-node info_candles.ts
*/
import { HyperliquidSDK } from '@quicknode/hyperliquid-sdk';
const ENDPOINT = process.env.QUICKNODE_ENDPOINT;
if (!ENDPOINT) {
console.error("Set QUICKNODE_ENDPOINT environment variable");
process.exit(1);
}
async function main() {
// Single SDK instance — access everything through sdk.info, sdk.core, sdk.evm, etc.
const sdk = new HyperliquidSDK(ENDPOINT);
const info = sdk.info;
console.log("=".repeat(50));
console.log("Historical Candles");
console.log("=".repeat(50));
// Last 24 hours
const now = Date.now();
const dayAgo = now - (24 * 60 * 60 * 1000);
// Fetch BTC 1-hour candles
console.log("\n1. BTC 1-Hour Candles (last 24h):");
try {
const candles = await info.candles("BTC", "1h", dayAgo, now);
console.log(` Retrieved ${candles.length} candles`);
if (candles.length > 0) {
for (const c of candles.slice(-3)) {
console.log(` O:${c.o} H:${c.h} L:${c.l} C:${c.c}`);
}
}
} catch (e) {
console.log(` Error: ${e}`);
console.log(" Note: candleSnapshot may not be available on this endpoint");
}
// Predicted funding rates (supported on QuickNode)
console.log("\n2. Predicted Funding Rates:");
try {
const fundings = await info.predictedFundings();
console.log(` ${fundings.length} assets with funding rates:`);
// Structure: [[coin, [[source, {fundingRate, ...}], ...]], ...]
for (const item of fundings.slice(0, 5)) {
if (Array.isArray(item) && item.length >= 2) {
const coin = item[0];
const sources = item[1];
if (sources && Array.isArray(sources) && sources.length > 0) {
// Get HlPerp funding rate if available
for (const src of sources) {
if (Array.isArray(src) && src.length >= 2 && src[0] === "HlPerp") {
const rate = parseFloat(src[1].fundingRate || "0") * 100;
console.log(` ${coin}: ${rate.toFixed(4)}%`);
break;
}
}
}
}
}
} catch (e) {
console.log(` Error: ${e}`);
}
console.log("\n" + "=".repeat(50));
console.log("Done!");
}
main().catch(console.error);