hyperquant 0.25__py3-none-any.whl → 0.26__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.
hyperquant/core.py CHANGED
@@ -350,7 +350,7 @@ class Exchange(ExchangeBase):
350
350
  self.record_history(time)
351
351
 
352
352
  # 自动更新账户状态
353
- self.Update({symbol: price}, time=time)
353
+ self.Update({symbol: price}, time=time)
354
354
 
355
355
  return trade
356
356
 
@@ -377,41 +377,92 @@ class Exchange(ExchangeBase):
377
377
  trades.append(trade)
378
378
  return trades
379
379
 
380
- def Update(self, close_price, symbols=None, **kwargs):
380
+ def _recalc_aggregates(self):
381
+ """基于 self.account 中已保存的各 symbol 状态,重算聚合字段。"""
382
+ usdt = self.account['USDT']
383
+ usdt['unrealised_profit'] = 0
384
+ usdt['hold'] = 0
385
+ usdt['long'] = 0
386
+ usdt['short'] = 0
387
+
388
+ for symbol in self.trade_symbols:
389
+ if symbol not in self.account:
390
+ continue
391
+ sym = self.account[symbol]
392
+ px = sym.get('price', 0)
393
+ amt = sym.get('amount', 0)
394
+ hp = sym.get('hold_price', 0)
395
+
396
+ # 仅当价格有效时计入聚合
397
+ if px is not None and not np.isnan(px) and px != 0:
398
+ sym['unrealised_profit'] = (px - hp) * amt
399
+ sym['value'] = amt * px
400
+
401
+ if amt > 0:
402
+ usdt['long'] += sym['value']
403
+ elif amt < 0:
404
+ usdt['short'] += sym['value']
405
+
406
+ usdt['hold'] += abs(sym['value'])
407
+ usdt['unrealised_profit'] += sym['unrealised_profit']
408
+
409
+ usdt['total'] = round(self.account['USDT']['realised_profit'] + self.initial_balance + usdt['unrealised_profit'], 6)
410
+ usdt['leverage'] = round(usdt['hold'] / usdt['total'] if usdt['total'] != 0 else 0.0, 3)
411
+
412
+ def Update(self, close_price=None, symbols=None, partial=True, **kwargs):
413
+ """
414
+ 更新账户状态。
415
+ - partial=True:只更新给定 symbols 的逐符号状态,然后对所有符号做一次聚合重算(推荐)。
416
+ - partial=False:与原逻辑兼容;当提供一部分 symbol 时,也会聚合重算,不会清空未提供符号的信息。
417
+
418
+ 支持三种入参形式:
419
+ 1) close_price 为 dict/Series:symbols 自动取其键/索引
420
+ 2) close_price 为标量 + symbols 为单个字符串
421
+ 3) 显式传 symbols=list[...],close_price 为 dict/Series(从中取价)
422
+ 如果既不传 close_price 也不传 symbols,则只做一次聚合重算(例如你先前已经手动修改了某些 symbol 的 price)。
423
+ """
381
424
  if self.recorded and 'time' not in kwargs:
382
425
  raise ValueError("Time parameter is required in recorded mode.")
383
426
 
384
427
  time = kwargs.get('time', pd.Timestamp.now())
385
- self.account['USDT']['unrealised_profit'] = 0
386
- self.account['USDT']['hold'] = 0
387
- self.account['USDT']['long'] = 0
388
- self.account['USDT']['short'] = 0
428
+
429
+ # 解析 symbols & 价格获取器
389
430
  if symbols is None:
390
- # symbols = self.trade_symbols
391
- # 如果symbols是dict类型, 则取出所有的key, 如果是Series类型, 则取出所有的index
392
431
  if isinstance(close_price, dict):
393
432
  symbols = list(close_price.keys())
394
433
  elif isinstance(close_price, pd.Series):
395
- symbols = close_price.index
434
+ symbols = list(close_price.index)
396
435
  else:
397
- raise ValueError("Symbols should be a list, dict or Series.")
398
-
399
- for symbol in symbols:
400
- if symbol not in self.trade_symbols:
436
+ symbols = []
437
+ elif isinstance(symbols, str):
438
+ symbols = [symbols]
439
+
440
+ def get_px(sym):
441
+ if isinstance(close_price, (int, float, np.floating)) and len(symbols) == 1:
442
+ return float(close_price)
443
+ if isinstance(close_price, dict):
444
+ return close_price.get(sym, np.nan)
445
+ if isinstance(close_price, pd.Series):
446
+ return close_price.get(sym, np.nan)
447
+ return np.nan
448
+
449
+ # 仅更新传入的 symbols(部分更新,不动其它符号已保存信息)
450
+ for sym in symbols:
451
+ if sym not in self.trade_symbols or sym not in self.account:
452
+ # 未登记的交易对直接跳过(或可选择自动登记,但此处保持严格)
453
+ continue
454
+ px = get_px(sym)
455
+ if px is None or np.isnan(px):
456
+ # 价格无效则不覆盖旧价格
401
457
  continue
