hyperquant 1.48__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of hyperquant might be problematic. Click here for more details.

Files changed (42) hide show
  1. hyperquant/__init__.py +8 -0
  2. hyperquant/broker/auth.py +972 -0
  3. hyperquant/broker/bitget.py +311 -0
  4. hyperquant/broker/bitmart.py +720 -0
  5. hyperquant/broker/coinw.py +487 -0
  6. hyperquant/broker/deepcoin.py +651 -0
  7. hyperquant/broker/edgex.py +500 -0
  8. hyperquant/broker/hyperliquid.py +570 -0
  9. hyperquant/broker/lbank.py +661 -0
  10. hyperquant/broker/lib/edgex_sign.py +455 -0
  11. hyperquant/broker/lib/hpstore.py +252 -0
  12. hyperquant/broker/lib/hyper_types.py +48 -0
  13. hyperquant/broker/lib/polymarket/ctfAbi.py +721 -0
  14. hyperquant/broker/lib/polymarket/safeAbi.py +1138 -0
  15. hyperquant/broker/lib/util.py +22 -0
  16. hyperquant/broker/lighter.py +679 -0
  17. hyperquant/broker/models/apexpro.py +150 -0
  18. hyperquant/broker/models/bitget.py +359 -0
  19. hyperquant/broker/models/bitmart.py +635 -0
  20. hyperquant/broker/models/coinw.py +724 -0
  21. hyperquant/broker/models/deepcoin.py +809 -0
  22. hyperquant/broker/models/edgex.py +1053 -0
  23. hyperquant/broker/models/hyperliquid.py +284 -0
  24. hyperquant/broker/models/lbank.py +557 -0
  25. hyperquant/broker/models/lighter.py +868 -0
  26. hyperquant/broker/models/ourbit.py +1155 -0
  27. hyperquant/broker/models/polymarket.py +1071 -0
  28. hyperquant/broker/ourbit.py +550 -0
  29. hyperquant/broker/polymarket.py +2399 -0
  30. hyperquant/broker/ws.py +132 -0
  31. hyperquant/core.py +513 -0
  32. hyperquant/datavison/_util.py +18 -0
  33. hyperquant/datavison/binance.py +111 -0
  34. hyperquant/datavison/coinglass.py +237 -0
  35. hyperquant/datavison/okx.py +177 -0
  36. hyperquant/db.py +191 -0
  37. hyperquant/draw.py +1200 -0
  38. hyperquant/logkit.py +205 -0
  39. hyperquant/notikit.py +124 -0
  40. hyperquant-1.48.dist-info/METADATA +32 -0
  41. hyperquant-1.48.dist-info/RECORD +42 -0
  42. hyperquant-1.48.dist-info/WHEEL +4 -0
