hyperquant 1.57__tar.gz → 1.59__tar.gz
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.
- {hyperquant-1.57 → hyperquant-1.59}/PKG-INFO +1 -1
- {hyperquant-1.57 → hyperquant-1.59}/pyproject.toml +1 -1
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/polymarket.py +37 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/polymarket.py +141 -4
- {hyperquant-1.57 → hyperquant-1.59}/uv.lock +1 -1
- {hyperquant-1.57 → hyperquant-1.59}/.gitignore +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/README.md +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/requirements-dev.lock +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/requirements.lock +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/__init__.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/auth.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/bitget.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/bitmart.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/coinw.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/deepcoin.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/edgex.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/hyperliquid.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lbank.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/edgex_sign.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/hpstore.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/hyper_types.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/polymarket/ctfAbi.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/polymarket/safeAbi.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lib/util.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/lighter.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/apexpro.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/bitget.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/bitmart.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/coinw.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/deepcoin.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/edgex.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/hyperliquid.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/lbank.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/lighter.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/models/ourbit.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/ourbit.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/broker/ws.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/core.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/datavison/_util.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/datavison/binance.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/datavison/coinglass.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/datavison/okx.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/db.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/draw.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/logkit.py +0 -0
- {hyperquant-1.57 → hyperquant-1.59}/src/hyperquant/notikit.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hyperquant
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.59
|
|
4
4
|
Summary: A minimal yet hyper-efficient backtesting framework for quantitative trading
|
|
5
5
|
Project-URL: Homepage, https://github.com/yourusername/hyperquant
|
|
6
6
|
Project-URL: Issues, https://github.com/yourusername/hyperquant/issues
|
|
@@ -187,6 +187,13 @@ class Position(DataStore):
|
|
|
187
187
|
"no_qty": 0.0,
|
|
188
188
|
"yes_avg_cost": 0.0,
|
|
189
189
|
"no_avg_cost": 0.0,
|
|
190
|
+
"cash_flow": 0.0,
|
|
191
|
+
"open_cost": 0.0,
|
|
192
|
+
"realized_pnl": 0.0,
|
|
193
|
+
"pnl_if_yes_win": 0.0,
|
|
194
|
+
"pnl_if_no_win": 0.0,
|
|
195
|
+
"max_loss": 0.0,
|
|
196
|
+
"best_case_pnl": 0.0,
|
|
190
197
|
"net_diff": 0.0,
|
|
191
198
|
"portfolio_cost": 0.0,
|
|
192
199
|
"yes_asset": None,
|
|
@@ -200,9 +207,17 @@ class Position(DataStore):
|
|
|
200
207
|
no_qty = float(state.get("no_qty") or 0.0)
|
|
201
208
|
yes_avg = float(state.get("yes_avg_cost") or 0.0)
|
|
202
209
|
no_avg = float(state.get("no_avg_cost") or 0.0)
|
|
210
|
+
cash_flow = float(state.get("cash_flow") or 0.0)
|
|
211
|
+
open_cost = yes_qty * yes_avg + no_qty * no_avg
|
|
203
212
|
|
|
204
213
|
state["net_diff"] = yes_qty - no_qty
|
|
205
214
|
state["portfolio_cost"] = (yes_avg + no_avg) if (yes_qty > 0.0 and no_qty > 0.0) else 0.0
|
|
215
|
+
state["open_cost"] = open_cost
|
|
216
|
+
state["realized_pnl"] = cash_flow + open_cost
|
|
217
|
+
state["pnl_if_yes_win"] = cash_flow + yes_qty
|
|
218
|
+
state["pnl_if_no_win"] = cash_flow + no_qty
|
|
219
|
+
state["max_loss"] = min(state["pnl_if_yes_win"], state["pnl_if_no_win"])
|
|
220
|
+
state["best_case_pnl"] = max(state["pnl_if_yes_win"], state["pnl_if_no_win"])
|
|
206
221
|
|
|
207
222
|
def _upsert_row(self, row: dict[str, Any]) -> None:
|
|
208
223
|
existing = self.get({"asset": row["asset"]})
|
|
@@ -220,6 +235,13 @@ class Position(DataStore):
|
|
|
220
235
|
"no_qty": float(state.get("no_qty") or 0.0),
|
|
221
236
|
"yes_avg_cost": float(state.get("yes_avg_cost") or 0.0),
|
|
222
237
|
"no_avg_cost": float(state.get("no_avg_cost") or 0.0),
|
|
238
|
+
"cash_flow": float(state.get("cash_flow") or 0.0),
|
|
239
|
+
"open_cost": float(state.get("open_cost") or 0.0),
|
|
240
|
+
"realized_pnl": float(state.get("realized_pnl") or 0.0),
|
|
241
|
+
"pnl_if_yes_win": float(state.get("pnl_if_yes_win") or 0.0),
|
|
242
|
+
"pnl_if_no_win": float(state.get("pnl_if_no_win") or 0.0),
|
|
243
|
+
"max_loss": float(state.get("max_loss") or 0.0),
|
|
244
|
+
"best_case_pnl": float(state.get("best_case_pnl") or 0.0),
|
|
223
245
|
"net_diff": float(state.get("net_diff") or 0.0),
|
|
224
246
|
"portfolio_cost": float(state.get("portfolio_cost") or 0.0),
|
|
225
247
|
"ts": now_ms,
|
|
@@ -238,6 +260,8 @@ class Position(DataStore):
|
|
|
238
260
|
"size": yes_qty,
|
|
239
261
|
"avgPrice": yes_avg,
|
|
240
262
|
"totalAvgPrice": yes_avg,
|
|
263
|
+
"expected_pnl_if_win": common["pnl_if_yes_win"],
|
|
264
|
+
"expected_pnl_if_lose": common["pnl_if_no_win"],
|
|
241
265
|
**common,
|
|
242
266
|
}
|
|
243
267
|
)
|
|
@@ -251,6 +275,8 @@ class Position(DataStore):
|
|
|
251
275
|
"size": no_qty,
|
|
252
276
|
"avgPrice": no_avg,
|
|
253
277
|
"totalAvgPrice": no_avg,
|
|
278
|
+
"expected_pnl_if_win": common["pnl_if_no_win"],
|
|
279
|
+
"expected_pnl_if_lose": common["pnl_if_yes_win"],
|
|
254
280
|
**common,
|
|
255
281
|
}
|
|
256
282
|
)
|
|
@@ -298,6 +324,8 @@ class Position(DataStore):
|
|
|
298
324
|
delta = size * trade_sign * status_sign
|
|
299
325
|
if abs(delta) < 1e-12:
|
|
300
326
|
return
|
|
327
|
+
cash_delta = (-price * size if side == "BUY" else price * size) * status_sign
|
|
328
|
+
state["cash_flow"] = float(state.get("cash_flow") or 0.0) + cash_delta
|
|
301
329
|
|
|
302
330
|
qty_key = "yes_qty" if side_key == "YES" else "no_qty"
|
|
303
331
|
avg_key = "yes_avg_cost" if side_key == "YES" else "no_avg_cost"
|
|
@@ -1104,6 +1132,15 @@ class PolymarketDataStore(DataStoreCollection):
|
|
|
1104
1132
|
"no_qty": 3.0, // NO腿数量
|
|
1105
1133
|
"yes_avg_cost": 0.47, // YES腿VWAP成本
|
|
1106
1134
|
"no_avg_cost": 0.49, // NO腿VWAP成本
|
|
1135
|
+
"cash_flow": -0.88, // 交易净现金流(买入为负,卖出为正), FAILED会反向回滚
|
|
1136
|
+
"open_cost": 3.82, // 当前未平仓持仓成本 = yes_qty*yes_avg_cost + no_qty*no_avg_cost
|
|
1137
|
+
"realized_pnl": 2.94, // 已实现盈亏 = cash_flow + open_cost
|
|
1138
|
+
"pnl_if_yes_win": 4.12, // 若YES结算为1、NO归零时的最终盈亏
|
|
1139
|
+
"pnl_if_no_win": 2.12, // 若NO结算为1、YES归零时的最终盈亏
|
|
1140
|
+
"max_loss": 2.12, // 两种结算情景下的较差结果
|
|
1141
|
+
"best_case_pnl": 4.12, // 两种结算情景下的较优结果
|
|
1142
|
+
"expected_pnl_if_win": 4.12, // 对当前asset而言,该outcome获胜时的预期最终盈亏
|
|
1143
|
+
"expected_pnl_if_lose": 2.12,// 对当前asset而言,该outcome落败时的预期最终盈亏
|
|
1107
1144
|
"net_diff": 2.0, // 净敞口 = yes_qty - no_qty
|
|
1108
1145
|
"portfolio_cost": 0.96, // 组合成本 = yes_avg_cost + no_avg_cost(双腿都有仓时)
|
|
1109
1146
|
"ts": 1772269014393, // 本地更新时间戳(ms)
|
|
@@ -8,7 +8,7 @@ from functools import lru_cache
|
|
|
8
8
|
import os
|
|
9
9
|
import time
|
|
10
10
|
from typing import Any, Awaitable, Callable, Iterable, Iterator, Literal, Mapping, Sequence
|
|
11
|
-
from urllib.parse import urlsplit, urlunsplit
|
|
11
|
+
from urllib.parse import urlencode, urlsplit, urlunsplit
|
|
12
12
|
|
|
13
13
|
import json
|
|
14
14
|
|
|
@@ -232,7 +232,8 @@ class Polymarket:
|
|
|
232
232
|
private_key: str | None = None,
|
|
233
233
|
chain_id: int | None = None,
|
|
234
234
|
signature_type: int | None = None,
|
|
235
|
-
funder: str | None = None
|
|
235
|
+
funder: str | None = None,
|
|
236
|
+
forward_url: str | None = None, # geoblock转发
|
|
236
237
|
) -> None:
|
|
237
238
|
# Logger (per-class, safe default)
|
|
238
239
|
self.logger = logging.getLogger(f"{API_NAME}.{self.__class__.__name__}")
|
|
@@ -247,6 +248,7 @@ class Polymarket:
|
|
|
247
248
|
self.client = client
|
|
248
249
|
self.rest_api = (rest_api or DEFAULT_REST_ENDPOINT).rstrip("/")
|
|
249
250
|
self.ws_public = ws_public or DEFAULT_WS_ENDPOINT
|
|
251
|
+
self.forward_url = forward_url
|
|
250
252
|
|
|
251
253
|
self.chain_id = chain_id or 137
|
|
252
254
|
# Default to POLY_GNOSIS_SAFE (2) to match common proxy flows used in examples/tests.
|
|
@@ -802,13 +804,19 @@ class Polymarket:
|
|
|
802
804
|
|
|
803
805
|
async def create_api_key(self, nonce: int | None = None) -> dict[str, Any]:
|
|
804
806
|
params = {"nonce": nonce} if nonce is not None else None
|
|
805
|
-
|
|
807
|
+
if self.forward_url:
|
|
808
|
+
data = await self._forward_l1_auth_request("POST", "/auth/api-key", params=params)
|
|
809
|
+
else:
|
|
810
|
+
data = await self._rest("POST", "/auth/api-key", params=params)
|
|
806
811
|
self._store_api_creds(data)
|
|
807
812
|
return data
|
|
808
813
|
|
|
809
814
|
async def derive_api_key(self, nonce: int | None = None) -> dict[str, Any]:
|
|
810
815
|
params = {"nonce": nonce} if nonce is not None else None
|
|
811
|
-
|
|
816
|
+
if self.forward_url:
|
|
817
|
+
data = await self._forward_l1_auth_request("GET", "/auth/derive-api-key", params=params)
|
|
818
|
+
else:
|
|
819
|
+
data = await self._rest("GET", "/auth/derive-api-key", params=params)
|
|
812
820
|
self._store_api_creds(data)
|
|
813
821
|
return data
|
|
814
822
|
|
|
@@ -1506,6 +1514,135 @@ class Polymarket:
|
|
|
1506
1514
|
"Content-Type": "application/json",
|
|
1507
1515
|
}
|
|
1508
1516
|
|
|
1517
|
+
def _build_l1_signed_headers(self, *, nonce: int = 0) -> dict[str, str]:
|
|
1518
|
+
from eth_account import Account as _A
|
|
1519
|
+
try:
|
|
1520
|
+
from eth_account.messages import encode_typed_data as _enc
|
|
1521
|
+
except ImportError:
|
|
1522
|
+
from eth_account.messages import encode_structured_data as _enc
|
|
1523
|
+
|
|
1524
|
+
private_key, _, signer_addr = self._get_signing_context()
|
|
1525
|
+
timestamp = int(time.time())
|
|
1526
|
+
typed_data = {
|
|
1527
|
+
"types": {
|
|
1528
|
+
"EIP712Domain": [
|
|
1529
|
+
{"name": "name", "type": "string"},
|
|
1530
|
+
{"name": "version", "type": "string"},
|
|
1531
|
+
{"name": "chainId", "type": "uint256"},
|
|
1532
|
+
],
|
|
1533
|
+
"ClobAuth": [
|
|
1534
|
+
{"name": "address", "type": "address"},
|
|
1535
|
+
{"name": "timestamp", "type": "string"},
|
|
1536
|
+
{"name": "nonce", "type": "uint256"},
|
|
1537
|
+
{"name": "message", "type": "string"},
|
|
1538
|
+
],
|
|
1539
|
+
},
|
|
1540
|
+
"domain": {
|
|
1541
|
+
"name": "ClobAuthDomain",
|
|
1542
|
+
"version": "1",
|
|
1543
|
+
"chainId": int(self.chain_id),
|
|
1544
|
+
},
|
|
1545
|
+
"primaryType": "ClobAuth",
|
|
1546
|
+
"message": {
|
|
1547
|
+
"address": signer_addr,
|
|
1548
|
+
"timestamp": str(timestamp),
|
|
1549
|
+
"nonce": int(nonce),
|
|
1550
|
+
"message": "This message attests that I control the given wallet",
|
|
1551
|
+
},
|
|
1552
|
+
}
|
|
1553
|
+
encoded = _enc(full_message=typed_data)
|
|
1554
|
+
signature = _A.sign_message(encoded, private_key).signature.hex()
|
|
1555
|
+
if not signature.startswith("0x"):
|
|
1556
|
+
signature = f"0x{signature}"
|
|
1557
|
+
return {
|
|
1558
|
+
"POLY_ADDRESS": signer_addr,
|
|
1559
|
+
"POLY_SIGNATURE": signature,
|
|
1560
|
+
"POLY_TIMESTAMP": str(timestamp),
|
|
1561
|
+
"POLY_NONCE": str(int(nonce)),
|
|
1562
|
+
"Content-Type": "application/json",
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
async def _forward_l1_auth_request(
|
|
1566
|
+
self,
|
|
1567
|
+
method: str,
|
|
1568
|
+
path: str,
|
|
1569
|
+
*,
|
|
1570
|
+
params: Mapping[str, Any] | None = None,
|
|
1571
|
+
) -> dict[str, Any]:
|
|
1572
|
+
session: aiohttp.ClientSession = getattr(self.client, "_session", None)
|
|
1573
|
+
if session is None:
|
|
1574
|
+
raise RuntimeError("pybotters client session missing")
|
|
1575
|
+
if not self.forward_url:
|
|
1576
|
+
raise RuntimeError("forward_url is required for forward auth request")
|
|
1577
|
+
|
|
1578
|
+
nonce = 0
|
|
1579
|
+
if params and "nonce" in params:
|
|
1580
|
+
try:
|
|
1581
|
+
nonce = int(params["nonce"])
|
|
1582
|
+
except (TypeError, ValueError):
|
|
1583
|
+
nonce = 0
|
|
1584
|
+
|
|
1585
|
+
query = urlencode(dict(params), doseq=True) if params else ""
|
|
1586
|
+
url = f"{self.rest_api.rstrip('/')}{path}"
|
|
1587
|
+
if query:
|
|
1588
|
+
url = f"{url}?{query}"
|
|
1589
|
+
req = {
|
|
1590
|
+
"method": method.upper(),
|
|
1591
|
+
"url": url,
|
|
1592
|
+
"headers": self._build_l1_signed_headers(nonce=nonce),
|
|
1593
|
+
"body": None,
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
endpoint = self._normalize_forward_batch_url(self.forward_url)
|
|
1597
|
+
async with session.post(endpoint, json={"requests": [req]}) as resp:
|
|
1598
|
+
if resp.status >= 400:
|
|
1599
|
+
text = await resp.text()
|
|
1600
|
+
raise RuntimeError(f"Forward auth request failed: {resp.status} {text}")
|
|
1601
|
+
payload = await resp.json(content_type=None)
|
|
1602
|
+
|
|
1603
|
+
first: Any
|
|
1604
|
+
if isinstance(payload, list):
|
|
1605
|
+
first = payload[0] if payload else {}
|
|
1606
|
+
elif isinstance(payload, dict):
|
|
1607
|
+
for key in ("responses", "results", "data"):
|
|
1608
|
+
value = payload.get(key)
|
|
1609
|
+
if isinstance(value, list):
|
|
1610
|
+
first = value[0] if value else {}
|
|
1611
|
+
break
|
|
1612
|
+
else:
|
|
1613
|
+
first = payload
|
|
1614
|
+
else:
|
|
1615
|
+
raise RuntimeError(f"Unexpected forward response: {payload!r}")
|
|
1616
|
+
|
|
1617
|
+
def _parse_maybe_json(data: Any) -> Any:
|
|
1618
|
+
if isinstance(data, str):
|
|
1619
|
+
text = data.strip()
|
|
1620
|
+
try:
|
|
1621
|
+
return json.loads(text)
|
|
1622
|
+
except Exception:
|
|
1623
|
+
return text
|
|
1624
|
+
return data
|
|
1625
|
+
|
|
1626
|
+
if isinstance(first, dict):
|
|
1627
|
+
# worker forwarder shape: { ok, status, bodyPreview }
|
|
1628
|
+
if first.get("ok") is False or int(first.get("status", 200)) >= 400:
|
|
1629
|
+
detail = _parse_maybe_json(first.get("bodyPreview") or first.get("body") or first)
|
|
1630
|
+
raise RuntimeError(
|
|
1631
|
+
f"Forward upstream auth failed: status={first.get('status')} detail={detail!r}"
|
|
1632
|
+
)
|
|
1633
|
+
|
|
1634
|
+
body = _parse_maybe_json(first.get("body"))
|
|
1635
|
+
if isinstance(body, dict):
|
|
1636
|
+
return body
|
|
1637
|
+
|
|
1638
|
+
preview = _parse_maybe_json(first.get("bodyPreview"))
|
|
1639
|
+
if isinstance(preview, dict):
|
|
1640
|
+
return preview
|
|
1641
|
+
|
|
1642
|
+
if all(k in first for k in ("apiKey", "secret", "passphrase")):
|
|
1643
|
+
return first
|
|
1644
|
+
raise RuntimeError(f"Forward response missing auth body: {payload!r}")
|
|
1645
|
+
|
|
1509
1646
|
async def _submit_forward_requests(self, requests_payload: Sequence[Mapping[str, Any]]) -> Any:
|
|
1510
1647
|
cfg = self._forward_orders_cfg
|
|
1511
1648
|
if not cfg:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|