402
- if not np.isnan(close_price[symbol]):
403
- self.account[symbol]['unrealised_profit'] = (close_price[symbol] - self.account[symbol]['hold_price']) * self.account[symbol]['amount']
404
- self.account[symbol]['price'] = close_price[symbol]
405
- self.account[symbol]['value'] = self.account[symbol]['amount'] * close_price[symbol]
406
- if self.account[symbol]['amount'] > 0:
407
- self.account['USDT']['long'] += self.account[symbol]['value']
408
- if self.account[symbol]['amount'] < 0:
409
- self.account['USDT']['short'] += self.account[symbol]['value']
410
- self.account['USDT']['hold'] += abs(self.account[symbol]['value'])
411
- self.account['USDT']['unrealised_profit'] += self.account[symbol]['unrealised_profit']
412
-
413
- self.account['USDT']['total'] = round(self.account['USDT']['realised_profit'] + self.initial_balance + self.account['USDT']['unrealised_profit'], 6)
414
- self.account['USDT']['leverage'] = round(self.account['USDT']['hold'] / self.account['USDT']['total'], 3)
458
+
459
+ self.account[sym]['price'] = float(px)
460
+ amt = self.account[sym]['amount']
461
+ self.account[sym]['value'] = amt * float(px)
462
+ # 不在这里算 unrealised_profit,聚合阶段统一算
463
+
464
+ # 无论 partial 与否,最后都用“账户中保存的所有 symbol 当前状态”做一次聚合重算
465
+ self._recalc_aggregates()
415
466
 
416
467
  # 记录账户总资产到 history
417
468
  if self.recorded:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hyperquant
3
- Version: 0.25
3
+ Version: 0.26
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
@@ -19,7 +19,7 @@ Requires-Dist: cryptography>=44.0.2
19
19
  Requires-Dist: duckdb>=1.2.2
20
20
  Requires-Dist: numpy>=1.21.0
21
21
  Requires-Dist: pandas>=2.2.3
22
- Requires-Dist: pybotters>=1.9.0
22
+ Requires-Dist: pybotters>=1.9.1
23
23
  Requires-Dist: pyecharts>=2.0.8
24
24
  Description-Content-Type: text/markdown
25
25
 
@@ -1,5 +1,5 @@
1
1
  hyperquant/__init__.py,sha256=gUuAPVpg5k8X_dpda5OpqmMyZ-ZXNQq-xwx-6JR5Jr4,131
2
- hyperquant/core.py,sha256=vKv8KElo1eGhr_aw0I-j6ZxPOneDx86KqAoOI-wbq0A,18838
2
+ hyperquant/core.py,sha256=7XrpuHvccWl9lNyVihqaptupqUMsG3xYmQr8eEDrwS4,20610
3
3
  hyperquant/db.py,sha256=i2TjkCbmH4Uxo7UTDvOYBfy973gLcGexdzuT_YcSeIE,6678
4
4
  hyperquant/draw.py,sha256=up_lQ3pHeVLoNOyh9vPjgNwjD0M-6_IetSGviQUgjhY,54624
5
5
  hyperquant/logkit.py,sha256=WALpXpIA3Ywr5DxKKK3k5EKubZ2h-ISGfc5dUReQUBQ,7795
@@ -11,6 +11,6 @@ hyperquant/datavison/_util.py,sha256=92qk4vO856RqycO0YqEIHJlEg-W9XKapDVqAMxe6rbw
11
11
  hyperquant/datavison/binance.py,sha256=3yNKTqvt_vUQcxzeX4ocMsI5k6Q6gLZrvgXxAEad6Kc,5001
12
12
  hyperquant/datavison/coinglass.py,sha256=PEjdjISP9QUKD_xzXNzhJ9WFDTlkBrRQlVL-5pxD5mo,10482
13
13
  hyperquant/datavison/okx.py,sha256=yg8WrdQ7wgWHNAInIgsWPM47N3Wkfr253169IPAycAY,6898
14
- hyperquant-0.25.dist-info/METADATA,sha256=re5JjxsJCj3BOunjieCHlp7c_fOuyaHZVZc8RDrCNKw,4317
15
- hyperquant-0.25.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- hyperquant-0.25.dist-info/RECORD,,
14
+ hyperquant-0.26.dist-info/METADATA,sha256=YAc40ZCIrhQE-5pyXnxPRz3bVSToOyJKiB2XHFspzZs,4317
15
+ hyperquant-0.26.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
+ hyperquant-0.26.dist-info/RECORD,,