-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrading_example.py
More file actions
135 lines (104 loc) · 3.82 KB
/
trading_example.py
File metadata and controls
135 lines (104 loc) · 3.82 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Trading Example — Place orders on Hyperliquid.
This example shows how to place market and limit orders using the SDK.
Requirements:
pip install hyperliquid-sdk
Usage:
export ENDPOINT="https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN"
export PRIVATE_KEY="0x..."
python trading_example.py
"""
import os
import sys
from hyperliquid_sdk import HyperliquidSDK, Order
# Get endpoint and private key from environment
ENDPOINT = os.environ.get("ENDPOINT") or os.environ.get("QUICKNODE_ENDPOINT")
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")
if not ENDPOINT:
print("Error: Set ENDPOINT environment variable")
print(" export ENDPOINT='https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN'")
sys.exit(1)
if not PRIVATE_KEY:
print("Error: Set PRIVATE_KEY environment variable")
print(" export PRIVATE_KEY='0xYourPrivateKey'")
sys.exit(1)
def main():
print("Hyperliquid Trading Example")
print("=" * 50)
# Initialize SDK with endpoint and private key
sdk = HyperliquidSDK(ENDPOINT, private_key=PRIVATE_KEY)
print(f"Address: {sdk.address}")
print(f"Endpoint: {ENDPOINT[:50]}...")
print()
# ==========================================================================
# Example 1: Market Buy $100 of BTC
# ==========================================================================
print("Example 1: Market Buy")
print("-" * 30)
try:
order = sdk.market_buy("BTC", notional=100)
print(f"Order placed: {order}")
print(f" Order ID: {order.oid}")
print(f" Status: {order.status}")
print(f" Filled: {order.filled_size} @ avg {order.avg_price}")
except Exception as e:
print(f" Error: {e}")
print()
# ==========================================================================
# Example 2: Limit Order
# ==========================================================================
print("Example 2: Limit Order")
print("-" * 30)
try:
# Build limit order using fluent API
order = Order.buy("ETH").size(0.1).price(2000.0).gtc()
# Place order
result = sdk.order(order)
print(f"Order placed: {result}")
print(f" Order ID: {result.oid}")
print(f" Status: {result.status}")
except Exception as e:
print(f" Error: {e}")
print()
# ==========================================================================
# Example 3: Stop Loss Order
# ==========================================================================
print("Example 3: Stop Loss Order")
print("-" * 30)
try:
# Build stop loss using fluent API
order = Order.sell("BTC").size(0.01).price(59900.0).reduce_only()
result = sdk.order(order)
print(f"Stop loss placed: {result}")
except Exception as e:
print(f" Error: {e}")
print()
# ==========================================================================
# Example 4: Cancel Orders
# ==========================================================================
print("Example 4: Cancel All Orders")
print("-" * 30)
try:
# Cancel all BTC orders
sdk.cancel_all("BTC")
print("Cancelled all BTC orders")
except Exception as e:
print(f" Error: {e}")
print()
# ==========================================================================
# Example 5: Close Position
# ==========================================================================
print("Example 5: Close Position")
print("-" * 30)
try:
# Market close BTC position
result = sdk.close_position("BTC")
print(f"Position closed: {result}")
except Exception as e:
print(f" Error: {e}")
print()
print("=" * 50)
print("Done!")
if __name__ == "__main__":
main()