@@ -0,0 +1,284 @@
1
+ from __future__ import annotations
2
+
3
+ import time
4
+ from aiohttp import ClientWebSocketResponse
5
+ import aiohttp
6
+
7
+ def callback(msg, ws: ClientWebSocketResponse = None):
8
+ print("Received message:", msg)
9
+
10
+
11
+ import asyncio
12
+ import pybotters
13
+ from pybotters.store import DataStore, DataStoreCollection
14
+ from pybotters.models.hyperliquid import HyperliquidDataStore
15
+ from typing import TYPE_CHECKING, Awaitable
16
+
17
+ if TYPE_CHECKING:
18
+ from pybotters.typedefs import Item
19
+ from pybotters.ws import ClientWebSocketResponse
20
+
21
+
22
+
23
+ # {'channel': 'orderUpdates', 'data': [{'order': {'coin': 'HYPE', 'side': 'A', 'limitPx': '22.887', 'sz': '1.12', 'oid': 29641480516, 'timestamp': 1746766108031, 'origSz': '1.12', 'reduceOnly': True}, 'status': 'rejected', 'statusTimestamp': 1746766108031}]}
24
+ class OrderStore(DataStore):
25
+ _KEYS = ["oid"]
26
+
27
+ def _onmessage(self, msg: Item) -> None:
28
+
29
+ for rec in msg:
30
+ order = rec["order"]
31
+ item = {
32
+ **order,
33
+ "status": rec.get("status"),
34
+ "px": None,
35
+ 'fee': None,
36
+ "statusTimestamp": rec.get("statusTimestamp"),
37
+ }
38
+
39
+ if item["status"] == "open":
40
+ self._update([item])
41
+ else:
42
+ self._delete([item])
43
+
44
+ class FillStore(DataStore):
45
+ _KEYS = ["oid"]
46
+
47
+ def _onmessage(self, msg: Item) -> None:
48
+ for fill in msg:
49
+ self._update([fill])
50
+
51
+
52
+
53
+ class Account(DataStore):
54
+ _KEYS = ["marginCoin", "value"]
55
+
56
+ def _onmessage(self, data: list[Item]) -> None:
57
+ self._update(
58
+ [
59
+ {
60
+ "marginCoin": 'USDC',
61
+ 'value': float(item['accountValue']),
62
+ 'frozen': float(item['totalMarginUsed']),
63
+ 'available': float(item['accountValue']) - float(item['totalMarginUsed']),
64
+ }
65
+ for item in data
66
+ ]
67
+ )
68
+
69
+ class SpotAccount(DataStore):
70
+
71
+ _KEYS = ["coin"]
72
+
73
+ def _onmessage(self, data: list[Item]) -> None:
74
+ self._update(
75
+ [
76
+ {
77
+ "coin": item['coin'],
78
+ "total": float(item['total']),
79
+ "frozen": float(item['hold']),
80
+ "available": float(item['total']) - float(item['hold']),
81
+ "entryNtl": float(item['entryNtl']),
82
+ }
83
+ for item in data
84
+ ]
85
+ )
86
+
87
+ class PositionStore(DataStore):
88
+ _KEYS = ["coin"]
89
+
90
+ def _onmessage(self, data: list[Item]) -> None:
91
+
92
+ self._clear()
93
+ self._update([
94
+
95
+ {
96
+ "coin": item['position']['coin'],
97
+ "sz": float(item['position']['szi']),
98
+ "px": float(item['position']['entryPx']),
99
+ 'unpnl': float(item['position']['unrealizedPnl']),
100
+ 'rt': float(item['position']['returnOnEquity']),
101
+ 'lv': int(item['position']['leverage']['value']),
102
+ 'update_time': int(time.time() * 1000)
103
+ }
104
+ for item in data
105
+ ])
106
+
107
+
108
+
109
+ class MyHyperStore(HyperliquidDataStore):
110
+ ORDER_TYPE = 'orderUpdates'
111
+ WEBDATA2_TYPE = 'webData2'
112
+ ORDER_FILL_TYPE = 'userFills'
113
+
114
+ def _init(self) -> None:
115
+ self._create("orders", datastore_class=OrderStore)
116
+ self._create("account", datastore_class=Account)
117
+ self._create("positions", datastore_class=PositionStore)
118
+ self._create("spot_account", datastore_class=SpotAccount)
119
+ self._create("fills", datastore_class=FillStore)
120
+ super()._init()
121
+
122
+ def _onmessage(self, msg: Item, ws: ClientWebSocketResponse | None = None) -> None:
123
+
124
+ if msg.get("channel") == self.ORDER_TYPE:
125
+ self.orders._onmessage(msg.get('data', []))
126
+ elif msg.get("channel") == self.WEBDATA2_TYPE:
127
+ # print(msg.get('data', {}).get('clearinghouseState', {}))
128
+ act_data = msg.get('data', {}).get('clearinghouseState', {}).get('crossMarginSummary', [])
129
+ if act_data:
130
+ self.account._onmessage([act_data])
131
+
132
+ pos_data = msg.get('data', {}).get('clearinghouseState', {}).get('assetPositions', [])
133
+ self.positions._onmessage(pos_data)
134
+
135
+ spot_act_data = msg.get('data', {}).get('spotState', {}).get('balances', [])
136
+ self.spot_account._onmessage(spot_act_data)
137
+
138
+ elif msg.get("channel") == self.ORDER_FILL_TYPE:
139
+ fills = msg.get('data', {}).get('fills', [])
140
+ is_snap = msg.get('data', {}).get('isSnapshot', False)
141
+ if not is_snap:
142
+ self.fills._onmessage(fills)
143
+
144
+ super()._onmessage(msg, ws)
145
+
146
+
147
+ async def initialize(self, *aws: tuple[str, Awaitable[aiohttp.ClientResponse]]) -> None:
148
+ for a in aws:
149
+ method, f = a
150
+ resp = await f
151
+ data = await resp.json()
152
+
153
+ if method == "orders":
154
+ self.orders._onmessage(
155
+ [
156
+ {
157
+ 'order': o,
158
+ 'status': "open",
159
+ 'statusTimestamp': int(time.time() * 1000)
160
+ } for o in data
161
+ ]
162
+ )
163
+ elif method == "positions":
164
+ # 处理持仓数据初始化
165
+ pos_data = data.get('assetPositions', [])
166
+ if pos_data:
167
+ self.positions._onmessage(pos_data)
168
+
169
+ # 处理账户数据初始化
170
+ # act_data = data.get('crossMarginSummary', {})
171
+ # if act_data:
172
+ # self.account._onmessage([act_data])
173
+ pass
174
+
175
+ @property
176
+ def orders(self) -> OrderStore:
177
+ """``orders`` data stream.
178
+
179
+ https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
180
+
181
+ Data structure:
182
+
183
+ .. code:: python
184
+ [
185
+ {
186
+ "coin": "HYPE",
187
+ "side": "A",
188
+ "limitPx": "22.887",
189
+ "sz": "1.12",
190
+ "oid": 29641480516,
191
+ "timestamp": 1746766108031,
192
+ "origSz": "1.12",
193
+ "reduceOnly": True
194
+ "status": "open",
195
+ "statusTimestamp": 1746766108031
196
+ }...
197
+ ]
198
+ """
199
+ return self._get("orders", OrderStore)
200
+ @property
201
+ def account(self) -> Account:
202
+ """``account`` data stream.
203
+ https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
204
+ Data structure:
205
+ .. code:: python
206
+ [
207
+ {
208
+ "marginCoin": 'USDC',
209
+ 'value': float(item['accountValue']),
210
+ 'frozen': float(item['totalMarginUsed']),
211
+ 'available': float(item['accountValue']) - float(item['totalMarginUsed']),
212
+ }...
213
+ ]
214
+ """
215
+ return self._get("account", Account)
216
+
217
+ @property
218
+ def positions(self) -> PositionStore:
219
+ """
220
+ 获取当前的持仓信息。
221
+ Data structure:
222
+ .. code:: python
223
+ [
224
+ {
225
+ "coin": item['position']['coin'],
226
+ "sz": float(item['position']['szi']),
227
+ "px": float(item['position']['entryPx']),
228
+ 'unpnl': float(item['position']['unrealizedPnl']),
229
+ 'rt': float(item['position']['returnOnEquity']),
230
+ 'lv': int(item['position']['leverage']['value']),
231
+ 'update_time': int(time.time() * 1000)
232
+ }...
233
+ ]
234
+ """
235
+
236
+ return self._get("positions", PositionStore)
237
+
238
+ @property
239
+ def spot_account(self) -> SpotAccount:
240
+ """``spot_account`` data stream.
241
+ https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
242
+ Data structure:
243
+ .. code:: python
244
+ [
245
+ {
246
+ "coin": 'FEUSD',
247
+ "sz": "21.0",
248
+ "px": "0.9719",
249
+ "unpnl": "0.0",
250
+ "rt": "0.0",
251
+ "lv": 1,
252
+ }...
253
+ ]
254
+ """
255
+ return self._get("spot_account", SpotAccount)
256
+
257
+ @property
258
+ def fills(self) -> FillStore:
259
+ """``fills`` data stream.
260
+ https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions
261
+ Data structure:
262
+ .. code:: python
263
+ [
264
+ {
265
+ "coin": 'FEUSD',
266
+ "px": "0.9719",
267
+ "sz": "21.0",
268
+ "side": 'buy',
269
+ "time": 1679999999999,
270
+ "startPosition": '0.0',
271
+ "dir": 'buy',
272
+ "closedPnl": '0.0',
273
+ "hash": '0x123456789abcdef',
274
+ "oid": 123456789,
275
+ "crossed": True,
276
+ "fee": '-0.0001',
277
+ "tid": 987654321,
278
+ "liquidation": None,
279
+ "feeToken": 'USDC',
280
+ }...
281
+ ]
282
+ """
283
+ return self._get("fills", FillStore)
284
+